Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.38 KB | None | 0 0
  1. -------------------------------------------------------------------
  2. --                          Config                               --
  3. -- Uban system by Meggido, implemented for TnB by Suicide Bomber --
  4. -- Modified for TacoScript 2 by Advantage                        --
  5. -------------------------------------------------------------------
  6.  
  7. -----------this part goes in admin_cc.lua eventually ------------------
  8. --[[
  9. function ccGban( ply, command, argv, args )
  10.     if #argv < 1 then
  11.         ply:PrintMessage( 3, "Invalid Arguments" );
  12.         return
  13.     end
  14.  
  15.     local target = TS.RunPlayerSearch( ply, argv[1], true );
  16.     if not target or not TS.AdminCanTarget( ply, target ) then
  17.         ply:PrintMessage( 3, "Invalid Target" );
  18.         return
  19.     end
  20.    
  21.     local time = tonumber( argv[ 2 ] or 0 )
  22.     if not time then
  23.         ply:PrintMessage( 3, "Invalid Time" );
  24.         return
  25.     end
  26.     if( not ply:IsSuperAdmin() ) then
  27.         if( time > 1440 or time < 1 ) then
  28.             ply:PrintMessage( 2, "Maximum of 1440 minutes exceeded!" );
  29.             return
  30.         end
  31.     end
  32.    
  33.     local comment = argv[ 3 ] or ""
  34.  
  35.     local b, e = pcall( gBanUser, ply, target, time * 60, comment )
  36.     if b then
  37.         TS.PrintMessageAll( 3, TS.GetConsoleNick( ply ) .. " globaly banned " .. TS.GetConsoleNick( target ));
  38.         ply:PrintMessage( 3, "Global Ban Successful" );
  39.     else
  40.         Msg( "GBan failure. Error is: " .. tostring( e ) .. "\n" )
  41.         ply:PrintMessage( 3, "Global ban failed!" );
  42.     end
  43. end
  44. TS.AdminCommand( "rp_ban", ccGban, "<user> [<time>] [<comment>] - Globally bans a user, time is in minutes. 0 or blank for perma.")
  45. TS.AdminCommand( "rp_gban", ccGban, "<user> [<time>] [<comment>] - Globally bans a user, time is in minutes. 0 or blank for perma.")
  46. ]]--
  47. function ccGban( ply, cmd, args)
  48.  
  49.     if( ply:EntIndex() ~= 0 and not ply:CanUseAdminCommand() ) then return; end
  50.    
  51.     local btime = tonumber( args[2] );
  52.    
  53.     local name = args[1];
  54.    
  55.     if( not name ) then
  56.         Console( ply, "rpa_ban - <user> [<time>] [<comment>] - Bans a user, time is in minutes. 0 or blank for perma.")
  57.     end
  58.    
  59.     if( not btime ) then
  60.         Console( ply, "Invalid Time")
  61.         return;
  62.     end
  63.    
  64.     if( not ply:IsSuperAdmin() ) then
  65.         if( btime < 1 or btime > 10080 ) then
  66.             Console( ply, "Time must be between 1 and 10080 ");
  67.             return;
  68.         end
  69.     end
  70.    
  71.     local msg = "";
  72.    
  73.     if( args[3] ) then
  74.    
  75.         for n=3 , #arg do
  76.        
  77.             if(msg ~= "" ) then
  78.                 msg = msg .. " ";
  79.             end
  80.             msg = msg .. arg[n];
  81.         end
  82.     end
  83.    
  84.     local succ, result = TS.FindPlayerByName( name );
  85.     TS.ErrorMessage( ply, true, succ, result );
  86.  
  87.    
  88.     if(succ) then
  89.    
  90.     local b, e = pcall( gBanUser, ply, succ, btime * 60, msg )
  91.     if b then
  92.         umsg.Start( "MiscCon" );
  93.         umsg.String( succ .. " was banned by " .. ply .. " for " .. btime .. " minutes: " .. msg );
  94.         umsg.End();
  95.     else
  96.         Msg( "GBan failure. Error is: " .. tostring( e ) .. "\n" )
  97.         ply:PrintMessage( 3, "Global ban failed!" );
  98.     end
  99.    
  100.     end
  101.    
  102. end
  103. concommand.Add( "rpa_ban", ccGban );
  104.  
  105. function ccGbanId( ply, command, argv, args )
  106.     if( not ply:IsSuperAdmin() ) then
  107.         return
  108.     end
  109.     if #argv < 1 then
  110.         Console(ply, "Invalid Arguments")
  111.         return
  112.     end
  113.  
  114.     local steamid = argv[ 1 ]
  115.    
  116.     local time = tonumber( argv[ 2 ] or 0 )
  117.     if not time then
  118.         Console( ply, "Invalid Time" );
  119.         return
  120.     end
  121.    
  122.     local comment = argv[ 3 ] or ""
  123.  
  124.     local b, e = pcall( gManualBan, ply, steamid, nil, time * 60, comment )
  125.    
  126.     if b then
  127.    
  128.         local steamidtable = { }
  129.         for k, v in pairs( player.GetAll() ) do
  130.             steamidtable[v:SteamID()] = v;
  131.         end
  132.        
  133.         if( steamidtable[steamid] ) then
  134.             local target = steamidtable[steamid];
  135.             -- If they are in the server, kick them
  136.             local str = string.format( "kickid %s Banned for %d minutes by %s\n", target:SteamID(), time,  ply:Nick()   )
  137.             game.ConsoleCommand( str );
  138.         end
  139.        
  140.         Console( ply, "Global Ban Successful" );
  141.     else
  142.         Msg( "GBan failure. Error is: " .. tostring( e ) .. "\n" )
  143.         Console( ply, "Global ban failed!" );  
  144.     end
  145. end
  146. --TS.AdminCommand( "rp_banid", ccGbanId, "<steamid> [<time>] [<comment>] - Globally bans a steamid, time is in minutes. 0 or blank for perma.")
  147. --TS.AdminCommand( "rp_gbanid", ccGbanId, "<steamid> [<time>] [<comment>] - Globally bans a steamid, time is in minutes. 0 or blank for perma.")
  148. concommand.Add("rpa_banid", ccGbanID)
  149.  
  150. local function ccGunbanid( ply, command, argv, args )
  151.     if( not ply:IsRick() ) then
  152.         return
  153.     end
  154.     if #argv < 1 then
  155.         Console(ply, "Invalid Arguments" );
  156.         return
  157.     end
  158.  
  159.     gClearBan( argv[ 1 ] )
  160.     Console( ply, "Global unban successful (assuming this ban existed)." );
  161. end
  162. --TS.AdminCommand( "rp_gunbanid", ccGunbanid, "<steamid> - Globally unbans a user's steamid.")
  163. concommand.Add("rpa_unbanid", ccGunbanid);
  164.  
  165.  
  166.  
  167.  
  168.  
  169. ------------------------------------------------------------------------
  170.  
  171. local gbhost = "localhost"
  172. local gbusername = "bansys"
  173. local gbpassword = "@7e&HADr-pra9?ED"
  174. local gbdatabase = "bans"
  175. local gbport = 3306
  176. local table = "gbanstrp"
  177.  
  178. local updatetime = 1 -- How often, in minutes, it updates the bans from mysql.
  179. -- (So if a person banned from one server joins another server using the same DB, it could take up to this long to get rid of him/her)
  180.  
  181. local persistent = true -- Use a persistent MySQL connection?
  182.  
  183. local baninterval = 44000 -- Ban time to report to console. We can't use 0 because we don't want it to be saved with writeid. 44k minutes == 1 month.
  184. -- If you plan on keeping your server on one map without restarting for more than a month at a time (hah) then you'll need to up this value.
  185.  
  186. --[[
  187. Table structure:
  188. CREATE TABLE gbanstrp (
  189. id INT UNSIGNED PRIMARY KEY NOT NULL UNIQUE AUTO_INCREMENT,
  190. steamid CHAR( 12 ) NOT NULL,
  191. name CHAR( 31 ),
  192. time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  193. unban_time TIMESTAMP NOT NULL DEFAULT 0,
  194. comment CHAR( 128 ),
  195. serverip INT UNSIGNED NOT NULL,
  196. serverport SMALLINT UNSIGNED NOT NULL DEFAULT 27015,
  197. adminname CHAR( 31 ),
  198. adminsteamid CHAR( 12 )
  199. );
  200. ]]--
  201.  
  202. require( "mysql" )
  203.  
  204. local db
  205.  
  206. Bans = {} -- Keep track of all the last known bans
  207.  
  208. function gDoQuery( query, type )
  209.     local result, isok, err = mysql.query( db, query, type or mysql.QUERY_NUMERIC )
  210.  
  211.     if not isok and err == "" then isok = true end -- False positive
  212.  
  213.     if not isok then
  214.         error( tostring( err ), 2 )
  215.         return nil
  216.     end
  217.  
  218.     if result then
  219.         -- print( query ) -- For debug
  220.         -- PrintTable( result )
  221.     end
  222.  
  223.     return result
  224. end
  225.  
  226. function Connect()
  227.     if db then return db end -- Still connected
  228.  
  229.     db, err = mysql.connect( gbhost, gbusername, gbpassword, gbdatabase, gbport )
  230.     if db == 0 then
  231.         db = nil
  232.         error( tostring( err ), 1 )
  233.         return
  234.     end
  235.  
  236.     return db
  237. end
  238.  
  239. function Disconnect( force )
  240.     if not db then return end -- Already disconnected
  241.     if persistent and not force then return end -- Don't disconnect, persistent
  242.  
  243.     local succ, err = mysql.disconnect( db )
  244.     if not succ then
  245.         error( tostring( err ), 2 )
  246.     end
  247.    
  248.     db = nil
  249. end
  250. hook.Add( "ShutDown", "UBanClose", function() Disconnect( true ) end ) -- Force closed on shutdown.
  251.  
  252. function Escape( str )
  253.     if not db then
  254.         Msg( "Not connected to DB.\n" )
  255.         return
  256.     end
  257.    
  258.     if not str then return end
  259.  
  260.     local esc, err = mysql.escape( db, str )
  261.     if not esc then
  262.         error( tostring( err ), 2 )
  263.         return nil
  264.     end
  265.  
  266.     -- print( "esc=" .. esc ) -- For debug
  267.     return esc
  268. end
  269.  
  270. -- Because we use this a lot
  271. function Format( str )
  272.     if not str then return "NULL" end
  273.     return string.format( "%q", str )
  274. end
  275.  
  276. -- Ban user
  277. function gBanUser( banner, ply, time, comment )
  278.     local time2
  279.     if not time or time == 0 then
  280.         time2 = baninterval
  281.     else
  282.         time2 = math.min( time / 60, baninterval )
  283.     end
  284.     bannername = "Console"
  285.     if banner and banner:IsValid() and banner:IsPlayer() then
  286.         bannername = banner:Nick()
  287.     end
  288.    
  289.     local str = string.format( "kickid %s Banned for %d minutes by %s\n", ply:SteamID(), time2,  bannername)
  290.     game.ConsoleCommand( str )
  291.  
  292.     local str = string.format( "banid %f %s kick\n", time2, ply:SteamID() ) -- Convert time to minutes
  293.     game.ConsoleCommand( str )
  294.  
  295.     return gManualBan( banner, ply:SteamID(), ply:Nick(), time, comment )
  296. end
  297.  
  298. function gManualBan( banner, steamid, name, time, comment )
  299.     if not steamid then
  300.         error( "Bad arguments passed to ManualBan", 2 )
  301.         return
  302.     end
  303.  
  304.     Connect() -- Make sure we're connected
  305.  
  306.     local curuser = gDoQuery( "SELECT CURRENT_USER()" )
  307.     curuser = curuser[ 1 ][ 1 ] -- Get to the info we want
  308.  
  309.     local curip = curuser:gsub( ".-@", "" ) or "127.0.0.1"
  310.     if curip == "localhost" then
  311.         curip = "127.0.0.1"
  312.     end
  313.     local curport = GetConVarNumber( "hostport" )
  314.  
  315.     steamid = steamid:upper():gsub( "STEAM_", "" )
  316.  
  317.     local bannername
  318.     local bannersteam
  319.     if banner and banner:IsValid() and banner:IsPlayer() then
  320.         bannername = banner:Nick()
  321.         bannersteam = banner:SteamID():upper():gsub( "STEAM_", "" )
  322.     end
  323.    
  324.     local timestring = "0" -- ban duration part
  325.     if time and time > 0 then
  326.         timestring = "ADDTIME( NOW(), SEC_TO_TIME( " .. time .. " ) )"
  327.     end
  328.    
  329.     local result
  330.     result = gDoQuery( "INSERT INTO " .. table .. " ( steamid, name, unban_time, comment, serverip, serverport, adminname, adminsteamid ) VALUES( \"" ..
  331.        Escape( steamid ) .. "\", " .. Format( Escape( name ) ) .. ", " .. timestring .. ", " .. Format( Escape( comment ) ) .. ", INET_ATON( \"" .. curip .. "\" ), " .. curport .. ", " .. Format( Escape( bannername ) ) .. ", " .. Format( Escape( bannersteam ) ) .. " )" )
  332.    
  333.     if time == 0 then
  334.         Bans[ steamid ] = baninterval
  335.     else
  336.         Bans[ steamid ] = time
  337.     end
  338.  
  339.     Disconnect() -- Make sure we're disconnected
  340.  
  341.     return result
  342. end
  343.  
  344. function gClearBan( steamid )
  345.     steamid = steamid:upper():gsub( "STEAM_", "" )
  346.  
  347.     Connect() -- Make sure we're connected
  348.  
  349.     local results = gDoQuery( "UPDATE " .. table .. " SET unban_time=NOW(), comment=CONCAT( \"(ban lifted before expired) \", comment ) WHERE (NOW() < unban_time OR unban_time = 0) AND steamid=\"" .. Escape( steamid ) .. "\"", mysql.QUERY_FIELDS ) -- Select active bans
  350.     game.ConsoleCommand( "removeid STEAM_" .. steamid .. "\n" )
  351.    
  352.     Bans[ steamid ] = nil
  353.  
  354.     Disconnect() -- Make sure we're disconnected
  355. end
  356.  
  357. function GetBans()
  358.     Connect() -- Make sure we're connected
  359.  
  360.     local results = gDoQuery( "SELECT id, steamid, TIME_TO_SEC( TIMEDIFF( unban_time, NOW() ) ) as timeleft, name, time, unban_time, comment, INET_NTOA( serverip ) as serverip, serverport, adminname, adminsteamid FROM " .. table .. " WHERE NOW() < unban_time OR unban_time = 0", mysql.QUERY_FIELDS ) -- Select active bans
  361.  
  362.     Disconnect() -- Make sure we're disconnected
  363.  
  364.     return results
  365. end
  366.  
  367. function gDoBans()
  368.     Connect()
  369.  
  370.     local results = gDoQuery( "SELECT steamid, TIME_TO_SEC( TIMEDIFF( unban_time, NOW() ) ) as timeleft FROM " .. table .. " WHERE NOW() < unban_time OR unban_time = 0", mysql.QUERY_FIELDS ) -- Select active bans
  371.  
  372.     local steamids = {}
  373.  
  374.     for _, t in ipairs( results ) do
  375.         local steamid = t.steamid
  376.  
  377.         local time = t.timeleft
  378.         if not time or time == 0 then
  379.             time = baninterval
  380.         else
  381.             time = math.min( time / 60, baninterval )
  382.         end
  383.  
  384.         steamids[ steamid ] = math.max( time, steamids[ steamid ] or 0 ) -- We're doing this so oddly so we can catch multiple results and use the largest one.
  385.     end
  386.  
  387.     -- We're using this following chunk of code to identify current steamids in the server
  388.     local cursteamids = {}
  389.     local players = player.GetAll()
  390.     for _, ply in ipairs( players ) do
  391.         cursteamids[ ply:SteamID() ] = ply
  392.     end
  393.  
  394.     for steamid, time in pairs( steamids ) do -- loop through all currently banned ids
  395.         if cursteamids[ "STEAM_" .. steamid ] then -- Currently connected
  396.             local str = string.format( "kickid STEAM_%s Banned on global ban list\n", steamid )
  397.             game.ConsoleCommand( str )
  398.             Bans[ steamid ] = nil -- Clear their ban info to make sure they get banned. (A 'reban' should only ever arise if console removeid's a steamid)
  399.         end
  400.  
  401.         if not Bans[ steamid ] or Bans[ steamid ] < time or Bans[ steamid ] > time + baninterval * 2 then -- If we don't already have them marked as banned or it's a new time
  402.             local str = string.format( "banid %f STEAM_%s kick\n", time, steamid )
  403.             game.ConsoleCommand( str )
  404.             -- print( str ) -- For debug
  405.         end
  406.         Bans[ steamid ] = time
  407.     end
  408.    
  409.     for steamid in pairs( Bans ) do -- loop through all recorded bans
  410.         if not steamids[ steamid ] then -- If they're not on the ban list we just pulled off the server, they're out of jail!
  411.             game.ConsoleCommand( "removeid STEAM_" .. steamid .. "\n" )
  412.             Bans[ steamid ] = nil
  413.         end
  414.     end
  415.    
  416.     Disconnect()
  417. end
  418.  
  419. gDoBans() -- Initial
  420. timer.Create( "GBantimer", updatetime * 60, 0, gDoBans ) -- Updates
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement