/* AMXX Mod script.
*
*
*  Join_IP 1.2 created by jsauce
* 
*
*  Okay so this plugin gets a user's info when they are authorized, then
*  it displays it to everyone when they join and leave. If you are an admin you 
*  get to see the user's ip address and steamid. All other user's just get to see 
*  the player's name! 
*  
*
*  1.2 Join_IP + Colored Chat [by jsn]
*
*  1.1 Changed access() to get_user_flags() seems to work better for everyone.
*      Also changed how the rank shows, and changed default show level to ADMIN_LEVEL_A
*  1.0 initial release		// rank idea from Kindzhon's enter/leave plugin.
*
*/


#include <amxmodx>
#include <amxmisc>

#define ADMIN ADMIN_LEVEL_A	// Change to something else if you like.

#define M_CONNECT "^x05Зашел на серв %s [%s].приветствуем!"
#define MU_CONNECT "^x05Зашел на серв %s. приветствуем!"
#define M_DISCONNECT "^x05Покинул Серв %s [%s]."
#define MU_DISCONNECT "^x05Покинул Серв %s."

enum ChatColor
{
	CHATCOLOR_YELLOW, 	// Yellow
	CHATCOLOR_GREEN, 	// Green Color
	CHATCOLOR_TEAM_COLOR, 	// Red, grey, blue
	CHATCOLOR_GREY, 	// grey
	CHATCOLOR_RED = 1 , 		// Red
	CHATCOLOR_BLUE, 	// Blue
}

new g_TeamName[][] = 
{
	"",
	"TERRORIST",
	"CT",
	"SPECTATOR"
}

new g_msgSayText
new g_msgTeamInfo

public plugin_init()
{
  register_plugin("Join IP","1.3","jsn")
  g_msgSayText = get_user_msgid("SayText")
  g_msgTeamInfo = get_user_msgid("TeamInfo")
}
public client_authorized(id)
{
	set_task(0.5,"client_is_auth",id)
}

public client_is_auth(id)
{
	new user[32],ip[32],players[32],inum
	get_user_name(id,user,31)
	get_user_ip(id,ip,31,1)
	get_players(players,inum)

	for (new i = 0; i < inum; ++i)
	{
    		if(get_user_flags(players[i]) & ADMIN)
		{
      			colorChat(players[i], CHATCOLOR_GREEN, M_CONNECT, user, ip)
      		}
      		else
		{ 
  			colorChat(players[i], CHATCOLOR_GREEN, MU_CONNECT, user)
  		}
  	}
	return PLUGIN_CONTINUE
}



public client_disconnect(id)
{
	new user[32],ip[32],players[32],inum
	get_user_name(id,user,31)
	get_user_ip(id,ip,31,1)
	get_players(players,inum)

	for (new i = 0; i < inum; ++i)
	{
    		if(get_user_flags(players[i]) & ADMIN)
		{
      			colorChat(players[i], CHATCOLOR_RED, M_DISCONNECT, user, ip)
      		}
      		else
		{ 
  			colorChat(players[i], CHATCOLOR_RED, MU_DISCONNECT, user)
  		}
  	}
	return PLUGIN_CONTINUE
}


// ColorChat function, stolen from http://forums.alliedmods.net/showthread.php?t=45753
colorChat(id, ChatColor:color, const msg[], {Float,Sql,Result,_}:...)
{
	new team, index, MSG_Type
	new bool:teamChanged = false
	new message[192]

	switch(color)
	{
		case CHATCOLOR_YELLOW: // Yellow
		{
			message[0] = 0x01;
		}
		case CHATCOLOR_GREEN: // Green
		{
			message[0] = 0x04;
		}
		default: // Grey, Red, Blue
		{
			message[0] = 0x03;
		}
	}

	vformat(message[1], 190, msg, 4);

	if (id == 0)
	{
		index = findAnyPlayer();
		MSG_Type = MSG_ALL;
	}
	else
	{
		index = id;
		MSG_Type = MSG_ONE;
	}
	
	if (index != 0)
	{
		team = get_user_team(index);	

		if (color == CHATCOLOR_RED && team != 1)
		{
			messageTeamInfo(index, MSG_Type, g_TeamName[1])
			teamChanged = true
		}
		else if (color == CHATCOLOR_BLUE && team != 2)
		{
			messageTeamInfo(index, MSG_Type, g_TeamName[2])
			teamChanged = true
		}
		else if (color == CHATCOLOR_GREY && team != 0)
		{
			messageTeamInfo(index, MSG_Type, g_TeamName[0])
			teamChanged = true
		}
	
		messageSayText(index, MSG_Type, message);
			
		if (teamChanged)
		{
			messageTeamInfo(index, MSG_Type, g_TeamName[team]);
		}
	}
}

messageSayText(id, type, message[])
{
	message_begin(type, g_msgSayText, _, id)
	write_byte(id)		
	write_string(message)
	message_end()
}

messageTeamInfo(id, type, team[])
{
	message_begin(type, g_msgTeamInfo, _, id)
	write_byte(id)
	write_string(team)
	message_end()
}

findAnyPlayer()
{
	new players[32], inum
	
	get_players(players, inum)
	
	for (new a = 0; a < inum; ++a)
	{
		if(is_user_connected(players[a]))
			return players[a];
	}

	return 0
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1049\\ f0\\ fs18 \n\\ par }
*/
