plugins/antiflood.sp
author Matt Woodrow <pred@alliedmods.net>
Tue Sep 02 07:10:11 2008 +0000 (2008-09-02)
changeset 2248 d433aa792147
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Added a nominations section to maplists.cfg
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Anti-Flood Plugin
ds@1952
     5
 * Protects against chat flooding.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
#pragma semicolon 1
ds@1952
    35
ds@1952
    36
#include <sourcemod>
ds@1952
    37
ds@1952
    38
public Plugin:myinfo = 
ds@1952
    39
{
ds@1952
    40
	name = "Anti-Flood",
ds@1952
    41
	author = "AlliedModders LLC",
ds@1952
    42
	description = "Protects against chat flooding",
ds@1952
    43
	version = SOURCEMOD_VERSION,
ds@1952
    44
	url = "http://www.sourcemod.net/"
ds@1952
    45
};
ds@1952
    46
ds@1952
    47
new Float:g_LastTime[MAXPLAYERS + 1] = {0.0, ...};		/* Last time player used say or say_team */
ds@1952
    48
new g_FloodTokens[MAXPLAYERS + 1] = {0, ...};			/* Number of flood tokens player has */
ds@1952
    49
ds@1952
    50
new Handle:sm_flood_time;								/* Handle to sm_flood_time convar */
ds@1952
    51
ds@1952
    52
public OnPluginStart()
ds@1952
    53
{
ds@1952
    54
	sm_flood_time = CreateConVar("sm_flood_time", "0.75", "Amount of time allowed between chat messages");
ds@1952
    55
}
ds@1952
    56
ds@1952
    57
public OnClientPutInServer(client)
ds@1952
    58
{
ds@1952
    59
	g_LastTime[client] = 0.0;
ds@1952
    60
	g_FloodTokens[client] = 0;
ds@1952
    61
}
ds@1952
    62
ds@1952
    63
new Float:max_chat;
ds@1952
    64
ds@1952
    65
public bool:OnClientFloodCheck(client)
ds@1952
    66
{
ds@1952
    67
	max_chat = GetConVarFloat(sm_flood_time);
ds@1952
    68
	
ds@1952
    69
	if (max_chat <= 0.0 
ds@1952
    70
		|| (GetUserFlagBits(client) & ADMFLAG_ROOT) == ADMFLAG_ROOT)
ds@1952
    71
	{
ds@1952
    72
		return false;
ds@1952
    73
	}
ds@1952
    74
	
ds@1952
    75
	if (g_LastTime[client] > GetGameTime())
ds@1952
    76
	{
ds@1952
    77
		/* If player has 3 or more flood tokens, block their message */
ds@1952
    78
		if (g_FloodTokens[client] >= 3)
ds@1952
    79
		{
ds@1952
    80
			return true;
ds@1952
    81
		}
ds@1952
    82
	}
ds@1952
    83
	
ds@1952
    84
	return false;
ds@1952
    85
}
ds@1952
    86
ds@1952
    87
public OnClientFloodResult(client, bool:blocked)
ds@1952
    88
{
ds@1952
    89
	if (max_chat <= 0.0 
ds@1952
    90
		|| (GetUserFlagBits(client) & ADMFLAG_ROOT) == ADMFLAG_ROOT)
ds@1952
    91
	{
ds@1952
    92
		return;
ds@1952
    93
	}
ds@1952
    94
	
ds@1952
    95
	new Float:curTime = GetGameTime();
ds@1952
    96
	new Float:newTime = curTime + max_chat;
ds@1952
    97
	
ds@1952
    98
	if (g_LastTime[client] > curTime)
ds@1952
    99
	{
ds@1952
   100
		/* If the last message was blocked, update their time limit */
ds@1952
   101
		if (blocked)
ds@1952
   102
		{
ds@1952
   103
			newTime += 3.0;
ds@1952
   104
		}
ds@1952
   105
		/* Add one flood token when player goes over chat time limit */
ds@1952
   106
		else if (g_FloodTokens[client] < 3)
ds@1952
   107
		{
ds@1952
   108
			g_FloodTokens[client]++;
ds@1952
   109
		}
ds@1952
   110
	}
ds@1952
   111
	else if (g_FloodTokens[client] > 0)
ds@1952
   112
	{
ds@1952
   113
		/* Remove one flood token when player chats within time limit (slow decay) */
ds@1952
   114
		g_FloodTokens[client]--;
ds@1952
   115
	}
ds@1952
   116
	
ds@1952
   117
	g_LastTime[client] = newTime;
ds@1952
   118
}