/* AMX Mod script.
*
*  Description:
*  Plugin to start and stop record HLTV.
*
*  All code was taken from:
*  amx_hltv (c) 2003, [OSA]Odin
*  amx_match_super_deluxe by SPiRiT and en3my
*  mapsmenu by OLO
*  And a very little part of code from me
*  All greeting goes to this guy and not me.
*
* Requirements:
*  - Tested with AMX v0.9.3
*  - Udp Module (Not include in package > http://amxmod.net/amx-modules.php)
*  - HLTV Proxy
*
* Cvars:
* amx_hltv_proxypassword set the hltv rcon password (default: hltvpassword)
*
* Note: You can copy this cvar in your AMX.CFG. (Very good idea)
* amx_hltv_proxypassword "passwordsetinyourhltv.cfg"
*
* Check value 'adminpassword' in your
* hltv.cfg (if // behind, remove it and set your password)
* adminpassword    "mypassword"
*
* Commands:
* amx_hltv_rec - Start record HLTV (HLTV Proxy must be started of course)
* amx_hltv_stop - Stop record HLTV
*
*
* NOTE: Dont add value after amx_hltv_rec.
* Format of recordfile: Hostname-yymmddhhmm-mapname.dem
*
* Dont forget: Invalid adminpassword = NO RECORD
*
* Tips:
* If you want a small Menu, set cmds.ini in addons/amx
* "HLTV Start Record" "amx_hltv_rec" "b" "u"
* "HLTV Stop Record" "amx_hltv_stop" "b" "u"
*
*/


#include <amxmod>
#include <amxmisc>
#include <udp>

#define ACCESS_HLTV ADMIN_LEVEL_A

new address_hltv = -1
new record_hltv = 0
new error_hltv = 2
new g_logFile[16]


public plugin_init()
{
   register_plugin("AMX HLTV","1.2","[OSA]Odin")
   register_concmd("amx_hltv_rec","hltv_rec",ACCESS_HLTV,"- start record HLTV")
   register_concmd("amx_hltv_stop","hltv_stoprec",ACCESS_HLTV,"- stop record HLTV")
   
   register_cvar("amx_hltv_proxypassword","hltvpassword")
      
   get_time("amx_hltv_%m%d.log",g_logFile,15)
}

public hltv_init(id,level,cid){

   // get the id of the hltv server
    new address[32] = "cs.novotec.ru";
    new port = 27020;

      console_print(id,"* HLTV found: (%s:%d)",address,port)
      client_print(id,print_chat,"* HLTV found: (%s:%d)",address,port)
      address_hltv = set_iaddress("127.0.0.1",port)
      new hltvpassword[32]
      get_cvar_string("amx_hltv_proxypassword",hltvpassword,31)
      client_print(id,print_chat,"* Challenged the access with success")

      new rcv[256],snd[256]
      new rconid[13]
      new pass[64]
      get_cvar_string("amx_hltv_proxypassword",pass,63)

      setc(snd,4,0xff)
      copy(snd[4],255,"challenge rcon")
      setc(snd[18],1,'^n')
   
      query_udp_msg(address_hltv,rcv,255,snd)
      copy(rconid,12,rcv[15])
      replace(rconid,255,"^n","")
      setc(snd,4,0xff)
      format(snd[4],255,"rcon %s ^"%s^" stoprecording^n",rconid,pass)
      send_udp_msg(address_hltv,snd)
      format(snd[4],255,"rcon %s ^"%s^" stop^n",rconid,pass)
      send_udp_msg(address_hltv,snd)
      error_hltv = 0

   return PLUGIN_HANDLED
}

