Raised This Month: $ Target: $400
 0% 

Swear filter


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ayman Khaled
Senior Member
Join Date: Mar 2017
Location: Palestine
Old 05-30-2017 , 10:34   Swear filter
Reply With Quote #1

Hello guys,
i have this plugin
the problem is when i put a word in swear_filters.ini like
"regex" "ass|ks" ( ks is a swear word in Arabic )
when i write Classic , Server take it as a swear because Cl'ass'ic word
/rankstats, thanks and so on, server take them as a swear .

PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <regex>

// Comment out this line to only block the messages from chat
// #define GAG_PLAYER

#if defined GAG_PLAYER
#include <nvault>
#include <unixtime>
#endif

// max length of the pattern
// if it seems that it cuts off when loading, increase this value appropriately
#define MAX_PATTERN_LEN 128

enum _:FilterData {
    
Filter_IsRegex,
    
Filter_Pattern,
    
Filter_String[MAX_PATTERN_LEN 1],
    
Filter_MatchIndex,
    
Filter_HideOnly
};

new Array:
gFilterData;
new 
gNumFilters;

new Array:
gWhiteLists;
new 
gNumWhiteLists;

new 
gReturnFromRegex;

#if defined GAG_PLAYER
new Array:gGagTimes;
new 
gNumTimes;

new const 
gDefaultTimes[] = {
    
51015
};

new 
gCvarWarning;

new 
gVault;
#endif

new gCvarLog;
new 
gLogDir[64];

new 
gCvarHideHard;

public 
plugin_init() {
    new const 
VERSION[] = "0.0.6";
#if defined GAG_PLAYER
    
register_plugin("Swear Filter [+Gag]"VERSION"Exolent");
#else
    
register_plugin("Swear Filter"VERSION"Exolent");
#endif
    
    // load the swear pattern before anything
    // so we can stop the plugin in case there are no words
    
LoadPattern();
    
    
// load white lists
    
LoadWhiteLists();
    
#if defined GAG_PLAYER
    
register_srvcmd("swear_filter_times""CmdSetTimes");
#endif
    
    
register_clcmd("say",      "CmdSay");
    
register_clcmd("say_team""CmdSay");
    
#if defined GAG_PLAYER
    
gCvarWarning register_cvar("swear_filter_warning""* WARNING! You will be gagged next time you swear!");
    
    
gGagTimes ArrayCreate();
    
    
// default times
    
for(new 0sizeof(gDefaultTimes); i++) {
        
ArrayPushCell(gGagTimesgDefaultTimes[i]);
    }
    
    
gNumTimes sizeof(gDefaultTimes);
    
    
// prepare our vault
    
gVault nvault_open("swear_filter");
    
    
// grab this time's info
    
new yearmonthdayhourminutesecond;
    
UnixToTime(get_systime(), yearmonthdayhourminutesecond);
    
    
// prune from the beginning of time to the start of the day
    
nvault_prune(gVault0TimeToUnix(yearmonthday000));
#endif
    
    
gCvarLog register_cvar("swear_filter_log""1");
    
gCvarHideHard register_cvar("swear_filter_hide_hard""0");
    
    
get_localinfo("amxx_logs"gLogDircharsmax(gLogDir));
    
add(gLogDircharsmax(gLogDir), "/swear_filter");
    
    if(!
dir_exists(gLogDir)) {
        
mkdir(gLogDir);
    }
}

public 
plugin_end() {
    new 
filterData[FilterData];
    
    for(new 
0gNumFiltersi++) {
        
ArrayGetArray(gFilterDataifilterData);
        
        if(
filterData[Filter_IsRegex]) {
            
regex_free(Regex:filterData[Filter_Pattern]);
        }
    }
    
    
ArrayDestroy(gFilterData);
    
#if defined GAG_PLAYER
    
ArrayDestroy(gGagTimes);
    
    
nvault_close(gVault);
#endif
    
    
new Regex:pattern;
    
    for(new 
0gNumWhiteListsi++) {
        
pattern ArrayGetCell(gWhiteListsi);
        
        
regex_free(pattern);
    }
    
    
ArrayDestroy(gWhiteLists);
}

#if defined GAG_PLAYER
public CmdSetTimes() {
    new 
numArgs read_argc() - 1;
    
    if(
numArgs 1) {
        
log_amx("No times provided to swear_filter_times command.");
        return 
PLUGIN_HANDLED;
    }
    
    new 
arg[12];
    for(new 
1<= numArgsi++) {
        
read_argv(iargcharsmax(arg));
        
        
ArrayPushCell(gGagTimesstr_to_num(arg));
    }
    
    
gNumTimes numArgs;
    
    
log_amx("Gag times set.");
    
    return 
PLUGIN_HANDLED;
}
#endif

