#include <amxmodx>
#include <amxmisc>
#include <regex>

#define PATTERN	"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" // \b
stock const advertising_words[][] = 
{
	"www.",
	".pl",
	".com",
	".net",
	".org",
	".no-ip",
	".ru"
} 

new g_allArgs[1024], Regex:g_result, g_error[64], g_returnvalue;

public plugin_init()
{
	register_plugin("Anty Reklama", "1.0", "PimP517");
	register_clcmd("say", "hook_say");
	register_clcmd("say_team", "hook_say");
	register_message(get_user_msgid("SayText"), "message")
}

//Chat
public hook_say(id)
{
	//Strony www
	read_args(g_allArgs, 1023)
	//Strony
	for(new i = 0 ; i < sizeof (advertising_words) ; i++) 
    { 
        if(containi(g_allArgs, advertising_words[i]) != -1) 
        { 
			return 1;  
        } 
    } 
	//Adresy IP
	g_result = regex_match(g_allArgs, PATTERN, g_returnvalue, g_error, 63)
	switch (g_result)
	{
		case REGEX_MATCH_FAIL:
		{
			log_amx("REGEX_MATCH_FAIL! %s", g_error)
			return PLUGIN_CONTINUE
		}
		case REGEX_PATTERN_FAIL:
		{
			log_amx("REGEX_PATTERN_FAIL! %s", g_error)
			return PLUGIN_CONTINUE
		}
		case REGEX_NO_MATCH: 
		{
			return PLUGIN_CONTINUE
		}
		default: 
		{
			regex_free(g_result)
			return PLUGIN_HANDLED 
		}
	}
	return PLUGIN_CONTINUE
}

//W nicku
public message()
{
	new arg[32]
	get_msg_arg_string(2, arg, 31)
	if(containi(arg,"name")!=-1)
	{
		return PLUGIN_HANDLED 
	}
	return PLUGIN_CONTINUE
}

public client_infochanged(id)
{
	new newname[32],oldname[32]
	get_user_info(id, "name", newname,31)
	get_user_name(id,oldname,31)
	if(!is_user_connected(id) || is_user_bot(id)) return PLUGIN_CONTINUE
	//Strony
	for(new i = 0 ; i < sizeof (advertising_words) ; i++) 
	{ 
		if(containi(newname, advertising_words[i]) != -1) 
		{ 
			set_user_info(id,"name",oldname)
			return PLUGIN_HANDLED
		} 
	}
	g_result = regex_match(newname, PATTERN, g_returnvalue, g_error, 63)
	switch (g_result)
	{
		case REGEX_MATCH_FAIL:
		{
			log_amx("REGEX_MATCH_FAIL! %s", g_error)
			return PLUGIN_CONTINUE
		}
		case REGEX_PATTERN_FAIL:
		{
			log_amx("REGEX_PATTERN_FAIL! %s", g_error)
			return PLUGIN_CONTINUE
		}
		case REGEX_NO_MATCH: 
		{
			return PLUGIN_CONTINUE
		}
		default: 
		{
			regex_free(g_result)
			set_user_info(id,"name",oldname)
			return PLUGIN_HANDLED
		}
	}
	return PLUGIN_CONTINUE
}

public client_putinserver(id)
{
	new name[32]
	get_user_name(id,name,31);
	for(new i = 0 ; i < sizeof (advertising_words) ; i++) 
	{ 
		if(containi(name, advertising_words[i]) != -1) 
		{ 
			set_user_info(id,"name","Zmien Nick")
			return PLUGIN_HANDLED
		} 
	}
	g_result = regex_match(name, PATTERN, g_returnvalue, g_error, 63)
	switch (g_result)
	{
		case REGEX_MATCH_FAIL:
		{
			log_amx("REGEX_MATCH_FAIL! %s", g_error)
			return PLUGIN_CONTINUE
		}
		case REGEX_PATTERN_FAIL:
		{
			log_amx("REGEX_PATTERN_FAIL! %s", g_error)
			return PLUGIN_CONTINUE
		}
		case REGEX_NO_MATCH: 
		{
			return PLUGIN_CONTINUE
		}
		default: 
		{
			regex_free(g_result)
			set_user_info(id,"name","Zmien Nick")
			return PLUGIN_HANDLED
		}
	}
	return PLUGIN_CONTINUE
}