/*
[AMM] - AMX MOD X MESSENGER

Contributions: 
I dont want to take all of the credit by myslef so I could like to thank these people SPECIALLY
FatalisDK
hawk552cdn: To encouraging me into scripting

Description:
This is very usefull plugin for users that have to go to bathroom,momiscallin etc. and when you are
in the game play jus type /away <msg> it will send u automaticly to spectate mode and when some one 
<msg> "yourname" it will autorespond your msg to that user and if you want to return frm AFK type
/back and you will return to normal with the same score you had.

Features:
Shows when PLAYER/ADMIN Signs on and off
Shows when PLAYER/ADMIN go away 
Shows when PLAYER/ADMIN returns from away

Admin Command: amx_cvar amm_mode  1 = Enabled 0 = Disabled
Client Command: /away  <msg> to go AFK  ||  /msg <name> to private msg || /back to return from AFK

*/


#include <amxmodx>
#include <amxmisc>
#include <cstrike>

new g_AFKStatus[33]
new g_AFKMessage[33][128]

public plugin_init()
	{
	register_plugin("[AMM]", "1.2","Taher Ali")
	register_clcmd("say", "HookSay")
	register_cvar("amm_timer", "500.0")
	set_task(5.0, "loadadvert")
}

public loadadvert()
	{
	set_task(get_cvar_float("amm_timer"), "advert", 4342, "", 0, "b")
}

public HookSay(id)
	{
	new szArgs[128], szLeft[6], szRight[123], szMessage[128], szName[32]
	
	read_args(szArgs, 127)
	remove_quotes(szArgs)
	strbreak(szArgs, szLeft, 5, szRight, 122)
	
	get_user_name(id, szName, 31)
	
	if (equali(szLeft, "/away"))
		{
		g_AFKStatus[id] = 1
		g_AFKMessage[id] = szRight
		
		if (is_user_admin(id))
			format(szMessage, 127, "[AMM] ADMIN: %s went into away. [%s]", szName, szRight)
		else
			format(szMessage, 127, "[AMM] PLAYER: %s went into away.  [%s]", szName, szRight)
		
		GreenChatMessage(0, szMessage)
		
		cs_set_user_team(id, CS_TEAM_SPECTATOR)
		user_kill(id, 1)
		
		return PLUGIN_HANDLED
	}
	
	else if (equali(szLeft, "/back"))
		{
		g_AFKStatus[id] = 0
		g_AFKMessage[id] = ""
		
		if (is_user_admin(id))
			format(szMessage, 127, "[AMM] ADMIN: %s returned from away.", szName)
		else
			format(szMessage, 127, "[AMM] PLAYER: %s returned from away.", szName)
		
		GreenChatMessage(0, szMessage)
		
		if (cs_get_user_team(id) == CS_TEAM_SPECTATOR)
			cs_set_user_team(id, random_num(1, 2))
		
		return PLUGIN_HANDLED
	}
	
	else if(equali(szLeft, "/msg"))
		{
		new szUser[32], szChat[92], szHisName[32]
		strbreak(szRight, szUser, 31, szChat, 91)
		
		new Target = cmd_target(id, szUser,2)
		
		if (!Target)
			return PLUGIN_HANDLED
		
		get_user_name(id, szName, 31)   
		get_user_name(Target, szHisName, 31)
		
		if (g_AFKStatus[Target])
			{
			
			format(szMessage, 127, "[AMM] Auto Response from %s: %s", szHisName, g_AFKMessage[Target])
			GreenChatMessage(id, szMessage)
			
			format(szMessage, 127, "[AMM] %s -> %s : %s", szName, szHisName, szChat)
			GreenChatMessage(Target, szMessage)
		} else
		{       
			format(szMessage, 127, "[AMM] %s -> %s : %s", szName, szHisName, szChat)
			
			GreenChatMessage(id, szMessage)
			GreenChatMessage(Target, szMessage)
		}
		return PLUGIN_HANDLED
	}
	
	return PLUGIN_CONTINUE
}

public client_putinserver(id)
	{
	new szName[32], szMsg[64]
	
	get_user_name(id, szName, 31)
	
	if(is_user_admin(id))
		format(szMsg, 63, "[AMM] ADMIN: %s SIGNED ON", szName)
	else
		format(szMsg, 63, "[AMM] PLAYER: %s SIGNED ON", szName)
	
	GreenChatMessage(0, szMsg)
	
	g_AFKStatus[id] = 0
	g_AFKMessage[id] = ""
}

public client_disconnect(id)
	{
	new szName[32], szMsg[64]
	
	get_user_name(id, szName, 31)
	
	if(is_user_admin(id))
		format(szMsg, 63, "[AMM] ADMIN: %s SIGNED OFF", szName)
	else
		format(szMsg, 63, "[AMM] PLAYER: %s SIGNED OFF", szName)
	
	GreenChatMessage(0, szMsg)
	
}

public GreenChatMessage(id, szArgs[])
	{
	new szMsg[128]
	format(szMsg,127,"^x04%s",szArgs)
	
	if (id == 0)
		{
		new Players[32],Num
		get_players(Players,Num)
		
		for (new i = 0; i < Num; i++)
			{
			message_begin(MSG_ONE,get_user_msgid("SayText"),{0,0,0},Players[i])
			write_byte(Players[i])
			write_string(szMsg)
			message_end()
		}
	} else
	{
		message_begin(MSG_ONE, get_user_msgid("SayText"), {0, 0, 0}, id)
		write_byte(id)
		write_string(szMsg)
		message_end()
	}
	
}

public advert()
	{
	GreenChatMessage(0,"[AMM] AMX Mod X Messenge")
	GreenChatMessage(0,"[AMM] Type /away <msg> if you want to go AFK")
	GreenChatMessage(0,"[AMM] Type /msg <name> to private message")
	GreenChatMessage(0,"[AMM] Type /back to return from AFK")
	
	return PLUGIN_CONTINUE
} 