/* AMX Mod X Script
*
* Последние обновление: 15 апреля, 2009
* Авторы: Eugene N. и noga
*
* 	По вашим предложениям писать в ICQ 833783 или в теме на форуме
*
**/

#include <amxmodx>
#include <amxmisc>
#include <geoip>
#include <geoipse>

// Files
const XLEN = 128
new storedIps_File[XLEN] = "stored_ips.ini"
new storedIps_Count = 0
new storedIps[64][16]

new LogName_C[128]
new LogName_D[128]

// PCVARS

new gpc_c_sound_enable, sz_c_sound_file[64]
new gpc_d_sound_enable, sz_d_sound_file[64]

new gpc_c_msg_enable, gpc_c_skip_bots, gpc_c_skip_hltvs, gpc_c_msg_hudcolor, gpc_c_msg_hudxy, gpc_c_msg_type, gpcs_c_msg
new gpc_c_log_enable, gpc_c_log_skip_bots, gpc_c_log_skip_hltvs, gpcs_c_log_msg

new gpc_d_msg_enable, gpc_d_skip_bots, gpc_d_skip_hltvs, gpc_d_msg_hudcolor, gpc_d_msg_hudxy, gpc_d_msg_type, gpcs_d_msg
new gpc_d_log_enable, gpc_d_log_skip_bots, gpc_d_log_skip_hltvs, gpcs_d_log_msg


// Stuff
new steamidsProtect[][] = 
{
	"VALVE_ID_LAN",
	"VALVE_ID_PENDING",
	"STEAM_666:88:666",
	"STEAM_ID_PENDING",
	"STEAM_ID_LAN"
}

// ColorChat
enum ChatColor
{
	CHATCOLOR_NORMAL = 1,
	CHATCOLOR_GREEN,
	CHATCOLOR_TEAM_COLOR,
	CHATCOLOR_GREY,
	CHATCOLOR_RED,
	CHATCOLOR_BLUE,
}

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

new g_msgSayText
new g_msgTeamInfo

public plugin_precache()
{
	register_cvar("connected_sound_file", "buttons/bell1.wav")
	register_cvar("disconnected_sound_file", "fvox/blip.wav")
	
	get_cvar_string("connected_sound_file", sz_c_sound_file, sizeof sz_c_sound_file -1)
	new_replacer(sz_c_sound_file, sizeof sz_c_sound_file -1, ";", "")
	
	if(!precache_sound(sz_c_sound_file))
		log_amx("Can't precache '%s' from cvar 'connected_sound_file'", sz_c_sound_file)
	
	get_cvar_string("disconnected_sound_file", sz_d_sound_file, sizeof sz_d_sound_file -1)
	new_replacer(sz_d_sound_file, sizeof sz_d_sound_file -1, ";", "")
	
	if(!precache_sound(sz_d_sound_file))
		log_amx("Can't precache '%s' from cvar 'disconnected_sound_file'", sz_d_sound_file)
}

