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