Author Topic: Please check this for compatibility! serverCmdMessageSent  (Read 1448 times)

Chat is a sensitive thing that breaks if you don't do things right, so please check my function to make sure I'm not doing anything wrong.

Code: [Select]
function servercmdMessageSent(%client,%msg)
{
%msg = stripMLControlChars(trim(%msg));
%time = getSimTime();

if(%msg $= "")
return;

%mini = getMinigameFromObject(%client);
%team = %client.getTeam();

%name = %client.getPlayerName();
%pre  = %client.clanPrefix;
%suf  = %client.clanSuffix;

if(%mini.isSlayerMinigame)
{
if(%client.dead() && !%mini.allowDeadTalk)
{
messageClient(%client,'',"\c5No, you can't talk. How many dead people do you know that can talk?");
return;
}

if(isObject(%team))
{
%tColor = %team.getPref("Chat","Name Color");
if(%tColor $= "" || strLen(%tColor) != 6)
%color = %team.getColorHex();
else
%color = "<color:" @ %tColor @ ">";

switch(%mini.chat_teamDisplayMode)
{
case 1:
%all  = '%5%1\c3%2%5%3\c6: %4';

case 2:
%all  = '\c7%1%5%2\c7%3\c6: %4';

case 3:
%all  = '\c7[%5%6\c7] %1\c3%2\c7%3\c6: %4';
}
}
}

if(%all $= "")
%all  = '\c7%1\c3%2\c7%3\c6: %4';

//did they repeat the same message recently?
if(%msg $= %client.lastMsg && %time-%client.lastMsgTime < $SPAM_PROTECTION_PERIOD)
{
if(!%client.isSpamming)
{
messageClient(%client,'',"\c5Do not repeat yourself.");
if(!%client.isAdmin)
{

%client.isSpamming = 1;
%client.spamProtectStart = %time;
%client.schedule($SPAM_PENALTY_PERIOD,spamReset);
}
}
}

//are they sending messages too quickly?
if(!%client.isAdmin && !%client.isSpamming)
{
if(%client.spamMessageCount >= $SPAM_MESSAGE_THRESHOLD)
{
%client.isSpamming = 1;
%client.spamProtectStart = %time;
%client.schedule($SPAM_PENALTY_PERIOD,spamReset);
}
else
{
%client.spamMessageCount ++;
%client.schedule($SPAM_PROTECTION_PERIOD,spamMessageTimeout);
}
}

//tell them they're spamming and block the message
if(%client.isSpamming)
{
spamAlert(%client);
return;
}

//eTard Filter, which I hate, but have to include
if($Pref::Server::eTardFilter)
{
%list = strReplace($Pref::Server::eTardList,",","\t");

for(%i=0; %i < getFieldCount(%list); %i++)
{
%wrd = getField(%list,%i);
if(strStr(" " @ %msg @ " ",%wrd) != -1)
{
messageClient(%client,'',"\c5This is a civilized game. Please use full words.");
return;
}
}
}

//URLs
%pos = striPos(%msg,"http://");
if(%pos >= 0)
{
%url = getSubStr(%msg,%pos+7,striPos(%msg," ",%pos));
%msg = strReplace(%msg,"http://" @ %url,"<a:" @ %url @ ">" @ %url @ "</a>");
}

commandToAll('chatMessage',%client,'','',%all,%pre,%name,%suf,%msg,%color,%team.name);
echo(%client.getPlayerName() @ ":" SPC %msg);

%client.lastMsg = %msg;
%client.lastMsgTime = %time;

if(%mini.isSlayerMinigame)
{
if(isFunction("Slayer_" @ %mini.mode @ "_onChat"))
call("Slayer_" @ %mini.mode @ "_onChat",%mini,%client,%msg);
}
}

You can mush the whole flood protection into simply one if check.
Also, you should call serverCmdStopTalking (default chat does that (yes, client sends it too but it's for being certain)).

Code: [Select]
function servercmdMessageSent(%client,%msg)
{
serverCmdStopTalking(%client); // this part

%msg = stripMLControlChars(trim(%msg));
%time = getSimTime();

if(%msg $= "" || spamAlert(%client)) // this part
return;

%mini = getMinigameFromObject(%client);
%team = %client.getTeam();

%name = %client.getPlayerName();
%pre  = %client.clanPrefix;
%suf  = %client.clanSuffix;

if(%mini.isSlayerMinigame)
{
if(%client.dead() && !%mini.allowDeadTalk)
{
messageClient(%client,'',"\c5No, you can't talk. How many dead people do you know that can talk?");
return;
}

if(isObject(%team))
{
%tColor = %team.getPref("Chat","Name Color");
if(%tColor $= "" || strLen(%tColor) != 6)
%color = %team.getColorHex();
else
%color = "<color:" @ %tColor @ ">";

switch(%mini.chat_teamDisplayMode)
{
case 1:
%all  = '%5%1\c3%2%5%3\c6: %4';

case 2:
%all  = '\c7%1%5%2\c7%3\c6: %4';

case 3:
%all  = '\c7[%5%6\c7] %1\c3%2\c7%3\c6: %4';
}
}
}

if(%all $= "")
%all  = '\c7%1\c3%2\c7%3\c6: %4';

//did they repeat the same message recently?
if(%msg $= %client.lastMsg && %time-%client.lastMsgTime < $SPAM_PROTECTION_PERIOD)
{
if(!%client.isSpamming)
{
messageClient(%client,'',"\c5Do not repeat yourself.");
}
}

//eTard Filter, which I hate, but have to include
if($Pref::Server::eTardFilter)
{
%list = strReplace($Pref::Server::eTardList,",","\t");

for(%i=0; %i < getFieldCount(%list); %i++)
{
%wrd = getField(%list,%i);
if(strStr(" " @ %msg @ " ",%wrd) != -1)
{
messageClient(%client,'',"\c5This is a civilized game. Please use full words.");
return;
}
}
}

//URLs
%pos = striPos(%msg,"http://");
if(%pos >= 0)
{
%url = getSubStr(%msg,%pos+7,striPos(%msg," ",%pos));
%msg = strReplace(%msg,"http://" @ %url,"<a:" @ %url @ ">" @ %url @ "</a>");
}

commandToAll('chatMessage',%client,'','',%all,%pre,%name,%suf,%msg,%color,%team.name);
echo(%client.getPlayerName() @ ":" SPC %msg);

%client.lastMsg = %msg;
%client.lastMsgTime = %time;

if(%mini.isSlayerMinigame)
{
if(isFunction("Slayer_" @ %mini.mode @ "_onChat"))
call("Slayer_" @ %mini.mode @ "_onChat",%mini,%client,%msg);
}
}
« Last Edit: January 14, 2012, 01:17:33 AM by Port »


>includes flood protection
>blocks ML
>allows links
>includes e-tard filter and uses default e-tard setting
>uses 'chatMessage'
>echos message in console

Yeah you're doing it right.

Very nice, I needed to use flood protection in my mod with custom chat systems. Thanks Greek.