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

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

new admin_password[MAX_ADMINS][32]
new admin_name[MAX_ADMINS][32]
new admin_flags[MAX_ADMINS]
new admin_priv[MAX_ADMINS]
new admin_num = 0

public admin_cmd(){
	new data[32]
	read_argv(1,data,32)
	copy(admin_name[admin_num],32,data)
	read_argv(2,data,32)
	copy(admin_password[admin_num],32,data)
	read_argv(3,data,32)
	admin_priv[admin_num] = read_flags(data)
	read_argv(4,data,32)
	admin_flags[admin_num] = read_flags(data)
	admin_num++
	return PLUGIN_HANDLED
}

init_acces(id,name[],authid[],ip[], password[]){
	new index = -1
	new result = 0
	for(new i = 0; i < admin_num; ++i) {
		if (admin_flags[i] & FLAG_AUTHID) {
			if (equal(authid,admin_name[i])) {
				index = i
				break
			}
		}
		else if (admin_flags[i] & FLAG_IP) {
			if (contain(ip,admin_name[i])!=-1){
				index = i
				break
			}
		}
		else {
			if (admin_flags[i] & FLAG_TAG)	{
				if (contain(name,admin_name[i])!=-1){
					index = i
					break
				}
			}
			else if (equal(name,admin_name[i])) {
				index = i
				break
			}
		}
	}
	if (index != -1) {
		if (admin_flags[index] & FLAG_NOPASS){
			result |= 8
			set_user_flags(id,admin_priv[index])
			log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" triggered ^"access_level_nopw^" (level ^"%d^") (address ^"%s^")",
				name,get_user_userid(id),authid,admin_priv[index],ip)
		}
		else if (equal(password,admin_password[index])) {
			result |= 12
			set_user_flags(id,admin_priv[index])
			log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" triggered ^"access_level^" (level ^"%d^") (address ^"%s^")",
				name,get_user_userid(id),authid,admin_priv[index],ip)
		}
		else {
			result |= 1
			if (admin_flags[index] & FLAG_KICK)
				result |= 2
		}
	}
	else if (get_cvar_float("amx_mode")==2.0) {
		result |= 2
	}

	return result
}

public client_connect(id){
	client_cmd(id,"echo This server is using [AMX] Admin Plugin")

	if (get_cvar_float("amx_mode")==0.0)
		return PLUGIN_CONTINUE

	set_user_flags(id,-1) /* clear all flags */

	new userip[32]
	get_user_ip(id,userip,32)

	new username[32]
	get_user_name(id,username,32)

	new userauthid[32]
	get_user_authid(id,userauthid,32)

	new password[32]
	new passfield[32]
	get_cvar_string("amx_password_field",passfield,32)
	get_user_info(id,passfield,password,32)
	
	new result = init_acces(id,username,userauthid,userip,password)

	if (result & 1) {
		client_cmd(id,"echo [AMX] Invalid Password!")
	}
	if (result & 2) {
		client_cmd(id,"echo [AMX] You have no entry to the server...;disconnect")
	}
	if (result & 4) {
		client_cmd(id,"echo [AMX] Password accepted.")
	}
	if (result & 8) {
		client_cmd(id,"echo [AMX] Privileges set.")
	}

	return PLUGIN_CONTINUE
}

public plugin_init()
{
	register_plugin("Admin Base","0.6","default")
	register_srvcmd("amx_admin","admin_cmd")
	register_cvar("amx_mode","2.0")
	register_cvar("amx_password_field","_pw")
	server_cmd("exec addons/amx/admin.cfg")
	return PLUGIN_CONTINUE
}


