Author Topic: Sourcebans & ULX/ULib  (Read 68724 times)

0 Members and 1 Guest are viewing this topic.

Offline centran

  • Newbie
  • *
  • Posts: 43
  • Karma: 8
Sourcebans & ULX/ULib
« on: December 04, 2012, 12:02:25 PM »
Since the other topics are outdated I figure I will bring up this topic once again and post my implementation.

Sourcebans is pretty easy to implement with lexi's sourceban module http://lexi.org.uk/modules/sourcebans.html
You do NOT need to run sourcemod as lexi's module interacts directly to the sourcebans database without the need for metamod or sourcemod.

For the sourcebans module to work you will need a module called mysqloo.
Installation instructions and binaries can be found here http://www.facepunch.com/showthread.php?t=1220537

As a base you will want to install the sourceban module found here http://lexi.org.uk/modules/sourcebans.lua
Create the following directory and place the file in there. addons/ulx/lua/includes/modules/

I had to make a slight modification to the module because of how I had admins set up in sourcebans. The module will add the admins for the server you specify in the config file(which I will get to in a bit). i did not want to add admins to specific servers. So instead I changed the module to load all admins in sourcebans. Depending on how your sourcebans is set up you may or may not want to make the following change.

replace line 137
Code: Text
  1. --    ["Select Admins"] = "SELECT a.aid, a.user, a.authid, a.srv_group, a.srv_flags, a.immunity FROM %s_admins a, %s_admins_servers_groups g WHERE g.server_id = %i AND g.admin_id = a.aid";
  2. ["Select Admins"] = "SELECT a.aid, a.user, a.authid, a.srv_group, a.srv_flags, a.immunity FROM %s_admins a";

You will need to replace a couple lines in sourcebans.lua
replace line 29
Code: Text
  1. CreateConVar("sb_version", "1.521", FCVAR_SPONLY+FCVAR_REPLICATED+FCVAR_NOTIFY, "The current version of the SourceBans.lua module");
  2.  
replace line 25
Code: Text
  1. --require("gatekeeper");
  2.  

Next you need to edit and place the config file located here http://lexi.org.uk/modules/sourcebans_example.lua
I renamed the file sourcebans_init.lua and placed it in addons/ulx/lua/ulx/modules/

Once you have placed those two files above in those locations you will have access to sourcebans. Restart the server and join. Watch the console or look at the logs and you should see it load all your admins you have in sourcebans. If it doesn't then it will probably spit out an error about the database. Assuming it loaded the admins and you are an admin you should be able to run some new commands in console. Try running "sm_csay hello" in console to test if it is working. You can try a test ban with sm_ban. But we don't want to use the console commands... this is about ulx intergration!

To intergrate with ulx you have two options. The first option is the "safe" option and the second option is the nicer option.

For the safe option you will want to place the following code in addons/ulx/lua/ulx/modules/sh/ and name it sban.lua
Code: Text
  1. local CATEGORY_NAME = "sourcebans"
  2.  
  3. function ulx.sban( calling_ply, target_ply, minutes, reason )
  4.         if target_ply:IsBot() then
  5.                 ULib.tsayError( calling_ply, "Cannot ban a bot", true )
  6.                 return
  7.         end
  8.  
  9.         local steamid = target_ply:SteamID()
  10.         local name = target_ply:Nick()
  11.        
  12.         sourcebans.BanPlayerBySteamID( steamid, minutes*60, reason, calling_ply, name )
  13.  
  14.         local time = "for #i minute(s)"
  15.         if minutes == 0 then time = "permanently" end
  16.         local str = "#A banned #T " .. time
  17.         if reason and reason ~= "" then str = str .. " (#s)" end
  18.         ulx.fancyLogAdmin( calling_ply, str, target_ply, minutes ~= 0 and minutes or reason, reason )
  19. end
  20. local sban = ulx.command( CATEGORY_NAME, "ulx sban", ulx.sban, "!sban" )
  21. sban:addParam{ type=ULib.cmds.PlayerArg }
  22. sban:addParam{ type=ULib.cmds.NumArg, hint="minutes, 0 for perma", ULib.cmds.optional, ULib.cmds.allowTimeString, min=0 }
  23. sban:addParam{ type=ULib.cmds.StringArg, hint="reason", ULib.cmds.optional, ULib.cmds.takeRestOfLine, completes=ulx.common_kick_reasons }
  24. sban:defaultAccess( ULib.ACCESS_ADMIN )
  25. sban:help( "Bans target." )
  26.  
  27. function ulx.sbanid( calling_ply, steamid, minutes, reason )
  28.         steamid = steamid:upper()
  29.         if not ULib.isValidSteamID( steamid ) then
  30.                 ULib.tsayError( calling_ply, "Invalid steamid." )
  31.                 return
  32.         end
  33.  
  34.         local name
  35.         local plys = player.GetAll()
  36.         for i=1, #plys do
  37.                 if plys[ i ]:SteamID() == steamid then
  38.                         name = plys[ i ]:Nick()
  39.                         break
  40.                 end
  41.         end
  42.  
  43.         sourcebans.BanPlayerBySteamID( steamid, minutes*60, reason, calling_ply, name )
  44.  
  45.         local time = "for #i minute(s)"
  46.         if minutes == 0 then time = "permanently" end
  47.         local str = "#A banned steamid #s "
  48.         if name then
  49.                 steamid = steamid .. "(" .. name .. ") "
  50.         end
  51.         str = str .. time
  52.         if reason and reason ~= "" then str = str .. " (#4s)" end
  53.         ulx.fancyLogAdmin( calling_ply, str, steamid, minutes ~= 0 and minutes or reason, reason )
  54. end
  55. local sbanid = ulx.command( CATEGORY_NAME, "ulx sbanid", ulx.sbanid , "!sbanid" )
  56. sbanid:addParam{ type=ULib.cmds.StringArg, hint="steamid" }
  57. sbanid:addParam{ type=ULib.cmds.NumArg, hint="minutes, 0 for perma", ULib.cmds.optional, ULib.cmds.allowTimeString, min=0 }
  58. sbanid:addParam{ type=ULib.cmds.StringArg, hint="reason", ULib.cmds.optional, ULib.cmds.takeRestOfLine, completes=ulx.common_kick_reasons }
  59. sbanid:defaultAccess( ULib.ACCESS_SUPERADMIN )
  60. sbanid:help( "Bans steamid." )
  61.  
  62. function ulx.sunban( calling_ply, steamid )
  63.         steamid = steamid:upper()
  64.         if not ULib.isValidSteamID( steamid ) then
  65.                 ULib.tsayError( calling_ply, "Invalid steamid." )
  66.                 return
  67.         end
  68.  
  69.         name = ULib.bans[ steamid ] and ULib.bans[ steamid ].name
  70.  
  71.         sourcebans.UnbanPlayerBySteamID(steamid, "in game unban", calling_ply )
  72.  
  73.         if name then
  74.                 ulx.fancyLogAdmin( calling_ply, "#A unbanned steamid #s", steamid .. " (" .. name .. ")" )
  75.         else
  76.                 ulx.fancyLogAdmin( calling_ply, "#A unbanned steamid #s", steamid )
  77.         end
  78. end
  79. local sunban = ulx.command( CATEGORY_NAME, "ulx sunban", ulx.sunban )
  80. sunban:addParam{ type=ULib.cmds.StringArg, hint="steamid" }
  81. sunban:defaultAccess( ULib.ACCESS_ADMIN )
  82. sunban:help( "Unbans steamid." )
  83.  

Congratulations you now have the !sban !sbanid and !sunban commands
However I do not like that implementation as it separates out the banning into its own commands. I like sourcebans to be integrated into the ban commands so it integrates nicely with the gui, ban list, and gamemod banning such as getting banned in TTT for low karma.

Option two is the nicer one in my opinion but the more dangerous one... also the more annoying one because if you ever update ULib you will need to make these change again.

