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


#include <amxmod> /* read this file to learn the functions */

public admin_kick(id)
{
	if (!(get_user_flags(id)&ADMIN_KICK)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}
	if (read_argc() < 2){
		client_print(id,print_console,"[AMX] Usage: amx_kick < part of nick >")
		return PLUGIN_HANDLED
	}
	new arg[32]
	read_argv(1,arg,32)
	new player = find_player("b",arg)
	if (player)	{
		if (get_user_flags(player)&ADMIN_IMMUNITY){
			client_print(id,print_console,"[AMX] The player has immunity")
			return PLUGIN_HANDLED
		}

		new authid[16],authid2[16],name2[32],name[32],userid2
		get_user_authid(id,authid,16)
		get_user_authid(player,authid2,16)
		get_user_name(player,name2,32)
		get_user_name(id,name,32)
		userid2 = get_user_userid(player)

		log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" kick ^"%s<%d><%s><>^"",
			name,get_user_userid(id),authid, name2,userid2,authid2 )

		server_cmd("kick #%d",userid2)
		client_print(id,print_console,"[AMX] Client ^"%s^" kicked",name2)
	}
	else {
		client_print(id,print_console,"[AMX] Client with that part of nick not found")
	}

	return PLUGIN_HANDLED
}

public admin_ban(id)
{
	if (!(get_user_flags(id)&ADMIN_BAN)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}
	if (read_argc() < 3){
		client_print(id,print_console,"[AMX] Usage: amx_ban < minutes > < part of nick >")
		return PLUGIN_HANDLED
	}

	new arg[32]
	read_argv(2,arg,32)
	new player = find_player("b",arg)

	if (player)	{
		if (get_user_flags(player)&ADMIN_IMMUNITY){
			client_print(id,print_console,"[AMX] The player has immunity")
			return PLUGIN_HANDLED
		}
		else if (is_user_bot(player))	{
			client_print(id,print_console,"[AMX] Bot can't be banned")
			return PLUGIN_HANDLED
		}

		new minutes[32]
		read_argv(1,minutes,32)
		new authid[16],name2[32],authid2[16],name[32]
		get_user_authid(player,authid2,16)
		get_user_authid(id,authid,16)
		get_user_name(player,name2,32)
		get_user_name(id,name,32)
				
		log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" ban and kick ^"%s<%d><%s><>^" (minutes ^"%s^")",
			name,get_user_userid(id),authid, name2,get_user_userid(player),authid2,minutes )

		server_cmd("banid %s %s kick;writeid",minutes,authid2)
		client_print(id,print_console,"[AMX] Client ^"%s^" banned",name2)
	}
	else {
		client_print(id,print_console,"[AMX] Client with that part of nick not found")
	}

	return PLUGIN_HANDLED
}

public admin_slay(id)
{
	if (!(get_user_flags(id)&ADMIN_SLAY)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}

	if (read_argc() < 2){
		client_print(id,print_console,"[AMX] Usage: amx_slay < part of nick >")
		return PLUGIN_HANDLED
	}

	new arg[32]
	read_argv(1,arg,32)
	new player = find_player("b",arg)

	if (player){
		if (get_user_flags(player)&ADMIN_IMMUNITY){
			client_print(id,print_console,"[AMX] The player has immunity")
			return PLUGIN_HANDLED
		}

		user_kill(player)

		new authid[16],name2[32],authid2[16],name[32]
		get_user_authid(player,authid2,16)
		get_user_authid(id,authid,16)
		get_user_name(player,name2,32)
		get_user_name(id,name,32)

		log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" slay ^"%s<%d><%s><>^"",
			name,get_user_userid(id),authid, name2,get_user_userid(player),authid2 )

		client_print(id,print_console,"[AMX] Client ^"%s^" slayed",name2)
	}
	else{
		client_print(id,print_console,"[AMX] Client with that part of nick not found")
	}

	return PLUGIN_HANDLED
}

public delayed_map(mapname[])
{
	server_cmd("changelevel %s",mapname)
}

