
/* AMX Mod script. 
* 
* (c) Copyright 2002, OLO 
* This file is provided as is (no warranties). 
* 
*/ 

/* cmds:
*  amx_cc_check < cvar | alias >
*  checks if cvar or alias exists
*  (f.e.: amx_cc_check xxxaimbot)
*
*  amx_fc_cfg < cfg >
*  forces configuration
*  (f.e.: amx_fc_cfg cl_forwardspeed 400)
*
*  You may keep configuration in server.cfg
*  or in addons/amx/cfgchk.cfg
*/

#include <amxmod> 

#define MAX_FORCES 192
#define MAX_CHECKS 24

new cfg_check[MAX_CHECKS][32]
new cfg_num = 0

new force_pos[33]
new cfg_force[MAX_FORCES][32]
new cfg_vforce[MAX_FORCES][32]
new cfg_force_num = 0

new bool:check_success[33]
new close_code[33][6]
new check_pos[33]

do_checking(id,pos){
	new param[3]
	param[0] = id
	check_pos[id] = pos
	set_task(0.5,"fire_check",573473345+id,param,2)
}

public do_forcing(param[]){
	new id = param[0]
	new posid = force_pos[id]++
	client_cmd(id,"%s %s",cfg_force[posid],cfg_vforce[posid])
	client_cmd(id,"alias %s ^" ^"",cfg_force[posid])
	if (force_pos[id] >= cfg_force_num){
		new name[32]
		get_user_name(id,name,31)
		server_cmd("say ^"Finished configuration forcing on %s^"",name)		
	}
}

public fire_check(param[]){
	new id = param[0]
	check_success[id] = false
	format(close_code[id],5,"%c%c%c^0",random_num('a','z'),random_num('a','z'),random_num('a','z'))
	client_cmd(id,"%s;amxcg %s",cfg_check[check_pos[id]],close_code[id])
	set_task(10.0,"time_out",325235235+id,param,2)
	return PLUGIN_CONTINUE
}

public client_command(id){
	if (check_success[id]) return PLUGIN_CONTINUE
	new arg[16]
	read_argv(0,arg,15)
	if (equal(arg,cfg_check[check_pos[id]])){
		check_success[id] = true
		remove_task(325235235+id)
		return PLUGIN_HANDLED
	}
	return PLUGIN_CONTINUE
}

public client_putinserver(id){
	if (is_user_bot(id))
		return PLUGIN_CONTINUE
	if (cfg_num) do_checking(id,0)
	if (cfg_force_num) {
		new param[2]
		param[0] = id
		force_pos[id] = 0
		set_task(0.3,"do_forcing",46363463+id,param,2,"a",cfg_force_num - 1)
	}
	return PLUGIN_CONTINUE
}

public client_disconnect(id){
	check_success[id] = true
	remove_task(325235235+id)
	remove_task(573473345+id)
	remove_task(46363463+id)	
	return PLUGIN_CONTINUE
}

public close_gate(id){
	new arg[4]
	read_argv(1,arg,3)
	if (equal(arg,close_code[id])){
		if (!check_success[id]){
			new name[32]
			get_user_name(id,name,31)
			server_cmd("say ^"Found '%s' on player %s^"",cfg_check[check_pos[id]],name)
		}
		if (++check_pos[id] < cfg_num){
			do_checking(id,check_pos[id])
		}
		else {
			new name[32]
			get_user_name(id,name,32)
			server_cmd("say ^"Finished configuration checking on %s^"",name)
		}
	}
	else {
		new name[32]
		get_user_name(id,name,31)
		server_cmd("say ^"Player %s is blocking cfg checking^"",name)
	}
	remove_task(325235235+id)
	return PLUGIN_HANDLED
}

public time_out(param[]){
	new id = param[0]
	new name[32]
	get_user_name(id,name,31)
	server_cmd("say ^"Player %s is blocking cfg checking^"",name)
	return PLUGIN_CONTINUE
}

public read_cc_cfg(){
	if (cfg_num < MAX_CHECKS){
		new arg[32]
		read_argv(1,arg,31)		
		copy(cfg_check[cfg_num++],31,arg)
	}
	return PLUGIN_HANDLED
}

public read_fc_cfg(){
	if (cfg_force_num < MAX_FORCES){
		new arg[32]
		read_argv(1,arg,31)		
		copy(cfg_force[cfg_force_num],31,arg)
		read_argv(2,arg,31)			
		copy(cfg_vforce[cfg_force_num],31,arg)
		cfg_force_num++
	}
	return PLUGIN_HANDLED
}

public plugin_init(){
	register_plugin("Cfg Checker 0.7.3","0.7.3","default")
	register_clcmd("amxcg","close_gate")
	register_srvcmd("amx_cc_check","read_cc_cfg")
	register_srvcmd("amx_fc_cfg","read_fc_cfg")
	server_cmd("exec addons/amx/cfgchk.cfg")
	return PLUGIN_CONTINUE
}


