//originaly idea and code : https://forums.alliedmods.net/showthread.php?t=137697
#pragma semicolon 1
#include <amxmodx>
#include <reapi>
#include <regex>
#define MAX_EXCEPT_LENGTH 64
new FILE_EXCEPTIONS[] = "antispam_exceptions.ini";
new DIR_EXCEPTIONS[] = "addons/amxmodx/configs";
new Array: g_aExceptions;
public plugin_precache() {
new exceptFile[110];
formatex(exceptFile, charsmax(exceptFile), "%s/%s", DIR_EXCEPTIONS, FILE_EXCEPTIONS);
new file = fopen(exceptFile, "rt");
if (file) {
g_aExceptions = ArrayCreate(MAX_EXCEPT_LENGTH);
new buffer[MAX_EXCEPT_LENGTH + 64], exceptNum;
while (!feof(file)) {
fgets(file, buffer, charsmax(buffer));
if (buffer[0] == EOS || buffer[0] == ';') {
continue;
}
ArrayPushString(g_aExceptions, buffer);
exceptNum++;
}
server_print("[AB] Loaded %d exceptions", exceptNum);
}
fclose(file);
}
public plugin_init() {
register_plugin("Antispam bots", "1.0.2", "cpctrl");
register_clcmd("say", "check_clientsmg");
register_clcmd("say_team", "check_clientsmg");
RegisterHookChain(RG_CBasePlayer_SetClientUserInfoName, "CBasePlayer_SetClientUserInfoName", false);
}
public CBasePlayer_SetClientUserInfoName(id, oldName[], newName[]) {
if (is_invalid(newName)) {
client_print_color(id, -1, "^4***^1 Don't spam in nick.");
SetHookChainReturn(ATYPE_BOOL, false);
return HC_SUPERCEDE;
}
return HC_CONTINUE;
}
public client_putinserver(id) {
new name[MAX_NAME_LENGTH];
get_user_name(id, name, MAX_NAME_LENGTH - 1);
if (is_invalid(name)) {
new exceptName[MAX_NAME_LENGTH];
ArrayGetString(g_aExceptions, random(ArraySize(g_aExceptions)), exceptName, MAX_NAME_LENGTH - 1);
set_entvar(id, var_netname, exceptName);
}
return PLUGIN_CONTINUE;
}
public check_clientsmg(id) {
new args[191];
read_args(args, charsmax(args));
remove_quotes(args);
if (is_invalid(args)) {
client_print_color(id, -1, "^4***^1 Don't spam in chat.");
return PLUGIN_HANDLED;
}
return PLUGIN_CONTINUE;
}
stock bool: is_invalid(const checkText[]) {
new error[50], num, Regex: regex;
if (g_aExceptions && check_exception(checkText)) {
return false;
}
regex = regex_match(checkText, "[a-z0-9-]{3,}\.[a-z]{1,2}(\S)", num, error, 49, "i");
if (regex >= REGEX_OK) {
regex_free(regex);
return true;
}
regex = regex_match(checkText, "([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}", num, error, 49);
if (regex >= REGEX_OK) {
regex_free(regex);
return true;
}
return false;
}
stock bool: check_exception(const checkText[]) {
return (ArrayFindString(g_aExceptions, checkText) != -1) ? true : false;
}