Simple SpamMeter

From Soldat Community Wiki
Jump to: navigation, search

Script by Spkka (using xsplit by KeYDoN)

//CONSTANTS
const
MAXPLAYER = 16;
MAXSPAM = 100;

//VARIABLES
var
spamMeter: array [1..MAXPLAYER] of integer;
badWords: string;
GOOD, BAD: longint;

//XSPLIT
function xsplit(const source: string; const delimiter: string):TStringArray;
var
	i,x,d:integer;
	s:string;
begin
	d:=length(delimiter);
	x:=0;
	i:=1;
	SetArrayLength(Result,1);
	while(i<=length(source)) do
	begin
		s:=Copy(source,i,d);   	

	    	if(s=delimiter) then 
		begin
	    		i:=i+d;		
	    		inc(x,1);
	    		SetArrayLength(result,x+1);
	    	end else 
		begin  	     
	    		result[x]:= result[x]+copy(s,1,1);
	    		inc(i,1);
	  	end
	end
	if Result[ArrayHigh(Result)]='' then SetArrayLength(result,x);
end;

//ACTIVATESERVER
procedure ActivateServer();
var
	b: byte;
begin
	GOOD := $EE00FF00;
	BAD := $FFFF4444;

	for b := 1 to MAXPLAYER do
	begin
		//set spammeter for all id's to 0.
		spamMeter[b] := 0;
	end
	//read the badword file.
	//file should be in soldatserver/badwords/badwords.txt
	badWords := ReadFile('/badwords/badwords.txt');
end;

//APPONIDLE
procedure AppOnIdle(Ticks: integer);
var 
	i: byte;
begin
	//this will count down the spammeter with 1 per ID per second.
	for i := 1 to MAXPLAYER do
	begin
		spamMeter[i] := spamMeter[i] - 1;
	end	
end;

//ONPLAYERSPEAK
procedure OnPlayerSpeak(ID: byte; Text: string);
var 
	nick: string;
	numText: integer;
	b: byte;
	txts: TStringArray;
begin	
	//this will split the text by spaces.
	txts := xsplit( Text, ' ' );
	numText := Arrayhigh( txts );
	for b := 0 to numText do
	begin
		//check if text input contains a bad word.
		if Containsstring(badWords,lowercase(txts[b])) = true then
		begin
			//if so add 25 to spammeter.
			spamMeter[ID] := spamMeter[ID] + 25;
			//if spammeter is bigger then the max allowed spam, ban player.		
			if spamMeter[ID] > MAXSPAM then
			begin
				nick := (getPlayerStat(ID,'Name'));
				//ban with the reason spamming.
				BanPlayerReason(ID,0,nick+' spamming');
				spamMeter[ID] := 0;
			end else
			begin
				//if the spammeter is bigger then 30 alert player for spamming.
				if spamMeter[ID] > 30 then
				begin
					WriteConsole(ID,'Stop spamming the server!',BAD);
					WriteConsole(ID,'Spamming results in a permanent ban!',BAD);
				end
			end		
		exit;
		end
	end
end;

//ONLEAVEGAME
procedure OnLeaveGame(ID, Team: byte; Kicked: boolean);
begin
	//reset the spammeter from the player that is leaving.
	spamMeter[ID] := 0;
end;