Guest User

Sourcebans Integration Test

a guest
Jul 29th, 2010
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.     Sourcebans Integration Test
  3.     ~ Lexi
  4. --]]
  5.  
  6. require("mysqloo");
  7.  
  8. local config = {
  9.     hostname = "";
  10.     username = "";
  11.     password = "";
  12.     database = "";
  13.     dbprefix = "sbans_";
  14.     website  = "http://bans.ventmob.com";
  15.     portnumb = 3306;
  16.     serverid = 2;
  17. };
  18. local serverport = GetConVarNumber("hostport");
  19. local serverip = "188.222.119.52"; -- Put your ip here if the auto can't work it out.
  20. if (serverip == "") then do -- Thanks Chris! http://www.facepunch.com/showpost.php?p=23154204&postcount=1292
  21.     local hex2dec = {
  22.         ["a"] = 10,
  23.         ["b"] = 11,
  24.         ["c"] = 12,
  25.         ["d"] = 13,
  26.         ["e"] = 14,
  27.         ["f"] = 15,
  28.     }
  29.  
  30.     local dec = tonumber(string.format("%u", GetConVar("hostip"):GetString()))
  31. --  print(dec);
  32.     local split = string.ToTable(string.format("%x", dec))
  33.     local ip = ""
  34. --  PrintTable(split);
  35.     for i=1, #split, 2 do
  36.         local a = split[i]
  37.         local b = split[i+1]
  38. --      print(a,hex2dec[a],tonumber(a),b,hex2dec[b], tonumber(b))
  39.         ip = ip..((hex2dec[a] || tonumber(a))*16)+(hex2dec[b] || tonumber(b))
  40.        
  41.         if (i < (#split-2)) then
  42.             ip = ip.."."
  43.         end
  44.     end
  45.     serverip = ip;
  46. end end
  47. local admins;
  48. --local db;
  49. local queries = {};
  50. local qsuccess,qdata, qfail, dbconnect, dbfail, adminload;
  51. local yes, trying, no, err = 0,1,2,3;
  52. local function notifyerror(...)
  53.     ErrorNoHalt("[",os.date(),"][SBI] ",...);
  54.     print();
  55. end
  56. local function notifymessage(...)
  57.     local words = table.concat({"[",os.date(),"][SBI] ",...},"").."\n";
  58.     ServerLog(words);
  59.     Msg(words);
  60. end
  61. local function blank() end
  62. local function kickbanid(id, time)
  63. -- I just like [[]]s :D
  64.     game.ConsoleCommand("kickid " .. id .. " You are BANNED from this server!\n");
  65.     game.ConsoleCommand("banid " .. time .. " .. id .. ");
  66. end
  67.  
  68. --hook.Add("Initialize", "sourcebans integration",
  69. concommand.Add("sbstartup", function()
  70.     db = mysqloo.connect(config.hostname, config.username, config.password, config.database, config.portnumb);
  71.     db.onConnected = dbconnect;
  72.     db.onConnectionFailed = dbfail;
  73.     db.pending = {};
  74.     db:connect();
  75. end);
  76. --C:\Experiment\orangebox\srcds.exe -console -game garrysmod -port 27027 +map gm_flatgrass -maxplayers 10 +sv_defaultgamemode base
  77. hook.Add("ShutDown", "sourcebans integration", function()
  78.     if (db) then
  79.         db:abortAllQueries();
  80.     end
  81. end);
  82.  
  83. local banCheckerQuery1, banCheckerQuery2, banCheckerQuery3 = "SELECT bid, name, ends, authid, ip FROM " .. config.dbprefix .. "_bans WHERE removetype IS NULL AND (authid = '", --[[ SID GOES HERE ]] "' OR IP = '", --[[ IP GOES HERE ]] "') LIMIT 1";
  84. hook.Add("PlayerAuthed", "sourcebans integration", function(ply, sid, uid)
  85.     if (not db) then return; end
  86.     local ip = ply:IPAddress();
  87.     local qtxt = banCheckerQuery1 .. sid .. banCheckerQuery2 .. ip .. banCheckerQuery3;
  88.     if (db:status() == yes) then
  89.         local q = db:query(qtxt);
  90.         q.sid = sid;
  91.         q.ply = ply;
  92.         q.name = ply:Name();
  93.         q.onData = qdata;
  94.         q.onFailure = qfail;
  95. --      q.onSuccess = qsuccess
  96.         q:start();
  97.         queries[sid] = q;
  98.     else
  99.         table.insert(q.pending, {qtxt, ply});
  100.     end
  101.     if (not admins) then return; end
  102.     local info = admins[sid];
  103.     if (info) then
  104.         notifymessage(ply:Name()," is in group ", info.srv_group, " and will be set as such.");
  105.         ply:SetUserGroup(string.lower(info.srv_group));
  106.     end
  107. end);  
  108.  
  109. hook.Add("Tick", "Sourcebans Integration", function()
  110.     if (not db or #db.pending == 0 or db.retrytimer) then return; end
  111.     local status = db:status();
  112.     if (status == trying) then
  113.         return;
  114.     end
  115.     if (status == yes) then
  116.         local q,sid,ply;
  117.         for _, stuff in pairs(db.pending) do
  118.             ply = stuff[2]
  119.             sid = ply:SteamID()
  120.             q = db:query(stuff[1])
  121.             q.sid = sid;
  122.             q.ply = ply
  123.             q.name = ply:Name();
  124.             q.onData = qdata;
  125.             q.qtype = "Ban Checking";
  126.             q.onFailure = qfail;
  127.             q.onSuccess = qsuccess
  128.             q:start();
  129.             queries[sid] = q;
  130.         end
  131.         db.pending = {};
  132.         return;
  133.     elseif (status == err) then
  134.         notifyerror("The database object has suffered an internal error. Recreating...");
  135.         local pending = db.pending;
  136.         db = mysqloo.connect(config.hostname, config.username, config.password, config.database, config.portnumb);
  137.         db.onConnected = dbconnect;
  138.         db.onConnectionFailed = dbfail;
  139.         db.pending = pending;
  140.         db:connect();
  141.         return;
  142.     end
  143.     notifyerr("The server has lost connection to the database. Retrying...")
  144.     db:connect();
  145. end);
  146.    
  147.    
  148. concommand.Add("sm_rehash", function(p)
  149.     if (p and p:IsValid()) then return; end
  150.     adminload();
  151. end);
  152.  
  153. local logquery = "INSERT INTO " .. config.dbprefix .. "_banlog (sid, time, name, bid) VALUES( " .. config.serverid .. ", "
  154. function qdata(q, data)
  155.     print("qdata!");
  156.     PrintTable(data);
  157.     notifymessage("'",q.name,"' has been identified as '",data.name,"', who is banned. Kicking...")
  158.     kickbanid(q.sid, math.ceil((data.ends - os.time()) / 60));
  159. --  q.ply:Ban(data.ends - os.time(), "You are BANNED from this server!");
  160. --  q.ply:Kick("You are BANNED from this server!")-- .. config.website);   
  161. --  game.ConsoleCommand("banid " .. math.ceil((data.ends - os.time()) / 60) .. " " .. q.sid .. "\n");
  162. --  RunConsoleCommand("banid", data.ends - os.time(), q.sid);
  163.     local q = db:query(logquery .. os.time() .. ", '" .. db:escape(q.name) .. "', " .. data.bid .. ")");
  164.     q.sid = data.authid;
  165.     q.qtype = "Ban Logging Incrementer";
  166.     q.onFailure = qfail;
  167.     q.onSuccess = blank;
  168.     q.onData = blank;
  169.     q:start();
  170.     queries[data.authid] = q;
  171. end;
  172.  
  173. function qfail(q, err)
  174.     notifyerror(q.qtype, " query has failed for '", q.sid, "' with error: ", err);
  175. end;
  176.  
  177. function dbconnect(db)
  178.     print("The database has successfully connected!")
  179.     db.retrytimer = nil;
  180.     if (config.serverid < 0) then
  181.         local q = db:query("SELECT sid FROM %s_servers WHERE ip = '" .. serverip .. "' AND port = '" .. serverport .. "' LIMIT 1")
  182.         q.onSuccess = function(q, data) config.sererid = data.sid; end;
  183.         q:start();
  184.     end
  185.     if (admins) then return; end
  186.     adminload();
  187. end
  188.  
  189. local function failsafe()
  190.     if (db) then
  191.         db:connect();
  192.     end
  193. end
  194. function dbfail(db, err)
  195.     notifyerror("The database has failed to connect, error: ", err, ". Retrying in 60 seconds.");
  196.     db.retrytimer = true;
  197.     timer.Simple(60, failsafe);
  198. end
  199.  
  200. local adminquery,qdata;
  201. local loadingquery = "SELECT a.aid, a.user, a.authid, a.srv_group FROM " .. config.dbprefix .. "_admins a, " .. config.dbprefix .. "_admins_servers_groups g WHERE g.server_id = " .. config.serverid .. " AND g.admin_id = a.aid";
  202. function adminload()
  203.     print("admin querying goes here");
  204.     admins = {};
  205. --  local q = db:query("SELECT admin_id FROM "
  206. --  do return; end
  207.     --10:00 - sk89q: SELECT a.* FROM sbans_admins a, sbans_admins_server_groups g WHERE a.aid = #### AND g._server_id = #### AND g.admin_id = a.aid
  208. --  print(loadingquery);
  209.     local q  = db:query(loadingquery);
  210.     q.onFailure = qfail;
  211.     q.qtype = "Admin grabbing";
  212.     q.sid = "no good reason";
  213.     q.onData = qdata;
  214. --  q.onSuccess = function(q) print(q) PrintTable(q:getData()) end;
  215.     q:start();
  216.     adminquery = q;
  217. end
  218.  
  219. function qdata(q, data)
  220.     admins[data.authid] = data;
  221.     notifymessage("Added admin ", data.user, " with level ", data.srv_group);
  222. end
  223. local doban;
  224. function SBBanPlayer(ply, time, reason, admin)
  225.     doban(
  226.         ply:IPAddress(),
  227.         ply:SteamID(),
  228.         ply:Name(),
  229.         time,
  230.         reason,
  231.         admin and admins[admin:SteamID()].aid,
  232.         admin and admin:IPAddress()
  233.     );
  234. end
  235.  
  236. function SBBanPlayerBySteamID(sid, time, name, reason, admin)
  237.     doban("", sid, name, time, reason, admin and admins[admin:SteamID()].aid, admin and admin:IPAddress())
  238. end
  239. local banquery = "INSERT INTO " .. config.dbprefix .. "_bans (ip, authid, name, created, ends, length, reason, aid, adminIp, sid, country) VALUES('";
  240. local sepr = "','";
  241. function doban(ip, authid, name, length, reason, adminid, adminip)
  242.     local created, ends = os.time();
  243.     ends = created + length;
  244.     if (not adminid) then
  245.         adminid = "STEAM_ID_SERVER";
  246.         adminip = serverip;
  247.     end
  248.     local qtxt = banquery .. ip ..sepr .. authid .. sepr .. name .. "'," .. created .. "," .. ends .. "," .. length .. ",'" .. reason .. sepr .. adminid .. sepr .. adminip .. "'," .. config.serverid .. ",' ')";
  249.     print(qtxt);
  250. end
  251.  
  252.  
  253. concommand.Add("DumpAllTheTablesNShit",function()
  254.     print("queries")
  255.     PrintTable(queries)
  256.     print("admins");
  257.     if (admins) then
  258.         PrintTable(admins)
  259.     else
  260.         print();
  261.     end
  262. end)
RAW Paste Data