You will need to edit the following file ulib/lua/ulib/server/player.lua
At line 180 of the latest svn as of this writing you will find the ULib.addBan function. Replace the entire function with the following...
Code: Text
  1. function ULib.addBan( steamid, time, reason, name, admin )
  2.         local strTime = time ~= 0 and string.format( "for %s minute(s)", time ) or "permanently"
  3.         local showReason = string.format( "Banned %s: %s", strTime, reason )
  4.  
  5.         if ( sourcebans ) then
  6.                 sourcebans.BanPlayerBySteamID( steamid, time*60, reason, admin, name )
  7.         else
  8.                 local players = player.GetAll()
  9.                 for i=1, #players do
  10.                         if players[ i ]:SteamID() == steamid then
  11.                                 ULib.kick( players[ i ], showReason, admin )
  12.                         end
  13.                 end
  14.  
  15.                 -- This redundant kick code is to ensure they're kicked -- even if they're joining
  16.                 game.ConsoleCommand( string.format( "kickid %s %s\n", steamid, showReason or "" ) )
  17.                 game.ConsoleCommand( string.format( "banid %f %s kick\n", time, steamid ) )
  18.                 game.ConsoleCommand( "writeid\n" )
  19.         end
  20.  
  21.         local admin_name
  22.         if admin then
  23.                 admin_name = "(Console)"
  24.                 if admin:IsValid() then
  25.                         admin_name = string.format( "%s(%s)", admin:Name(), admin:SteamID() )
  26.                 end
  27.         end
  28.  
  29.         local t = {}
  30.         if ULib.bans[ steamid ] then
  31.                 t = ULib.bans[ steamid ]
  32.                 t.modified_admin = admin_name
  33.                 t.modified_time = os.time()
  34.         else
  35.                 t.admin = admin_name
  36.         end
  37.         t.time = t.time or os.time()
  38.         if time > 0 then
  39.                 t.unban = ( ( time * 60 ) + os.time() )
  40.         else
  41.                 t.unban = 0
  42.         end
  43.         if reason then
  44.                 t.reason = reason
  45.         end
  46.         if name then
  47.                 t.name = name
  48.         end
  49.         ULib.bans[ steamid ] = t
  50.         file.Write( ULib.BANS_FILE, ULib.makeKeyValues( ULib.bans ) )
  51. end
  52.  

You will also need to change the unban function. At line 245 of the latest svn as of this writing you will find the ULib.unban function. Replace the entire function with the following...
Code: Text
  1. function ULib.unban( steamid )
  2.  
  3.         --Default banlist
  4.         if file.Exists( "cfg/banned_user.cfg", "GAME" ) then
  5.                 ULib.execFile( "cfg/banned_user.cfg", "GAME" )
  6.         end
  7.         if ( sourcebans ) then
  8.                 sourcebans.UnbanPlayerBySteamID( steamid, "In game unban", nil)
  9.         else
  10.                 ULib.queueFunctionCall( game.ConsoleCommand, "removeid " .. steamid .. ";writeid\n" ) -- Execute after done loading bans
  11.         end
  12.  
  13.         --ULib banlist
  14.         ULib.bans[ steamid ] = nil
  15.         file.Write( ULib.BANS_FILE, ULib.makeKeyValues( ULib.bans ) )
  16.  
  17. end
  18.  

That's it. You will now have sourcebans fully integrated into ULX. Any module or gamemode that calls on ULib.ban will pass the banning onto sourcebans so long as the module sourcebans is loaded.
« Last Edit: December 10, 2012, 10:26:27 AM by centran »

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6197
  • Karma: 394
  • Project Lead
Re: Sourcebans & ULX/ULib
« Reply #1 on: December 04, 2012, 01:30:13 PM »
Hats off to you for a well written module, and in two colors! :)

We have discussed possibly integrating source bans into ULX, but it's great to see other developers working out solutions of their own and sharing.
Experiencing God's grace one day at a time.

Offline thegtz

  • Newbie
  • *
  • Posts: 25
  • Karma: 0
Re: Sourcebans & ULX/ULib
« Reply #2 on: December 06, 2012, 11:06:16 PM »
[ERROR] addons/ulx/lua/includes/modules/sourcebans.lua:29: ')' expected near '|'
  1. unknown - addons/ulx/lua/includes/modules/sourcebans.lua:0

