Y_Less

Chat flood protection

Oct 7th, 2007
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.97 KB | None | 0 0
  1. #include <a_samp>
  2.  
  3. // Limits you to a maximum of 10 bits of text in any 60s period
  4. #define TIME_LIMIT  60000
  5. #define MAX_TEXT    10
  6.  
  7. // Comment out the following lines to customise the code
  8. #define PROTECT_TEXT    // Filter text
  9. #define PROTECT_PMS     // Filter PMs
  10. #define PROTECT_CMD     // Filter Commands
  11.  
  12. #define IGNORE_ADMINS   // Let admins talk as much as they like
  13.  
  14. #define WARN_PLAYER     // Send the player a flood exceeded message
  15.  
  16. #if defined WARN_PLAYER
  17.  
  18.     // Color for warning message to be displayed in (red)
  19.     #define WARN_COLOR 0xFF0000AA
  20.    
  21.     // UNCOMMENT ONE OF THE FOLLOWING
  22.     // OR WRITE YOUR OWN MESSAGE
  23.    
  24.     // Message to be displayed to the player - without time
  25.     //#define WARN_MESSAGE "*** Flood limit exceeded, please try again in a moment"
  26.    
  27.     // Message to be displayed to the player - with time
  28.     #define WARN_MESSAGE "*** Flood limit exceeded, please try again in %d seconds"
  29.    
  30. #endif
  31.  
  32. /*
  33. ================================================================================
  34.  
  35.                         Edit ABOVE this line!
  36.             And where marked below for flitered commands
  37.  
  38. ================================================================================
  39. */
  40.  
  41. #if !defined PROTECT_TEXT && !defined PROTECT_PMS && !defined PROTECT_CMD
  42.     #error "You must filter somthing! - Make sure a PROTECT_XXX is uncommented."
  43. #endif
  44.  
  45.  
  46. public OnFilterScriptInit()
  47. {
  48.     print("\n---------------------------------------");
  49.     print("|    Flood protection filterscript    |");
  50.     print("|        (c) SA:MP team - 2007        |");
  51.     print("---------------------------------------\n");
  52.     return 1;
  53. }
  54.  
  55. #if defined PROTECT_TEXT
  56.  
  57.     // Check for floods on standard text
  58.     public OnPlayerText(playerid, text[])
  59.     {
  60.         // Call one function to reduce globals and combine flooding checks
  61.         if (!CheckFlooding(playerid)) return 1;
  62.         return 0;
  63.         #pragma unused text
  64.     }
  65.  
  66. #endif
  67.  
  68. #if defined PROTECT_PMS
  69.  
  70.     // Check for floods on private messages
  71.     public OnPlayerPrivmsg(playerid, recieverid, text[])
  72.     {
  73.         if (!CheckFlooding(playerid)) return 1;
  74.         return 0;
  75.         #pragma unused recieverid, text
  76.     }
  77.  
  78. #endif
  79.  
  80. #if defined PROTECT_CMD
  81.  
  82.     // Check for floods on specific commands
  83.     public OnPlayerCommandText(playerid, cmdtext[])
  84.     {
  85. /*
  86. ================================================================================
  87.  
  88.                         Edit BELOW this line!
  89.             Put command names you want flood protected here
  90.  
  91. ================================================================================
  92. */
  93.         static sFilteredCommands[][] =
  94.         {
  95.             "/me",
  96.             "/say",
  97.             "/announce"
  98.         };
  99. /*
  100. ================================================================================
  101.  
  102.                         Edit ABOVE this line!
  103.  
  104. ================================================================================
  105. */
  106.         // More processing required so save time here
  107.         #if defined IGNORE_ADMINS
  108.             if (IsPlayerAdmin(playerid)) return 0;
  109.         #endif
  110.         // Loop through text based commands
  111.         for (new i = 0; i < sizeof (sFilteredCommands); i++)
  112.         {
  113.             // If we have this command in the list
  114.             if (!strcmp(cmdtext, sFilteredCommands[i]))
  115.             {
  116.                 // Faster than two strlens
  117.                 new len = strlen(sFilteredCommands[i]);
  118.                 // Check command, e.g. /me not /meths
  119.                 if (cmdtext[len] == '\0' || cmdtext[len] == ' ')
  120.                 {
  121.                     // Mark as command processed
  122.                     if (CheckFlooding(playerid)) return 1;
  123.                     // Break out
  124.                     break;
  125.                 }
  126.             }
  127.         }
  128.         return 0;
  129.         #pragma unused cmdtext
  130.     }
  131.  
  132. #endif
  133.  
  134. #define CEIL_DIV(%1,%2) (((%1) + (%2) - 1) / (%2))
  135.  
  136. #if !defined WARN_COLOR
  137.     #define WARN_COLOR 0xFF0000AA
  138. #endif
  139.  
  140. #if !defined WARN_MESSAGE
  141.     #define WARN_MESSAGE "*** Flood limit exceeded, please try again in %d seconds"
  142. #endif
  143.  
  144. #if !defined TIME_LIMIT
  145.     #define TIME_LIMIT 60000
  146. #endif
  147.  
  148. #if !defined MAX_TEXT
  149.     #define MAX_TEXT 10
  150. #endif
  151.  
  152. CheckFlooding(playerid)
  153. {
  154.     // Holds the times of the last MAX_TEXT messages
  155.     static sTextTimes[MAX_PLAYERS][MAX_TEXT];
  156.     // Holds the current stack pointer for new messages
  157.     static sTextIndex[MAX_PLAYERS];
  158.     #if defined IGNORE_ADMINS
  159.         // If admins aren't flood protected break out
  160.         if (IsPlayerAdmin(playerid)) return 0;
  161.     #endif
  162.     // Works on Linux and Windows now
  163.     new tickCount = GetTickCount();
  164.     // Get the current point in the flood time array
  165.     new index = sTextIndex[playerid];
  166.     // Check this is less than the flood cutoff (normally MAX_TEXT before)
  167.     if (sTextTimes[playerid][index] <= (tickCount - TIME_LIMIT))
  168.     {
  169.         // Save the new time
  170.         sTextTimes[playerid][index] = tickCount;
  171.         // Increment to the next messages time
  172.         index++;
  173.         // Wrap around
  174.         index %= MAX_TEXT;
  175.         // Save this value statically
  176.         sTextIndex[playerid] = index;
  177.         // Mark as OK
  178.         return 0;
  179.     }
  180.     #if defined WARN_PLAYER
  181.         new str[128]; // More than enough normally
  182.         // Calculate the time remaining in seconds
  183.         format(str, sizeof (str), WARN_MESSAGE, CEIL_DIV(TIME_LIMIT - (tickCount - sTextTimes[playerid][index]), 1000));
  184.         // Send the client their warning
  185.         SendClientMessage(playerid, WARN_COLOR, str);
  186.     #endif
  187.     // Mark as bad
  188.     return 1;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment