
   /* AMX Mod script.
   *
   * (c) Copyright 2002, Namralkeeg
   * This file is provided as is (no warranties).
   *
   *  This plugin will perform kick, ban, or rename on someone based on a
   list of
   *  names  and  the  specified  flag.  It  will also pick from a set of
   provided names
   * to randomly rename someone.
   *
   *  The  default  is for someone to get banned for 120 minutes on a ban
   violation.
   * Default name for renaming someone is NewName if none are provided.
   *
   * Flags:
   * a = kick
   * b = ban
   * c = rename
   * e = exact match
   * d = substring match
   * l = log to file
   * f = rename to ip
   * g = rename to authid
   *
   Change Log:
   0.4.4: 1. Fixed Flag documentation.
   0.4.3: 1. Misc. Fixes.
   0.4.2:  1.  Added  checking to keep more than the maximum banned names
   from being added.
   0.4.1:  1.  Made  some  more tweaks to handle both steam and non-steam
   servers.
   0.4.0: 1. Made some changes to handle steam.
   0.3.2: 1. Added flags for renaming to ip or authid(wonid)
          2. Fixed logging bug.
   0.3.1: 1. Added Logging to addons/amx/logs/nameban.log
   0.3:   1. Added delayed kick function.
          2. Fixed bug where an admin would get renamed on join even with
   immunity.
   Example nameban.cfg:
   // Random names for people to get changed to.
   amx_nameban_names "I bow down to keeg"
   amx_nameban_names "You Will Lose"
   amx_nameban_names "ilovetofart"
   amx_nameban_names "Ilovetopee"
   amx_nameban_names "Noclue"
   amx_nameban_names "<== Potty Mouth"
   amx_nameban_names "LlamaBoy"
   amx_nameban_names "2Dumb2ChangeMyName"
   // Names to ban
   // Rename with substring search.
   amx_nameban "whome"      "cd"
   // Rename with exact match.
   amx_nameban "slacker"   "ce"
   // Kick on substring match.
   amx_nameban "kickme"   "ad"
   // Ban on substring match.
   amx_nameban "banme"      "bd"
   */
   #include <amxmod>
   #define NAMEBAN_KICK        (1<<0)  /* flag "a" */
   #define NAMEBAN_BAN         (1<<1)  /* flag "b" */
   #define NAMEBAN_RENAME      (1<<2)  /* flag "c" */
   #define NAMEBAN_EXACT       (1<<3)  /* flag "e" */
   #define NAMEBAN_SUBSTRING   (1<<4)  /* flag "d" */
   #define NAMEBAN_LOG         (1<<12) /* flag "l" */
   #define NAMEBAN_RENAME_IP   (1<<5)  /* flag "f" */
   #define NAMEBAN_RENAME_UID  (1<<6)  /* flag "g" */
   #define MAX_BAN_NAMES        100
   #define MAX_CHANGE_NAMES     10
   #define MAX_NAME_LENGTH      33
   #define DELAY_KICK_TIME         10.0
   #define DELAY_BAN_TIME       30.0
   #define DELAY_RENAME_TIME    30.0
   #define DELAY_RENAMEIP_TIME  30.0
   #define DELAY_RENAMEUID_TIME 30.0
   #if !defined NO_STEAM
   #define MAX_AUTHID_LENGTH    33
   #else
   #define MAX_AUTHID_LENGTH    17
   #endif
   new bantime[] =   {"120"}
   new defaultName[] = {"NewName"}
   new cfgfile[] = "addons/amx/nameban.cfg"
   new logfile[] = "addons/amx/logs/nameban.log"
   new bannedNameList[MAX_BAN_NAMES][MAX_NAME_LENGTH]
   new flagList[MAX_BAN_NAMES]
   new numBannedNames = 0
   new changeNameList[MAX_CHANGE_NAMES][MAX_NAME_LENGTH]
   new numChangeNames = 0
   public nameban()
   {
      if (read_argc() != 3){
         server_print("Usage: amx_nameban ^"name^" ^"flags^"")
         return PLUGIN_HANDLED
      }
      if( numBannedNames >= MAX_BAN_NAMES ) {
         server_print("Maximum number of banned names reached!")
         return PLUGIN_HANDLED
      }
      new currName[MAX_NAME_LENGTH]
      new flagStr[3]
      read_argv(1,currName,MAX_NAME_LENGTH)
      read_argv(2,flagStr,2)
      for(new i = 0; i < numBannedNames; i++)
      {
         if(equali(currName,bannedNameList[i]))
         {
                server_print("[AMX]     Banned     Name     %s    already
   registered!",currName)
                return PLUGIN_HANDLED
         }
      }
      copy(bannedNameList[numBannedNames],MAX_NAME_LENGTH,currName)
      if(containi(flagStr,"c")>=0)
         flagList[numBannedNames] |= NAMEBAN_RENAME
      else if(containi(flagStr,"f")>=0)
            flagList[numBannedNames] |= NAMEBAN_RENAME_IP
      else if(containi(flagStr,"g")>=0)
         flagList[numBannedNames] |= NAMEBAN_RENAME_UID
      else if(containi(flagStr,"a")>=0)
         flagList[numBannedNames] |= NAMEBAN_KICK
      else if(containi(flagStr,"b")>=0)
         flagList[numBannedNames] |= NAMEBAN_BAN
      else
         flagList[numBannedNames] |= NAMEBAN_RENAME
      if(containi(flagStr,"d")>=0)
         flagList[numBannedNames] |= NAMEBAN_SUBSTRING
      else if(containi(flagStr,"e")>=0)
         flagList[numBannedNames] |= NAMEBAN_EXACT
      else
         flagList[numBannedNames] |= NAMEBAN_SUBSTRING
      if(containi(flagStr,"l")>=0)
         flagList[numBannedNames] |= NAMEBAN_LOG
      server_print("[AMX] Banned Name added:
   %s",bannedNameList[numBannedNames])
      numBannedNames++
      return PLUGIN_HANDLED
   }
   public nameban_names()
   {
      if (read_argc() < 2) {
         server_print("Usage: amx_nameban_names ^"name^"")
         return PLUGIN_HANDLED
      }
      if( numChangeNames >= MAX_CHANGE_NAMES ) {
         server_print("Maximum number of change to names reached!")
         return PLUGIN_HANDLED
      }
      new currName[MAX_NAME_LENGTH]
      read_argv(1,currName,MAX_NAME_LENGTH)
      for(new i = 0; i < numChangeNames; i++)
      {
         if(equal(currName,changeNameList[i]))
         {
                server_print("[AMX]    Replacement    Name   %s   already
   registered!",currName)
                return PLUGIN_HANDLED
         }
      }
      copy(changeNameList[numChangeNames],MAX_NAME_LENGTH,currName)
      server_print("[AMX]         Replacement         Name         added:
   %s",changeNameList[numChangeNames])
      numChangeNames++
      return PLUGIN_HANDLED
   }
   #if !defined NO_STEAM
   public client_authorized(id)
   #else
   public client_connect(id)
   #endif
   {
      if(get_user_flags(id) & ADMIN_IMMUNITY)
         return PLUGIN_CONTINUE
      if(numBannedNames)
      {
         new playername[MAX_NAME_LENGTH]
         get_user_name(id,playername,MAX_NAME_LENGTH)
         return check_name(id,playername)
      }
      return PLUGIN_CONTINUE
   }
   public client_infochanged(id)
   {
   #if !defined NO_STEAM
      if ( !is_user_connected(id) )
            return PLUGIN_CONTINUE
   #endif
      if(get_user_flags(id) & ADMIN_IMMUNITY)
         return PLUGIN_CONTINUE
      if(numBannedNames)
      {
         new playername[MAX_NAME_LENGTH], oldname[MAX_NAME_LENGTH]
         get_user_info(id, "name", playername,MAX_NAME_LENGTH)
         get_user_name(id,oldname,MAX_NAME_LENGTH)
         if(!equal(oldname,playername))
            return check_name(id,playername)
      }
      return PLUGIN_CONTINUE
   }
   check_name(id,playername[])
   {
      new status = 0
      for(new i = 0; i < numBannedNames; i++)
      {
         status = 0
         if(flagList[i] & NAMEBAN_SUBSTRING) {
            status = containi(playername,bannedNameList[i])
            if(status >= 0)
               status = 1
            else if(status < 0)
               status = 0
         }
         else if(flagList[i] & NAMEBAN_EXACT)
            status = equali(playername,bannedNameList[i])
         if(status)
         {
            new idx[2]
            if(flagList[i] & NAMEBAN_KICK)
            {
               idx[0] = id
               idx[1] = flagList[i]
               set_task(DELAY_KICK_TIME,"delay_kick",0,idx,2)
            }
            else if(flagList[i] & NAMEBAN_BAN)
            {
               idx[0] = id
               idx[1] = flagList[i]
               set_task(DELAY_BAN_TIME,"delay_ban",0,idx,2)
            }
            else if(flagList[i] & NAMEBAN_RENAME)
            {
               idx[0] = id
               idx[1] = flagList[i]
               set_task(DELAY_RENAME_TIME,"delay_rename",0,idx,2)
            }
            else if(flagList[i] & NAMEBAN_RENAME_IP)
            {
               idx[0] = id
               idx[1] = flagList[i]
               set_task(DELAY_RENAMEIP_TIME,"delay_rename_ip",0,idx,2)
            }
            else if(flagList[i] & NAMEBAN_RENAME_UID)
            {
               idx[0] = id
               idx[1] = flagList[i]
               set_task(DELAY_RENAMEUID_TIME,"delay_rename_uid",0,idx,2)
            }
         }
      }
      return PLUGIN_CONTINUE
   }
   public delay_kick(idx[])
   {
      new flags = get_user_flags(idx[0])
      if(flags & ADMIN_IMMUNITY)
          return PLUGIN_CONTINUE
      new userid = get_user_userid(idx[0])
      client_cmd(idx[0],"echo  ^"[AMX]  You  have  been kicked for a name
   VIOLATION!^"")
      server_cmd("kick #%d",userid)
      if(idx[1] & NAMEBAN_LOG) {
          new authid[MAX_AUTHID_LENGTH],name[MAX_NAME_LENGTH]
          get_user_authid(idx[0],authid,MAX_AUTHID_LENGTH - 1)
          get_user_name(idx[0],name,MAX_NAME_LENGTH - 1)
          log_to_file(logfile,"^"%s<%d><%s><>^" kick ",name,userid,authid
   )
      }
      return PLUGIN_CONTINUE
   }
   public delay_ban(idx[])
   {
      new flags = get_user_flags(idx[0])
      if(flags & ADMIN_IMMUNITY)
          return PLUGIN_CONTINUE
      new authid[MAX_AUTHID_LENGTH]
      get_user_authid(idx[0],authid,MAX_AUTHID_LENGTH - 1)
      client_cmd(idx[0],"echo  ^"[AMX]  You  have  been banned for a name
   VIOLATION!.^"")
      server_cmd("banid %s %s kick;writeid",bantime,authid)
      if(idx[1] & NAMEBAN_LOG) {
              new name[MAX_NAME_LENGTH]
           get_user_name(idx[0],name,MAX_NAME_LENGTH - 1)
           log_to_file(logfile,"^"%s<%d><%s><>^"  ban  and  kick (minutes
   ^"%s^")",
           name,get_user_userid(idx[0]),authid,bantime )
      }
      return PLUGIN_CONTINUE
   }
   public delay_rename(idx[])
   {
      new flags = get_user_flags(idx[0])
      if(flags & ADMIN_IMMUNITY)
          return PLUGIN_CONTINUE
      new randomName
      new newName[MAX_NAME_LENGTH]
      new beforename[MAX_NAME_LENGTH]
      get_user_name(idx[0],beforename,MAX_NAME_LENGTH - 1)
      if(numChangeNames >= 1)
         randomName = random_num(1,numChangeNames)-1
      else if(numChangeNames == 1)
         randomName = 0
      else
         randomName = -1
      if(randomName >= 0) {
         set_user_info(idx[0],"name",changeNameList[randomName])
         copy(newName,MAX_NAME_LENGTH,changeNameList[randomName])
      }
      else {
         set_user_info(idx[0],"name",defaultName)
         copy(newName,MAX_NAME_LENGTH,defaultName)
      }
      client_cmd(idx[0],"echo   ^"[AMX]  That  Nick  Is  Banned  On  This
   Server.^"")
      client_cmd(idx[0],"echo ^"[AMX] Changing Nick to: %s.^"",newName)
      if(idx[1] & NAMEBAN_LOG) {
        new authid[MAX_AUTHID_LENGTH],name[MAX_NAME_LENGTH]
        get_user_authid(idx[0],authid,MAX_AUTHID_LENGTH - 1)
        get_user_name(idx[0],name,MAX_NAME_LENGTH - 1)
        log_to_file(logfile,"^"%s<%d><%s><>^" rename ^"%s^"",
        beforename,get_user_userid(idx[0]),authid,newName )
      }
      return PLUGIN_CONTINUE
   }
   public delay_rename_ip(idx[])
   {
       new flags = get_user_flags(idx[0])
       if(flags & ADMIN_IMMUNITY)
           return PLUGIN_CONTINUE
       new newName[MAX_NAME_LENGTH]
       new beforename[MAX_NAME_LENGTH]
       get_user_name(idx[0],beforename,MAX_NAME_LENGTH - 1)
       get_user_ip(idx[0],newName,MAX_NAME_LENGTH - 1,0);
       client_cmd(idx[0],"echo  ^"[AMX]  That  Nick  Is  Banned  On  This
   Server.^"")
       client_cmd(idx[0],"echo ^"[AMX] Changing Nick to: %s.^"",newName)
       set_user_info(idx[0],"name",newName)
       if(idx[1] & NAMEBAN_LOG) {
           new authid[MAX_AUTHID_LENGTH],name[MAX_NAME_LENGTH]
           get_user_authid(idx[0],authid,MAX_AUTHID_LENGTH - 1)
           get_user_name(idx[0],name,MAX_NAME_LENGTH - 1)
           log_to_file(logfile,"^"%s<%d><%s><>^" rename ^"%s^"",
               beforename,get_user_userid(idx[0]),authid,newName )
       }
       return PLUGIN_CONTINUE
   }
   public delay_rename_uid(idx[])
   {
       new newName[MAX_NAME_LENGTH]
       new beforename[MAX_NAME_LENGTH]
       get_user_name(idx[0],beforename,MAX_NAME_LENGTH - 1)
       get_user_authid(idx[0],newName,MAX_NAME_LENGTH - 1);
       client_cmd(idx[0],"echo  ^"[AMX]  That  Nick  Is  Banned  On  This
   Server.^"")
       client_cmd(idx[0],"echo ^"[AMX] Changing Nick to: %s.^"",newName)
       set_user_info(idx[0],"name",newName)
       if(idx[1] & NAMEBAN_LOG) {
           new authid[MAX_AUTHID_LENGTH],name[MAX_NAME_LENGTH]
           get_user_authid(idx[0],authid,MAX_AUTHID_LENGTH - 1)
           get_user_name(idx[0],name,MAX_NAME_LENGTH - 1)
           log_to_file(logfile,"^"%s<%d><%s><>^" rename ^"%s^"",
               beforename,get_user_userid(idx[0]),authid,newName )
       }
       return PLUGIN_CONTINUE
   }
   public nameban_reload()
   {
      numBannedNames = 0
      numChangeNames = 0
      server_cmd("exec %s", cfgfile)
   }
   public plugin_init()
   {
      register_plugin("AMX Nameban","0.4.5","Namralkeeg")
      register_srvcmd("amx_nameban", "nameban")
      register_srvcmd("amx_nameban_names","nameban_names")
      register_srvcmd("amx_nameban_reload","nameban_reload")
      register_cvar("KEEG_Nameban", "0.4.5",FCVAR_SERVER)
      server_cmd("exec %s",cfgfile)
      return PLUGIN_CONTINUE
   }