public CmdSay(id) {
    new 
args[194];
    
read_args(argscharsmax(args));
    
remove_quotes(args);
    
    new 
filterData[FilterData], match[192], bool:matched;
    for(new 
0jRegex:patterngNumFiltersi++) {
        
ArrayGetArray(gFilterDataifilterData);
        
        
// check if this matched a swear filter or contained a bad word
        
if(filterData[Filter_IsRegex] ? (regex_match_c(argsRegex:filterData[Filter_Pattern], gReturnFromRegex) > 0) : (containi(argsfilterData[Filter_String]) >= 0)) {
            
// if we matched a swear filter and we need to check for white list
            
if(filterData[Filter_IsRegex] && filterData[Filter_MatchIndex] >= 0) {
                
// make sure we have valid match index
                
if(filterData[Filter_MatchIndex] >= gReturnFromRegex) {
                    
log_amx("Could not check white list for this pattern ^"%s^" because the match index is too high.");
                } else {
                    
// let's check for white lists
                    
regex_substr(Regex:filterData[Filter_Pattern], filterData[Filter_MatchIndex], matchcharsmax(match));
                    
                    
matched false;
                    
                    for(
0gNumWhiteListsj++) {
                        
pattern ArrayGetCell(gWhiteListsj);
                        
                        if(
regex_match_c(matchpatterngReturnFromRegex) > 0) {
                            
matched true;
                            break;
                        }
                    }
                    
                    if(
matched) {
                        continue;
                    }
                }
            }
            
            
// if this is just to hide from chat
            
if(filterData[Filter_HideOnly]) {
                return 
get_pcvar_num(gCvarHideHard) ? PLUGIN_HANDLED PLUGIN_HANDLED_MAIN;
            }
            
            if(
get_pcvar_num(gCvarLog)) {
                new 
dateString[11];
                
get_time("%Y-%m-%d"dateStringcharsmax(dateString));
                
                new 
fileName[128];
                
formatex(fileNamecharsmax(fileName), "%s/%s.log"gLogDirdateString);
                
                new 
fopen(fileName"a+");
                
                if(
f) {
                    new 
timeString[9];
                    
get_time("%H:%M:%S"timeStringcharsmax(timeString));
                    
                    new 
name[32], steamID[35];
                    
get_user_name(idnamecharsmax(name));
                    
get_user_authid(idsteamIDcharsmax(steamID));
                    
                    
fprintf(f"Player: %s <%s>^n"namesteamID);
                    
fprintf(f"Time: %s^n"timeString);
                    
fprintf(f"Message: ^"%s^"^n"args);
                    
                    if(
filterData[Filter_IsRegex]) {
                        
fprintf(f"Matched pattern: ^"%s^"^n"filterData[Filter_String]);
                        
                        
fputs(f"Matches:^n");
                        
                        for(new 
0gReturnFromRegexi++) {
                            
regex_substr(Regex:filterData[Filter_Pattern], iargscharsmax(args));
                            
                            
fprintf(f"- ^"%s^"^n"args);
                        }
                    } else {
                        
fprintf(f"Contained string: ^"%s^"^n"filterData[Filter_String]);
                    }
                    
                    
fputs(f"^n=======================================^n^n");
                    
                    
fclose(f);
                } else {
                    
log_amx("Failed to open log file for writing: %s"fileName);
                }
            }
        
#if defined GAG_PLAYER
            
new steamID[35];
            
get_user_authid(idsteamIDcharsmax(steamID));
            
            new 
gagIndex nvault_get(gVaultsteamID);
            
            if(
gagIndex) {
                
server_cmd("amx_gag #%d %d"get_user_userid(id), ArrayGetCell(gGagTimesmin(gagIndexgNumTimes) - 1));
            } else {
                
get_pcvar_string(gCvarWarningargscharsmax(args));
                
                
client_print(idprint_chat"%s"args);
            }
            
            
num_to_str(gagIndex 1argscharsmax(args));
            
nvault_set(gVaultsteamIDargs);
#else
            
client_print(idprint_chat"* Your message was caught by the swear filter!");
#endif
            
            
return PLUGIN_HANDLED;
        }
    }
    
    return 
PLUGIN_CONTINUE;
}

