/********************************************************

   Admin connect sound plugin V1.4

   by flintst0nes http://flintst0nes.de.vu
   for   flintst0nes FUNSERVER   FAST DL
   visit steam CS1.6 @ 85.131.217.14:27025

   based on AMX MOD X MESSENGER and SDM

*********************************************************

   This plugin for AMX Mod X simply plays a wav or mp3
   file if an Admin joins the server.
   This is different to the sank join / part sound,
   because sank plays sounds on CONNECT, not on join

   Furthermore, a short notice is given, when a player
   joins or leaves.
   When an admin joins, this notice is green.

   I added sound support for user joins, too.

   The plugin automatically downloads the soundfile if
   necesary and - yes - it is compatible to
   sv_downloadurl ;)

   Just install the plugin the usual way copying the
   amxx-file to addons/amxmodx/plugins and append
   acs.amxx to yout plugins.ini
   A sampe addons/amxmodx/configs/acs.cfg will be
   automatically created.

   Please NOTE that mp3 files are located to
   e.g: sound/misc/cl_adminjoin.mp3 whereas
   wave files are typed without a leading sound/ in
   the pathname
   e.g: misc/cl_adminjoin.wav
   * DON'T USE SPACES*

   Therefora they are used by the parser, the followings
   strings are restricted for use in your custom strings:

 	.mp3
 	.wav

   Don't use them or your server will crash !

   No cvars need to be set.

*********************************************************

EXAMPLE acs.cfg

#ADMINJOINSOUND=misc/cl_adminjoin.wav
#ADMINJOINMSG=Admin %s has joined the server
#ADMINPARTSOUND=misc/cl_part.wav
#ADMINPARTMSG=Admin %s has left the server
#USERJOINSOUND=misc/cl_join.wav
#USERJOINMSG=User %s has joined the server
#USER PARTSOUND=misc/cl_part.wav
#USERPARTMSG=User %s has left the server
STEAM_0:0:47110815=JOINSOUND:misc/cl_specialadmin.wav
STEAM_0:0:47110815=PARTSOUND:sound/misc/cl_part.wav
STEAM_0:0:47110815=JOINMSG:Admin FOO has joined the server, with nick %s
STEAM_0:0:47110815=PARTMSG:Admin BAR has left the building, with nick %s

*********************************************************

CHANGELOG

Version 1.4

- used get_configsdir instead of hardcoded path
- added geoip support / set by cvar amx_acs_addgeo 0|1
- did some minor code optimisation

Version 1.3

- added multilingual support by using customizable textstrings
- added soundsupport for users also (by steam id)
- added admin / user leave sounds
- autoassign default settings when acs.cfg is incorrect
- increased performance through less code and variables
- checks if soundfile exists before precaching (server crashed)
- agein new config format. I'm sorry for that

Version 1.2

- added STEAM ID Support / every admin got his own sound

Version 1.1

- added text message on Admin / Users join
- added automatic sound download
- added mp3 support
- customized soundfile (set in acs.cfg)

Version 1.0

- plays a sound on admin connect

********************************************************/

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

public plugin_init() {
	register_plugin("Admin Connect Sound","1.4","flintst0nes.de.vu")
	register_cvar("amx_acs_addgeo","1",0)
	return PLUGIN_CONTINUE
}

public plugin_precache() {

	new soundstring[128]
	new soundfile[128]
	new line
	new txtlen

	new admin_steam_id[34]
	new soundpath[128]
	new temp[5]

	new cfgdir[128]
	new cfgpath[136]

	get_configsdir ( cfgdir, 128 )
	format(cfgpath, 128, "%s/acs.cfg", cfgdir)

   	if(file_exists(cfgpath)){
      while(((line=read_file(cfgpath,line,soundstring,140,txtlen))!=0)){

         strtok(soundfile, admin_steam_id, 34, soundstring, 128, '=')
         trim (admin_steam_id)
         trim (soundstring)
		 strtok(soundstring, temp, 10, soundpath, 118, ':')

          if(containi(soundpath,".wav") != -1){
            if (file_exists (soundpath)){
            	precache_sound(soundpath)
            }
          }else if(containi(soundpath,".mp3") != -1){
            if (file_exists (soundpath)){
            	precache_generic(soundpath)
            }
          }
      }
	}else{
		write_file(cfgpath,"#ALL=misc/cl_adminjoin.wav\n#ADMINJOINMSG=Admin %s has joined the server\n#ADMINPARTMSG=Admin %s has left the server\n #USERJOINMSG=User %s has joined the server\n#USERPARTMSG=User %s has left the server\n",-1)
	}
}

