plugins/antiflood.sp
author David Anderson <dvander@alliedmods.net>
Sun Jul 08 04:15:00 2007 +0000 (2007-07-08)
changeset 1059 255bb3a2f47b
parent 1027 4bb60993758e
child 1128 1696cbb2bcc4
permissions -rw-r--r--
removed 0 checks in case an offset will be 0
     1 /**
     2  * antiflood.sp
     3  * Protects against chat flooding.
     4  * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
     5  *
     6  * This program is free software; you can redistribute it and/or
     7  * modify it under the terms of the GNU General Public License
     8  * as published by the Free Software Foundation; either version 2
     9  * of the License, or (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  * 
    16  * You should have received a copy of the GNU General Public License
    17  * along with this program; if not, write to the Free Software
    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    19  *
    20  * Version: $Id$
    21  */
    22  
    23 #pragma semicolon 1
    24 
    25 #include <sourcemod>
    26 
    27 public Plugin:myinfo = 
    28 {
    29 	name = "Anti-Flood",
    30 	author = "AlliedModders LLC",
    31 	description = "Protects against chat flooding",
    32 	version = SOURCEMOD_VERSION,
    33 	url = "http://www.sourcemod.net/"
    34 };
    35 
    36 new Float:g_LastTime[MAXPLAYERS + 1] = {0.0, ...};		/* Last time player used say or say_team */
    37 new g_FloodTokens[MAXPLAYERS + 1] = {0, ...};			/* Number of flood tokens player has */
    38 
    39 new Handle:sm_flood_time;								/* Handle to sm_flood_time convar */
    40 
    41 public OnPluginStart()
    42 {
    43 	LoadTranslations("plugin.antiflood");
    44 	
    45 	RegConsoleCmd("say", CheckChatFlood);
    46 	RegConsoleCmd("say_team", CheckChatFlood);
    47 	sm_flood_time = CreateConVar("sm_flood_time", "0.75", "Amount of time allowed between chat messages");
    48 }
    49 
    50 public Action:CheckChatFlood(client, args)
    51 {
    52 	/* Chat from server console shouldn't be checked for flooding */
    53 	if (client == 0)
    54 	{
    55 		return Plugin_Continue;
    56 	}
    57 	
    58 	new Float:maxChat = GetConVarFloat(sm_flood_time);
    59 	
    60 	if (maxChat > 0.0)
    61 	{
    62 		new Float:curTime = GetGameTime();
    63 		
    64 		if (g_LastTime[client] > curTime)
    65 		{
    66 			/* If player has 3 or more flood tokens, block their message */
    67 			if (g_FloodTokens[client] >= 3)
    68 			{
    69 				PrintToChat(client, "[SM] %t", "Flooding the server");
    70 				g_LastTime[client] = curTime + maxChat + 3.0;
    71 				
    72 				return Plugin_Stop;	
    73 			}
    74 			
    75 			/* Add one flood token when player goes over chat time limit */
    76 			g_FloodTokens[client]++;
    77 		} else if (g_FloodTokens[client]) {
    78 			/* Remove one flood token when player chats within time limit (slow decay) */
    79 			g_FloodTokens[client]--;
    80 		}
    81 		
    82 		/* Store last time of chat usage */
    83 		g_LastTime[client] = curTime + maxChat;
    84 	}
    85 	
    86 	return Plugin_Continue;
    87 }