plugins/antiflood.sp
author David Anderson <dvander@alliedmods.net>
Thu Jan 03 06:11:26 2008 +0000 (2008-01-03)
changeset 1829 97cd42e800e8
parent 1330 ab94316bb52b
child 1925 61d7a244b641
permissions -rw-r--r--
added amb1336 - GetTrieSize()
ds@913
     1
/**
ds@1330
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
ds@1330
     4
 * SourceMod Anti-Flood Plugin
ds@913
     5
 * Protects against chat flooding.
ds@1330
     6
 *
ds@1330
     7
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
ds@1330
     8
 * =============================================================================
ds@913
     9
 *
ds@1330
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    12
 * Free Software Foundation.
ds@1330
    13
 * 
ds@1330
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    17
 * details.
ds@913
    18
 *
ds@1330
    19
 * You should have received a copy of the GNU General Public License along with
ds@1330
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1330
    21
 *
ds@1330
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    29
 * or <http://www.sourcemod.net/license.php>.
ds@913
    30
 *
ds@913
    31
 * Version: $Id$
ds@913
    32
 */
ds@1330
    33
ds@989
    34
#pragma semicolon 1
ds@913
    35
ds@913
    36
#include <sourcemod>
ds@913
    37
ds@913
    38
public Plugin:myinfo = 
ds@913
    39
{
ds@913
    40
	name = "Anti-Flood",
ds@913
    41
	author = "AlliedModders LLC",
ds@913
    42
	description = "Protects against chat flooding",
ds@913
    43
	version = SOURCEMOD_VERSION,
ds@913
    44
	url = "http://www.sourcemod.net/"
ds@913
    45
};
ds@913
    46
ds@913
    47
new Float:g_LastTime[MAXPLAYERS + 1] = {0.0, ...};		/* Last time player used say or say_team */
ds@913
    48
new g_FloodTokens[MAXPLAYERS + 1] = {0, ...};			/* Number of flood tokens player has */
ds@913
    49
ds@913
    50
new Handle:sm_flood_time;								/* Handle to sm_flood_time convar */
ds@913
    51
ds@913
    52
public OnPluginStart()
ds@913
    53
{
ferret99gt@1187
    54
	LoadTranslations("antiflood.phrases");
ds@913
    55
	
ds@913
    56
	RegConsoleCmd("say", CheckChatFlood);
ds@913
    57
	RegConsoleCmd("say_team", CheckChatFlood);
ds@913
    58
	sm_flood_time = CreateConVar("sm_flood_time", "0.75", "Amount of time allowed between chat messages");
ds@913
    59
}
ds@913
    60
faluco@1128
    61
public OnClientPutInServer(client)
faluco@1128
    62
{
faluco@1128
    63
	g_LastTime[client] = 0.0;
faluco@1128
    64
	g_FloodTokens[client] = 0;
faluco@1128
    65
}
faluco@1128
    66
ds@913
    67
public Action:CheckChatFlood(client, args)
ds@913
    68
{
ds@913
    69
	/* Chat from server console shouldn't be checked for flooding */
ds@913
    70
	if (client == 0)
ds@913
    71
	{
ds@913
    72
		return Plugin_Continue;
ds@913
    73
	}
ds@913
    74
	
ds@913
    75
	new Float:maxChat = GetConVarFloat(sm_flood_time);
ds@913
    76
	
ds@913
    77
	if (maxChat > 0.0)
ds@913
    78
	{
ds@913
    79
		new Float:curTime = GetGameTime();
ds@913
    80
		
ds@913
    81
		if (g_LastTime[client] > curTime)
ds@913
    82
		{
ds@913
    83
			/* If player has 3 or more flood tokens, block their message */
ds@913
    84
			if (g_FloodTokens[client] >= 3)
ds@913
    85
			{
ds@913
    86
				PrintToChat(client, "[SM] %t", "Flooding the server");
ds@913
    87
				g_LastTime[client] = curTime + maxChat + 3.0;
ds@913
    88
				
ds@913
    89
				return Plugin_Stop;	
ds@913
    90
			}
ds@913
    91
			
ds@913
    92
			/* Add one flood token when player goes over chat time limit */
ds@913
    93
			g_FloodTokens[client]++;
ds@913
    94
		} else if (g_FloodTokens[client]) {
ds@913
    95
			/* Remove one flood token when player chats within time limit (slow decay) */
ds@913
    96
			g_FloodTokens[client]--;
ds@913
    97
		}
ds@913
    98
		
ds@913
    99
		/* Store last time of chat usage */
ds@913
   100
		g_LastTime[client] = curTime + maxChat;
ds@913
   101
	}
ds@913
   102
	
ds@913
   103
	return Plugin_Continue;
ds@913
   104
}