L 12/06/2012 - 22:10:42: Lua Error:
[ERROR] addons/ulx/lua/ulx/modules/sourcebans_init.lua:5: Module not found!
  1. require - [C]:-1
   2. unknown - addons/ulx/lua/ulx/modules/sourcebans_init.lua:5
    3. include - [C]:-1
     4. unknown - addons/ulx/lua/ulx/init.lua:28
      5. include - [C]:-1
       6. unknown - addons/ulx/lua/ulib/modules/ulx_init.lua:2
        7. include - [C]:-1
         8. unknown - addons/ulib/lua/ulib/init.lua:75
          9. include - [C]:-1
           10. unknown - addons/ulib/lua/autorun/ulib_init.lua:3

Offline centran

  • Newbie
  • *
  • Posts: 43
  • Karma: 8
Re: Sourcebans & ULX/ULib
« Reply #3 on: December 07, 2012, 12:01:52 AM »
[ERROR] addons/ulx/lua/includes/modules/sourcebans.lua:29: ')' expected near '|'

Ah yes I forgot about that. I added it to the guide.
Replace line 29 with the following.
Code: [Select]
CreateConVar("sb_version", "1.521", FCVAR_SPONLY+FCVAR_REPLICATED+FCVAR_NOTIFY, "The current version of the SourceBans.lua module");

Offline Mors Quaedam

  • Newbie
  • *
  • Posts: 30
  • Karma: 1
Re: Sourcebans & ULX/ULib
« Reply #4 on: December 08, 2012, 06:03:58 AM »
I get this when I start my server:

Offline thegtz

  • Newbie
  • *
  • Posts: 25
  • Karma: 0
Re: Sourcebans & ULX/ULib
« Reply #5 on: December 08, 2012, 05:30:28 PM »
Do you know where I can get a working copy of gatekeeper for gmod 13?
-----
L 12/08/2012 - 18:54:52: Lua Error: Couldn't include file 'includes\modules\gatekeeper.lua' (File not found) (@addons/ulx/lua/includes/modules/sourcebans.lua (line 25))

Couldn't include file 'includes\modules\gatekeeper.lua' (File not found) (@addons/ulx/lua/includes/modules/sourcebans.lua (line 25))
L 12/08/2012 - 18:54:52: Lua Error:
[ERROR] addons/ulx/lua/includes/modules/sourcebans.lua:25: Module not found!
  1. require - [C]:-1
   2. unknown - addons/ulx/lua/includes/modules/sourcebans.lua:25
    3. require - [C]:-1
     4. unknown - addons/ulx/lua/ulx/modules/sourcebans_init.lua:5
      5. include - [C]:-1
       6. unknown - addons/ulx/lua/ulx/init.lua:28
        7. include - [C]:-1
         8. unknown - addons/ulx/lua/ulib/modules/ulx_init.lua:2
          9. include - [C]:-1
           10. unknown - addons/ulib/lua/ulib/init.lua:75
            11. include - [C]:-1
             12. unknown - addons/ulib/lua/autorun/ulib_init.lua:3

Offline centran

  • Newbie
  • *
  • Posts: 43
  • Karma: 8
Re: Sourcebans & ULX/ULib
« Reply #6 on: December 09, 2012, 05:02:03 PM »
Do you know where I can get a working copy of gatekeeper for gmod 13?

Don't use it. You don't need it.

Replace line 25 of sourcebans.lua with this
Code: [Select]
--require("gatekeeper");

Offline centran

  • Newbie
  • *
  • Posts: 43
  • Karma: 8
Re: Sourcebans & ULX/ULib
« Reply #7 on: December 09, 2012, 06:40:50 PM »
I get this when I start my server:


try the following.
Code: [Select]
local CATEGORY_NAME = "sourcebans"

function ulx.sban( calling_ply, target_ply, minutes, reason )
        if target_ply:IsBot() then
                ULib.tsayError( calling_ply, "Cannot ban a bot", true )
                return
        end

        local steamid = target_ply:SteamID()
        local name = target_ply:Nick()
       
        sourcebans.BanPlayerBySteamID( steamid, minutes*60, reason, calling_ply, name )

        local time = "for #i minute(s)"
        if minutes == 0 then time = "permanently" end
        local str = "#A banned #T " .. time
        if reason and reason ~= "" then str = str .. " (#s)" end
        ulx.fancyLogAdmin( calling_ply, str, target_ply, minutes ~= 0 and minutes or reason, reason )