LoadPattern() {
    new 
fileName[64];
    
get_configsdir(fileNamecharsmax(fileName));
    
add(fileNamecharsmax(fileName), "/swear_filters.ini");
    
    new 
fopen(fileName"rt");
    
    if(
f) {
        
gFilterData ArrayCreate(FilterData);
        
        new 
data[64 MAX_PATTERN_LEN], type[8], filterData[FilterData], indexString[12], hideString[12];
        
        new 
quoteIndexi;
        new const 
quoteReplacements[][] = {
            
"&quot;""\'""%'""!qu'"
        
};
        
        while(!
feof(f)) {
            
fgets(fdatacharsmax(data));
            
trim(data);
            
            
quoteIndex = -1;
            
            if(
contain(data"\^"") >= 0) {
                for(i = 0; i < sizeof(quoteReplacements); i++) {
                    if(contain(data, quoteReplacements[i]) == -1) {
                        quoteIndex = i;
                        
                        replace_all(data, charsmax(data), "
\^""quoteReplacements[i]);
                        break;
                    }
                }
            }
            
            if(!
data[0] || data[0] == ';' || data[0] == '/' && data[1] == '/'
            
|| parse(datatypecharsmax(type), filterData[Filter_String], charsmax(filterData[Filter_String]), indexStringcharsmax(indexString), hideStringcharsmax(hideString)) < 2) {
                continue;
            }
            
            
strtolower(type);
            
            if(!
equal(type"regex") && !equal(type"string")) {
                continue;
            }
            
            if(
quoteIndex != -1) {
                
replace_all(filterData[Filter_String], charsmax(filterData[Filter_String]), quoteReplacements[quoteIndex], "^"");
            }
            
            if((filterData[Filter_IsRegex] = (type[0] == 'r'))) {
                if(!indexString[0] || !is_str_num(indexString) || (filterData[Filter_MatchIndex] = str_to_num(indexString)) < 0) {
                    filterData[Filter_MatchIndex] = -1;
                }
                
                filterData[Filter_Pattern] = _:regex_compile(filterData[Filter_String], gReturnFromRegex, data, charsmax(data), "
i");
                
                if(Regex:filterData[Filter_Pattern] < REGEX_OK) {
                    log_amx("
Error with Regex pattern ^"%s^": %%s", filterData[Filter_String], gReturnFromRegex, data);
                    continue;
                }
            }
            
            filterData[Filter_HideOnly] = _:(equali(indexString, "
hide") || equali(hideString, "hide"));
            
            ArrayPushArray(gFilterData, filterData);
            gNumFilters++;
        }
        
        fclose(f);
        
        if(!gNumFilters) {
            ArrayDestroy(gFilterData);
            
            set_fail_state("
No swear words to blockDisabling plugin");
        }
    } else {
        log_amx("
Unable to open %s", fileName);
        
        set_fail_state("
No swear filter file found.");
    }
}

LoadWhiteLists() {
    gWhiteLists = ArrayCreate();
    
    new fileName[64];
    get_configsdir(fileName, charsmax(fileName));
    add(fileName, charsmax(fileName), "
/swear_whitelist.ini");
    
    new f = fopen(fileName, "
rt");
    
    if(f) {
        new data[MAX_PATTERN_LEN + 8], Regex:pattern, patternString[MAX_PATTERN_LEN + 1], len;
        new Regex:patternForPatterns = regex_compile("
^^\/.+\/$", gReturnFromRegex, data, charsmax(data));
        
        if(patternForPatterns < REGEX_OK) {
            log_amx("
Could not create pattern to find white lists: (%d) %s", gReturnFromRegex, data);
            
            fclose(f);
            return;
        }
        
        while(!feof(f)) {
            len = fgets(f, data, charsmax(data)) - trim(data);
            
            if(regex_match_c(data, patternForPatterns, gReturnFromRegex) <= 0) {
                continue;
            }
            
            copy(patternString, min(charsmax(patternString), len - 2), data[1]);
            
            pattern = regex_compile(patternString, gReturnFromRegex, data, charsmax(data), "
i");
            
            if(pattern < REGEX_OK) {
                log_amx("
Could not create pattern for ^"%s^": (%d) %s", patternString, gReturnFromRegex, data);
                continue;
            }
            
            ArrayPushCell(gWhiteLists, pattern);
            gNumWhiteLists++;
        }
        
        fclose(f);
    } else {
        log_amx("
Unable to open %s", fileName);
    }

__________________
Ayman Khaled is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 05-30-2017 , 10:52   Re: Swear filter
Reply With Quote #2

Use "\b" in front and in the end of the word.

"\bass\b|\bks\b"
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Ayman Khaled
Senior Member
Join Date: Mar 2017
Location: Palestine
Old 05-30-2017 , 11:19   Re: Swear filter
Reply With Quote #3

Quote:
Originally Posted by OciXCrom View Post
Use "\b" in front and in the end of the word.

"\bass\b|\bks\b"
Thanks a lot ! it's working.
__________________
Ayman Khaled is offline
PowereXe
Member
Join Date: Jan 2017
Location: Turkey, Istanbul
Old 05-30-2017 , 16:57   Re: Swear filter
Reply With Quote #4

This Plugin Works Well With Admin Chat Colors?
PowereXe is offline
Ayman Khaled
Senior Member
Join Date: Mar 2017
Location: Palestine
Old 05-30-2017 , 17:00   Re: Swear filter
Reply With Quote #5

Quote:
Originally Posted by PowereXe View Post
This Plugin Works Well With Admin Chat Colors?
idk, test it.
__________________
Ayman Khaled is offline
PowereXe
Member
Join Date: Jan 2017
Location: Turkey, Istanbul
Old 05-30-2017 , 17:08   Re: Swear filter
Reply With Quote #6

You Are Very Helpful Bro, Thx For Helping.
I Should Give +Karma To You.
PowereXe is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:57.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode