Foul Language Filter
by Owen Ortmayer · 07/03/2004 (11:42 pm) · 13 comments
Download Code File
*WARNING: This snippet contains a few expletive words to serve as example.
The project I'm working on may need something like this and I was bored so I whipped up a little piece of script that will replace explective words with any word of your choice. Decided to post it in case anyone could find it handy.
I coded it so you can hardcode the list of banned words and corresponding replacement words in the script file or read them in from a text file.
first you need to set up your list of banned words and replacement words
Now here's the function to clean a given string
Here given the string "damn this is crap" the function will return "darn this is crud"
To filter chat messages simply add the following code in common/server/commands.cs
Now if you want to store the list of banned words and replacement words in a text file the following function will read them in and override the hardcoded values.
Dont forget to tell the game to execute your script. I place mine in server/scripts/game.cs onServerCreated(). But if you need it to run earlier then place it elsewhere.
I simply have the code read in the lists from a file right after the exec:
Hope this helps. Note, this is just a very simple solution and not meant to be terribly robust.
*WARNING: This snippet contains a few expletive words to serve as example.
The project I'm working on may need something like this and I was bored so I whipped up a little piece of script that will replace explective words with any word of your choice. Decided to post it in case anyone could find it handy.
I coded it so you can hardcode the list of banned words and corresponding replacement words in the script file or read them in from a text file.
first you need to set up your list of banned words and replacement words
$bannedWords[0] = "damn"; $replacementWord[0] = "darn"; $bannedWords[1] = "crap"; $replacementWord[1] = "crud"; $bannedWords[2] = "asshole"; $replacementWord[2] = "bad man"; $numBannedWords = 3;
Now here's the function to clean a given string
function cleanString(%string)
{
for(%i=0; %i<$numBannedWords; %i++)
{
%string = Strreplace( %string, $bannedWords[%i], $replacementWord[%i] );
}
return %string;
}Here given the string "damn this is crap" the function will return "darn this is crud"
To filter chat messages simply add the following code in common/server/commands.cs
function serverCmdTeamMessageSent(%client, %text)
{
if(strlen(%text) >= $Pref::Server::MaxChatLen)
%text = getSubStr(%text, 0, $Pref::Server::MaxChatLen);
[b]%text = cleanString(%text);[/b]
chatMessageTeam(%client, %client.team, '\c3%1: %2', %client.name, %text);
}
function serverCmdMessageSent(%client, %text)
{
if(strlen(%text) >= $Pref::Server::MaxChatLen)
%text = getSubStr(%text, 0, $Pref::Server::MaxChatLen);
[b]%text = cleanString(%text);[/b]
chatMessageAll(%client, '\c4%1: %2', %client.name, %text);
}Now if you want to store the list of banned words and replacement words in a text file the following function will read them in and override the hardcoded values.
//banned word file should have one bannedWord, replacementWord
//pair per line seperated by a comma
//ex:
// damn, darn
// crap, crud
function readBannedWords(%bannedWordFile)
{
%file = new FileObject();
if( !%file.openForRead(%bannedWordFile) )
{
echo("file:" SPC %bannedWordFile SPC "not valid");
return;
}
%i = 0;
while( !%file.isEOF() )
{
%line = %file.readLine();
%bWord = "";
%stringLeft = nextToken(%line, bWord, ",");
%bWord = Trim(%bWord);
%rWord = Trim(%stringLeft);
$bannedWords[%i] = %bWord;
$replacementWord[%i] = %rWord;
%i++;
}
$numBannedWords = %i;
}Dont forget to tell the game to execute your script. I place mine in server/scripts/game.cs onServerCreated(). But if you need it to run earlier then place it elsewhere.
I simply have the code read in the lists from a file right after the exec:
exec("./stringClean.cs");
readBannedWords("starter.fps/server/scripts/bannedWords.txt");Hope this helps. Note, this is just a very simple solution and not meant to be terribly robust.
#2
07/04/2004 (6:09 am)
Actually no. I figured since Torque script was case insensitive it wouldn't matter. I guess you could add a Strlwr(%string) in there before processing.
#3
07/04/2004 (10:19 am)
What a $#@%! cool resource!
#4
07/04/2004 (12:36 pm)
Nice @#$@$# job, Owen. ;)
#5
07/04/2004 (3:21 pm)
This is f#*king cool
#6
One very minor optimization, if you're interested, change:
To:
That way you won't have to worry about your replacement text containing more characters than the replaced text in the chat window.
07/05/2004 (7:44 am)
Handy, thanks :)One very minor optimization, if you're interested, change:
if(strlen(%text) >= $Pref::Server::MaxChatLen)
%text = getSubStr(%text, 0, $Pref::Server::MaxChatLen);
%text = cleanString(%text);To:
%text = cleanString(%text);
if(strlen(%text) >= $Pref::Server::MaxChatLen)
%text = getSubStr(%text, 0, $Pref::Server::MaxChatLen);That way you won't have to worry about your replacement text containing more characters than the replaced text in the chat window.
#7
07/06/2004 (9:15 am)
Nice catch Keith
#8
07/08/2004 (4:59 am)
What if you typed "c r a p" or "_crap_" into chat?
#9
07/08/2004 (11:34 am)
For the first one you're @#$%ed:). It will catch the second one. Quote:If I find some more spare time I may try and make it a little more robust. It's sorta of lost cause b/c people will start using azz, craaap, and such.
Note, this is just a very simple solution and not meant to be terribly robust.
#10
I just hid the file and unless someone has all files unhidden then it wont be seen or found..
but there really should be a way to encrypt the files..
07/08/2004 (1:05 pm)
and how would you hide the text file also, if someone finds it and opens it wouldnt you have alot of people upset.. That a game has cuss words in it.. :)I just hid the file and unless someone has all files unhidden then it wont be seen or found..
but there really should be a way to encrypt the files..
#11
"Is this a cool item?"
Someone replies.
"No. Scrap it."
Wouldn't this transform that into.
"No. Scrud it."
Has potential though. Good to see you at work with it.
07/14/2004 (5:45 am)
What if you someone said."Is this a cool item?"
Someone replies.
"No. Scrap it."
Wouldn't this transform that into.
"No. Scrud it."
Has potential though. Good to see you at work with it.
#12
07/21/2004 (12:33 pm)
If you had "ass" as "donkey" and you typed the word "pass" wouldn't that change it to "pdonkey"
#13
11/05/2004 (6:27 pm)
I agree. People will just find replacements but good for trying! 
Torque Owner kc_0045
Default Studio Name
$bannedWords[3] = "jackass"; $replacementWord[3] = "donkey";
but ya just wondring well this work if you use caps? like well
cleanString("CRap")=crud?