Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Sourcebans Integration Test
- ~ Lexi
- --]]
- require("mysqloo");
- local config = {
- hostname = "";
- username = "";
- password = "";
- database = "";
- dbprefix = "sbans_";
- website = "http://bans.ventmob.com";
- portnumb = 3306;
- serverid = 2;
- };
- local serverport = GetConVarNumber("hostport");
- local serverip = "188.222.119.52"; -- Put your ip here if the auto can't work it out.
- if (serverip == "") then do -- Thanks Chris! http://www.facepunch.com/showpost.php?p=23154204&postcount=1292
- local hex2dec = {
- ["a"] = 10,
- ["b"] = 11,
- ["c"] = 12,
- ["d"] = 13,
- ["e"] = 14,
- ["f"] = 15,
- }
- local dec = tonumber(string.format("%u", GetConVar("hostip"):GetString()))
- -- print(dec);
- local split = string.ToTable(string.format("%x", dec))
- local ip = ""
- -- PrintTable(split);
- for i=1, #split, 2 do
- local a = split[i]
- local b = split[i+1]
- -- print(a,hex2dec[a],tonumber(a),b,hex2dec[b], tonumber(b))
- ip = ip..((hex2dec[a] || tonumber(a))*16)+(hex2dec[b] || tonumber(b))
- if (i < (#split-2)) then
- ip = ip.."."
- end
- end
- serverip = ip;
- end end
- local admins;
- --local db;
- local queries = {};
- local qsuccess,qdata, qfail, dbconnect, dbfail, adminload;
- local yes, trying, no, err = 0,1,2,3;
- local function notifyerror(...)
- ErrorNoHalt("[",os.date(),"][SBI] ",...);
- print();
- end
- local function notifymessage(...)
- local words = table.concat({"[",os.date(),"][SBI] ",...},"").."\n";
- ServerLog(words);
- Msg(words);
- end
- local function blank() end
- local function kickbanid(id, time)
- -- I just like [[]]s :D
- game.ConsoleCommand("kickid " .. id .. " You are BANNED from this server!\n");
- game.ConsoleCommand("banid " .. time .. " .. id .. ");
- end
- --hook.Add("Initialize", "sourcebans integration",
- concommand.Add("sbstartup", function()
- db = mysqloo.connect(config.hostname, config.username, config.password, config.database, config.portnumb);
- db.onConnected = dbconnect;
- db.onConnectionFailed = dbfail;
- db.pending = {};
- db:connect();
- end);
- --C:\Experiment\orangebox\srcds.exe -console -game garrysmod -port 27027 +map gm_flatgrass -maxplayers 10 +sv_defaultgamemode base
- hook.Add("ShutDown", "sourcebans integration", function()
- if (db) then
- db:abortAllQueries();
- end
- end);
- 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";
- hook.Add("PlayerAuthed", "sourcebans integration", function(ply, sid, uid)
- if (not db) then return; end
- local ip = ply:IPAddress();
- local qtxt = banCheckerQuery1 .. sid .. banCheckerQuery2 .. ip .. banCheckerQuery3;
- if (db:status() == yes) then
- local q = db:query(qtxt);
- q.sid = sid;
- q.ply = ply;
- q.name = ply:Name();
- q.onData = qdata;
- q.onFailure = qfail;
- -- q.onSuccess = qsuccess
- q:start();
- queries[sid] = q;
- else
- table.insert(q.pending, {qtxt, ply});
- end
- if (not admins) then return; end
- local info = admins[sid];
- if (info) then
- notifymessage(ply:Name()," is in group ", info.srv_group, " and will be set as such.");
- ply:SetUserGroup(string.lower(info.srv_group));
- end
- end);
- hook.Add("Tick", "Sourcebans Integration", function()
- if (not db or #db.pending == 0 or db.retrytimer) then return; end
- local status = db:status();
- if (status == trying) then
- return;
- end
- if (status == yes) then
- local q,sid,ply;
- for _, stuff in pairs(db.pending) do
- ply = stuff[2]
- sid = ply:SteamID()
- q = db:query(stuff[1])
- q.sid = sid;
- q.ply = ply
- q.name = ply:Name();
- q.onData = qdata;
- q.qtype = "Ban Checking";
- q.onFailure = qfail;
- q.onSuccess = qsuccess
- q:start();
- queries[sid] = q;
- end
- db.pending = {};
- return;
- elseif (status == err) then
- notifyerror("The database object has suffered an internal error. Recreating...");
- local pending = db.pending;
- db = mysqloo.connect(config.hostname, config.username, config.password, config.database, config.portnumb);
- db.onConnected = dbconnect;
- db.onConnectionFailed = dbfail;
- db.pending = pending;
- db:connect();
- return;
- end
- notifyerr("The server has lost connection to the database. Retrying...")
- db:connect();
- end);
- concommand.Add("sm_rehash", function(p)
- if (p and p:IsValid()) then return; end
- adminload();
- end);
- local logquery = "INSERT INTO " .. config.dbprefix .. "_banlog (sid, time, name, bid) VALUES( " .. config.serverid .. ", "
- function qdata(q, data)
- print("qdata!");
- PrintTable(data);
- notifymessage("'",q.name,"' has been identified as '",data.name,"', who is banned. Kicking...")
- kickbanid(q.sid, math.ceil((data.ends - os.time()) / 60));
- -- q.ply:Ban(data.ends - os.time(), "You are BANNED from this server!");
- -- q.ply:Kick("You are BANNED from this server!")-- .. config.website);
- -- game.ConsoleCommand("banid " .. math.ceil((data.ends - os.time()) / 60) .. " " .. q.sid .. "\n");
- -- RunConsoleCommand("banid", data.ends - os.time(), q.sid);
- local q = db:query(logquery .. os.time() .. ", '" .. db:escape(q.name) .. "', " .. data.bid .. ")");
- q.sid = data.authid;
- q.qtype = "Ban Logging Incrementer";
- q.onFailure = qfail;
- q.onSuccess = blank;
- q.onData = blank;
- q:start();
- queries[data.authid] = q;
- end;
- function qfail(q, err)
- notifyerror(q.qtype, " query has failed for '", q.sid, "' with error: ", err);
- end;
- function dbconnect(db)
- print("The database has successfully connected!")
- db.retrytimer = nil;
- if (config.serverid < 0) then
- local q = db:query("SELECT sid FROM %s_servers WHERE ip = '" .. serverip .. "' AND port = '" .. serverport .. "' LIMIT 1")
- q.onSuccess = function(q, data) config.sererid = data.sid; end;
- q:start();
- end
- if (admins) then return; end
- adminload();
- end
- local function failsafe()
- if (db) then
- db:connect();
- end
- end
- function dbfail(db, err)
- notifyerror("The database has failed to connect, error: ", err, ". Retrying in 60 seconds.");
- db.retrytimer = true;
- timer.Simple(60, failsafe);
- end
- local adminquery,qdata;
- 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";
- function adminload()
- print("admin querying goes here");
- admins = {};
- -- local q = db:query("SELECT admin_id FROM "
- -- do return; end
- --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
- -- print(loadingquery);
- local q = db:query(loadingquery);
- q.onFailure = qfail;
- q.qtype = "Admin grabbing";
- q.sid = "no good reason";
- q.onData = qdata;
- -- q.onSuccess = function(q) print(q) PrintTable(q:getData()) end;
- q:start();
- adminquery = q;
- end
- function qdata(q, data)
- admins[data.authid] = data;
- notifymessage("Added admin ", data.user, " with level ", data.srv_group);
- end
- local doban;
- function SBBanPlayer(ply, time, reason, admin)
- doban(
- ply:IPAddress(),
- ply:SteamID(),
- ply:Name(),
- time,
- reason,
- admin and admins[admin:SteamID()].aid,
- admin and admin:IPAddress()
- );
- end
- function SBBanPlayerBySteamID(sid, time, name, reason, admin)
- doban("", sid, name, time, reason, admin and admins[admin:SteamID()].aid, admin and admin:IPAddress())
- end
- local banquery = "INSERT INTO " .. config.dbprefix .. "_bans (ip, authid, name, created, ends, length, reason, aid, adminIp, sid, country) VALUES('";
- local sepr = "','";
- function doban(ip, authid, name, length, reason, adminid, adminip)
- local created, ends = os.time();
- ends = created + length;
- if (not adminid) then
- adminid = "STEAM_ID_SERVER";
- adminip = serverip;
- end
- local qtxt = banquery .. ip ..sepr .. authid .. sepr .. name .. "'," .. created .. "," .. ends .. "," .. length .. ",'" .. reason .. sepr .. adminid .. sepr .. adminip .. "'," .. config.serverid .. ",' ')";
- print(qtxt);
- end
- concommand.Add("DumpAllTheTablesNShit",function()
- print("queries")
- PrintTable(queries)
- print("admins");
- if (admins) then
- PrintTable(admins)
- else
- print();
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement