/*
Copyright 2011 - 2012, m0skVi4a ;]
Plugin created in Rousse, Bulgaria


Plugin thread 1:
https://forums.alliedmods.net/showthread.php?t=171460

Plugin thread 2:
http://amxmodxbg.org/forum/viewtopic.php?t=37116

Original posted by m0skVi4a ;]



Description:

This is Register System. You can put a password to your name and if someone connect to the server with the same name he will be kicked if he does not login.


Commands:

say /reg
say_team /reg
Open the register system menu


CVARS:

"rs_on"			 - 	Is the plugin on(1) or off(0).   Default: 1
"rs_save_type"		 -	Where to seve the information: to file(0) or to MySQL(1).   Default: 0
"rs_messages"		 - 	What messages will be displayed when the client connect - only hud messages(1), only chat messages(2) or hud and chat messages(3)   Default: 3
"rs_register_time"  	 - 	How much time has the client to register. If is set to 0 registration is not mandatory.   Default: 0
"rs_login_time" 	 - 	How much time has the client to login if is registered.   Default: 60.0
"rs_password_len"	 -	What is minimum length of the password.   Default: 6
"rs_attempts"  		 - 	How much attempts has the client to login if he type wrong password.   Default: 3
"rs_chngpass_times"	 -	How much times can the client change his password per map.   Default: 3
"rs_register_datetime"	 -	Is it allowed the plugin to store the register date of the client.   Default: 1
"rs_chngpass_datetime"	 -	Is it allowed the plugin to store the last change password date of the client.   Default: 1
"rs_name_change", "1"	 -	Which clients will be blocked to change their name: only logged(0) or to all clients(1).   Default: 1
"rs_password_prefix"	 -	The prefix of the setinfo for the auto login.   Default: _rspass
"rs_host"    		 -	The host for the database.   Default: 127.0.0.1
"rs_user"	 	 -	The username for the database login.   Default: root
"rs_pass"		 -	The password for the database login.   Default:
"rs_db" 		 - 	The database name.   Default: registersystem

All CVARS are without quotes


Credits:

m0skVi4a ;]    	-	for the idea and make the plugin
ConnorMcLeod 	- 	for his help to block the name change for logged players
Sylwester	-	for the idea for the encrypt
dark_style	-	for ideas in the plugin
kiko0735	-	for the test of the MySQL 


Changelog:

November 6, 2011   -  v1.0 BETA:
	-  First Release

November 20, 2011   -  v1.1 FINAL
	-  Fixed some bugs
	-  Added Change Password function
	-  Added Info/Help
	-  Added cvars to show when the client is registered and change his password
	-  Password are now encrypted for more safety

November 23, 2011   -  v1.1 FINAL FIX 1
	-  Fixed bug if the client type more than CVAR setted attempts passwords

November 28, 2011   -  v1.1 FINAL FIX 2
	-  Fixed bug if that the menu does not pop up when user connect
	
December 26, 2011   -  v2.0
	-  Fixed bug if player change his name and the system does not check the new name
	-  Added block chooseteam if the client is registered but not logged
	-  Added MySQL support
	-  Added .cfg file to manually set the settings of the system
	-  Added CVAR for to set which clients can change their names
	-  Added auto login on changevel or client retry
	
December 27, 2011   -  v2.0 FIX 1
	-  Fixed bug with the kick function
	
January 3, 2012   -  v2.0 FIX 2
	-  Fixed bug with the auto login function that does not work on steam clients


Visit www.forums.alliedmods.net
Visit www.amxmodxbg.org 
*/


#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <sqlx>

#define TASK_MESS 2133 
#define TASK_KICK 3312 
#define SALT "8c4f4370c53e0c1e1ae9acd577dddbed"

new g_on, g_save, g_mess, g_regtime, g_logtime, g_pass_length, g_attempts, g_chp_time, g_reg_dt, g_chp_dt, g_name, g_pass_pr, g_host, g_user, g_pass, g_db;
new reg_file[256], conf_dir[64], file[192], params[2];
new name[32], check_name[32], check_pass[34], namepass[512], typedpass[32], old_pass[32], new_pass[32], passsalt[64], hash[34], pass_prefix[32];
new bool:is_logged[33], bool:is_registered[33], attempts[33], times[33];
new Handle:g_SQLTuple
new g_error[512]
new password[34][34];
new SayTxT;
new const prefix[]="[REGISTER SYSTEM]";

public plugin_init() 
{
	register_plugin("Register System", "2.0 FIX 2", "m0skVi4a ;]")

	g_on = register_cvar("rs_on", "1")
	g_save = register_cvar("rs_save_type", "0")
	g_mess = register_cvar("rs_messages", "3")
	g_regtime = register_cvar("rs_register_time", "0")
	g_logtime = register_cvar("rs_login_time", "60.0")
	g_pass_length = register_cvar("rs_password_length", "6")
	g_attempts = register_cvar("rs_attempts", "3")
	g_chp_time = register_cvar("rs_chngpass_times", "3")
	g_reg_dt = register_cvar("rs_register_datetime", "1")
	g_chp_dt = register_cvar("rs_chngpass_datetime", "1")
	g_name = register_cvar("rs_name_change", "1")
	g_pass_pr = register_cvar("rs_password_prefix", "_rspasss")
	g_host = register_cvar("rs_host", "127.0.0.1")
	g_user = register_cvar("rs_user", "root")
	g_pass = register_cvar("rs_pass", "")
	g_db = register_cvar("rs_db", "registersystem")

	register_message(get_user_msgid("ShowMenu"), "ShowMenu")
	register_message(get_user_msgid("VGUIMenu"), "VGUIMenu")
	register_menucmd(register_menuid("Main Menu"), 1023, "HandleMenu")
	register_menucmd(register_menuid("Password Menu"), 1023, "HandleConfirmPassword")
	register_clcmd("say /reg", "Menu")
	register_clcmd("say_team /reg", "Menu")
	register_clcmd("LOGIN_PASS", "Login");
	register_clcmd("REGISTER_PASS", "Register")
	register_clcmd("CHANGE_PASS", "ChangePassword")

	register_forward(FM_ClientUserInfoChanged, "NameChange")

	register_dictionary("register_system.txt")
	SayTxT = get_user_msgid("SayText")
}

public plugin_cfg()
{
	if(!get_pcvar_num(g_on))
		return PLUGIN_HANDLED

	get_configsdir(conf_dir, charsmax(conf_dir))
	formatex(file, charsmax(file), "%s/registersystem.cfg", conf_dir)
		
	if(!file_exists(file))
	{
		server_print("Register System config - %s not found!", file)
	}
	else
	{
		server_cmd("exec %s", file);
		server_print("Executing Register System config -  %s", file)
	}

	if(get_pcvar_num(g_save))
	{
		MYSQL()
	}
	else
	{
		File()
	}
	return PLUGIN_CONTINUE
}

public File()
{
	get_configsdir(conf_dir, charsmax(conf_dir))
	formatex(reg_file, charsmax(reg_file), "%s/regusers.ini", conf_dir)
	
	if(!file_exists(reg_file))
	{
		write_file(reg_file,";Register System file^n;Modifying may cause the clients to can not Login!^n^n")
		server_print("%L", LANG_SERVER, "FILE_ERROR", reg_file)
	}
}

public MYSQL()
{
	new Host[64], User[32], Pass[32], DB[128];

	get_pcvar_string(g_host, Host, charsmax(Host))
	get_pcvar_string(g_user, User, charsmax(User))
	get_pcvar_string(g_pass, Pass, charsmax(Pass))
	get_pcvar_string(g_db, DB, charsmax(DB))

	g_SQLTuple = SQL_MakeDbTuple(Host, Pass, Pass, DB)

	new errorcode, Handle:SqlConnection = SQL_Connect(g_SQLTuple, errorcode, g_error, charsmax(g_error))

	if(SqlConnection == Empty_Handle) 
	{ 
		server_print("MySQL connection failed")
		set_fail_state(g_error)
	}
	else
	{ 
		server_print("MySQL connection succesful")
	}

	new Handle:Query

	Query = SQL_PrepareQuery(SqlConnection, "CREATE TABLE IF NOT EXISTS registersystem (Name VARCHAR(32), Password VARCHAR(34), Registered VARCHAR(25), Pass_Change VARCHAR(25))")

	if(!SQL_Execute(Query)) 
	{
		SQL_QueryError(Query, g_error, charsmax(g_error))
		set_fail_state(g_error)
	}

	SQL_FreeHandle(Query)
	SQL_FreeHandle(SqlConnection)
}

public client_connect(id)
{
	is_logged[id] = false
	is_registered[id] = false
	attempts[id] = 0
	remove_task(id+TASK_MESS)
	remove_task(id+TASK_KICK)
	CheckUser(id)
}

public client_putinserver(id)
{
	ShowMess(id)
}

public client_disconnect(id)
{
	is_logged[id] = false
	is_registered[id] = false
	attempts[id] = 0
	remove_task(id+TASK_MESS)
	remove_task(id+TASK_KICK)
}

public ShowMenu(msgid, dest, id)
{
	new menu_text[64]

	get_msg_arg_string(4, menu_text, charsmax(menu_text))

	if(!equal(menu_text, "#Team_Select") || !equal(menu_text, "#Team_Select_Spect"))
	{
		return PLUGIN_CONTINUE
	}

	if(!is_registered[id] && get_pcvar_float(g_regtime) != 0)
	{
		return PLUGIN_HANDLED
	}
	else if(is_registered[id] && !is_logged[id])
	{
		return PLUGIN_HANDLED
	}

	return PLUGIN_CONTINUE
}

public VGUIMenu(msgid, dest, id)
{
	if(get_msg_arg_int(1) != 2)
	{
		return PLUGIN_CONTINUE
	}

	if(!is_registered[id] && get_pcvar_float(g_regtime) != 0)
	{
		return PLUGIN_HANDLED
	}
	else if(is_registered[id] && !is_logged[id])
	{
		return PLUGIN_HANDLED
	}

	return PLUGIN_CONTINUE
}

public CheckUser(id)
{
	if(!get_pcvar_num(g_on) || is_user_bot(id))
		return PLUGIN_HANDLED

	if(get_pcvar_num(g_save))
	{
		new query[512]
		get_user_name(id, name, charsmax(name))

		new data[1]
		data[0] = id

		formatex(query, charsmax(query), "SELECT `Password` FROM `registersystem` WHERE (`Name` = '%s');", name)

		SQL_ThreadQuery(g_SQLTuple, "QuerySelectData", query, data, 1)
	}
	else
	{
		new file = fopen(reg_file, "r")

		while(!feof(file))
		{
			get_user_name(id, name, charsmax(name))
			fgets(file, namepass, charsmax(namepass))
			parse(namepass, check_name, charsmax(check_name), check_pass, charsmax(check_pass))

			if(namepass[0] == ';')
				continue

			if(equal(check_name, name))
			{
				is_registered[id] = true
				password[id] = check_pass
				CheckAutoLogin(id)

				break
			}
		}
		fclose(file)
	}
	return PLUGIN_CONTINUE
}

public QuerySelectData(FailState, Handle:Query, error[], errorcode, data[], datasize, Float:fQueueTime)
{ 
	if(FailState == TQUERY_CONNECT_FAILED || FailState == TQUERY_QUERY_FAILED)
	{
		log_amx("%s", error)
		return
	}
	else
	{
		new id = data[0];
		new Colpass = SQL_FieldNameToNum(Query, "Password")

		while(SQL_MoreResults(Query)) 
		{
			SQL_ReadResult(Query, Colpass, check_pass, charsmax(check_pass))
			is_registered[id] = true
			password[id] = check_pass
			CheckAutoLogin(id)
			SQL_NextRow(Query)
		}
	}
}

public CheckAutoLogin(id)
{
	new client_password[32];
	get_pcvar_string(g_pass_pr, pass_prefix, charsmax(pass_prefix))
	get_user_info(id, pass_prefix, client_password, charsmax(client_password))
	formatex(passsalt, charsmax(passsalt), "%s%s", client_password, SALT)
	md5(passsalt, hash)
	
	if(equal(hash, password[id]))
	{
		is_logged[id] = true
	}
}

public ShowMess(id)
{
	if(!get_pcvar_num(g_on))
		return PLUGIN_HANDLED

	set_task(5.0, "Messages", id+TASK_MESS)

	params[0] = id

	if(!is_registered[id])
	{
		if(get_pcvar_float(g_regtime) != 0)
		{
			params[1] = 1
			set_task(get_pcvar_float(g_regtime), "KickPlayer", id+TASK_KICK, params, sizeof params)
			return PLUGIN_HANDLED
		}
	}
	else
	{
		params[1] = 2
		set_task(get_pcvar_float(g_logtime), "KickPlayer", id+TASK_KICK, params, sizeof params)
		return PLUGIN_HANDLED
	}
	return PLUGIN_CONTINUE
}

public Messages(id)
{
	id -= TASK_MESS
	
	if(get_pcvar_num(g_mess) == 1 || get_pcvar_num(g_mess) == 3)
	{
		if(!is_registered[id])
		{
			if(get_pcvar_num(g_regtime) != 0)
			{
				set_hudmessage(255, 0, 0, -1.0, -1.0, 0, 0.5, 5.0, 2.0, 2.0, -1)
				show_hudmessage(id, "%L", LANG_SERVER, "REGISTER_HUD", get_pcvar_num(g_regtime))
			}
			else
			{
				set_hudmessage(0, 255, 0, -1.0, -1.0, 0, 0.5, 5.0, 2.0, 2.0, -1)
				show_hudmessage(id, "%L", LANG_SERVER, "YOUCANREG_HUD", get_pcvar_num(g_regtime))
			}
		}
		else if(!is_logged[id])
		{
			set_hudmessage(255, 0, 0, -1.0, -1.0, 0, 0.5, 5.0, 2.0, 2.0, -1)
			show_hudmessage(id, "%L", LANG_SERVER, "LOGIN_HUD", get_pcvar_num(g_logtime))
		}	
	}

	if(get_pcvar_num(g_mess) == 2 || get_pcvar_num(g_mess) == 3)
	{
		if(!is_registered[id])
		{
			if(get_pcvar_num(g_regtime) != 0)
			{
				client_printcolor(id, "%L", LANG_SERVER, "REGISTER_CHAT", prefix, get_pcvar_num(g_regtime))
			}
			else
			{
				client_printcolor(id, "%L", LANG_SERVER, "YOUCANREG_CHAT", prefix, get_pcvar_num(g_regtime))
			}
		}
		else if(!is_logged[id])
		{
			client_printcolor(id, "%L", LANG_SERVER, "LOGIN_CHAT", prefix, get_pcvar_num(g_logtime))
		}
	}
	Menu(id)
}

public Menu(id)
{
	if(!get_pcvar_num(g_on) || !is_user_connected(id))
		return PLUGIN_HANDLED

	static menu[512];
	new keys, lenght;
	lenght = 0

	if(is_registered[id])
	{
		if(is_logged[id])
		{
			lenght += formatex(menu[lenght], charsmax(menu) - 1, "%L%L", LANG_SERVER, "MENU_LOG", LANG_SERVER, "MENU_EXIT")
			keys = MENU_KEY_3|MENU_KEY_4|MENU_KEY_0
		}
		else
		{
			lenght += formatex(menu[lenght], charsmax(menu) - 1, "%L", LANG_SERVER, "MENU_REG")
			keys = MENU_KEY_1|MENU_KEY_4
		}
	}
	else
	{		
		lenght += formatex(menu[lenght], charsmax(menu) - 1, "%L", LANG_SERVER, "MENU_NOT_REG", get_pcvar_num(g_regtime) != 0 ? "" : "\w")
		keys = MENU_KEY_2|MENU_KEY_4
			
		if(get_pcvar_num(g_regtime) == 0)
		{
			lenght += formatex(menu[lenght], charsmax(menu) - 1, "%L", LANG_SERVER, "MENU_EXIT")
			keys |= (MENU_KEY_0)
		}
	}
	show_menu(id, keys, menu, -1, "Main Menu")
	
	return PLUGIN_CONTINUE
}

public HandleMenu(id, key)
{
	switch(key)
	{
		case 0:
		{
			client_cmd(id, "messagemode LOGIN_PASS")
			Menu(id)
		}
		case 1:
		{
			client_cmd(id, "messagemode REGISTER_PASS")
			Menu(id)
		}
		case 2:
		{
			client_cmd(id, "messagemode CHANGE_PASS")
			Menu(id)
		}
		case 3:
		{
			Info(id)
			Menu(id)
		}
		case 9:
		{
			return PLUGIN_HANDLED
		}
	}
	return PLUGIN_HANDLED
}

public Info(id)
{
	show_motd(id, "rshelpmotd.txt", "Register System Help")
}

public Login(id)
{
	if(!get_pcvar_num(g_on))
		return PLUGIN_HANDLED

	if(!is_registered[id])
	{	
		client_printcolor(id, "%L", LANG_SERVER, "LOG_NOTREG", prefix)
		return PLUGIN_HANDLED
	}

	if(is_logged[id])
	{
		client_printcolor(id, "%L", LANG_SERVER, "LOG_LOGGED", prefix);
		return PLUGIN_HANDLED
	}
	
	read_argv(1, typedpass, charsmax(typedpass))

	if(equal(typedpass, ""))
		return PLUGIN_HANDLED

	formatex(passsalt, charsmax(passsalt), "%s%s", typedpass, SALT)
	md5(passsalt, hash)

	if(!equal(hash, password[id]))
	{	
		attempts[id]++
		client_printcolor(id, "%L", LANG_SERVER, "LOG_PASS_INVALID", prefix, attempts[id], get_pcvar_num(g_attempts))

		if(attempts[id] >= get_pcvar_num(g_attempts))
		{
			params[0] = id
			params[1] = 3
			set_task(2.0, "KickPlayer", id+TASK_KICK, params, sizeof params)
			return PLUGIN_HANDLED
		}
		else
		{
			client_cmd(id, "messagemode LOGIN_PASS")
		}
		return PLUGIN_HANDLED
	}
	else
	{
		is_logged[id] = true
		attempts[id] = 0
		remove_task(id+TASK_KICK)
		client_printcolor(id, "%L", LANG_SERVER, "LOG_LOGING", prefix)
		get_pcvar_string(g_pass_pr, pass_prefix, charsmax(pass_prefix))
		client_cmd(id, "setinfo %s %s",pass_prefix, typedpass)
		Menu(id)
	}
	return PLUGIN_CONTINUE
}

public Register(id)
{
	if(!get_pcvar_num(g_on))
		return PLUGIN_HANDLED

	read_argv(1, typedpass, charsmax(typedpass))
	new passlength = strlen(typedpass)

	if(equal(typedpass, ""))
		return PLUGIN_HANDLED
	
	if(is_registered[id])
	{
		client_printcolor(id, "%L", LANG_SERVER, "REG_EXISTS", prefix)
		return PLUGIN_HANDLED
	}

	if(passlength < get_pcvar_num(g_pass_length))
	{
		client_printcolor(id, "%L", LANG_SERVER, "REG_LEN", prefix, get_pcvar_num(g_pass_length))
		client_cmd(id, "messagemode REGISTER_PASS")
		return PLUGIN_HANDLED
	}

	password[id] = typedpass
	ConfirmPassword(id)
	return PLUGIN_CONTINUE
}