public plugin_init()
{
	register_plugin("Players Info", "1.01", "Eugene N. & noga")
	
	register_concmd("players_locations", "cmdShowLocations", ADMIN_ALL, "- Locations of players")
	
	
	gpc_c_sound_enable = register_cvar("connected_sound_enable", "1")
	
	gpc_c_msg_enable = register_cvar("connected_msg_enable", "1")
	
	gpc_c_skip_bots = register_cvar("connected_skip_bots", "0")
	
	gpc_c_skip_hltvs = register_cvar("connected_skip_hltvs", "0")
	
	gpc_c_msg_type = register_cvar("connected_msg_type", "0")
	
	gpc_c_msg_hudcolor = register_cvar("connected_msg_hudcolor", "255 170 000")
	
	gpc_c_msg_hudxy = register_cvar("connected_msg_hudxy", "0.0 0.60")
	
	gpcs_c_msg = register_cvar("connected_msg", "!t%nick%!n connected")
	
	gpc_c_log_enable = register_cvar("connected_log_enable", "1")
	
	gpc_c_log_skip_bots = register_cvar("connected_log_skip_bots", "0")
	
	gpc_c_log_skip_hltvs = register_cvar("connected_log_skip_hltvs", "0")
	
	gpcs_c_log_msg = register_cvar("connected_log_msg", 
	"Присоединился: %nick% [%ip%] [%steamid%] [%country% , %city%]")
	
	
	gpc_d_sound_enable = register_cvar("disconnected_sound_enable", "1")
	
	gpc_d_msg_enable = register_cvar("disconnected_msg_enable", "1")
	
	gpc_d_skip_bots = register_cvar("disconnected_skip_bots", "0")
	
	gpc_d_skip_hltvs = register_cvar("disconnected_skip_hltvs", "0")
	
	gpc_d_msg_type = register_cvar("disconnected_msg_type", "0")
	
	gpc_d_msg_hudcolor = register_cvar("disconnected_msg_hudcolor", "255 170 000")
	
	gpc_d_msg_hudxy = register_cvar("disconnected_msg_hudxy", "0.0 0.67")
	
	gpcs_d_msg = register_cvar("disconnected_msg", "!t%nick%!n disconnected")
	
	gpc_d_log_enable = register_cvar("disconnected_log_enable", "1")
	
	gpc_d_log_skip_bots = register_cvar("disconnected_log_skip_bots", "0")
	
	gpc_d_log_skip_hltvs = register_cvar("disconnected_log_skip_hltvs", "0")
	
	gpcs_d_log_msg = register_cvar("disconnected_log_msg", 
	"Отсоединился: %nick% [%ip%] [%steamid%] [%country% , %city%]")
	
	
	g_msgSayText = get_user_msgid("SayText")
	g_msgTeamInfo = get_user_msgid("TeamInfo")
}

public plugin_cfg()
{
	new cfgdir[XLEN]
	get_configsdir(cfgdir, XLEN -1)
	
	server_cmd("exec %s/players_info.cfg", cfgdir)
	
	format(storedIps_File, XLEN -1, "%s/%s", cfgdir, storedIps_File)
	
	if(!file_exists(storedIps_File))
	{
		write_file(storedIps_File, "; Plugin by Eugene N. & noga^n; ")
		write_file(storedIps_File, "; Сюда добавляйте IP игроков, которые не должны")
		write_file(storedIps_File, "; показываться при коннекте\дисконнекте на сервере")
		write_file(storedIps_File, "; ^n; Комментарии: // или ;^n")
	}
	else
	{
		new szFile = fopen(storedIps_File, "rt")
		
		new buffer[16]
		
		while(!feof(szFile))
		{
			fgets(szFile, buffer, sizeof buffer -1)
			
			if((strlen(buffer) <= 0) || (buffer[0] == ';') || ((buffer[0] == '/') && (buffer[1] == '/')))
				continue
			
			parse(buffer, storedIps[storedIps_Count], 15)
			
			storedIps_Count++
		}
		
		fclose(szFile)
	}
	
	
	
	new logsdir[XLEN]
	get_localinfo("amxx_logs", logsdir, XLEN -1)
	
	new direxists[XLEN];
	format(direxists, XLEN -1, "/%s/Players Info Logs", logsdir)
	if(!dir_exists(direxists))
		mkdir(direxists)
	
	format(logsdir, XLEN -1, "%s/Connected Players", direxists)
	if(!dir_exists(logsdir))
		mkdir(logsdir)
	
	
	
	new monthyear[12]
	get_time("%d-%m-%Y", monthyear, sizeof monthyear -1)
	
	new file[XLEN]
	format(file, XLEN -1, "%s.txt", monthyear)
	
	
	
	
	format(LogName_C, XLEN -1, "%s/%s", logsdir, file)
	if(!file_exists(LogName_C))
		write_file(LogName_C, " ")
	
	format(logsdir, XLEN -1, "%s/Disconnected Players", direxists)
	if(!dir_exists(logsdir))
		mkdir(logsdir)
	
	format(LogName_D, XLEN -1, "%s/%s", logsdir, file)
	if(!file_exists(LogName_D))
		write_file(LogName_D, " ")
}