public admin_map(id)
{
	if (!(get_user_flags(id) & ADMIN_MAP)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}
	if (read_argc() < 2){
		client_print(id,print_console,"[AMX] Usage: amx_map < mapname > [ delay in sec. ]")
		return PLUGIN_HANDLED
	}

	new arg[32]
	new len = read_argv(1,arg,32)

	if (is_map_valid(arg)==0){
		client_print(id,print_console,"[AMX] Map with that name not found or map is invalid")
		return PLUGIN_HANDLED
	}

	client_print(id,print_console,"[AMX] Changing map")

	if (read_argc() > 2){
		message_begin(MSG_ALL, SVC_INTERMISSION)
		message_end()
		new arg2[32]
		read_argv(2,arg2,32)
		set_task( floatstr(arg2) ,"delayed_map",0,arg,len + 1)
	}
	else {
		set_task(0.5,"delayed_map",0,arg,len + 1)
	}

	new authid[16],name[32]
	get_user_authid(id,authid,16)
	get_user_name(id,name,32)

	log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" changelevel ^"%s^"",
		name,get_user_userid(id),authid, arg)

	return PLUGIN_HANDLED
}

public admin_cvar(id)
{
	if (!(get_user_flags(id) & ADMIN_CVAR)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}
	if (read_argc() < 3){
		client_print(id,print_console,"[AMX] Usage: amx_cvar < cvar > < value >")
		return PLUGIN_HANDLED
	}

	new arg[32], arg2[64]
	read_argv(1,arg,32)
	read_argv(2,arg2,64)

	if (cvar_exists(arg)){
		if (equal(arg,"rcon_password") && !(get_user_flags(id) & ADMIN_RCON)){
			client_print(id,print_console,"[AMX] You have no access to that cvar")
			return PLUGIN_HANDLED
		}
		else if (equal(arg,"sv_password") && !(get_user_flags(id) & ADMIN_PASSWORD)){
			client_print(id,print_console,"[AMX] You have no access to that cvar")
			return PLUGIN_HANDLED
		}

		new authid[16],name[32]
		get_user_authid(id,authid,16)
		get_user_name(id,name,32)

		log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" set cvar (name ^"%s^") (value ^"%s^")",
			name,get_user_userid(id),authid, arg,arg2)

		set_cvar_string(arg,arg2);
		client_print(id,print_console,"[AMX] Cvar ^"%s^" changed to ^"%s^"",arg,arg2)
	}
	else{
		client_print(id,print_console,"[AMX] Unknown cvar")
	}

	return PLUGIN_HANDLED
}

public admin_cfg(id)
{
	if (!(get_user_flags(id) & ADMIN_CFG)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}

	if (read_argc() < 2){
		client_print(id,print_console,"[AMX] Usage: amx_cfg < fliename >")
		return PLUGIN_HANDLED
	}

	new arg[128]
	read_argv(1,arg,128)

	if (file_exists(arg)){

		new authid[16],name[32]
		get_user_authid(id,authid,16)
		get_user_name(id,name,32)

		log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" execute cfg (file ^"%s^")",
			name,get_user_userid(id),authid, arg)

		client_print(id,print_console,"[AMX] Executing file ^"%s^"",arg)
		server_cmd("exec %s",arg)
	}
	else{
		client_print(id,print_console,"[AMX] File not found",arg)
	}

	return PLUGIN_HANDLED
}

public admin_rcon(id)
{
	if (!(get_user_flags(id) & ADMIN_RCON)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}
	if (read_argc() < 2){
		client_print(id,print_console,"[AMX] Usage: amx_rcon < commad line >")
		return PLUGIN_HANDLED
	}

	new arg[128]
	read_args(arg,128)

	new authid[16],name[32]
	get_user_authid(id,authid,16)
	get_user_name(id,name,32)

	log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" server console (cmdline ^"%s^")",
		name,get_user_userid(id),authid, arg)

	client_print(id,print_console,"[AMX] Commmand line ^"%s^" sent to server console",arg)
	server_cmd(arg)

	return PLUGIN_HANDLED
}

public plugin_init()
{
	register_plugin("Admin Commands","0.5","default")
	register_clcmd("amx_kick","admin_kick",ADMIN_KICK,"amx_kick < part of nick >")
	register_clcmd("amx_ban","admin_ban",ADMIN_BAN,"amx_ban < minutes > < part of nick >")
	register_clcmd("amx_slay","admin_slay",ADMIN_SLAY,"amx_slay < part of nick >")
	register_clcmd("amx_map","admin_map",ADMIN_MAP,"amx_map < mapname > [ delay in sec. ]")
	register_clcmd("amx_cvar","admin_cvar",ADMIN_CVAR,"amx_cvar < cvar > < value >")
	register_clcmd("amx_cfg","admin_cfg",ADMIN_CFG,"amx_cfg < fliename >")
	register_clcmd("amx_rcon","admin_rcon",ADMIN_RCON,"amx_rcon < commad line >")
	return PLUGIN_CONTINUE
}


