/*

Admin Tag Enforce 0.9.8
Written by GeekGod
Updated By xeroblood
(C)2003 GeekGod Industries

Description:   Enforces admins to wear your tag so that admin abuse becomes
               less of a problem.   Having AMX and "cool toys" can pose a
               problem for admin abuse so I hope this mod will help in
               cutting down on this problem.

               To enable this mod, simply define the cvar amx_admin_enforcetag
               in amx.cfg on your server.   Further, if you do not wish to
               allow ADMIN_IMMUNITY admins to skip this check, set
               amx_admin_ignoreimmunity to 0 in amx.cfg.

               Finally, this mod has potential to do all kinds of things and I
               am very open to other amx coders patches.   Please send them to
               geekgod@geekgod.com and I will include them in the main article
               on amxmod.net's forum.

CVARs:
amx_admin_enforcetag     - Anything other than the default of 0 will be enforced.
                           Example: defining this as [bsd] will enforce [bsd] in
                           front of all admins

amx_admin_ignoreimmunity - Default is 1 (on).  Set to 0 to ignore ADMIN_IMMUNITY
                           flag and still force the server/clan tag.


Updated By: ~dd| xeroblood (aka sufferer)
--------------------------------------------

-- Added Tag Checking for players who aren't admins, if they wear the tag it gets removed!
-- Added Support for any/all Admin Flags (not just ADMIN_CHAT flag) this means if a player
   has any admin flags, then they get checked!
-- Added a CVAR for appending Clan Tag to Front or End of Player Name (see below)

amx_admin_tag_at_end     - 1 = Add Tags To End of Admin Name, 0 = Add Tags to the
                           end of Admin Name (default is 0)

*/

#include <amxmod>
#include <amxmisc>

#define MAX_PLAYERS     32 + 1
#define MAX_NAME_LENGTH 64


/////////////////////////////////////////////////
// I don't think this works on STEAM, so I added
// client_connect/authorized functions..
//
//public client_putinserver(id)  <-- Removed
/////////////////////////////////////////////////

#if !defined NO_STEAM
public client_authorized( id )
#else
public client_connect( id )
#endif
{
        new svIndex[2]
        svIndex[0] = id
        set_task(15.0,"check_tags",0,svIndex,0) /* initially check 15 seconds after connect */
        return PLUGIN_CONTINUE
}

public check_tags(svIndex[])
{
        new amx_admin_enforcetag[MAX_NAME_LENGTH]
        new amx_admin_ignoreimmunity
        amx_admin_ignoreimmunity=get_cvar_num("amx_admin_ignoreimmunity")
        get_cvar_string("amx_admin_enforcetag",amx_admin_enforcetag,MAX_NAME_LENGTH)
        new id = svIndex[0]
        //new players[32],playersnum		// Removed This (Not needed)
                                
	if(!equali(amx_admin_enforcetag,"0"))
        {
                for(id=1; id<MAX_PLAYERS; id++)
                {
                        if(is_user_connected(id) && access(id, read_flags("abcdefghijklmnopqrstu")) ) // Added read_flags() to test against Any ADMIN Flag (not just ADMIN_CHAT)
                        {
				///////////////////////////////////////////////////////////
				// I Took out these lines since they aren't used anywhere..
                                //playersnum = id
                                //get_players(players,playersnum);
				///////////////////////////////////////////////////////////

                                new currentplayer[MAX_NAME_LENGTH]
        			get_user_name(id, currentplayer, MAX_NAME_LENGTH-1)
                                if(containi(currentplayer,amx_admin_enforcetag) == -1)
                                {
                                        if(get_user_flags(id)&ADMIN_IMMUNITY && amx_admin_ignoreimmunity == 1)
                                        {
                                                // ignoring user because he has immunity.
                                                // we can hook code in here later if we want.
						// (or reverse condition of IF statement and remove this block ;))
                                        } else
                                        {
                                                client_print(id,print_chat,"* [AMX] * AdminTag Enforce by [.:b:s:d:.] GeekGod & ~dd| xeroblood")
                                                client_print(id,print_chat,"* [AMX] * Applying the mandatory server tag. ")
                                                new finalname[MAX_NAME_LENGTH]
						if( get_cvar_num("amx_admin_tag_at_end") == 0 )
                                                	format(finalname,MAX_NAME_LENGTH-1,"name ^"%s %s^"", amx_admin_enforcetag, currentplayer)
                                                else
							format(finalname,MAX_NAME_LENGTH-1,"name ^"%s %s^"", currentplayer, amx_admin_enforcetag)
						client_cmd(id, finalname)
                                        }
                                }
                        }else if(is_user_connected(id))
			{
				///////////////////////////////////////////////////////////
				// I Added Reverse checking in function RemoveTag()..
                                // Players that aren't admins Can't wear the Tag
				///////////////////////////////////////////////////////////
				
				RemoveTag( id )
			}
                }
        }
        set_task(240.0,"check_tags",0,svIndex,0) /* check every 240 seconds */
        return PLUGIN_CONTINUE
}

public RemoveTag( id )
{
	new szUser[MAX_NAME_LENGTH], szTag[MAX_NAME_LENGTH]

	get_cvar_string( "amx_admin_enforcetag", szTag, MAX_NAME_LENGTH )
	get_user_name( id, szUser, MAX_NAME_LENGTH-1 )
	
	if( containi( szUser, szTag ) > -1 )
	{
		client_print( id, print_chat, "* [AMX] * AdminTag Enforce by [.:b:s:d:.] GeekGod & ~dd| xeroblood" )
		client_print( id, print_chat, "* [AMX] * Removing the restricted server tag." )

		replace( szUser, MAX_NAME_LENGTH-1, szTag, "" )
		client_cmd( id, "name ^"%s^"", szUser )
	}
	return
}

////////////////////////////////////////////
// Added the following function to test
// when players change their name!!
////////////////////////////////////////////
public client_infochanged( id ) 
{
	if ( !is_user_connected( id ) ) return PLUGIN_CONTINUE

	new szNewName[MAX_NAME_LENGTH]
	new szOldName[MAX_NAME_LENGTH]

	get_user_info( id, "name", szNewName, MAX_NAME_LENGTH-1 )
	get_user_name( id, szOldName, MAX_NAME_LENGTH-1 ) 

	if( !equal( szNewName, szOldName ) )
	{
		new svIndex[2]
		svIndex[0] = id
		set_task( 1.0, "check_tags", 0, svIndex, 0 )
	}
	return PLUGIN_CONTINUE
} 

public plugin_init()
{
        register_plugin("Admin Tag Enforce","0.9.8","[bsd]GeekGod")
        register_cvar("amx_admin_enforcetag","0") /* anything other than 0 will enforce that tag. define in amx.cfg */
        register_cvar("amx_admin_ignoreimmunity","1")

	register_cvar( "amx_admin_tag_at_end", "0" )

        new svIndex[2]
        svIndex[0] = 0
        set_task(60.0,"check_tags",0,svIndex,0) /* initially check 60 seconds after init */
        return PLUGIN_CONTINUE
}