public client_putinserver(id)
{
	if(get_pcvar_num(gpc_c_sound_enable))
	{
		if(strlen(sz_c_sound_file))
		{
			new_replacer(sz_c_sound_file, sizeof sz_c_sound_file -1, ";", "")
			
			client_cmd(0, "spk %s", sz_c_sound_file)
		}
	}
	
	new isBot = is_user_bot(id)
	new isHltv = is_user_hltv(id)
	
	if
	(
		get_pcvar_num(gpc_c_msg_enable) 
		&&
		(
			(isBot && get_pcvar_num(gpc_c_skip_bots))
			|| (isHltv && get_pcvar_num(gpc_c_skip_hltvs))
			|| (!isBot && !isHltv)
		)
	)
	
	{
		showMessage(id, 0)
	}
	
	if
	(
		get_pcvar_num(gpc_c_log_enable)
		&&
		(
			(isBot && get_pcvar_num(gpc_c_log_skip_bots))
			|| (isHltv && get_pcvar_num(gpc_c_log_skip_hltvs))
			|| (!isBot && !isHltv)
		)
	)
	
	{
		logging(id, 0)
	}
}

public client_disconnect(id)
{
	if(get_pcvar_num(gpc_d_sound_enable))
	{
		if(strlen(sz_d_sound_file))
		{
			client_cmd(0, "spk %s", sz_d_sound_file)
		}
	}
	
	new isBot = is_user_bot(id)
	new isHltv = is_user_hltv(id)
	
	if
	(
		get_pcvar_num(gpc_d_msg_enable) 
		&&
		(
			(isBot && get_pcvar_num(gpc_d_skip_bots))
			|| (isHltv && get_pcvar_num(gpc_d_skip_hltvs))
			|| (!isBot && !isHltv)
		)
	)
	
	{
		showMessage(id, 1)
	}
	
	if
	(
		get_pcvar_num(gpc_d_log_enable)
		&&
		(
			(isBot && get_pcvar_num(gpc_d_log_skip_bots))
			|| (isHltv && get_pcvar_num(gpc_d_log_skip_hltvs))
			|| (!isBot && !isHltv)
		)
	)
	
	{
		logging(id, 1)
	}
}
	
	
public showMessage(id, mode)
{
	static message[191], name[32], ip[16], steamid[35], country[32], city[32], i = 0
	
	get_user_name(id, name, sizeof name -1)
	
	if(strlen(name) > 20)
		add(name, 20, "...")
	
	get_user_ip(id, ip, sizeof ip -1, 1)
	
	if(IsLocalIp(ip))
	{
		country = "LAN"
		city = "LAN"
	}
	else
	{
		geoip_country(ip, country, sizeof country -1)
		geoip_city(ip, city, sizeof city -1)
	}
	
	for(i=0;i<storedIps_Count -1;i++)
	{
		if(equali(ip, storedIps[i]))
		{
			ip = "protected"
			
			break
		}
	}
	
	get_user_authid(id, steamid, sizeof steamid -1)
	
	for(i=0;i<sizeof steamidsProtect;i++)
	{
		if(equali(steamid, steamidsProtect[i]))
		{
			steamid = "No Steam ID"
			
			break
		}
	}
	
	switch(mode)
	{
		case 0: get_pcvar_string(gpcs_c_msg, message, sizeof message -1)
		case 1: get_pcvar_string(gpcs_d_msg, message, sizeof message -1)
	}
	
	new_replacer(message, sizeof message -1, "%nick%", name)
	new_replacer(message, sizeof message -1, "%ip%", ip)
	new_replacer(message, sizeof message -1, "%steamid%", steamid)
	new_replacer(message, sizeof message -1, "%country%", country)
	new_replacer(message, sizeof message -1, "%city%", city)
	
	new players[32], num
	get_players(players, num)
	
	switch(mode)
	{
		case 0:
		{
			switch(get_pcvar_num(gpc_c_msg_type))
			{
				case 1:
				{
					new szCvarHudTemp[12], RGB[3] = { 255, 170, 0 }, tempstrnu[3][4]
					get_pcvar_string(gpc_c_msg_hudcolor, szCvarHudTemp, sizeof szCvarHudTemp -1)
					
					parse(szCvarHudTemp, tempstrnu[0], 3, tempstrnu[1], 3, tempstrnu[2], 3)
					RGB[0] = str_to_num(tempstrnu[0])
					RGB[1] = str_to_num(tempstrnu[1])
					RGB[2] = str_to_num(tempstrnu[2])
					
					new Float:Hud_XY[2] = { 0.0, 0.60 }, tempstrflo[2][6]
					get_pcvar_string(gpc_c_msg_hudxy, szCvarHudTemp, sizeof szCvarHudTemp -1)
					
					parse(szCvarHudTemp, tempstrflo[0], 5, tempstrflo[1], 5)
					Hud_XY[0] = str_to_float(tempstrflo[0])
					Hud_XY[1] = str_to_float(tempstrflo[1])
					
					new_replacer(message, sizeof message -1, "!t", "")
					new_replacer(message, sizeof message -1, "!g", "")
					new_replacer(message, sizeof message -1, "!n", "")
					
					for(i=0;i<num;i++)
					{
						set_hudmessage(RGB[0], RGB[1], RGB[2], Hud_XY[0], Hud_XY[1], 0, 6.0, 6.0, 0.1, 0.2, 3)
						show_hudmessage(players[i], "%s", message)
					}
				}
				default:
				{
					new_replacer(message, sizeof message -1, "!t", "^x03")
					new_replacer(message, sizeof message -1, "!g", "^x04")
					new_replacer(message, sizeof message -1, "!n", "^x01")
					
					for(i=0;i<num;i++)
					{
						colorChat(players[i], CHATCOLOR_NORMAL, "%s", message)
					}
				}
			}
		}
		case 1:
		{
			switch(get_pcvar_num(gpc_d_msg_type))
			{
				case 1:
				{
					new szCvarHudTempDis[12], RGBDis[3] = { 255, 170, 0 }, tempstrnuDis[3][4]
					get_pcvar_string(gpc_d_msg_hudcolor, szCvarHudTempDis, sizeof szCvarHudTempDis -1)
					
					parse(szCvarHudTempDis, tempstrnuDis[0], 3, tempstrnuDis[1], 3, tempstrnuDis[2], 3)
					RGB[0] = str_to_num(tempstrnuDis[0])
					RGB[1] = str_to_num(tempstrnuDis[1])
					RGB[2] = str_to_num(tempstrnuDis[2])
					
					new Float:Hud_YX[2] = { 0.0, 0.67 }, tempstrfloDis[2][6]
					get_pcvar_string(gpc_d_msg_hudxy, szCvarHudTempDis, sizeof szCvarHudTempDis -1)
					
					parse(szCvarHudTempDis, tempstrfloDis[0], 5, tempstrfloDis[1], 5)
					Hud_YX[0] = str_to_float(tempstrfloDis[0])
					Hud_YX[1] = str_to_float(tempstrfloDis[1])
					
					new_replacer(message, sizeof message -1, "!t", "")
					new_replacer(message, sizeof message -1, "!g", "")
					new_replacer(message, sizeof message -1, "!n", "")
					
					for(i=0;i<num;i++)
					{
						set_hudmessage(RGBDis[0], RGBDis[1], RGBDis[2], Hud_YX[0], Hud_YX[1], 0, 6.0, 6.0, 0.1, 0.2, 3)
						show_hudmessage(players[i], "%s", message)
					}
				}
				default:
				{
					new_replacer(message, sizeof message -1, "!t", "^x03")
					new_replacer(message, sizeof message -1, "!g", "^x04")
					new_replacer(message, sizeof message -1, "!n", "^x01")
					
					for(i=0;i<num;i++)
					{
						colorChat(players[i], CHATCOLOR_NORMAL, "%s", message)
					}
				}
			}
		}
	}
}

public logging(id, mode)
{
	static message[191], name[32], ip[16], steamid[35], country[32], city[32], i = 0
	
	get_user_name(id, name, sizeof name -1)
	
	if(strlen(name) > 20)
		add(name, 20, "...")
	
	get_user_ip(id, ip, sizeof ip -1, 1)
	
	if(IsLocalIp(ip))
	{
		country = "LAN"
		city = "LAN"
	}
	else
	{
		geoip_country(ip, country, sizeof country -1)
		geoip_city(ip, city, sizeof city -1)
	}
	
	for(i=0;i<storedIps_Count -1;i++)
	{
		if(equali(ip, storedIps[i]))
		{
			ip = "protected"
			
			break
		}
	}
	
	get_user_authid(id, steamid, sizeof steamid -1)
	
	for(i=0;i<sizeof steamidsProtect;i++)
	{
		if(equali(steamid, steamidsProtect[i]))
		{
			steamid = "No Steam ID"
			
			break
		}
	}
	
	switch(mode)
	{
		case 0: get_pcvar_string(gpcs_c_log_msg, message, sizeof message -1)
		case 1: get_pcvar_string(gpcs_d_log_msg, message, sizeof message -1)
	}
	
	new_replacer(message, sizeof message -1, "%nick%", name)
	new_replacer(message, sizeof message -1, "%ip%", ip)
	new_replacer(message, sizeof message -1, "%steamid%", steamid)
	new_replacer(message, sizeof message -1, "%country%", country)
	new_replacer(message, sizeof message -1, "%city%", city)
	
	switch(mode)
	{
		case 0: log_to_file(LogName_C, "%s", message)
		case 1: log_to_file(LogName_D, "%s", message)
	}
}

