
   /* AMX Mod script.
   *
   * MySQL Ban Management
   *  by JustinHoMi
   *
   */
   /* db schema
   *
   CREATE TABLE amx_bans (
     player_id varchar(30) NOT NULL default '',
     player_name varchar(40) NOT NULL default '',
     reason varchar(128) NOT NULL default '',
     admin_id varchar(40) NOT NULL default '',
     admin_name varchar(30) NOT NULL default '',
     length int(10) unsigned NOT NULL default '0',
     time_created timestamp(14) NOT NULL,
     server varchar(25) NOT NULL default '',
     PRIMARY KEY  (player_id)
   ) TYPE=MyISAM;
   *
   */
   #include <amxmod>
   #include <mysql>
   new merror[32]
   new mhost[64]
   new muser[32]
   new mpass[32]
   new mdb[32]
   new mysql = 0
   new server_ip[16], server_port[16], server[32]
   new state = 1         // 1 = sql_init had not been run
   /* 0 = run sql_init during plugin_init
   *  1 = delay sql_init until client_connect or first query */
   new delay_load = 1
   public sql_init(){
       get_cvar_string("ip",server_ip,16)
       get_cvar_string("port",server_port,16)
       add(server,32,server_ip)
       add(server,32,":")
       add(server,32,server_port)
       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)
       mysql = mysql_connect(mhost,muser,mpass,mdb,merror,32)
       if(mysql < 1){
           mysql_error(mysql,merror,32)
           server_print("[AMX]   MySQL   error  :  could  not  connect  :
   '%s'",merror)
       }
       else
           server_print("[AMX] MySQL Ban successfully connected.")
       if (delay_load) state = 0
       return PLUGIN_CONTINUE
   }
   public sql_ban(id){
       if (delay_load && state)
           sql_init()
       if (!(get_user_flags(id)&ADMIN_BAN)){
           client_print(id,print_console,"[AMX]  You  have  no  access to
   that command")
           return PLUGIN_HANDLED
       }
       if (read_argc() < 4){
           client_print(id,print_console,"[AMX] Usage: amx_ban < length >
   < authid or nick > < reason >")
           return PLUGIN_HANDLED
       }
       new arg[32]
       read_argv(2,arg,32)
       // locate player
       new    player = find_player("c",arg) //by wonid
       if (!player)
           player = find_player("bl",arg) // by nick
       if (player)    {
           if (get_user_flags(player)&ADMIN_IMMUNITY){
               client_print(id,print_console,"[AMX]    The   player   has
   immunity")
               return PLUGIN_HANDLED
           }
           else if (is_user_bot(player))    {
               client_print(id,print_console,"[AMX] Bot can't be banned")
               return PLUGIN_HANDLED
           }
           // get ban length and convert to seconds
           new ban_length_s[10],ban_length_s2[10]
           read_argv(1,ban_length_s,10)
           new multiplier = 60
           new carg
           if(contain(ban_length_s,"h") != -1){
               multiplier = 60 * 60
               carg = 'h'
           }
           else if(contain(ban_length_s,"d") != -1){
               multiplier = 60 * 60 * 24
               carg = 'd'
           }
           else if(contain(ban_length_s,"w") != -1){
               multiplier = 60 * 60 * 24 * 7
               carg = 'w'
           }
           copyc(ban_length_s2,10,ban_length_s,carg)
           new ban_length = strtonum(ban_length_s2) * multiplier
           // get reason and get ready to format it
           new text[64],temp[10],temp2[32]
           read_args(text,64)
           parse(text,temp,10,temp2,32)
           new length1 = strlen(temp)
           new length2 = strlen(temp2)
           new length = length1 + length2
           length+=2
           new reason[64]
           read_args(reason,64)
           // get user info to put in table
           new authid[16],name2[32],authid2[16],name[32]
           get_user_authid(player,authid2,16)
           get_user_authid(id,authid,16)
           get_user_name(player,name2,32)
           get_user_name(id,name,32)
           // format the query
           new query[256]
           format(query,256,"INSERT             into             amx_bans
   (player_id,player_name,reason,admin_id,admin_name,length,server)
   values('%s','%s','%s','%s','%s','%i','%s')",authid2,name2,reason[lengt
   h],authid,name,ban_length,server)
           // connect to database and insert data
           //new sql = mysql_connect(mhost,muser,mpass,mdb,merror,32)
           mysql_query(mysql,query)
           mysql_error(mysql,merror,32)
           if (merror[0]){
               server_print("[AMX] MYSQL error: %s",merror)
               return PLUGIN_HANDLED
           }
           // convert ban length back to minutes for display
           ban_length = ban_length / 60
           server_print("[AMX] Banning player for %i",ban_length)
           // format time message
           new time_msg[32]
           if (ban_length == 0)
               time_msg = "Permanent"
           else
               format(time_msg,32,"%i minutes",ban_length)
           // format ban window
           new buffer[256]
           format(buffer,256,"You  have  been  banned from this server by
   %s^nReason:    %s^nLength:    %s.^n^nFor    more   information   visit
   www.modkillers.com^nYou     will     be     disconnected     in     10
   seconds.",name,reason[length],time_msg)
           show_motd(player,buffer,"You have been banned");
           new id_str[3]
           numtostr(player,id_str,3)
           //delay the kick and display console message
           set_task(10.0,"delayed_kick",1,id_str,3)
           client_cmd(player,  "echo  ^"[AMX]  You  have been banned from
   this server.^"")
           client_cmd(player,         "echo        ^"[AMX]        Reason:
   %s.^"",reason[length])
           client_cmd(player, "echo ^"[AMX] Length: %s.^"",time_msg)
       }
       else {
           client_print(id,print_console,"[AMX]  Client with that part of
   nick not found")
       }
       return PLUGIN_HANDLED
   }
   public sql_unban(id){
       if (delay_load && state)
           sql_init()
       if (!(get_user_flags(id)&ADMIN_BAN)){
           client_print(id,print_console,"[AMX]  You  have  no  access to
   that command")
           return PLUGIN_HANDLED
       }
       if (read_argc() < 2){
           client_print(id,print_console,"[AMX] Usage: amx_unban < authid
   >")
           return PLUGIN_HANDLED
       }
       new authid[32]
       read_argv(1,authid,32)
       // format the query
       new query[256]
       format(query,256,"DELETE         from        amx_bans        where
   player_id='%s'",authid)
       // connect to database and delete row
       //new sql = mysql_connect(mhost,muser,mpass,mdb,merror,32)
       mysql_query(mysql,query)
       mysql_error(mysql,merror,32)
       if (merror[0]){
           server_print("[AMX] MYSQL error: %s",merror)
           return PLUGIN_HANDLED
       }
       //  this  may  need  to  be  expanded  to  check to see whether it
   unbanned or not
       client_print(id,print_console,"[AMX]      Removing     ban     for
   %s...",authid)
       return PLUGIN_HANDLED
   }
   public client_connect(id){
       if (delay_load && state)
           sql_init()
       new authid[16]
       get_user_authid(id,authid,16)
       //new sql = mysql_connect(mhost,muser,mpass,mdb,merror,32)
       mysql_error(mysql,merror,32)
       if (merror[0]){
           server_print("[AMX] MYSQL error: %s",merror)
           return PLUGIN_HANDLED
       }
       new query[256]
       format(query,256,"SELECT   reason,length,now()-time_created   FROM
   amx_bans WHERE player_id='%s'",authid)
       mysql_query(mysql,query)
       mysql_error(mysql,merror,32)
       if (merror[0]){
           server_print("[AMX] MYSQL error: %s",merror)
           return PLUGIN_HANDLED
       }
       if(mysql_nextrow(mysql)>0){
           new reason[128],ban_length_s[10],ban_time_s[32]
           mysql_getfield(mysql,1,reason,128)
           mysql_getfield(mysql,2,ban_length_s,10)
           mysql_getfield(mysql,3,ban_time_s,32)
           //new ban_length,ban_time
           new ban_length = strtonum(ban_length_s)
           new ban_time = strtonum(ban_time_s)
           //numtostr(ban_length,ban_length_s,10)
           //numtostr(ban_time,ban_time_s,32)
           server_print("[AMX]        Bantime        %i,        Banlength
   %i",ban_time,ban_length)
           if  ((ban_time < ban_length) || (ban_length == 0)){ // if temp
   ban not up or perm
               new time_msg[32]
               if (ban_length == 0){
                   time_msg = "Permanent"
               }
               else{
                   new ban_left = (ban_length - ban_time) / 60
                   format(time_msg,32,"%i minutes",ban_left)
               }
               client_cmd(id,  "echo  ^"[AMX]  You  have been banned from
   this server.^"")
               client_cmd(id, "echo ^"[AMX] Reason: %s.^"",reason)
               client_cmd(id, "echo ^"[AMX] Time left: %s.^"",time_msg)
               new id_str[3]
               numtostr(id,id_str,3)
               set_task(1.0,"delayed_kick",0,id_str,3)
           }
           else{ // if temp ban has expired
               server_print("[AMX]  SQL  Ban:  Removing  temporary ban on
   %s.",authid)
               format(query,256,"DELETE      FROM      amx_bans     WHERE
   player_id='%s'",authid)
               mysql_query(mysql,query)
               mysql_error(mysql,merror,32)
               if    (merror[0])    server_print("[AMX]    MYSQL   error:
   %s",merror)
           }
       }
       return PLUGIN_CONTINUE
   }
   public delayed_kick(id_str[]){
       new player_id = strtonum(id_str)
       new userid = get_user_userid(player_id)
       server_cmd("kick #%d",userid)
       return PLUGIN_CONTINUE
   }
   public plugin_end()
   {
       if (mysql > 0)
           mysql_close(mysql)
       return PLUGIN_CONTINUE
   }
   public plugin_init(){
       register_plugin("MySQL Ban Management","0.45b","JustinHoMi")
       register_clcmd("amx_ban","sql_ban",ADMIN_BAN,"amx_ban < length > <
   authid or nick > < reason >")
       register_clcmd("amx_unban","sql_unban",ADMIN_BAN,"amx_unban      <
   authid >")
       if (!delay_load) sql_init()
       return PLUGIN_CONTINUE
   }