//
// plays a sound by given path
//
public playSoundfile(soundfile[]){

    new players[32]
    new player,num,i
	get_players(players,num,"c")

	for(i=0;i<num;i++){
    	player = players[i]
        if(containi(soundfile,".wav") != -1){
            client_cmd(player, "play ^"%s^"", soundfile)
        }else if(containi(soundfile,".mp3") != -1){
            client_cmd(i, "mp3 play ^"%s^"", soundfile)
        }
    }
}

//
// client join event
//
public client_putinserver(id){

	// cfg file handling
	new line, txtlen, cfgline[128], cfgcontent[128], soundpath[128]

	// user handling
	new szName[32],	authid[34], admin_steam_id[34]

	// messagestring
	new szMsg[169]

	// config variables
	new ADMINJOINSOUND[128], ADMINJOINMSG[128], USERJOINSOUND[128], USERJOINMSG[128]
	new STEAMJOINSOUND[128], STEAMJOINMSG[128], DIRECTOR[10], isAdmin = 0

	// geoip data
    new userip[16], usercountry[45]

	// get user data
	get_user_name(id, szName, 31)
	get_user_authid (id, authid, 34)
	if(is_user_admin(id)){
		isAdmin = 1
	}
	get_user_ip ( id, userip, 16, 1 )
	geoip_country ( userip, usercountry, 45 )

	// configdir
	new cfgdir[128]
	new cfgpath[136]

	get_configsdir ( cfgdir, 128 )
	format(cfgpath, 128, "%s/acs.cfg", cfgdir)
   	if(file_exists(cfgpath)){
      while(((line=read_file(cfgpath,line,cfgline,140,txtlen))!=0)){

         strtok(cfgline, admin_steam_id, 34, cfgcontent, 128, '=')
         trim (admin_steam_id)
         trim (cfgcontent)

         if(equal(admin_steam_id,"#ADMINJOINSOUND")){
            ADMINJOINSOUND = cfgcontent
         }
         if(equal(admin_steam_id,"#ADMINJOINMSG")){
            ADMINJOINMSG = cfgcontent
         }
         if(equal(admin_steam_id,"#USERJOINSOUND")){
            USERJOINSOUND = cfgcontent
         }
         if(equal(admin_steam_id,"#USERJOINMSG")){
            USERJOINMSG = cfgcontent
         }
 		 if(equal(authid, admin_steam_id)){
 		 	 strtok(cfgcontent, DIRECTOR, 10, soundpath, 118, ':')
             if(equal(DIRECTOR,"JOINSOUND")){
                STEAMJOINSOUND = soundpath
             }
             if(equal(DIRECTOR,"JOINMSG")){
                STEAMJOINMSG = soundpath
             }
         }
	  }
   }

   // check data for sounds
   if(!equal(STEAMJOINSOUND, "")){
   		 playSoundfile(STEAMJOINSOUND)
   }else{
   		if( (!equal(ADMINJOINSOUND, "")) && (isAdmin) ){
   			playSoundfile(ADMINJOINSOUND)
   		}else if (!equal(USERJOINSOUND, "")){
   			playSoundfile(USERJOINSOUND)
   		}
   }

   // check data for msgs
   if(!equal(STEAMJOINMSG, "")){
   		format(szMsg, 169, STEAMJOINMSG, szName)
   		szMsg = addGEOIP(szMsg, usercountry)
   		sendChatMessage(0, szMsg, 1)
   }else{
   		if( (!equal(ADMINJOINMSG, "")) && (isAdmin) ){
   			format(szMsg, 169, ADMINJOINMSG, szName)
   			szMsg = addGEOIP(szMsg, usercountry)
   			sendChatMessage(0, szMsg, 1)
   		}else if( (equal(ADMINJOINMSG, "")) && (isAdmin) ){
   			format(szMsg, 169, "Admin %s has joined the server", szName)
   			szMsg = addGEOIP(szMsg, usercountry)
   			sendChatMessage(0, szMsg, 1)
   		}else if (!equal(USERJOINMSG, "")){
   			format(szMsg, 169, USERJOINMSG, szName)
   			szMsg = addGEOIP(szMsg, usercountry)
   			sendChatMessage(0, szMsg,0)
   		}else{
   			format(szMsg, 169, "%s has joined the server", szName)
   			szMsg = addGEOIP(szMsg, usercountry)
   			sendChatMessage(0, szMsg,0)
   		}
   }
}