end
local sban = ulx.command( CATEGORY_NAME, "ulx sban", ulx.sban, "!sban" )
sban:addParam{ type=ULib.cmds.PlayerArg }
sban:addParam{ type=ULib.cmds.NumArg, hint="minutes, 0 for perma", ULib.cmds.optional, ULib.cmds.allowTimeString, min=0 }
sban:addParam{ type=ULib.cmds.StringArg, hint="reason", ULib.cmds.optional, ULib.cmds.takeRestOfLine, completes=ulx.common_kick_reasons }
sban:defaultAccess( ULib.ACCESS_ADMIN )
sban:help( "Bans target." )

function ulx.sbanid( calling_ply, steamid, minutes, reason )
        steamid = steamid:upper()
        if not ULib.isValidSteamID( steamid ) then
                ULib.tsayError( calling_ply, "Invalid steamid." )
                return
        end

        local name
        local plys = player.GetAll()
        for i=1, #plys do
                if plys[ i ]:SteamID() == steamid then
                        name = plys[ i ]:Nick()
                        break
                end
        end

        sourcebans.BanPlayerBySteamID( steamid, minutes*60, reason, calling_ply, name )

        local time = "for #i minute(s)"
        if minutes == 0 then time = "permanently" end
        local str = "#A banned steamid #s "
        if name then
                steamid = steamid .. "(" .. name .. ") "
        end
        str = str .. time
        if reason and reason ~= "" then str = str .. " (#4s)" end
        ulx.fancyLogAdmin( calling_ply, str, steamid, minutes ~= 0 and minutes or reason, reason )
end
local sbanid = ulx.command( CATEGORY_NAME, "ulx sbanid", ulx.sbanid , "!sbanid" )
sbanid:addParam{ type=ULib.cmds.StringArg, hint="steamid" }
sbanid:addParam{ type=ULib.cmds.NumArg, hint="minutes, 0 for perma", ULib.cmds.optional, ULib.cmds.allowTimeString, min=0 }
sbanid:addParam{ type=ULib.cmds.StringArg, hint="reason", ULib.cmds.optional, ULib.cmds.takeRestOfLine, completes=ulx.common_kick_reasons }
sbanid:defaultAccess( ULib.ACCESS_SUPERADMIN )
sbanid:help( "Bans steamid." )

function ulx.sunban( calling_ply, steamid )
        steamid = steamid:upper()
        if not ULib.isValidSteamID( steamid ) then
                ULib.tsayError( calling_ply, "Invalid steamid." )
                return
        end

        name = ULib.bans[ steamid ] and ULib.bans[ steamid ].name

        sourcebans.UnbanPlayerBySteamID(steamid, "in game unban", calling_ply )

        if name then
                ulx.fancyLogAdmin( calling_ply, "#A unbanned steamid #s", steamid .. " (" .. name .. ")" )
        else
                ulx.fancyLogAdmin( calling_ply, "#A unbanned steamid #s", steamid )
        end
end
local sunban = ulx.command( CATEGORY_NAME, "ulx sunban", ulx.sunban )
sunban:addParam{ type=ULib.cmds.StringArg, hint="steamid" }
sunban:defaultAccess( ULib.ACCESS_ADMIN )
sunban:help( "Unbans steamid." )

Offline Mors Quaedam

  • Newbie
  • *
  • Posts: 30
  • Karma: 1
Re: Sourcebans & ULX/ULib
« Reply #8 on: December 10, 2012, 09:41:47 AM »
Upon running !sbanid I get this


Offline Mors Quaedam

  • Newbie
  • *
  • Posts: 30
  • Karma: 1
Re: Sourcebans & ULX/ULib
« Reply #9 on: December 10, 2012, 10:01:15 AM »
I dug a bit deeper with this error by putting require("sourcebans") at the top of the sban.lua, and I get this error now:


Offline centran

  • Newbie
  • *
  • Posts: 43
  • Karma: 8