public hltv_rec(id,level,cid){
    
    if (address_hltv==-1)
      hltv_init(id,level,cid)

    if (error_hltv == 1)
      return PLUGIN_HANDLED

    new rcv[255], snd[256]
    new rconid[13]
    new pass[64]
    get_cvar_string("amx_hltv_proxypassword",pass,63)

    setc(snd,4,0xff)
    copy(snd[4],255,"challenge rcon")
    setc(snd[18],1,'^n')

    if (query_udp_msg(address_hltv,rcv,255,snd) < 1){
    console_print(id,"* WARNING: Lost HLTV Connection: (%s)",address_hltv)
    client_print(id,print_chat,"* WARNING: Lost HLTV Connection: (%s)",address_hltv)
    address_hltv = -1
    record_hltv = 0
    return PLUGIN_HANDLED
    }

    if (record_hltv == 1){
      client_print(id,print_chat,"* There is already one record")
      return PLUGIN_HANDLED
    }

    copy(rconid,12,rcv[15])
    replace(rconid,255,"^n","")
    setc(snd,4,0xff)

    new demoname[256],hostname[64]
    get_cvar_string("hostname",hostname,63)
    format(demoname,256,"%s",hostname)

    // Remove bad strings before recording

    while(replace(demoname,256,"/","-")) {}
    while(replace(demoname,256,"\","-")) {}
    while(replace(demoname,256,":","-")) {}
    while(replace(demoname,256,"*","-")) {}
    while(replace(demoname,256,"?","-")) {}
    while(replace(demoname,256,">","-")) {}
    while(replace(demoname,256,"<","-")) {}
    while(replace(demoname,256,"|","-")) {}
    while(replace(demoname,256," ","")) {}

    format(snd[4],255,"rcon %s ^"%s^" connect cs.novotec.ru:27017^n",rconid,pass)
    send_udp_msg(address_hltv,snd)
    format(snd[4],255,"rcon %s ^"%s^" record ../../../html/cs/files/demos/%s^n",rconid,pass,demoname)
    send_udp_msg(address_hltv,snd)

    new authid[32],name[32]
    get_user_authid(id,authid,31)
    get_user_name(id,name,31)
    switch(get_cvar_num("amx_show_activity")) {
    case 2: client_print(0,print_chat,"ADMIN %s: Start record HLTV",name)
    case 1: client_print(0,print_chat,"ADMIN: Start record HLTV",name)
    }
  
    log_to_file(g_logFile,"Cmd: ^"%s<%d><%s><>^" Start record HLTV ",name,get_user_userid(id),authid)

    record_hltv = 1
    return PLUGIN_HANDLED
}

public hltv_stoprec(id,level,cid){

    if (address_hltv==-1)
      hltv_init(id,level,cid)

    if (error_hltv == 1)
      return PLUGIN_HANDLED

    new rcv[256],snd[256]
    new rconid[13]
    new pass[64]
    get_cvar_string("amx_hltv_proxypassword",pass,63)

    setc(snd,4,0xff)
    copy(snd[4],255,"challenge rcon")
    setc(snd[18],1,'^n')
   
    if (query_udp_msg(address_hltv,rcv,255,snd) < 1){
    client_print(id,print_chat,"* WARNING: Lost HLTV Connection")
    address_hltv = -1
    record_hltv = 0
    return PLUGIN_HANDLED
    }

    if (record_hltv == 0){
      client_print(id,print_chat,"* There is no record to cancel")
      return PLUGIN_HANDLED
    }

    copy(rconid,12,rcv[15])
    replace(rconid,255,"^n","")
    setc(snd,4,0xff)
    format(snd[4],255,"rcon %s ^"%s^" stoprecording^n",rconid,pass)
    send_udp_msg(address_hltv,snd)
    format(snd[4],255,"rcon %s ^"%s^" stop^n",rconid,pass)
    send_udp_msg(address_hltv,snd)


    new authid[32],name[32]
    get_user_authid(id,authid,31)
    get_user_name(id,name,31)
    switch(get_cvar_num("amx_show_activity")) {
    case 2: client_print(0,print_chat,"ADMIN %s: Stop record HLTV",name)
    case 1: client_print(0,print_chat,"ADMIN: Stop record HLTV",name)
    }
  
    log_to_file(g_logFile,"Cmd: ^"%s<%d><%s><>^" Stop record HLTV ",name,get_user_userid(id),authid)
   
    record_hltv = 0
    return PLUGIN_HANDLED
}
