#include <amxmod>
#include <mysql>

/* AMXMOD script=-
* Track all connections to the server - steamid/name/ip - to the mysql database
* An index is kept on steamid/name/ip so no duplicates with those three will occur.
* Also keeps a connection count and last time/date of connection for each entry.
*
* The purpose of this module is to facilitate keeping people you don't want on your
* server away. You can query the db for steamid, ip and name and find out what other ip,
* name or steamid someone is using. The easiest quick thing to use is phpMyAdmin,
* which you can get from http://www.phpmyadmin.net/.
*
* INSTALLATION:
* 1) You must create a table in your amx database using the sql below.
* 2) Add contrack.amx to your amx plugins.ini
* 3) Restart your server or wait for a mapchange and run amx list at the console
*    to verify its running
*
* By [AoL]LuckyJ luckyj@armyofllamas.org
*/

/* SQL statement to create the table used by this plugin
*  Be sure to run it on the amx database!

CREATE TABLE connections (
  id varchar(32) NOT NULL default '',
  name varchar(32) NOT NULL default '',
  ip varchar(15) NOT NULL default '',
  server_ip varchar(15) NOT NULL default '',
  server_port varchar(5) NOT NULL default '',
  connects int(11) NOT NULL default '0',
  last_connect datetime NOT NULL default '0000-00-00 00:00:00',
  UNIQUE KEY id_name_ip (id,name,ip,server_ip,server_port)
) TYPE=MyISAM;

*/

new mysql
new server_ip[16]
new server_port[6]

public initialize_plugin(){
  new mhost[64],muser[32],mpass[32],mdb[32],error[128]
  get_cvar_string("amx_mysql_host",mhost,64)
  get_cvar_string("amx_mysql_user",muser,32)
  get_cvar_string("amx_mysql_pass",mpass,32)
  get_cvar_string("amx_mysql_db",mdb,32)
  get_cvar_string("ip",server_ip,16)
  get_cvar_string("port",server_port,6)
  
  mysql = mysql_connect(mhost,muser,mpass,mdb,error,127)
  if(mysql < 1){
    server_print("[CONTRACK]: MySQL error: can't connect: '%s'",error)
    return PLUGIN_HANDLED 
  }
  return PLUGIN_CONTINUE
}

public client_authorized(id)
{
  if(is_user_bot(id))
    return PLUGIN_CONTINUE
  if(is_user_hltv(id))
    return PLUGIN_CONTINUE
  new steamid[33], name[33], client_ip[16]
  get_user_name(id,name,32)
  get_user_ip(id,client_ip,15,1)
  get_user_authid(id,steamid,32)
  enter_info(steamid,name,client_ip)
  return PLUGIN_CONTINUE
}

public client_infochanged(id)
{
  if(is_user_bot(id))
    return PLUGIN_CONTINUE
  if(is_user_hltv(id))
    return PLUGIN_CONTINUE
  new steamid[33], oldname[33], newname[33], client_ip[16]
  get_user_name(id,oldname,32)
  get_user_info(id,"name",newname,32)
  get_user_ip(id,client_ip,15,1)
  get_user_authid(id,steamid,32)
  enter_info(steamid,newname,client_ip)
  return PLUGIN_CONTINUE
}

public plugin_init()
{
  register_plugin("LuckyJs Connection Tracking w/MySQL","1.2","LuckyJ") 
  set_task(3.5,"initialize_plugin")
  return PLUGIN_CONTINUE
}

enter_info(steamid[], name[], client_ip[])
{
  if(equal(steamid,"HLTV"))
    return PLUGIN_CONTINUE

  new error[128], query[1025]
  format(query,1024,"INSERT INTO `connections` (`id`, `name`, `ip`, `server_ip`, `server_port`, `connects`, `last_connect`) VALUES ('%s','%s','%s','%s','%s',1,now())", steamid, name, client_ip, server_ip, server_port)
  if(mysql_query(mysql,query) < 1) {
    mysql_error(mysql,error,127)
    server_print("[CONTRACK]: MySQL error: can't insert new connection record: '%s'",error)
  }
  if(containi(error,"Duplicate entry") != -1) {
    format(query,1024,"UPDATE `connections` SET connects=connects+1 where id='%s' AND name='%s' AND ip='%s' AND server_ip='%s' AND server_port='%s'", steamid, name, client_ip, server_ip, server_port)
    if(mysql_query(mysql,query) < 1) {
      mysql_error(mysql,error,127)
      server_print("[CONTRACK]: MySQL error: can't update connection record count: '%s'",error)
    }
    format(query,1024,"UPDATE `connections` SET last_connect=now() where id='%s' AND name='%s' AND ip='%s' AND server_ip='%s' AND server_port='%s'", steamid, name, client_ip, server_ip, server_port)
    if(mysql_query(mysql,query) < 1) {
      mysql_error(mysql,error,127)
      server_print("[CONTRACK]: MySQL error: can't update connection record last connect date/time: '%s'",error)
    }
  }
  return PLUGIN_CONTINUE
}