Badword Filter

Description

Whenever a player says a badword (one of the ones listed in BadWordsList.txt) they are optionally kicked, and their number of used warnings is increased. Once they use up all of their warnings, they are either kicked or banned.

Download

Source Code

  1. //config
  2. const
  3. NumWarnings = 3; //number of times a player can curse before getting kicked or banned
  4. FileName = 'BadWordsList.txt'; //file which holds the badwords, each seperated with a space
  5. Ban = false; //When a player reaches the curse limit, ban the player? (true or false)
  6. KillOnOffense = true; //With each offense, kill the player? (true or false)
  7. TextColor = $ffff00001; //color of messages told to player
  8. BanLength = 15; //minutes a player will be banned for, if the Ban = true;
  9.  
  10. //holders of information
  11. var
  12. Warnings: array[1..32] of integer; //holds the number of warnings for each player
  13. FileContents: string; //contents of badwords file
  14. BadWords: TStringArray;
  15. HaveBadWords: boolean;
  16.  
  17. //credits to DorkeyDear - http://www.soldatforums.com/index.php?topic=27844.0
  18. function Explode(Source: string; const Delimiter: string): array of string;
  19. var TempStr: string;
  20. begin
  21. Source := Source + Delimiter;
  22. repeat
  23. TempStr := GetPiece(Source, Delimiter, 0);
  24. SetArrayLength(Result, GetArrayLength(Result) + 1);
  25. Result[GetArrayLength(Result) - 1] := TempStr;
  26. Delete(Source, 1, Length(TempStr) + Length(Delimiter));
  27. until Length(Source) = 0;
  28. end;
  29.  
  30. //load badwords list
  31. procedure ActivateServer();
  32. begin
  33. if not FileExists(FileName) then
  34. begin
  35. WriteLn('Cannot find badwords file')
  36. HaveBadWords := false;
  37. exit;
  38. end;
  39. FileContents := UpperCase(ReadFile(FileName)); //we're making the list of badword's case uppercase so they are case insensitive
  40. BadWords := Explode(FileContents, ' ');
  41. HaveBadWords := true;
  42. end;
  43.  
  44. //whenever someone says something, check it.
  45. procedure OnPlayerSpeak(PlayerID: Byte; Text: string);
  46. var i: integer;
  47. var Bad: boolean;
  48. begin
  49. //do we any badwords to check?
  50. if not HaveBadWords then
  51. exit;
  52. //for now, make it false
  53. Bad := false;
  54. //check it!
  55. for i := 0 to GetArrayLength(BadWords) - 1 do
  56. begin
  57. //if it is a bad word, make it known that we cursed
  58. if ContainsString(UpperCase(Text), BadWords[i]) then //we're making what they said upercase so the badwords are case insensitive
  59. Bad := true;
  60. end;
  61. //saying something naughty?
  62. if Bad then
  63. begin
  64. //increase number of warnings used
  65. inc(Warnings[PlayerID], 1);
  66. //reached limit
  67. if Warnings[PlayerID] = NumWarnings then
  68. begin
  69. //reset limit for this id
  70. Warnings[PlayerID] := 0;
  71. //Ban or kick?
  72. if Ban then
  73. begin
  74. BanPlayer(PlayerID, 15);
  75. exit;
  76. end;
  77. KickPlayer(PlayerID);
  78. exit;
  79. end;
  80. //under limit..
  81. if Warnings[PlayerID] < NumWarnings then
  82. begin
  83. //should we kill him / her?
  84. if KillOnOffense then
  85. DoDamage(PlayerID, 4000);
  86. //notify player of this
  87. WriteConsole(PlayerID, 'You have been warned for offensive language.', TextColor);
  88. WriteConsole(PlayerID, 'If you curse '+inttostr(NumWarnings - Warnings[PlayerID])+' more time(s), you will be '+iif(Ban,'banned','kicked')+'.', TextColor);
  89. end;
  90. end;
  91. end;
  92.