Re: Sourcebans & ULX/ULib
« Reply #10 on: December 10, 2012, 10:22:49 AM »
Ahh yes... another critical step I failed to mention.
You need the mysqloo module which can be found here http://www.facepunch.com/showthread.php?t=1220537
Windows
Linux

You need to place the mysqloo module in your /lua/bin/ folder

You will also need libMySQL installed wherever you scrds_run file is... linux in the orangebox folder, windows in Steam/steamapps/steam_username/garrysmod
Windows
Linux

Offline Mors Quaedam

  • Newbie
  • *
  • Posts: 30
  • Karma: 1
Re: Sourcebans & ULX/ULib
« Reply #11 on: December 10, 2012, 12:18:37 PM »
Okay, I've done a crapload of fixes (as you also forgot the gatekeeper), but !sban won't put bans onto the SourceBans database.

Dat empty banlist:


Anyway, I've found the working gatekeeper lua for gmod 13. Place it in srcds/orangebox/garrysmod/lua/includes/modules:

Code: [Select]
gatekeeper={}
local spawning={}
 
gameevent.Listen("player_connect")
gameevent.Listen("player_disconnect")
 
function gatekeeper.Drop(userid, reason)
    game.ConsoleCommand(string.format("kickid %d %s\n",userid,reason:gsub(';|\n','')))
end
 
function gatekeeper.GetNumClients()
    local active=#player.GetAll()
    return {spawning=#spawning, active=active, total=#spawning+active}
end
 
function gatekeeper.GetUserByAddress(addr)
    for k,v in pairs(player.GetAll()) do
        if(v:IPAddress()==addr) then
            return v:UserID()
        end
    end
end
 
hook.Add("player_connect", "GateKeeper", function(data)
    local ret=hook.Call("PlayerPasswordAuth", GAMEMODE, data.name, "", data.networkid, data.address)
    print(ret)
    if(type(ret)=="string") then
        gatekeeper.Drop(data.userid, ret)
        return
    elseif(type(ret)=="boolean") then
        if(ret) then
            gatekeeper.Drop(data.userid, "Bad Password")
            return
        end
    end
    table.insert(spawning, data.userid)
end)
 
hook.Add("player_disconnect", "GateKeeper", function(data)
    for k,v in pairs(spawning) do
        if(data.userid==v) then
            table.remove(spawning, k)
            break
        end
    end
end)
 
hook.Add("PlayerInitialSpawn", "GateKeeper", function(ply)
    for k,v in pairs(spawning) do
        if(ply:UserID()==v) then
            table.remove(spawning, k)
            break
        end
    end
end)

function gatekeeper.DropAllClients(reason)
    for k,v in pairs(spawning) do
        gatekeeper.Drop(v, reason)
    end
    for k,v in pairs(player.GetAll()) do
        gatekeeper.Drop(v, reason)
    end
end

Offline centran

  • Newbie
  • *
  • Posts: 43
  • Karma: 8
Re: Sourcebans & ULX/ULib
« Reply #12 on: December 10, 2012, 01:49:57 PM »
can you confirm that sourcebans is being loaded properly? you should see it load the admin list in console/logs.
you should also be able to run sm_csay in your game console to verify the admin list was set up and you are in it.

Is there any console/log errors when you try to ban someone?

Offline thegtz

  • Newbie
  • *
  • Posts: 25
  • Karma: 0
Re: Sourcebans & ULX/ULib
« Reply #13 on: December 10, 2012, 10:35:51 PM »
Don't use it. You don't need it.

Replace line 25 of sourcebans.lua with this
Code: [Select]
--require("gatekeeper");

Thanks. Seems to be working perfectly now.

Offline Mors Quaedam

  • Newbie
  • *
  • Posts: 30
  • Karma: 1
Re: Sourcebans & ULX/ULib
« Reply #14 on: December 11, 2012, 10:23:19 AM »
Worked for about a minute, after installing SB module on other two gmod servers, all 3 servers (and the TF2 server) stopped contacting the MySQL server and can no longer sm_rehash.

Edit: Never mind, I fixed it.
« Last Edit: December 11, 2012, 10:30:05 AM by Mors Quaedam »