public cmdShowLocations(id)
{
	new players[32], num
	get_players(players, num, "ch")
	
	if(num <= 0)
	{
		console_print(id, "Sorry, players not found on server")
		
		return PLUGIN_HANDLED
	}
	
	console_print(id, "%s %-22.22s %-22.22s %-22.22s", "#", "Player", "Country", "City")
	
	new pid, name[32], ip[16], country[32], city[32]
	
	for(new i=0;i<num;i++)
	{
		pid = players[i]
		
		get_user_name(pid, name, 31)
		
		if(strlen(name) > 17)
			add(name, 17, "...")
		
		get_user_ip(pid, ip, 15, 1)
		
		if(IsLocalIp(ip))
		{
			country = "LAN"
			city = "LAN"
		}
		else
		{
			geoip_country(ip, country, 31)
			geoip_city(ip, city, 31)
		}
		
		console_print(id, "%-2d %-22.22s %-22.22s %-22.22s", i+1, name, country, city)
	}
	
	return PLUGIN_HANDLED
}

stock new_replacer(text[], len, const what[], const with[])
	while(replace(text, len, what, with)) {}

bool:IsLocalIp(checked_ip[16])
{
	new temp[32]
	
	copy(temp, 3, checked_ip)
	
	if(equal(temp, "10.") || equal(temp, "127"))
		return true
	
	copy(temp, 7, checked_ip)
	
	if(equal(temp, "192.168"))
		return true
	
	return false
}

colorChat(id, ChatColor:color, const msg[], {Float,Sql,Result,_}:...)
{
	new team, index, MSG_Type
	new bool:teamChanged = false
	static message[192]
	
	switch(color)
	{
		case CHATCOLOR_NORMAL:
		{
			message[0] = 0x01
		}
		case CHATCOLOR_GREEN:
		{
			message[0] = 0x04
		}
		default:
		{
			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()
{
	static players[32], inum, pid
	
	get_players(players, inum, "ch")
	
	for (new a = 0; a < inum; a++)
	{
		pid = players[a]
		
		if(is_user_connected(pid))
			return pid
	}
	
	return 0
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1049\\ f0\\ fs16 \n\\ par }
*/