public ChangePassword(id)
{
	if(!get_pcvar_num(g_on) || !is_registered[id] || !is_logged[id])
		return PLUGIN_HANDLED
			
	read_argv(1, typedpass, charsmax(typedpass))
	parse(typedpass, old_pass, 31, new_pass, 31)
	formatex(passsalt, charsmax(passsalt), "%s%s", old_pass, SALT)
	md5(passsalt, hash)
		
	new passlenght = strlen(new_pass)
		
	if(equal(typedpass, ""))
		return PLUGIN_HANDLED

	if(!equali(hash, password[id]))
	{
		client_printcolor(id, "%L", LANG_SERVER, "CHANGE_NO", prefix)
		return PLUGIN_HANDLED
	}
		
	if(times[id] >= get_pcvar_num(g_chp_time))
	{
		client_printcolor(id, "%L", LANG_SERVER, "CHANGE_TIMES", prefix, get_pcvar_num(g_chp_time))
		return PLUGIN_HANDLED
	}

	if(passlenght < get_pcvar_num(g_pass_length))
	{
		client_printcolor(id, "%L", LANG_SERVER, "REG_LEN", prefix, get_pcvar_num(g_pass_length))
		client_cmd(id, "messagemode CHANGE_PASS")
		return PLUGIN_HANDLED
	}

	password[id] = new_pass
	ConfirmPassword(id)		
	return PLUGIN_CONTINUE
}

public ConfirmPassword(id)
{
	if(!get_pcvar_num(g_on) || !is_user_connected(id))
		return PLUGIN_HANDLED

	static passmenu[256];
	new keys;

	formatex(passmenu, charsmax(passmenu), "%L", LANG_SERVER, "MENU_PASS", password[id])
	keys = MENU_KEY_1|MENU_KEY_2|MENU_KEY_0
	
	show_menu(id, keys, passmenu, -1, "Password Menu")
	return PLUGIN_CONTINUE
}

public HandleConfirmPassword(id, key)
{
	switch(key)
	{
		case 0:
		{
			new datetime[26];
			get_user_name(id, name, charsmax(name))
			get_time("on %d/%m/%Y at %H:%M:%S", datetime, 25)
			formatex(passsalt, charsmax(passsalt), "%s%s", password[id],  SALT)
			md5(passsalt, hash)

			if(is_registered[id])
			{
				if(get_pcvar_num(g_save))
				{
					if(get_pcvar_num(g_chp_dt))
					{
						formatex(namepass, charsmax(namepass), "UPDATE registersystem SET Password = '%s', Pass_Change = '%s' WHERE Name = '%s';", hash, datetime, name)
					}
					else
					{
						formatex(namepass, charsmax(namepass), "UPDATE registersystem SET Password = '%s' WHERE Namå = '%s';", hash, name)
					}
					SQL_ThreadQuery(g_SQLTuple, "QuerySetData", namepass)
				}
				else
				{
					new file = fopen(reg_file, "r");
					new line;

					while(!feof(file))
					{
						fgets(file, namepass, 255)
						line++
						parse(namepass, namepass, charsmax(namepass))

						if(equal(namepass, name))
						{						
							if(get_pcvar_num(g_chp_dt))
							{	
								formatex(namepass, charsmax(namepass), "^"%s^" ^"%s^" //Password changed %s", name, hash, datetime)
							}
							else
							{
								formatex(namepass, charsmax(namepass), "^"%s^" ^"%s^"", name, hash)
							}
							write_file(reg_file, namepass, line - 1)							

							break
						}
					}
					fclose(file)
				}
				get_pcvar_string(g_pass_pr, pass_prefix, charsmax(pass_prefix))
				client_cmd(id, "setinfo %s %s",pass_prefix, password[id])
				client_printcolor(id, "%L", LANG_SERVER, "CHANGE_NEW", prefix, new_pass)
				password[id] = hash
				times[id]++	
			}
			else
			{
				if(get_pcvar_num(g_save))
				{
					if(get_pcvar_num(g_chp_dt))
					{
						formatex(namepass, charsmax(namepass), "INSERT INTO `registersystem` (`Name`, `Password`, `Registered`) VALUES ('%s', '%s', '%s');", name, hash, datetime)
					}
					else
					{
						formatex(namepass, charsmax(namepass), "INSERT INTO `registersystem` (`Name`, `Password`) VALUES ('%s', '%s');", name, hash)
					}
					SQL_ThreadQuery(g_SQLTuple, "QuerySetData", namepass)
				}
				else
				{
					new file = fopen(reg_file, "a")
					if(get_pcvar_num(g_reg_dt))
					{
						format(namepass, charsmax(namepass), "^n^"%s^" ^"%s^"^n;Account Created %s^n", name, hash, datetime)
					}
					else
					{
						format(namepass, charsmax(namepass), "^n^"%s^" ^"%s^"", name, hash)
					}
					fprintf(file, namepass)
					fclose(file)
				}
				remove_task(id+TASK_KICK)
				params[1] = 2
				set_task(get_pcvar_float(g_logtime), "KickPlayer", id+TASK_KICK, params, sizeof params)
				client_printcolor(id, "%L", LANG_SERVER, "REG_REGIS", prefix, password[id], get_pcvar_num(g_logtime))
				is_registered[id] = true
				password[id] = hash
			}			
			Menu(id)
		}
		case 1:
		{
			password[id] = old_pass
			if(is_registered[id])
			{
				client_cmd(id, "messagemode CHANGE_PASS")
			}
			else
			{
				client_cmd(id, "messagemode REGISTER_PASS")
			}
		}
		case 9:
		{
			Menu(id)
			password[id] = old_pass
			return PLUGIN_HANDLED
		}
	}
	return PLUGIN_HANDLED
}

