plugins/antiflood.sp
author Asher Baker <asherkin@gmail.com>
Thu Nov 15 17:49:13 2012 +0000 (2012-11-15)
changeset 3685 baf5bdadd494
parent 3166 87a8cfed8dd2
child 4259 ca0fdabc2d92
permissions -rw-r--r--
Improved console config editing (bug 5470, r=psychonic).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Anti-Flood Plugin
     5  * Protects against chat flooding.
     6  *
     7  * SourceMod (C)2004-2008 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 	sm_flood_time = CreateConVar("sm_flood_time", "0.75", "Amount of time allowed between chat messages");
    55 }
    56 
    57 public OnClientPutInServer(client)
    58 {
    59 	g_LastTime[client] = 0.0;
    60 	g_FloodTokens[client] = 0;
    61 }
    62 
    63 new Float:max_chat;
    64 
    65 public bool:OnClientFloodCheck(client)
    66 {
    67 	max_chat = GetConVarFloat(sm_flood_time);
    68 	
    69 	if (max_chat <= 0.0 
    70  		|| CheckCommandAccess(client, "sm_flood_access", ADMFLAG_ROOT, true))
    71 	{
    72 		return false;
    73 	}
    74 	
    75 	if (g_LastTime[client] > GetGameTime())
    76 	{
    77 		/* If player has 3 or more flood tokens, block their message */
    78 		if (g_FloodTokens[client] >= 3)
    79 		{
    80 			return true;
    81 		}
    82 	}
    83 	
    84 	return false;
    85 }
    86 
    87 public OnClientFloodResult(client, bool:blocked)
    88 {
    89 	if (max_chat <= 0.0 
    90  		|| CheckCommandAccess(client, "sm_flood_access", ADMFLAG_ROOT, true))
    91 	{
    92 		return;
    93 	}
    94 	
    95 	new Float:curTime = GetGameTime();
    96 	new Float:newTime = curTime + max_chat;
    97 	
    98 	if (g_LastTime[client] > curTime)
    99 	{
   100 		/* If the last message was blocked, update their time limit */
   101 		if (blocked)
   102 		{
   103 			newTime += 3.0;
   104 		}
   105 		/* Add one flood token when player goes over chat time limit */
   106 		else if (g_FloodTokens[client] < 3)
   107 		{
   108 			g_FloodTokens[client]++;
   109 		}
   110 	}
   111 	else if (g_FloodTokens[client] > 0)
   112 	{
   113 		/* Remove one flood token when player chats within time limit (slow decay) */
   114 		g_FloodTokens[client]--;
   115 	}
   116 	
   117 	g_LastTime[client] = newTime;
   118 }