//
// client leave event
//
public client_disconnect(id){

	// cfg file handling
	new line, txtlen, cfgline[128], cfgcontent[128], soundpath[128]

	// user handling
	new szName[32],	authid[34], admin_steam_id[34]

	// messagestring
	new szMsg[128]

	// config variables
	new ADMINPARTSOUND[128], ADMINPARTMSG[128], USERPARTSOUND[128], USERPARTMSG[128]
	new STEAMPARTSOUND[128], STEAMPARTMSG[128], DIRECTOR[10], isAdmin = 0

	// get user data
	get_user_name(id, szName, 31)
	get_user_authid (id, authid, 34)
	if(is_user_admin(id)){
		isAdmin = 1
	}

	// configdir
	new cfgdir[128]
	new cfgpath[136]

	get_configsdir ( cfgdir, 128 )
	format(cfgpath, 128, "%s/acs.cfg", cfgdir)
   	if(file_exists(cfgpath)){
      while(((line=read_file(cfgpath,line,cfgline,140,txtlen))!=0)){

         strtok(cfgline, admin_steam_id, 34, cfgcontent, 128, '=')
         trim (admin_steam_id)
         trim (cfgcontent)

         if(equal(admin_steam_id,"#ADMINPARTSOUND")){
            ADMINPARTSOUND = cfgcontent
         }
         if(equal(admin_steam_id,"#ADMINPARTMSG")){
            ADMINPARTMSG = cfgcontent
         }
         if(equal(admin_steam_id,"#USERPARTSOUND")){
            USERPARTSOUND = cfgcontent
         }
         if(equal(admin_steam_id,"#USERPARTMSG")){
            USERPARTMSG = cfgcontent
         }
 		 if(equal(authid, admin_steam_id)){
 		 	 strtok(cfgcontent, DIRECTOR, 10, soundpath, 118, ':')
             if(equal(DIRECTOR,"PARTSOUND")){
                STEAMPARTSOUND = soundpath
             }
             if(equal(DIRECTOR,"PARTMSG")){
                STEAMPARTMSG = soundpath
             }
         }
	  }
   }

   // check data for sounds
   if(!equal(STEAMPARTSOUND, "")){
   		 playSoundfile(STEAMPARTSOUND)
   }else{
   		if( (!equal(ADMINPARTSOUND, "")) && (isAdmin) ){
   			playSoundfile(ADMINPARTSOUND)
   		}else if (!equal(USERPARTSOUND, "")){
   			playSoundfile(USERPARTSOUND)
   		}
   }

   // check data for msgs
   if(!equal(STEAMPARTMSG, "")){
   		format(szMsg, 128, STEAMPARTMSG, szName)
   		sendChatMessage(0, szMsg, 0)
   }else{
   		if( (!equal(ADMINPARTMSG, "")) && (isAdmin) ){
   			format(szMsg, 128, ADMINPARTMSG, szName)
   			sendChatMessage(0, szMsg, 0)
   		}else if( (equal(ADMINPARTMSG, "")) && (isAdmin) ){
   			format(szMsg, 128, "Admin %s has left the server", szName)
   			sendChatMessage(0, szMsg, 0)
   		}else if (!equal(USERPARTMSG, "")){
   			format(szMsg, 128, USERPARTMSG, szName)
   			sendChatMessage(0, szMsg,0)
   		}else{
   			format(szMsg, 128, "%s has left the server", szName)
   			sendChatMessage(0, szMsg,0)
   		}
   }
}

//
// public chat msg
//
public sendChatMessage(id, szArgs[], green)
	{
	new szMsg[128]

	if(green == 1){
		format(szMsg,127,"^x04%s",szArgs)
	}else{
		format(szMsg,127,"%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 addGEOIP(msg[], country[]){
	new newMsg[169]
	if(get_cvar_num("amx_acs_addgeo")){
		format(newMsg,169,"%s (%s)",msg,country)
	}else{
		format(newMsg,169,"%s",msg)
	}
	return newMsg
}