public QuerySetData(FailState, Handle:Query, error[],errcode, data[], datasize)
{
	if(FailState == TQUERY_CONNECT_FAILED || FailState == TQUERY_QUERY_FAILED)
	{
		log_amx("%s", error)
		return
	}
}

public NameChange(id) 
{
	if(!get_pcvar_num(g_on) || !is_user_connected(id))
		return FMRES_IGNORED

	new oldname[32], newname[32]
	pev(id, pev_netname, oldname, charsmax(oldname))
	if(oldname[0])
	{
		get_user_info(id, "name", newname, charsmax(newname))
		if(!equal(oldname, newname))
		{
			if(get_pcvar_num(g_name))
			{
				set_user_info(id, "name", oldname)
				client_printcolor(id, "%L", LANG_SERVER, "NAME_CHANGE", prefix)
				return FMRES_HANDLED
			}
			else
			{
				if(is_logged[id])
				{
					set_user_info(id, name, oldname)
					client_printcolor(id, "%L", LANG_SERVER, "NAME_CHANGE", prefix)
					return FMRES_HANDLED
				}
				else
				{
					if(is_registered[id])
					{
						is_registered[id] = false
						remove_task(id+TASK_KICK)
					}
					CheckUser(id)
					ShowMess(id)
				}
			}
		}
	}
	return FMRES_IGNORED
}

public KickPlayer(parameter[])
{
	new id = parameter[0]
	new reason = parameter[1]

	if(is_user_connected(id))
	{
		new userid = get_user_userid(id)
		
		if(reason == 1)
		{
			if(is_registered[id])
				return PLUGIN_HANDLED

			server_cmd("kick #%i ^"%L^"", userid, LANG_PLAYER, "KICK_REG")
			return PLUGIN_CONTINUE
		}
		else if(reason == 2)
		{
			if(is_logged[id])
				return PLUGIN_HANDLED

			server_cmd("kick #%i ^"%L^"", userid, LANG_PLAYER, "KICK_LOG")
			return PLUGIN_CONTINUE
		}
		else if(reason == 3)
		{
			server_cmd("kick #%i ^"%L^"", userid, LANG_PLAYER, "KICK_ATMP", get_pcvar_num(g_attempts))
		}
	}
	return PLUGIN_CONTINUE
}

stock client_printcolor(const id, const input[], any:...)
{
	new count = 1, players[32];
	static msg[191];
	vformat(msg, 190, input, 3)
	replace_all(msg,190,"!g","^4")
	replace_all(msg,190,"!n","^1")
	replace_all(msg,190,"!t","^3")
	replace_all(msg,190,"!w","^0")
	if(id) players[0] = id
	else get_players(players , count , "ch")
	{
		for(new i = 0; i < count; i++)
		{
			if(is_user_connected(players[i]))
			{
				message_begin(MSG_ONE_UNRELIABLE, SayTxT,_, players[i])
				write_byte(players[i])
				write_string(msg)
				message_end()
			}
		}
	}
}
