diff --git a/lua/msync/client_gui/cl_admin_gui.lua b/lua/msync/client_gui/cl_admin_gui.lua index ef16f88..cfc47ff 100644 --- a/lua/msync/client_gui/cl_admin_gui.lua +++ b/lua/msync/client_gui/cl_admin_gui.lua @@ -7,6 +7,9 @@ MSync.AdminPanel = MSync.AdminPanel or {} Returns: panel ]] function MSync.AdminPanel.InitMySQL( sheet ) + + MSync.log(MSYNC_DBG_DEBUG, "Initializing MySQL settings panel") + local pnl = vgui.Create( "DPanel", sheet ) local mysqlip_text = vgui.Create( "DLabel", pnl ) @@ -75,32 +78,82 @@ function MSync.AdminPanel.InitMySQL( sheet ) title_info:SetSize(400, 15) title_info:SetText( "--Information--" ) - local info = vgui.Create( "DLabel", pnl ) - info:SetPos( 200, 30 ) - info:SetColor( Color( 0, 0, 0 ) ) - info:SetSize(400, 200) - info:SetText( [[ - Support: https://www.Aperture-Development.de - GitHub: https://github.com/Aperture-Development/MSync-2 - LICENCE: To know what you are allowed to do and what not, - read the LICENCE file in the root directory of the addon. - If there is no file, the Licence by-nc-sa 4.0 International applies. - - Developer: This Addon was created by Aperture Development. - Copyright 2018 - Aperture Development - ]] ) + local info = vgui.Create( "RichText", pnl ) + info:SetPos( 200, 45 ) + info:SetSize(350, 150) + info:InsertColorChange(10, 10, 10, 255) + info:AppendText("MSync2 - Now with Steak\n\nSupport: ") + info:InsertColorChange(72, 72, 155, 255) + info:InsertClickableTextStart("OpenWebsite") + info:AppendText("https://www.Aperture-Development.de") + info:InsertClickableTextEnd() + info:InsertColorChange(10, 10, 10, 255) + info:AppendText("\nGitHub: ") + info:InsertColorChange(72, 72, 155, 255) + info:InsertClickableTextStart("OpenGitHub") + info:AppendText("https://github.com/Aperture-Development/MSync-2") + info:InsertClickableTextEnd() + info:InsertColorChange(10, 10, 10, 255) + info:AppendText("\nLicence:\n") + info:InsertColorChange(80, 80, 80, 255) + info:AppendText("To know what you are allowed to do and what not, read the LICENCE file in the root directory of the addon. If there is no file, the licence by-nc-sa 4.0 International applies.\n\n") + info:InsertColorChange(10, 10, 10, 255) + info:AppendText("This addon was created by Aperture Development\n") + info:InsertColorChange(10, 10, 10, 255) + info:AppendText("Copyright 2018 - Aperture Development") + + info.Paint = function( pnl, w, h ) + draw.RoundedBox( 5, 0, 0, w, h, Color(215, 215, 215) ) + end + + info.ActionSignal = function( pnl, signalName, signalValue ) + if signalName == "TextClicked" then + if signalValue == "OpenWebsite" then + gui.OpenURL( "https://www.Aperture-Development.de" ) + elseif signalValue == "OpenGitHub" then + gui.OpenURL( "https://github.com/Aperture-Development/MSync-2" ) + end + end + end local dbstatus = vgui.Create( "DLabel", pnl ) dbstatus:SetPos( 200, 210 ) dbstatus:SetColor( Color( 0, 0, 0 ) ) dbstatus:SetSize(400, 15) - dbstatus:SetText( "DB Connection status: -Not Implemented-" ) + dbstatus:SetText( "Database Status: " ) + + local dbstatus_info = vgui.Create( "DLabel", pnl ) + dbstatus_info:SetPos( 300, 210 ) + dbstatus_info:SetColor( Color( 80, 80, 80 ) ) + dbstatus_info:SetSize(400, 15) + dbstatus_info:SetText( "Please wait..." ) + + local function getConnectionStatus() + dbstatus_info:SetColor( Color( 80, 80, 80 ) ) + dbstatus_info:SetText( "Please wait..." ) + timer.Simple(3, function() + MSync.net.getDBStatus() + timer.Create("msync.dbConnectionStatus", 3, 10, function() + if MSync.DBStatus == nil then return end + + if MSync.DBStatus then + dbstatus_info:SetColor( Color( 80, 255, 80 ) ) + dbstatus_info:SetText( "Connected" ) + else + dbstatus_info:SetColor( Color( 255, 80, 80 ) ) + dbstatus_info:SetText( "Not Connected" ) + end + timer.Remove("msync.dbConnectionStatus") + end) + end) + end local save_button = vgui.Create( "DButton", pnl ) save_button:SetText( "Save Settings" ) save_button:SetPos( 25, 290 ) save_button:SetSize( 130, 30 ) save_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "Saving settings") MSync.settings.mysql.host = mysqlip:GetValue() MSync.settings.mysql.port = mysqlport:GetValue() MSync.settings.mysql.database = mysqldb:GetValue() @@ -115,6 +168,7 @@ function MSync.AdminPanel.InitMySQL( sheet ) saveconnect_button:SetPos( 155, 290 ) saveconnect_button:SetSize( 130, 30 ) saveconnect_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "Saving settings and connecting to the database") MSync.settings.mysql.host = mysqlip:GetValue() MSync.settings.mysql.port = mysqlport:GetValue() MSync.settings.mysql.database = mysqldb:GetValue() @@ -123,6 +177,7 @@ function MSync.AdminPanel.InitMySQL( sheet ) MSync.settings.serverGroup = servergroup:GetValue() MSync.net.sendSettings(MSync.settings) MSync.net.connectDB() + getConnectionStatus() end local connect_button = vgui.Create( "DButton", pnl ) @@ -130,7 +185,9 @@ function MSync.AdminPanel.InitMySQL( sheet ) connect_button:SetPos( 285, 290 ) connect_button:SetSize( 130, 30 ) connect_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "Connecting to the database") MSync.net.connectDB() + getConnectionStatus() end local reset_button = vgui.Create( "DButton", pnl ) @@ -138,19 +195,52 @@ function MSync.AdminPanel.InitMySQL( sheet ) reset_button:SetPos( 415, 290 ) reset_button:SetSize( 130, 30 ) reset_button.DoClick = function() - mysqlip:SetText("127.0.0.1") - mysqlport:SetText("3306") - mysqldb:SetText("msync") - mysqluser:SetText("root") - mysqlpassword:SetText("****") - servergroup:SetText("allserver") - MSync.settings.mysql.host = mysqlip:GetValue() - MSync.settings.mysql.port = mysqlport:GetValue() - MSync.settings.mysql.database = mysqldb:GetValue() - MSync.settings.mysql.username = mysqluser:GetValue() - MSync.settings.mysql.password = "" - MSync.settings.serverGroup = servergroup:GetValue() - MSync.net.sendSettings(MSync.settings) + MSync.log(MSYNC_DBG_DEBUG, "Reset confirm request"); + + local resetConfirm_panel = vgui.Create( "DFrame" ) + resetConfirm_panel:SetSize( 350, 100 ) + resetConfirm_panel:SetTitle( "MSync Reset - Confirm" ) + resetConfirm_panel:Center() + resetConfirm_panel:MakePopup() + + local save_text = vgui.Create( "DLabel", resetConfirm_panel ) + save_text:SetPos( 15, 20 ) + save_text:SetColor( Color( 255, 255, 255 ) ) + save_text:SetText( "This action will reset all MySQL settings back to default, causing MSync to be unable to connect to the database when restarting the server. Are you sure you want to do that?" ) + save_text:SetSize(320, 50) + save_text:SetWrap( true ) + + local accept_button = vgui.Create( "DButton", resetConfirm_panel ) + accept_button:SetText( "Yes" ) + accept_button:SetPos( 15, 70 ) + accept_button:SetSize( 160, 20 ) + accept_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "Reset of MySQL configuration confirmed") + mysqlip:SetText("127.0.0.1") + mysqlport:SetText("3306") + mysqldb:SetText("msync") + mysqluser:SetText("root") + mysqlpassword:SetText("****") + servergroup:SetText("allserver") + MSync.settings.mysql.host = mysqlip:GetValue() + MSync.settings.mysql.port = mysqlport:GetValue() + MSync.settings.mysql.database = mysqldb:GetValue() + MSync.settings.mysql.username = mysqluser:GetValue() + MSync.settings.mysql.password = "" + MSync.settings.serverGroup = servergroup:GetValue() + MSync.net.sendSettings(MSync.settings) + + resetConfirm_panel:Close() + end + + local deny_button = vgui.Create( "DButton", resetConfirm_panel ) + deny_button:SetText( "No" ) + deny_button:SetPos( 175, 70 ) + deny_button:SetSize( 160, 20 ) + deny_button.DoClick = function() + MSync.log(MSYNC_DBG_INFO, "Reset of MySQL configuration cancelled"); + resetConfirm_panel:Close() + end end if not MSync.settings == nil then @@ -163,6 +253,8 @@ function MSync.AdminPanel.InitMySQL( sheet ) timer.Create("msync.t.checkForSettings", 0.5, 0, function() if not MSync.settings or not MSync.settings.mysql then return end; + MSync.log(MSYNC_DBG_DEBUG, "Got server settings, updating MySQL settings panel") + mysqlip:SetText(MSync.settings.mysql.host) mysqlport:SetText(MSync.settings.mysql.port) mysqldb:SetText(MSync.settings.mysql.database) @@ -172,6 +264,8 @@ function MSync.AdminPanel.InitMySQL( sheet ) end) end + getConnectionStatus() + return pnl end @@ -181,6 +275,9 @@ end Returns: panel ]] function MSync.AdminPanel.InitModules( sheet ) + + MSync.log(MSYNC_DBG_DEBUG, "Initializing modulestate panel") + local pnl = vgui.Create( "DPanel", sheet ) local ModuleList = vgui.Create( "DListView", pnl ) @@ -203,12 +300,17 @@ function MSync.AdminPanel.InitModules( sheet ) local cursor_x, cursor_y = ModuleList:CursorPos() local DMenu = vgui.Create("DMenu", ModuleList) DMenu:SetPos(cursor_x, cursor_y) - DMenu:AddOption(MSync.serverModules[ident].Description) + DMenu:AddOption(MSync.serverModules[ident].Description):SetDisabled(true) DMenu:AddSpacer() DMenu:AddOption("Enable") DMenu:AddOption("Disable") DMenu.OptionSelected = function(menu,optPnl,optStr) MSync.net.toggleModule(ident, optStr) + if optStr == "Enable" then + line:SetColumnText( 3, "true" ) + elseif optStr == "Disable" then + line:SetColumnText( 3, "false" ) + end end end return pnl @@ -220,6 +322,9 @@ end Returns: panel ]] function MSync.AdminPanel.InitModuleSettings( sheet ) + + MSync.log(MSYNC_DBG_DEBUG, "Initializing module settings panel") + local pnl = vgui.Create( "DColumnSheet", sheet ) local files, _ = file.Find("msync/client_gui/modules/*.lua", "LUA") @@ -231,6 +336,7 @@ function MSync.AdminPanel.InitModuleSettings( sheet ) MSync.modules[info.ModuleIdentifier]["init"]() MSync.modules[info.ModuleIdentifier]["net"]() pnl:AddSheet( info.Name, MSync.modules[info.ModuleIdentifier].adminPanel(pnl)) + MSync.log(MSYNC_DBG_DEBUG, "Added settings tab for module: ") end end @@ -243,6 +349,8 @@ end ]] function MSync.AdminPanel.InitPanel() + MSync.log(MSYNC_DBG_DEBUG, "Opening Admin GUI") + --if not LocalPlayer():query("msync.admingui") then return false end; MSync.net.getSettings() diff --git a/lua/msync/client_gui/cl_modules.lua b/lua/msync/client_gui/cl_modules.lua index 81ee7a4..9fda110 100644 --- a/lua/msync/client_gui/cl_modules.lua +++ b/lua/msync/client_gui/cl_modules.lua @@ -27,7 +27,7 @@ function MSync.initModules() v["net"]() v["ulx"]() v["hooks"]() - print("["..v["info"]["Name"].."] Module loaded") + MSync.log(MSYNC_DBG_INFO, "["..v["info"]["Name"].."] Module loaded") end end @@ -39,7 +39,6 @@ end Returns: nothing ]] function MSync.loadModule(path) - local initTransaction = MSync.DBServer:createTransaction() local info = include(path) MSync.modules[info.ModuleIdentifier].init() @@ -47,6 +46,41 @@ function MSync.loadModule(path) MSync.modules[info.ModuleIdentifier].ulx() MSync.modules[info.ModuleIdentifier].hooks() - print("["..MSync.modules[info.Name].."] Module loaded") + MSync.log(MSYNC_DBG_INFO, "["..MSync.modules[info.ModuleIdentifier]["info"]["Name"].."] Module loaded") end + +--[[ + Description: Enables a single already loaded clientside module + Arguments: Module path + Returns: nothing +]] +function MSync.enableModule( module ) + if MSync.modules[module] then + MSync.modules[module].init() + MSync.modules[module].net() + MSync.modules[module].ulx() + MSync.modules[module].hooks() + MSync.log(MSYNC_DBG_INFO, "["..MSync.modules[module]["info"]["Name"].."] Module loaded") + else + MSync.log(MSYNC_DBG_WARNING, "Cannot enable non-existant module \"" .. module .. "\"") + end +end + +--[[ + Description: Disabled a single already loaded clientside module + Arguments: Module path + Returns: nothing +]] +function MSync.disableModule( module ) + if MSync.modules[module] then + if MSync.modules[module].disable then + MSync.modules[module].disable() + MSync.log(MSYNC_DBG_INFO, "["..MSync.modules[module]["info"]["Name"].."] Module disabled") + else + MSync.log(MSYNC_DBG_WARNING, "Cannot disable outdated module \"" .. module .. "\"") + end + else + MSync.log(MSYNC_DBG_WARNING, "Cannot disable non-existant module \"" .. module .. "\"") + end +end diff --git a/lua/msync/client_gui/cl_net.lua b/lua/msync/client_gui/cl_net.lua index 74a92c3..7fdc1a0 100644 --- a/lua/msync/client_gui/cl_net.lua +++ b/lua/msync/client_gui/cl_net.lua @@ -6,6 +6,7 @@ MSync.net = MSync.net or {} Returns: nothing ]] function MSync.net.getSettings() + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.getSettings") net.Start("msync.getSettings") net.SendToServer() end @@ -15,6 +16,7 @@ end Returns: nothing ]] function MSync.net.getModules() + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.getModules") net.Start("msync.getModules") net.SendToServer() end @@ -24,6 +26,7 @@ end Returns: nothing ]] function MSync.net.toggleModule(ident, state) + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.toggleModule Param.: " .. ident .. " " .. state) net.Start("msync.toggleModule") net.WriteString(ident) net.WriteString(state) @@ -35,6 +38,7 @@ end Returns: nothing ]] function MSync.net.sendSettings(table) + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.sendSettings Param.: " .. tostring(table)) net.Start("msync.sendSettings") net.WriteTable(table) net.SendToServer() @@ -45,23 +49,36 @@ end Returns: nothing ]] function MSync.net.connectDB() + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.connectDB") net.Start("msync.connectDB") net.SendToServer() end +--[[ + Description: function to ask for database connection + Returns: nothing +]] +function MSync.net.getDBStatus() + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.connectionStatus") + net.Start("msync.connectionStatus") + net.SendToServer() +end + --[[ Description: Net Receiver - Gets called when the server sends a table to the client Returns: nothing ]] net.Receive( "msync.sendTable", function( len, pl ) + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.sendTable") local type = net.ReadString() local table = net.ReadTable() - if type == "settings" then MSync.settings = table; print("Got Settings!") - elseif type == "modules" then MSync.serverModules = table + if type == "settings" then MSync.settings = table; MSync.log(MSYNC_DBG_INFO, "Got settings from server") + elseif type == "modules" then MSync.serverModules = table; MSync.log(MSYNC_DBG_INFO, "Got modules from server") elseif type == "modulestate" then MSync.moduleState = table MSync.loadModules() + MSync.log(MSYNC_DBG_INFO, "Got module states from server") end end ) @@ -70,6 +87,7 @@ end ) Returns: nothing ]] net.Receive( "msync.sendMessage", function( len, pl ) + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.sendMessage") local state = net.ReadString() if state == "error" then @@ -86,6 +104,7 @@ end ) Returns: nothing ]] net.Receive( "msync.openAdminGUI", function( len, pl ) + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.openAdminGUI") MSync.AdminPanel.InitPanel() end ) @@ -95,4 +114,27 @@ end ) ]] net.Receive( "msync.dbStatus", function( len, pl ) MSync.DBStatus = net.ReadBool() + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.dbStatus Return: " .. tostring(MSync.DBStatus)) +end ) + +--[[ + Description: Net Receiver - Gets called when a module has been enabled + Returns: nothing +]] +net.Receive( "msync.enableModule", function( len, pl ) + local module = net.ReadString() + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.enableModule Return: " .. module) + MSync.enableModule( module ) + MSync.moduleState[module] = true +end ) + +--[[ + Description: Net Receiver - Gets called when a module has been disabled + Returns: nothing +]] +net.Receive( "msync.disableModule", function( len, pl ) + local module = net.ReadString() + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.disableModule Return: " .. module) + MSync.disableModule( module ) + MSync.moduleState[module] = nil end ) \ No newline at end of file diff --git a/lua/msync/client_gui/modules/cl_mbsync.lua b/lua/msync/client_gui/modules/cl_mbsync.lua index a9c1cfe..65033c0 100644 --- a/lua/msync/client_gui/modules/cl_mbsync.lua +++ b/lua/msync/client_gui/modules/cl_mbsync.lua @@ -5,7 +5,7 @@ MSync.modules = MSync.modules or {} * @package MySQL Ban Sync * @author Aperture Development * @license root_dir/LICENCE - * @version 1.1.1 + * @version 1.3.2 ]] --[[ @@ -15,7 +15,7 @@ local info = { Name = "MySQL Ban Sync", ModuleIdentifier = "MBSync", Description = "Synchronise bans across your servers", - Version = "1.1.1" + Version = "1.3.2" } --[[ @@ -29,10 +29,12 @@ MSync.modules[info.ModuleIdentifier].info = info Define additional functions that are later used ]] MSync.modules[info.ModuleIdentifier].init = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Initializing"); MSync.modules[info.ModuleIdentifier].banTable = {} MSync.modules[info.ModuleIdentifier].getTablePage = function(tbl, maxResults, page) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.getTablePage Param.: $tbl $maxResults $page",{["tbl"] = tbl,["maxResults"] = maxResults,["page"] = page})); local tempTbl = {} local i = 0 local startPos = 0 + (maxResults*page) @@ -49,6 +51,7 @@ MSync.modules[info.ModuleIdentifier].init = function() end MSync.modules[info.ModuleIdentifier].displayTable = function(panel, tbl, maxResults, page) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.displayTable Param.: $panel $tbl $maxResults $page",{["panel"] = panel,["tbl"] = tbl,["maxResults"] = maxResults,["page"] = page})); panel:Clear() local table = MSync.modules[info.ModuleIdentifier].getTablePage(tbl, maxResults, page) local length = 0 @@ -63,16 +66,19 @@ MSync.modules[info.ModuleIdentifier].init = function() end MSync.modules[info.ModuleIdentifier].getTablePages = function(tbl, resultsPerPage) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.getTablePages Param.: $tbl $resultsPerPage",{["tbl"] = tbl, ["resultsPerPage"] = resultsPerPage})); return math.Round(#tbl / resultsPerPage) end MSync.modules[info.ModuleIdentifier].explodeTable = function(tbl, part) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.explodeTable Param.: $tbl $part",{["tbl"] = tbl, ["part"] = part})); for k,v in pairs(part) do tbl[k] = v end end MSync.modules[info.ModuleIdentifier].sortTable = function(tbl, key, asc) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.sortTable Param.: $tbl $key $asc",{["tbl"] = tbl, ["key"] = key, ["asc"] = asc})); local sorting = true local tempTable = table.DeSanitise( tbl ) local keys = table.GetKeys( tempTable ) @@ -114,6 +120,7 @@ MSync.modules[info.ModuleIdentifier].init = function() end MSync.modules[info.ModuleIdentifier].searchTable = function(tbl, term) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.searchTable Param.: $tbl $term", {["tbl"] = tbl,["term"] = term})); local searchTerm = "" if type(term) == 'string' then @@ -140,6 +147,7 @@ MSync.modules[info.ModuleIdentifier].init = function() end if matches then + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Search: Found match " .. k); tempTbl[k] = v end end @@ -148,6 +156,8 @@ MSync.modules[info.ModuleIdentifier].init = function() end MSync.modules[info.ModuleIdentifier].banPanel = function( tbl ) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.banPanel Param.: $tbl",{["tbl"] = tbl})); + local panel = vgui.Create( "DFrame" ) panel:SetSize( 350, 500 ) panel:SetTitle( "MBSync - Ban User " ) @@ -233,6 +243,8 @@ MSync.modules[info.ModuleIdentifier].init = function() ban_table:AddColumn( "SteamID" ) ban_table:AddColumn( "SteamID64" ) ban_table.OnRowSelected = function( lst, index, pnl ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Ban Panel: Selected row " .. index); + steamid_textentry:SetText(pnl:GetColumnText( 2 )) end @@ -247,6 +259,8 @@ MSync.modules[info.ModuleIdentifier].init = function() ban_button:SetPos( 15, 425 ) ban_button:SetSize( 320, 30 ) ban_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Ban confirm request"); + local banConfirm_panel = vgui.Create( "DFrame" ) banConfirm_panel:SetSize( 350, 100 ) banConfirm_panel:SetTitle( "MBSync Ban - Confirm" ) @@ -265,6 +279,8 @@ MSync.modules[info.ModuleIdentifier].init = function() accept_button:SetPos( 15, 70 ) accept_button:SetSize( 160, 20 ) accept_button.DoClick = function() + MSync.log(MSYNC_DBG_INFO, "[MBSync] Ban confirmed, banning player and closing panel"); + RunConsoleCommand("msync.MBSync.banSteamID", steamid_textentry:GetValue(), length_textentry:GetValue(), allservers_dropdown:GetValue(), reason_textentry:GetValue()) panel:Close() banConfirm_panel:Close() @@ -275,6 +291,8 @@ MSync.modules[info.ModuleIdentifier].init = function() deny_button:SetPos( 175, 70 ) deny_button:SetSize( 160, 20 ) deny_button.DoClick = function() + MSync.log(MSYNC_DBG_INFO, "[MBSync] Ban denied, closing confirmation panel"); + banConfirm_panel:Close() end end @@ -284,6 +302,7 @@ MSync.modules[info.ModuleIdentifier].init = function() cancel_button:SetPos( 15, 460 ) cancel_button:SetSize( 320, 30 ) cancel_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Ban cancellation request"); local cancel_panel = vgui.Create( "DFrame" ) cancel_panel:SetSize( 350, 100 ) cancel_panel:SetTitle( "MBSync Ban - Confirm" ) @@ -302,6 +321,7 @@ MSync.modules[info.ModuleIdentifier].init = function() accept_button:SetPos( 15, 70 ) accept_button:SetSize( 160, 20 ) accept_button.DoClick = function() + MSync.log(MSYNC_DBG_INFO, "[MBSync] Ban canceled, closing panels"); -- do nothing and close panel panel:Close() cancel_panel:Close() @@ -312,6 +332,7 @@ MSync.modules[info.ModuleIdentifier].init = function() deny_button:SetPos( 175, 70 ) deny_button:SetSize( 160, 20 ) deny_button.DoClick = function() + MSync.log(MSYNC_DBG_INFO, "[MBSync] Ban cancel aborted, returning to ban panel"); cancel_panel:Close() end end @@ -319,6 +340,7 @@ MSync.modules[info.ModuleIdentifier].init = function() end MSync.modules[info.ModuleIdentifier].advancedInfoPanel = function(tbl) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.advancedInfoPanel Param.: $tbl",{["tbl"] = tbl})); local panel = vgui.Create( "DFrame" ) panel:SetSize( 350, 455 ) @@ -484,6 +506,7 @@ MSync.modules[info.ModuleIdentifier].init = function() close_button:SetPos( 15, 410 ) close_button:SetSize( 320, 30 ) close_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Close info panel"); panel:Close() end @@ -494,6 +517,8 @@ MSync.modules[info.ModuleIdentifier].init = function() ]] if not (tbl == nil) then + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Filling in ban data"); + nickname_textentry:SetText( tbl["nickname"] ) steamid_textentry:SetText( tbl["steamid"] ) steamid64_textentry:SetText( tbl["steamid64"] ) @@ -515,6 +540,8 @@ MSync.modules[info.ModuleIdentifier].init = function() end MSync.modules[info.ModuleIdentifier].editBanPanel = function(tbl) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.editBanPanel Param.: $tbl",{["tbl"] = tbl})); + local panel = vgui.Create( "DFrame" ) panel:SetSize( 350, 280 ) panel:SetTitle( "MBSync Edit Ban" ) @@ -591,11 +618,7 @@ MSync.modules[info.ModuleIdentifier].init = function() banallservers_textentry:AddChoice( "false" ) banallservers_textentry:SetSortItems( false ) banallservers_textentry.OnSelect = function( self, index, value ) - if value == "true" then - print("True") - elseif value == "false" then - print("False") - end + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Ban on all servers: " .. value); end local banlreason_text = vgui.Create( "DLabel", panel ) @@ -619,6 +642,7 @@ MSync.modules[info.ModuleIdentifier].init = function() save_button:SetPos( 15, 200 ) save_button:SetSize( 320, 30 ) save_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Ban edit confirmation request"); local save_panel = vgui.Create( "DFrame" ) save_panel:SetSize( 350, 100 ) save_panel:SetTitle( "MBSync Edit Ban - Edit" ) @@ -637,6 +661,7 @@ MSync.modules[info.ModuleIdentifier].init = function() accept_button:SetPos( 15, 70 ) accept_button:SetSize( 160, 20 ) accept_button.DoClick = function() + MSync.log(MSYNC_DBG_INFO, "[MBSync] Edited data accepted, editing ban and closing panel"); RunConsoleCommand( "msync.MBSync.editBan", tbl["banId"], banlength_textentry:GetValue(), banallservers_textentry:GetValue(), banreason_textentry:GetValue()) panel:Close() save_panel:Close() @@ -647,6 +672,7 @@ MSync.modules[info.ModuleIdentifier].init = function() deny_button:SetPos( 175, 70 ) deny_button:SetSize( 160, 20 ) deny_button.DoClick = function() + MSync.log(MSYNC_DBG_INFO, "[MBSync] Edited data denied, closing panel"); -- CLOSE ACCEPT PANEL save_panel:Close() end @@ -657,6 +683,7 @@ MSync.modules[info.ModuleIdentifier].init = function() cancel_button:SetPos( 15, 235 ) cancel_button:SetSize( 320, 30 ) cancel_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Ban edit cancellation request"); local cancel_panel = vgui.Create( "DFrame" ) cancel_panel:SetSize( 350, 100 ) cancel_panel:SetTitle( "MBSync Edit Ban - Cancel" ) @@ -675,6 +702,7 @@ MSync.modules[info.ModuleIdentifier].init = function() accept_button:SetPos( 15, 70 ) accept_button:SetSize( 160, 20 ) accept_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Ban edit cancelled"); panel:Close() cancel_panel:Close() end @@ -684,6 +712,7 @@ MSync.modules[info.ModuleIdentifier].init = function() deny_button:SetPos( 175, 70 ) deny_button:SetSize( 160, 20 ) deny_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Ban edit cancellation request denied"); cancel_panel:Close() end end @@ -695,6 +724,7 @@ MSync.modules[info.ModuleIdentifier].init = function() ]] if not (tbl == nil) then + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Filling in ban data"); nickname_textentry:SetText( tbl["nickname"] ) steamid_textentry:SetText( tbl["steamid"] ) steamid64_textentry:SetText( tbl["steamid64"] ) @@ -793,6 +823,7 @@ MSync.modules[info.ModuleIdentifier].adminPanel = function(sheet) ban_table:SortByColumn( sortby.Column, sortby.Descending ) function displayTable(tbl) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Displaying data to table"); ban_table:Clear() for k,v in pairs(tbl) do local length = "" @@ -813,20 +844,24 @@ MSync.modules[info.ModuleIdentifier].adminPanel = function(sheet) end save_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Save settings requested"); local num = tonumber(delay_textentry:GetValue()) if num then if num < 30 then + MSync.log(MSYNC_DBG_ERROR, "[MBSync] You should not select a value below \"30\" seconds. We recommend to have it at \"300\" seconds"); chat.AddText(Color(255, 60, 60),"[MBSync_ERROR] ",Color(255,170,0),"You should not select a value below ",Color(60, 255, 60),"30",Color(255,170,0)," seconds. We recommend to have it at ",Color(60, 255, 60),"300",Color(255,170,0)," seconds") else MSync.modules[info.ModuleIdentifier].settings["syncDelay"] = num MSync.modules[info.ModuleIdentifier].sendSettings() end else + MSync.log(MSYNC_DBG_ERROR, "[MBSync] The value you entered is invalid"); chat.AddText(Color(255, 60, 60),"[MBSync_ERROR] ",Color(255,170,0),"That is not a valid value!") end end search_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Searching table"); if search_textentry:GetValue() then tempTable = {} tempTable = MSync.modules[info.ModuleIdentifier].searchTable(MSync.modules[info.ModuleIdentifier].banTable, search_textentry:GetValue()) @@ -840,6 +875,7 @@ MSync.modules[info.ModuleIdentifier].adminPanel = function(sheet) end reload_button.DoClick = function() + MSync.log(MSYNC_DBG_INFO, "[MBSync] Reloading data"); MSync.modules[info.ModuleIdentifier].getBanTable(true) timer.Create("msync.mbsync.waitForBanTable", 1, 0, function() @@ -868,9 +904,10 @@ MSync.modules[info.ModuleIdentifier].adminPanel = function(sheet) end ban_table.OnRowRightClick = function(panel, lineID, line) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Row " .. lineID .. " right clicked"); + local ident = line:GetValue(1) local cursor_x, cursor_y = panel:CursorPos() - print(cursor_x) local DMenu = vgui.Create("DMenu", panel) if cursor_y > 170 then @@ -886,6 +923,7 @@ MSync.modules[info.ModuleIdentifier].adminPanel = function(sheet) DMenu:AddOption("Edit") DMenu:AddOption("Advanced Info") DMenu.OptionSelected = function(menu,optPnl,optStr) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Option \"" .. optStr .. "\" selected"); if optStr == "Unban" then MSync.modules[info.ModuleIdentifier].unban(line:GetColumnText( 1 )) elseif optStr == "Edit" then @@ -897,6 +935,7 @@ MSync.modules[info.ModuleIdentifier].adminPanel = function(sheet) end sortby_dropdown.OnSelect = function( self, index, value ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Sortby dropdown selected"); if value == "Ban ID" then sortby.Column = 1 if sortby.Descending then @@ -961,6 +1000,8 @@ MSync.modules[info.ModuleIdentifier].adminPanel = function(sheet) timer.Create("msync.mbsync.waitForSettings", 1, 0, function() if not MSync.modules[info.ModuleIdentifier].settings then return end + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Got settings from server, filling in data"); + delay_textentry:SetText(MSync.modules[info.ModuleIdentifier].settings["syncDelay"]) timer.Remove("msync.mbsync.waitForSettings") @@ -973,6 +1014,7 @@ end Define the client panel for client usage ( or as example: use it as additional admin gui which does not need msync.admingui permission) ]] MSync.modules[info.ModuleIdentifier].clientPanel = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.clientPanel"); --[[ Get Ban table and then wait for the reply before showing it @@ -1078,6 +1120,7 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() } local function checkPage() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: checkPage"); if ( (tablePage+1) >= pages ) then lastpage_button:SetDisabled(true) nextpage_button:SetDisabled(true) @@ -1096,6 +1139,7 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() end ban_table.OnRowRightClick = function(panel, lineID, line) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Row \"" .. lineID .. "\" right clicked"); local ident = line:GetValue(1) local cursor_x, cursor_y = panel:CursorPos() local DMenu = vgui.Create("DMenu", panel) @@ -1104,6 +1148,7 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() DMenu:AddOption("Edit") DMenu:AddOption("Advanced Info") DMenu.OptionSelected = function(menu,optPnl,optStr) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Option \"" .. optStr .. "\" selected"); if optStr == "Unban" then MSync.modules[info.ModuleIdentifier].unban(line:GetColumnText( 1 )) elseif optStr == "Edit" then @@ -1115,6 +1160,7 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() end sortby_dropdown.OnSelect = function( self, index, value ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Sortby: Selected \"" .. value .. "\""); --ban_table if value == "Ban ID" then sortby.Column = "banId" @@ -1144,6 +1190,7 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() end listascdesc_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Changing if list should be ascending or descending"); if sortby.Descending then sortby.Descending = false listascdesc_button:SetText( "List: Desc" ) @@ -1157,6 +1204,7 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() end search_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Searching table"); if search_textentry:GetValue() then tempTable = {} tempTable = MSync.modules[info.ModuleIdentifier].searchTable(MSync.modules[info.ModuleIdentifier].banTable, search_textentry:GetValue()) @@ -1171,10 +1219,12 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() end sync_button.DoClick = function() + MSync.log(MSYNC_DBG_INFO, "[MBSync] Reloading ban table"); MSync.modules[info.ModuleIdentifier].getBanTable() timer.Create("msync.mbsync.waitForBanTable", 3, 0, function() - if MSync.modules[info.ModuleIdentifier].temporary["unfinished"] then return end + if MSync.modules[info.ModuleIdentifier].temporary["unfinished"] then MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Still waiting for some data from server"); return end + MSync.log(MSYNC_DBG_INFO, "[MBSync] Got all ban data from server! Reloading table now"); tempTable = MSync.modules[info.ModuleIdentifier].sortTable(MSync.modules[info.ModuleIdentifier].banTable, sortby.Column, sortby.Descending) pages = MSync.modules[info.ModuleIdentifier].getTablePages(tempTable, 20) tablePage = 0 @@ -1192,6 +1242,8 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() end firstpage_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Go to first page"); + tablePage = 0 pageof_text:SetText( (tablePage+1).."/"..pages ) MSync.modules[info.ModuleIdentifier].displayTable(ban_table, tempTable, 20, tablePage) @@ -1199,6 +1251,8 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() end previouspage_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Go to previous page"); + tablePage = tablePage - 1 pageof_text:SetText( (tablePage+1).."/"..pages ) MSync.modules[info.ModuleIdentifier].displayTable(ban_table, tempTable, 20, tablePage) @@ -1206,6 +1260,8 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() end nextpage_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Go to next page"); + tablePage = tablePage + 1 pageof_text:SetText( (tablePage+1).."/"..pages ) MSync.modules[info.ModuleIdentifier].displayTable(ban_table, tempTable, 20, tablePage) @@ -1213,6 +1269,8 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() end lastpage_button.DoClick = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Go to last page"); + tablePage = pages-1 pageof_text:SetText( (tablePage+1).."/"..pages ) MSync.modules[info.ModuleIdentifier].displayTable(ban_table, tempTable, 20, tablePage) @@ -1225,7 +1283,9 @@ MSync.modules[info.ModuleIdentifier].clientPanel = function() end timer.Create("msync.mbsync.waitForBanTable", 1, 0, function() - if MSync.modules[info.ModuleIdentifier].temporary["unfinished"] then return end + if MSync.modules[info.ModuleIdentifier].temporary["unfinished"] then MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Still waiting for some data from server");return end + + MSync.log(MSYNC_DBG_INFO, "[MBSync] Got all ban data from server! Reloading table now"); tempTable = MSync.modules[info.ModuleIdentifier].sortTable(MSync.modules[info.ModuleIdentifier].banTable, sortby.Column, sortby.Descending) pages = MSync.modules[info.ModuleIdentifier].getTablePages(tempTable, 20) @@ -1268,6 +1328,8 @@ MSync.modules[info.ModuleIdentifier].net = function() Returns: nothing ]] MSync.modules[info.ModuleIdentifier].unban = function(userid) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.unban Param.:" .. userid); + if not type(userid) == "number" then userid = tonumber(userid) end @@ -1281,6 +1343,8 @@ MSync.modules[info.ModuleIdentifier].net = function() Returns: nothing ]] net.Receive( "msync."..info.ModuleIdentifier..".sendMessage", function( len, ply ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.sendMessage"); + local type = net.ReadFloat() if type == 0 then chat.AddText( Color( 237, 135, 26 ), "[MBSync] ", Color( 255, 255, 255), net.ReadString()) @@ -1292,6 +1356,8 @@ MSync.modules[info.ModuleIdentifier].net = function() Returns: nothing ]] net.Receive( "msync."..info.ModuleIdentifier..".openBanGUI", function( len, ply ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.openBanGUI"); + MSync.modules[info.ModuleIdentifier].banPanel(net.ReadTable()) end ) @@ -1300,6 +1366,8 @@ MSync.modules[info.ModuleIdentifier].net = function() Returns: nothing ]] net.Receive( "msync."..info.ModuleIdentifier..".openBanTable", function( len, ply ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.openBanTable"); + MSync.modules[info.ModuleIdentifier].clientPanel() end ) @@ -1310,6 +1378,8 @@ MSync.modules[info.ModuleIdentifier].net = function() Returns: nothing ]] MSync.modules[info.ModuleIdentifier].getBanTable = function(fulltable) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.getBanTable Param.: " .. tostring(fulltable)); + MSync.modules[info.ModuleIdentifier].temporary = {} MSync.modules[info.ModuleIdentifier].banTable = {} @@ -1323,8 +1393,10 @@ MSync.modules[info.ModuleIdentifier].net = function() Returns: nothing ]] net.Receive( "msync."..info.ModuleIdentifier..".recieveDataCount", function( len, ply ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.recieveDataCount"); + local num = net.ReadFloat() - if not MSync.modules[info.ModuleIdentifier].temporary["unfinished"] then + if num > 0 and not MSync.modules[info.ModuleIdentifier].temporary["unfinished"] then MSync.modules[info.ModuleIdentifier].temporary["count"] = num MSync.modules[info.ModuleIdentifier].temporary["recieved"] = 0 MSync.modules[info.ModuleIdentifier].temporary["unfinished"] = true @@ -1337,6 +1409,8 @@ MSync.modules[info.ModuleIdentifier].net = function() Returns: nothing ]] net.Receive( "msync."..info.ModuleIdentifier..".recieveData", function( len, ply ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] msync.MBSync.recieveData"); + MSync.modules[info.ModuleIdentifier].explodeTable(MSync.modules[info.ModuleIdentifier].banTable, net.ReadTable()) MSync.modules[info.ModuleIdentifier].temporary["recieved"] = MSync.modules[info.ModuleIdentifier].temporary["recieved"] + 1 @@ -1371,6 +1445,8 @@ MSync.modules[info.ModuleIdentifier].net = function() Returns: nothing ]] net.Receive( "msync."..info.ModuleIdentifier..".sendSettingsPly", function( len, ply ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.sendSettingsPly"); + MSync.modules[info.ModuleIdentifier].settings = net.ReadTable() end ) @@ -1379,6 +1455,8 @@ MSync.modules[info.ModuleIdentifier].net = function() Returns: nothing ]] MSync.modules[info.ModuleIdentifier].getSettings = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.getSettings"); + net.Start("msync."..info.ModuleIdentifier..".getSettings") net.SendToServer() end @@ -1388,6 +1466,8 @@ MSync.modules[info.ModuleIdentifier].net = function() Returns: nothing ]] MSync.modules[info.ModuleIdentifier].sendSettings = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.sendSettings"); + net.Start("msync."..info.ModuleIdentifier..".sendSettings") net.WriteTable(MSync.modules[info.ModuleIdentifier].settings) net.SendToServer() @@ -1408,6 +1488,13 @@ MSync.modules[info.ModuleIdentifier].hooks = function() -- end +--[[ + Define a function to run on the clients when the module gets disabled +]] +MSync.modules[info.ModuleIdentifier].disable = function() + +end + --[[ Return info ( Just for single module loading ) ]] diff --git a/lua/msync/client_gui/modules/cl_mrsync.lua b/lua/msync/client_gui/modules/cl_mrsync.lua index b74ecde..f0be49d 100644 --- a/lua/msync/client_gui/modules/cl_mrsync.lua +++ b/lua/msync/client_gui/modules/cl_mrsync.lua @@ -6,7 +6,7 @@ MSync.modules.MRSync = MSync.modules.MRSync or {} * @package MySQL Rank Sync * @author Aperture Development * @license root_dir/LICENCE - * @version 2.2.0 + * @version 2.2.3 ]] --[[ @@ -16,7 +16,7 @@ MSync.modules.MRSync.info = { Name = "MySQL Rank Sync", ModuleIdentifier = "MRSync", Description = "Synchronise your ranks across your servers", - Version = "2.2.0" + Version = "2.2.3" } --[[ @@ -30,6 +30,7 @@ end Define the admin panel for the settings ]] function MSync.modules.MRSync.adminPanel(sheet) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Initializing AdminGUI panel"); local pnl = vgui.Create( "DPanel", sheet ) pnl:Dock(FILL) @@ -50,6 +51,7 @@ function MSync.modules.MRSync.adminPanel(sheet) allserver_table:SetMultiSelect( false ) allserver_table:AddColumn( "Allserver Ranks" ) allserver_table.OnRowRightClick = function(panel, lineID, line) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Right clicked " .. line:GetValue(1)); local ident = line:GetValue(1) local cursor_x, cursor_y = panel:CursorPos() local DMenu = vgui.Create("DMenu", panel) @@ -58,6 +60,7 @@ function MSync.modules.MRSync.adminPanel(sheet) DMenu.OptionSelected = function(menu,optPnl,optStr) if MSync.modules.MRSync.settings.syncall[line:GetValue(1)] then + MSync.log(MSYNC_DBG_INFO, "[MRSync] Removing \"" .. line:GetValue(1) .. "\" from allservers ranks"); allserver_table:RemoveLine(lineID) MSync.modules.MRSync.settings.syncall[line:GetValue(1)] = nil MSync.modules.MRSync.sendSettings() @@ -72,7 +75,9 @@ function MSync.modules.MRSync.adminPanel(sheet) allserver_button:SetSize( 130, 20 ) allserver_button.DoClick = function() if string.len(allserver_textentry:GetValue()) > 0 and not MSync.modules.MRSync.settings.nosync[allserver_textentry:GetValue()] and not MSync.modules.MRSync.settings.syncall[allserver_textentry:GetValue()] then - if string.match(allserver_textentry:GetValue(), "^%s*$") or string.match(allserver_textentry:GetValue(), "^%s") or string.match(allserver_textentry:GetValue(), "%s$") then return end + if string.match(allserver_textentry:GetValue(), "^%s*$") or string.match(allserver_textentry:GetValue(), "^%s") or string.match(allserver_textentry:GetValue(), "%s$") then MSync.log(MSYNC_DBG_WARNING, "[MRSync] String contains one or more whitespaces at the end or the beginning, not adding to list"); return end + + MSync.log(MSYNC_DBG_INFO, "[MRSync] Adding \"" .. allserver_textentry:GetValue() .. "\" to allservers rank list"); allserver_table:AddLine(allserver_textentry:GetValue()) MSync.modules.MRSync.settings.syncall[allserver_textentry:GetValue()] = true allserver_textentry:SetText("") @@ -97,6 +102,7 @@ function MSync.modules.MRSync.adminPanel(sheet) nosync_table:SetMultiSelect( false ) nosync_table:AddColumn( "Nosync Ranks" ) nosync_table.OnRowRightClick = function(panel, lineID, line) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Right clicked " .. line:GetValue(1)); local ident = line:GetValue(1) local cursor_x, cursor_y = panel:CursorPos() local DMenu = vgui.Create("DMenu", panel) @@ -105,6 +111,7 @@ function MSync.modules.MRSync.adminPanel(sheet) DMenu.OptionSelected = function(menu,optPnl,optStr) if MSync.modules.MRSync.settings.nosync[line:GetValue(1)] then + MSync.log(MSYNC_DBG_INFO, "[MRSync] Removing \"" .. line:GetValue(1) .. "\" from nosync ranks"); nosync_table:RemoveLine(lineID) MSync.modules.MRSync.settings.nosync[line:GetValue(1)] = nil MSync.modules.MRSync.sendSettings() @@ -119,7 +126,9 @@ function MSync.modules.MRSync.adminPanel(sheet) nosync_button:SetSize( 130, 20 ) nosync_button.DoClick = function() if string.len(nosync_textentry:GetValue()) > 0 and not MSync.modules.MRSync.settings.nosync[nosync_textentry:GetValue()] and not MSync.modules.MRSync.settings.syncall[nosync_textentry:GetValue()] then - if string.match(nosync_textentry:GetValue(), "^%s*$") or string.match(nosync_textentry:GetValue(), "^%s") or string.match(nosync_textentry:GetValue(), "%s$") then return end + if string.match(nosync_textentry:GetValue(), "^%s*$") or string.match(nosync_textentry:GetValue(), "^%s") or string.match(nosync_textentry:GetValue(), "%s$") then MSync.log(MSYNC_DBG_WARNING, "[MRSync] String contains one or more whitespaces at the end or the beginning, not adding to list"); return end + + MSync.log(MSYNC_DBG_INFO, "[MRSync] Adding \"" .. allserver_textentry:GetValue() .. "\" to nosync rank list"); nosync_table:AddLine(nosync_textentry:GetValue()) MSync.modules.MRSync.settings.nosync[nosync_textentry:GetValue()] = true nosync_textentry:SetText("") @@ -135,6 +144,7 @@ function MSync.modules.MRSync.adminPanel(sheet) timer.Create("mrsync.t.checkSettings", 1, 0, function() if not MSync.modules.MRSync.settings then return end + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Got settings from server, adding to panel"); for k,_ in pairs(MSync.modules.MRSync.settings.syncall) do allserver_table:AddLine(k) end @@ -146,6 +156,8 @@ function MSync.modules.MRSync.adminPanel(sheet) timer.Remove("mrsync.t.checkSettings") end) else + + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Settings found, adding to panel"); for k,_ in pairs(MSync.modules.MRSync.settings.syncall) do allserver_table:AddLine(k) end @@ -179,6 +191,7 @@ function MSync.modules.MRSync.net() Returns: nothing ]] function MSync.modules.MRSync.sendSettings() + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Exec: MRSync.sendSettings"); net.Start("msync.mrsync.sendSettings") net.WriteTable(MSync.modules.MRSync.settings) net.SendToServer() @@ -191,6 +204,7 @@ function MSync.modules.MRSync.net() Returns: nothing ]] function MSync.modules.MRSync.getSettings() + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Exec: MRSync.getSettings"); net.Start("msync.mrsync.getSettings") net.SendToServer() end @@ -200,6 +214,7 @@ function MSync.modules.MRSync.net() Returns: nothing ]] net.Receive("msync.mrsync.sendSettingsPly", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Net: msync.mrsync.sendSettingsPly"); MSync.modules.MRSync.settings = net.ReadTable() end ) end @@ -218,6 +233,13 @@ function MSync.modules.MRSync.hooks() end +--[[ + Define a function to run on the clients when the module gets disabled +]] +MSync.modules.MRSync.disable = function() + +end + --[[ Return info ( Just for single module loading ) ]] diff --git a/lua/msync/client_gui/modules/cl_samplemodule.lua b/lua/msync/client_gui/modules/cl_samplemodule.lua index e9ac24d..f273a23 100644 --- a/lua/msync/client_gui/modules/cl_samplemodule.lua +++ b/lua/msync/client_gui/modules/cl_samplemodule.lua @@ -6,23 +6,29 @@ MSync.modules.SampleModule = MSync.modules.SampleModule or {} * @package Sample Module * @author Aperture Development * @license root_dir/LICENCE - * @version 1.0.0 + * @version 1.0.1 ]] --[[ Define name, description and module identifier ]] -MSync.modules.SampleModule.info = { +local info = { Name = "Sample Module", ModuleIdentifier = "SampleModule", Description = "A basic example module on how to create modules", - Version = "0.0.1" + Version = "1.0.1" } +--[[ + Prepare Module +]] +MSync.modules[info.ModuleIdentifier] = MSync.modules[info.ModuleIdentifier] or {} +MSync.modules[info.ModuleIdentifier].info = info + --[[ Define additional functions that are later used ]] -function MSync.modules.SampleModule.init() +MSync.modules[info.ModuleIdentifier].init = function() function MSync.modules.SampleModule.SampleFunction() return true @@ -33,7 +39,7 @@ end --[[ Define the admin panel for the settings ]] -function MSync.modules.SampleModule.adminPanel(sheet) +MSync.modules[info.ModuleIdentifier].adminPanel = function(sheet) local pnl = vgui.Create( "DPanel", sheet ) pnl:Dock(FILL) return pnl @@ -42,7 +48,7 @@ end --[[ Define the client panel for client usage ( or as example: use it as additional admin gui which does not need msync.admingui permission) ]] -function MSync.modules.SampleModule.clientPanel() +MSync.modules[info.ModuleIdentifier].clientPanel = function() local pnl = vgui.Create( "DPanel" ) return pnl @@ -51,7 +57,7 @@ end --[[ Define net receivers and util.AddNetworkString ]] -function MSync.modules.SampleModule.net() +MSync.modules[info.ModuleIdentifier].net = function() net.Receive( "my_message", function( len, pl ) if ( IsValid( pl ) and pl:IsPlayer() ) then print( "Message from " .. pl:Nick() .. " received. Its length is " .. len .. "." ) @@ -64,20 +70,27 @@ end --[[ Define ulx Commands and overwrite common ulx functions (module does not get loaded until ulx has fully been loaded) ]] -function MSync.modules.SampleModule.ulx() +MSync.modules[info.ModuleIdentifier].ulx = function() end --[[ Define hooks your module is listening on e.g. PlayerDisconnect ]] -function MSync.modules.SampleModule.hooks() +MSync.modules[info.ModuleIdentifier].hooks = function() hook.Add("initialize", "msync_sampleModule_init", function() end) end +--[[ + Define a function to run on the clients when the module gets disabled +]] +MSync.modules[info.ModuleIdentifier].disable = function() + +end + --[[ Return info ( Just for single module loading ) ]] -return MSync.modules.SampleModule.info \ No newline at end of file +return MSync.modules[info.ModuleIdentifier].info \ No newline at end of file diff --git a/lua/msync/server/modules/sv_mbsync.lua b/lua/msync/server/modules/sv_mbsync.lua index 56b80b8..fc96ba1 100644 --- a/lua/msync/server/modules/sv_mbsync.lua +++ b/lua/msync/server/modules/sv_mbsync.lua @@ -5,7 +5,7 @@ MSync.modules = MSync.modules or {} * @package MySQL Ban Sync * @author Aperture Development * @license root_dir/LICENSE - * @version 1.1.1 + * @version 1.3.2 ]] --[[ @@ -15,7 +15,7 @@ local info = { Name = "MySQL Ban Sync", ModuleIdentifier = "MBSync", Description = "Synchronise bans across your servers", - Version = "1.1.1" + Version = "1.3.2" } --[[ @@ -24,11 +24,13 @@ local info = { MSync.modules[info.ModuleIdentifier] = MSync.modules[info.ModuleIdentifier] or {} MSync.modules[info.ModuleIdentifier].info = info MSync.modules[info.ModuleIdentifier].recentDisconnects = MSync.modules[info.ModuleIdentifier].recentDisconnects or {} +local userTransactions = userTransactions or {} --[[ Define mysql table and additional functions that are later used ]] MSync.modules[info.ModuleIdentifier].init = function( transaction ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.init") transaction:addQuery( MSync.DBServer:query([[ CREATE TABLE IF NOT EXISTS `tbl_mbsync` ( `p_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, @@ -49,6 +51,8 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Description: Function to update the database to the newest version, in case it isn't up to date ]] MSync.modules[info.ModuleIdentifier].updateDB = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.updateDB") + local selectDbVersion = MSync.DBServer:prepare( [[ SELECT version FROM `tbl_msyncdb_version` WHERE module_id=?; ]] ) @@ -56,8 +60,10 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) selectDbVersion.onSuccess = function( q, data ) -- Do nothing for now + MSync.log(MSYNC_DBG_INFO, "[MBSync] Checking database version") if data[1] then if data[1].version < 1 then + MSync.log(MSYNC_DBG_INFO, "[MBSync] Database version of MBSync is too old, updating it to Version 1") local updates = MSync.DBServer:createTransaction() updates:addQuery( MSync.DBServer:query([[ ALTER TABLE tbl_mbsync @@ -69,8 +75,11 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) ON DUPLICATE KEY UPDATE version=VALUES(version); ]])) updates:start() + else + MSync.log(MSYNC_DBG_INFO, "[MBSync] Database version is up-to-date. No update necessary") end else + MSync.log(MSYNC_DBG_INFO, "[MBSync] No data was returned, updating MBSync table to current version") local updates = MSync.DBServer:createTransaction() updates:addQuery( MSync.DBServer:query([[ ALTER TABLE tbl_mbsync @@ -87,15 +96,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) end selectDbVersion.onError = function( q, err, sql ) - print("------------------------------------") - print("[MBSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MBSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end selectDbVersion:start() @@ -108,7 +109,17 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Returns: nothing ]] MSync.modules[info.ModuleIdentifier].banUser = function(ply, calling_ply, length, reason, allserver) - if MSync.modules[info.ModuleIdentifier].banTable[ply:SteamID64()] then MSync.modules[info.ModuleIdentifier].msg(calling_ply, "User "..ply:Nick().." is already banned from this server."); return end + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.banUser Param.: $ply $calling_ply $length \"$reason\" $allserver", {["ply"] = ply:Nick(),["calling_ply"] = tostring(calling_ply),["length"] = length,["reason"] = reason,["allserver"] = allserver})) + + if MSync.modules[info.ModuleIdentifier].banTable[ply:SteamID64()] then + MSync.log(MSYNC_DBG_INFO, "[MBSync] User \"" .. ply:SteamID64() .. "\" is already banned, editing old ban instead") + if not length == 0 then + length = ((os.time() - MSync.modules[info.ModuleIdentifier].banTable[util.SteamIDTo64(userid)].timestamp)+(length*60))/60 + end + MSync.modules[info.ModuleIdentifier].editBan( MSync.modules[info.ModuleIdentifier].banTable[ply:SteamID64()]['banId'], reason, length, calling_ply, allserver) + return + end + local banUserQ = MSync.DBServer:prepare( [[ INSERT INTO `tbl_mbsync` (user_id, admin_id, reason, date_unix, length_unix, server_group) VALUES ( @@ -154,7 +165,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) banData["unban"] = length msgLength = "Permanent" else - msgLength = ULib.secondsToStringTime(length) + msgLength = ULib.secondsToStringTime(length*60) end if reason == "" then @@ -167,15 +178,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) end banUserQ.onError = function( q, err, sql ) - print("------------------------------------") - print("[MBSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MBSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end banUserQ:start() @@ -186,7 +189,17 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Returns: nothing ]] MSync.modules[info.ModuleIdentifier].banUserID = function(userid, calling_ply, length, reason, allserver) - if MSync.modules[info.ModuleIdentifier].banTable[util.SteamIDTo64(userid)] then MSync.modules[info.ModuleIdentifier].msg(calling_ply, "User "..userid.." is already banned from this server."); return end + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.banUserID Param.: $userid $calling_ply $length \"$reason\" $allserver", {["userid"] = userid,["calling_ply"] = tostring(calling_ply),["length"] = length,["reason"] = reason,["allserver"] = allserver})) + + if MSync.modules[info.ModuleIdentifier].banTable[util.SteamIDTo64(userid)] then + MSync.log(MSYNC_DBG_INFO, "[MBSync] User \"" .. userid .. "\" is already banned, editing old ban instead") + if not (length == 0) then + length = ((os.time() - MSync.modules[info.ModuleIdentifier].banTable[util.SteamIDTo64(userid)].timestamp)+(length*60))/60 + end + MSync.modules[info.ModuleIdentifier].editBan( MSync.modules[info.ModuleIdentifier].banTable[util.SteamIDTo64(userid)]['banId'], reason, length, calling_ply, allserver) + return + end + local banUserIdQ = MSync.DBServer:prepare( [[ INSERT INTO `tbl_mbsync` (user_id, admin_id, reason, date_unix, length_unix, server_group) VALUES ( @@ -229,7 +242,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) banData["unban"] = length msgLength = "Permanent" else - msgLength = ULib.secondsToStringTime(length) + msgLength = ULib.secondsToStringTime(length*60) end if reason == "" then @@ -248,18 +261,11 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) banUserIdQ.onError = function( q, err, sql ) if string.match( err, "^Column 'user_id' cannot be null$" ) then + MSync.log(MSYNC_DBG_INFO, "[MBSync] User does not exist! Creating user before retrying") MSync.mysql.addUserID(userid) MSync.modules[info.ModuleIdentifier].banUserID(userid, calling_ply, length, reason, allserver) else - print("------------------------------------") - print("[MBSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MBSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end end @@ -271,6 +277,8 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Returns: nothing ]] MSync.modules[info.ModuleIdentifier].editBan = function(banId, reason, length, calling_ply, allserver) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.editBan Param.: $banid \"$reason\" $length $calling_ply $allserver", {["banid"] = banId,["reason"] = reason,["length"] = length,["calling_ply"] = tostring(calling_ply),["allserver"] = allserver})) + local editBanQ = MSync.DBServer:prepare( [[ UPDATE `tbl_mbsync` SET @@ -289,7 +297,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) else editBanQ:setString(5, "allservers") end - editBanQ:setString(6, banId) + editBanQ:setString(6, tostring(banId)) editBanQ.onSuccess = function( q, data ) MSync.modules[info.ModuleIdentifier].getActiveBans() @@ -297,15 +305,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) end editBanQ.onError = function( q, err, sql ) - print("------------------------------------") - print("[MBSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MBSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end editBanQ:start() @@ -316,6 +316,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Returns: nothing ]] MSync.modules[info.ModuleIdentifier].unBanUserID = function(calling_ply, banId) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.unBanUserID Param: " .. tostring(calling_ply) .. " " .. banId) local unBanUserIdQ = MSync.DBServer:prepare( [[ UPDATE `tbl_mbsync` SET ban_lifted=(SELECT p_user_id FROM tbl_users WHERE steamid=? AND steamid64=?) @@ -327,19 +328,12 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) unBanUserIdQ.onSuccess = function( q, data ) MSync.modules[info.ModuleIdentifier].getActiveBans() + MSync.modules[info.ModuleIdentifier].msg(calling_ply, "Removed ban with id "..banId) end unBanUserIdQ.onError = function( q, err, sql ) - print("------------------------------------") - print("[MBSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MBSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end unBanUserIdQ:start() @@ -350,6 +344,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Returns: nothing ]] MSync.modules[info.ModuleIdentifier].unBanUser = function(ply_steamid, calling_ply) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.unBanUser Param.: " .. ply_steamid .. " " .. tostring(calling_ply)) local unBanUserQ = MSync.DBServer:prepare( [[ UPDATE `tbl_mbsync` SET @@ -369,19 +364,12 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) unBanUserQ.onSuccess = function( q, data ) MSync.modules[info.ModuleIdentifier].getActiveBans() + MSync.modules[info.ModuleIdentifier].msg(calling_ply, "Unbanned "..ply_steamid) end unBanUserQ.onError = function( q, err, sql ) - print("------------------------------------") - print("[MBSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MBSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end unBanUserQ:start() @@ -392,6 +380,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Returns: nothing ]] MSync.modules[info.ModuleIdentifier].getBans = function(ply, fullTable) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.getBans Param.: " .. ply:Nick() .. " " .. tostring(fullTable)) local getBansQ = MSync.DBServer:prepare( [[ SELECT tbl_mbsync.p_id, @@ -424,8 +413,9 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) local banTable = {} - print("[MBSync] Recieved all ban data") + MSync.log(MSYNC_DBG_INFO, "[MBSync] Recieved ban data from Database") if fullTable then + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Generating full ban table to send to \"" .. ply:Nick() .. "\"") for k,v in pairs(data) do banTable[v.p_id] = { @@ -453,6 +443,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) end else + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Generating short ban table to send to \"" .. ply:Nick() .. "\"") for k,v in pairs(data) do if not v['unban_admin.steamid'] and ((not((v.date_unix+v.length_unix) < os.time())) or (v.length_unix==0)) then banTable[v["banned.steamid64"]] = { @@ -484,15 +475,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) end getBansQ.onError = function( q, err, sql ) - print("------------------------------------") - print("[MBSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MBSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end getBansQ:start() @@ -503,6 +486,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Returns: nothing ]] MSync.modules[info.ModuleIdentifier].getActiveBans = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.getActiveBans") local getActiveBansQ = MSync.DBServer:prepare( [[ SELECT tbl_mbsync.*, @@ -532,7 +516,6 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) getActiveBansQ.onSuccess = function( q, data ) local banTable = {} - print("[MBSync] Recieved ban data") for k,v in pairs(data) do banTable[v["steamid64"]] = { banId = v.p_id, @@ -546,6 +529,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) adminNickname = v["admin.nickname"] } end + MSync.log(MSYNC_DBG_INFO, "[MBSync] Local ban table updated with new data") MSync.modules[info.ModuleIdentifier].banTable = banTable @@ -567,6 +551,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) local message = ULib.getBanMessage( ban.banned.steamid, banData) + MSync.log(MSYNC_DBG_WARNING, "[MBSync] User \"" .. ply:Nick() "\" was banned while data resynchronized, kicking from server") v:Kick(message) else -- Do nothing @@ -575,15 +560,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) end getActiveBansQ.onError = function( q, err, sql ) - print("------------------------------------") - print("[MBSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MBSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end getActiveBansQ:start() @@ -594,6 +571,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Returns: nothing ]] MSync.modules[info.ModuleIdentifier].exportBansToULX = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.exportBansToULX") local exportActiveBans = MSync.DBServer:prepare( [[ SELECT tbl_mbsync.*, @@ -623,15 +601,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) exportActiveBans.onSuccess = function( q, data ) - local function escapeString( str ) - if not str then - return "NULL" - else - return sql.SQLStr(str) - end - end - - print("[MBSync] Exporting Bans to ULX") + print("[MBSync] Exporting all MBSync bans to ULX. Please wait...") for k,v in pairs(data) do local unban if v.length_unix == 0 then @@ -640,13 +610,21 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) unban = v.date_unix + v.length_unix end + local admin + if v["admin.steamid"] == "STEAM_0:0:0" then + admin = v["admin.nickname"] + else + admin = v["admin.nickname"].."("..v["admin.steamid"]..")" + end + ULib.bans[ v["steamid"] ] = { - admin = v["admin.nickname"], + admin = admin, time = v.date_unix, unban = unban, reason = v.reason, name = v["banned.nickname"] } + userTransactions[v["steamid64"]] = true hook.Call( ULib.HOOK_USER_BANNED, _, v["steamid"], ULib.bans[ v["steamid"] ] ) end ULib.fileWrite( ULib.BANS_FILE, ULib.makeKeyValues( ULib.bans ) ) @@ -655,35 +633,114 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) end exportActiveBans.onError = function( q, err, sql ) - print("------------------------------------") - print("[MBSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MBSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end exportActiveBans:start() end concommand.Add("msync."..info.ModuleIdentifier..".export", function( ply, cmd, args ) + if ply:IsValid() then return end MSync.modules[info.ModuleIdentifier].exportBansToULX() end) + --[[ + Description: Function to allow the import of all ULX bans to MBSync + Arguments: + - allservers [boolean] - Should all bans be imported as local bans or as allserver bans? ( true = allservers, false = server group) + Returns: nothing + ]] + MSync.modules[info.ModuleIdentifier].importBansFromULX = function( allservers ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.importBansFromULX Param.: " .. tostring(allservers)) + local transactions = {} + local banTransaction = MSync.DBServer:createTransaction() + + print("[MBSync] Importing bans from ULX into MBSync, please wait...") + for k,v in pairs(ULib.bans) do + --[[ + - Create User + - Create ban query + ]] + transactions[k] = MSync.DBServer:prepare( [[ + INSERT INTO `tbl_mbsync` (user_id, admin_id, reason, date_unix, length_unix, server_group) + VALUES ( + (SELECT p_user_id FROM tbl_users WHERE steamid=?), + (SELECT p_user_id FROM tbl_users WHERE steamid=?), + ?, ?, ?, + (SELECT p_group_id FROM tbl_server_grp WHERE group_name=?) + ); + ]] ) + local timestamp = os.time() + transactions[k]:setString(1, k) + + if v['modified_admin'] then + if v['modified_admin'] == "(Console)" then + transactions[k]:setString(2,"STEAM_0:0:0") + else + transactions[k]:setString(2,string.match(v['modified_admin'], "STEAM_%d:%d:%d+")) + end + else + if v['admin'] == "(Console)" then + transactions[k]:setString(2,"STEAM_0:0:0") + else + transactions[k]:setString(2,string.match(v['admin'], "STEAM_%d:%d:%d+")) + end + end + + transactions[k]:setString(3, v['reason'] or "(None given)") + transactions[k]:setNumber(4, tonumber(v['time'])) + + if tonumber(v['unban']) == 0 then + transactions[k]:setNumber(5, tonumber(v['unban'])) + else + transactions[k]:setNumber(5, tonumber(v['unban']) - tonumber(v['time'])) + end + + if not allserver then + transactions[k]:setString(6, MSync.settings.data.serverGroup) + else + transactions[k]:setString(6, "allservers") + end + + banTransaction:addQuery(transactions[k]) + MSync.log(MSYNC_DBG_INFO, "[MBSync] Imported ban for SteamID: " .. k) + end + + banTransaction.onSuccess = function() + print("[MBSync] Ban import from ULX finished successfully") + end + + banTransaction.onError = function(tr, err) + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MBSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) + end + + banTransaction:start() + end + concommand.Add("msync."..info.ModuleIdentifier..".import", function( ply, cmd, args ) + if ply:IsValid() then return end + local allservers = false + + if args[1] == "true" then + allservers = true + end + + MSync.modules[info.ModuleIdentifier].importBansFromULX( allservers ) + end) + --[[ Description: Function to load the MSync settings file Returns: true ]] MSync.modules[info.ModuleIdentifier].loadSettings = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec.: MBSync.loadSettings") if not file.Exists("msync/"..info.ModuleIdentifier..".txt", "DATA") then MSync.modules[info.ModuleIdentifier].settings = { syncDelay = 300 } file.Write("msync/"..info.ModuleIdentifier..".txt", util.TableToJSON(MSync.modules[info.ModuleIdentifier].settings, true)) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] No configuration found, generated default one") else MSync.modules[info.ModuleIdentifier].settings = util.JSONToTable(file.Read("msync/mbsync.txt", "DATA")) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Configuration found, loaded into MBSync") end return true @@ -694,6 +751,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Returns: true if the settings file exists ]] MSync.modules[info.ModuleIdentifier].saveSettings = function() + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.saveSettings") file.Write("msync/"..info.ModuleIdentifier..".txt", util.TableToJSON(MSync.modules[info.ModuleIdentifier].settings, true)) return file.Exists("msync/"..info.ModuleIdentifier..".txt", "DATA") end @@ -703,6 +761,8 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) Returns: table split in 10er part counts ]] MSync.modules[info.ModuleIdentifier].splitTable = function(tbl) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.splitTable Param.: " .. tostring(tbl)) + local i = 0 local dataSet = 0 local splitTableData = {} @@ -727,6 +787,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction ) MSync.modules[info.ModuleIdentifier].loadSettings() if not MSync.modules[info.ModuleIdentifier].banTable then + MSync.log(MSYNC_DBG_WARNING, "[MBSync] Ban table not found yet, requesting now") MSync.modules[info.ModuleIdentifier].getActiveBans() end end @@ -745,6 +806,7 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".sendMessage") MSync.modules[info.ModuleIdentifier].msg = function(ply, content, msgType) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.msg Param.: $ply \"$content\" $msgType") if type(ply) == "string" and not (ply == "STEAM_0:0:0") then ply = player.GetBySteamID( ply ) end @@ -753,6 +815,7 @@ MSync.modules[info.ModuleIdentifier].net = function() if not IsValid(ply) then print("[MBSync] "..content) else + MSync.log(MSYNC_DBG_INFO, "[MBSync] "..content) if not msgType then msgType = 0 end -- Basic message if msgType == 0 then @@ -776,6 +839,7 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".sendBanTable") MSync.modules[info.ModuleIdentifier].sendSettings = function(ply, banTable) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.sendSettings Param.: " .. tostring(ply) .. " " .. tostring(banTable)) net.Start("msync."..info.ModuleIdentifier..".sendBanTable") net.WriteTable(banTable) net.Send(ply) @@ -789,6 +853,7 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".openBanTable") MSync.modules[info.ModuleIdentifier].openBanTable = function(ply) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.openBanTable Param.: " .. tostring(ply)) net.Start("msync."..info.ModuleIdentifier..".openBanTable") net.Send(ply) end @@ -799,7 +864,8 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".banid") net.Receive("msync."..info.ModuleIdentifier..".banid", function(len, ply) - if not ply:query("msync."..info.ModuleIdentifier..".banPlayer") then return end + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.banid Player: " .. tostring(ply)) + if not ply:query("msync."..info.ModuleIdentifier..".banPlayer") then MSync.log(MSYNC_DBG_WARNING, "[MBSync] User \"" .. ply:Nick() .. "\" tried to ban a user without permission");return end local ban = net.ReadTable() @@ -834,7 +900,8 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".editBan") net.Receive("msync."..info.ModuleIdentifier..".editBan", function(len, ply) - if not ply:query("msync."..info.ModuleIdentifier..".banPlayer") then return end + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.editBan Player: " .. tostring(ply)) + if not ply:query("msync."..info.ModuleIdentifier..".banPlayer") then MSync.log(MSYNC_DBG_WARNING, "[MBSync] User \"" .. ply:Nick() .. "\" tried to ban a user without permission");return end local editedBan = net.ReadTable() @@ -869,7 +936,8 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".unban") net.Receive("msync."..info.ModuleIdentifier..".unban", function(len, ply) - if not ply:query("msync."..info.ModuleIdentifier..".unBanID") then return end + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.unban Player: " .. tostring(ply)) + if not ply:query("msync."..info.ModuleIdentifier..".unBanID") then MSync.log(MSYNC_DBG_WARNING, "[MBSync] User \"" .. ply:Nick() .. "\" tried to unban a user without permission");return end local banid = net.ReadFloat() @@ -880,9 +948,27 @@ MSync.modules[info.ModuleIdentifier].net = function() if not banid then return end; --[[ - Run edit function to edit ban data + When unbanning someone using the banid, we need to still unban the user in ULX, so this is the best solution. + + We loop trough the active ban table, search for the banid ( it has to be there, otherwise the user can't know it ) and then unban the steamid + ]] + local steamid = "" + for k,v in pairs(MSync.modules[info.ModuleIdentifier].banTable) do + if v.banid == banid then + steamid = v.banned['steamid'] + end + end + + --[[ + Run unban function that takes the banid ]] MSync.modules[info.ModuleIdentifier].unBanUserID(ply, banid) + + --[[ + For ulx's sake, we unban the user in ulx for the case that a user has been banned using ulx ban and now gets unbanned using mbsync unbanid + ]] + userTransactions[util.SteamIDTo64(target_steamid)] = true + ULib.unban(target_steamid, calling_ply) end ) --[[ @@ -893,6 +979,7 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".sendSettingsPly") MSync.modules[info.ModuleIdentifier].sendSettings = function(ply) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.sendSettings Param.: " .. tostring(ply)) net.Start("msync."..info.ModuleIdentifier..".sendSettingsPly") net.WriteTable(MSync.modules[info.ModuleIdentifier].settings) net.Send(ply) @@ -904,7 +991,8 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".getSettings") net.Receive("msync."..info.ModuleIdentifier..".getSettings", function(len, ply) - if not ply:query("msync.getSettings") then return end + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.getSettings Player: " .. tostring(ply)) + if not ply:query("msync.getSettings") then MSync.log(MSYNC_DBG_WARNING, "[MBSync] User \"" .. ply:Nick() .. "\" tried to get the settings without permission");return end MSync.modules[info.ModuleIdentifier].sendSettings(ply) end ) @@ -915,7 +1003,8 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".sendSettings") net.Receive("msync."..info.ModuleIdentifier..".sendSettings", function(len, ply) - if not ply:query("msync.sendSettings") then return end + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.sendSettings Player: " .. tostring(ply)) + if not ply:query("msync.sendSettings") then MSync.log(MSYNC_DBG_WARNING, "[MBSync] User \"" .. ply:Nick() .. "\" tried to send settings to the server without permission");return end MSync.modules[info.ModuleIdentifier].settings = net.ReadTable() MSync.modules[info.ModuleIdentifier].saveSettings() @@ -929,6 +1018,7 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".openBanGUI") MSync.modules[info.ModuleIdentifier].openBanGUI = function(ply) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.openBanGUI Param.: " .. tostring(ply)) local tableLength = table.Count(MSync.modules[info.ModuleIdentifier].recentDisconnects) local disconnectTable = {} @@ -957,7 +1047,7 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".recieveDataCount") MSync.modules[info.ModuleIdentifier].sendCount = function(ply, number) - print(number) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.sendCount Param.: " .. tostring(ply) .. " " .. number) net.Start("msync."..info.ModuleIdentifier..".recieveDataCount") net.WriteFloat(number) net.Send(ply) @@ -972,6 +1062,7 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".recieveData") MSync.modules[info.ModuleIdentifier].sendPart = function(ply, part) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Exec: MBSync.sendPart Param.: " .. tostring(ply) .. " " .. tostring(part)) net.Start("msync."..info.ModuleIdentifier..".recieveData") net.WriteTable(part) net.Send(ply) @@ -983,12 +1074,13 @@ MSync.modules[info.ModuleIdentifier].net = function() ]] util.AddNetworkString("msync."..info.ModuleIdentifier..".getBanTable") net.Receive("msync."..info.ModuleIdentifier..".getBanTable", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Net: msync.MBSync.getBanTable Player: " .. tostring(ply)) local fullTable = net.ReadBool() if fullTable then - if not ply:query("msync.openAdminGUI") then return end + if not ply:query("msync.openAdminGUI") then MSync.log(MSYNC_DBG_WARNING, "[MBSync] User \"" .. ply:Nick() .. "\" tried to get the full ban table without permission");return end MSync.modules[info.ModuleIdentifier].getBans(ply, fullTable) else - if not ply:query("msync."..info.ModuleIdentifier..".openBanTable") then return end + if not ply:query("msync."..info.ModuleIdentifier..".openBanTable") then MSync.log(MSYNC_DBG_WARNING, "[MBSync] User \"" .. ply:Nick() .. "\" tried to get the short ban table without permission");return end MSync.modules[info.ModuleIdentifier].getBans(ply, false) end end ) @@ -1012,6 +1104,7 @@ MSync.modules[info.ModuleIdentifier].ulx = function() reason [string] - the ban reason - OPTIONAL - Default: "banned by staff" ]] MSync.modules[info.ModuleIdentifier].Chat.banPlayer = function(calling_ply, target_ply, length, allserver, reason) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.Chat.banPlayer Param.: $calling_ply $target_player $length $allserver \"$reason\"", {["calling_ply"] = calling_ply,["target_player"] = target_ply,["length"] = length,["allserver"] = allserver,["reason"] = reason})) local calling_steamid = "" if not IsValid(calling_ply) then @@ -1056,6 +1149,7 @@ MSync.modules[info.ModuleIdentifier].ulx = function() BanPlayer:addParam{ type=ULib.cmds.StringArg, hint="reason", ULib.cmds.optional, ULib.cmds.takeRestOfLine, completes=ulx.common_kick_reasons } BanPlayer:defaultAccess( ULib.ACCESS_SUPERADMIN ) BanPlayer:help( "Opens the MBSync GUI ( without parameters ) or bans a player" ) + --[[ ban the targeted steamid @@ -1066,6 +1160,7 @@ MSync.modules[info.ModuleIdentifier].ulx = function() reason [string] - the ban reason - OPTIONAL - Default: "banned by staff" ]] MSync.modules[info.ModuleIdentifier].Chat.banSteamID = function(calling_ply, target_steamid, length, allserver, reason) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.Chat.banSteamID Param.: $calling_ply $target_steamid $length $allserver \"$reason\"", {["calling_ply"] = calling_ply,["target_steamid"] = target_steamid,["length"] = length,["allserver"] = allserver,["reason"] = reason})) local calling_steamid = "" if not IsValid(calling_ply) then @@ -1119,6 +1214,7 @@ MSync.modules[info.ModuleIdentifier].ulx = function() target_steamid [string] - the target steamid ]] MSync.modules[info.ModuleIdentifier].Chat.unBanID = function(calling_ply, target_steamid) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.Chat.unBanID Param.: $calling_ply $target_steamid", {["calling_ply"] = calling_ply,["target_steamid"] = target_steamid})) local calling_steamid = "" if not IsValid(calling_ply) then @@ -1137,6 +1233,12 @@ MSync.modules[info.ModuleIdentifier].ulx = function() Unban user with given steamid ]] MSync.modules[info.ModuleIdentifier].unBanUser(target_steamid, calling_steamid) + + --[[ + For ulx's sake, we unban the user in ulx for the case that a user has been banned using ulx ban and now gets unbanned using mbsync unbanid + ]] + userTransactions[util.SteamIDTo64(target_steamid)] = true + ULib.unban(target_steamid, calling_ply) end local BanPlayer = ulx.command( "MSync", "msync."..info.ModuleIdentifier..".unBanID", MSync.modules[info.ModuleIdentifier].Chat.unBanID, "!munban" ) BanPlayer:addParam{ type=ULib.cmds.StringArg, hint="steamid"} @@ -1150,6 +1252,7 @@ MSync.modules[info.ModuleIdentifier].ulx = function() target_steamid [string] - the target steamid ]] MSync.modules[info.ModuleIdentifier].Chat.checkBan = function(calling_ply, target_steamid) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.Chat.checkBan Param.: $calling_ply $target_player", {["calling_ply"] = calling_ply,["target_steamid"] = target_steamid})) if not IsValid(calling_ply) then -- Do nothing else @@ -1202,7 +1305,8 @@ MSync.modules[info.ModuleIdentifier].ulx = function() none ]] MSync.modules[info.ModuleIdentifier].Chat.openBanTable = function(calling_ply) - if not IsValid(calling_ply) then print("[MBSync] This command can only be executed in-game"); return; end + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.Chat.openBanTable Param.: $calling_ply", {["calling_ply"] = calling_ply})) + if not IsValid(calling_ply) then MSync.log(MSYNC_DBG_ERROR, "[MBSync] This command can only be executed in-game"); return; end if not calling_ply:query("msync."..info.ModuleIdentifier..".openBanTable") then return end; -- Open Ban Table MSync.modules[info.ModuleIdentifier].openBanTable(calling_ply) @@ -1221,6 +1325,7 @@ MSync.modules[info.ModuleIdentifier].ulx = function() reason [string] - the ban reason - OPTIONAL - Default: "banned by staff" ]] MSync.modules[info.ModuleIdentifier].Chat.editBan = function(calling_ply, ban_id, length, allserver, reason) + MSync.log(MSYNC_DBG_DEBUG, MSync.formatString("[MBSync] Exec: MBSync.Chat.banPlayer Param.: $calling_ply $ban_id $length $allserver \"$reason\"", {["calling_ply"] = calling_ply,["ban_id"] = ban_id,["length"] = length,["allserver"] = allserver,["reason"] = reason})) local calling_steamid = "" if not IsValid(calling_ply) then @@ -1275,6 +1380,7 @@ MSync.modules[info.ModuleIdentifier].hooks = function() MSync.modules[info.ModuleIdentifier].getActiveBans() hook.Add("CheckPassword", "msync."..info.ModuleIdentifier..".banCheck", function( steamid64 ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Checking ban status for \"" .. steamid64 .. "\"") if MSync.modules[info.ModuleIdentifier].banTable[steamid64] then local ban = MSync.modules[info.ModuleIdentifier].banTable[steamid64] local unbanDate @@ -1286,15 +1392,13 @@ MSync.modules[info.ModuleIdentifier].hooks = function() --[[ Print to console that a banned user tries to join ]] - print("---== [MBSync] ==---") - print("A banned player tried to join the server.") - print("-- Informations --") - print("Nickname: "..ban.banned.nickname) - print("SteamID: "..ban.banned.steamid) - print("Ban Date: "..os.date( "%c", ban.timestamp)) - print("Unban Date: "..unbanDate) - print("Banned by: "..ban.adminNickname) - print("---== [END] ==---") + MSync.log(MSYNC_DBG_WARNING, MSync.formatString("\n---== [MBSync] ==---\nA banned player tried to join the server.\n-- Informations --\nNickname: $nickname\nSteamID: $steamid\nBan Date: $date\nUnban Date: $unbanDate\nBanned by: $admin\n---== [END] ==---", { + ["nickname"] = ban.banned.nickname, + ["steamid"] = ban.banned.steamid, + ["date"] = os.date( "%c", ban.timestamp), + ["unbanDate"] = unbanDate, + ["admin"] = ban.adminNickname + })) --[[ Translate ban data for ULib @@ -1313,11 +1417,24 @@ MSync.modules[info.ModuleIdentifier].hooks = function() local message = ULib.getBanMessage( ban.banned.steamid, banData) return false, message else + --[[ + This function has been disabled in favor of security and to not cause previously ulx banned players to be unbanned. + It has been commented out because of possible usage in the future + + if ULib.bans[util.SteamIDFrom64(steamid64)] then + userTransactions[steamid64] = true + ULib.unban(target_steamid, calling_ply) + -- Disabled because it could cause security issues with whitelisted servers + --return true + end + ]] + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] User with steamid \"" .. steamid64 .. "\" has passed the ban check") return end end) hook.Add("PlayerDisconnected", "msync."..info.ModuleIdentifier..".saveDisconnects", function( ply ) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] Disconnect: Adding player \"" .. tostring(ply) .. "\" to list of recent disconnects") if ply:IsBot() then return end local tableLength = table.Count(MSync.modules[info.ModuleIdentifier].recentDisconnects) local data = { @@ -1328,6 +1445,73 @@ MSync.modules[info.ModuleIdentifier].hooks = function() MSync.modules[info.ModuleIdentifier].recentDisconnects[tableLength] = data end) + + hook.Add("ULibPlayerBanned", "msync.mbsync.ulxban", function(steamid, banData) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] ULX ban detected for user \"" .. steamid .. "\"") + + local ban = {} + + if userTransactions[util.SteamIDTo64(steamid)] then + userTransactions[util.SteamIDTo64(steamid)] = nil + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] User has already been banned in MBSync, aborting ban") + return + end + + if banData.unban == 0 then + ban.length = 0 + else + ban.length = (banData.unban-os.time())/60 + end + + if not banData.reason then + banData.reason = "(None given)" + end + + if banData.modified_admin then + if banData.modified_admin == "(Console)" then + ban.admin = "STEAM_0:0:0" + else + ban.admin = string.match(banData.modified_admin, "STEAM_%d:%d:%d+") + end + else + if banData.admin == "(Console)" then + ban.admin = "STEAM_0:0:0" + else + ban.admin = string.match(banData.admin, "STEAM_%d:%d:%d+") + end + end + + MSync.modules[info.ModuleIdentifier].banUserID(steamid, ban.admin, ban.length, banData.reason, false) + end) + + hook.Add("ULibPlayerUnBanned", "msync.mbsync.ulxunban", function(steamid, admin) + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] ULX Unban detected for user \"" .. steamid .. "\"") + + if userTransactions[util.SteamIDTo64(steamid)] then + userTransactions[util.SteamIDTo64(steamid)] = nil + MSync.log(MSYNC_DBG_DEBUG, "[MBSync] User already has been unbanned from MBSync, aborting unban") + return + end + + local admin_id = "" + if not IsValid(admin) then + admin_id = "STEAM_0:0:0" + else + admin_id = admin:SteamID() + end + MSync.modules[info.ModuleIdentifier].unBanUser(steamid, admin_id) + end) +end + +--[[ + Define a function to run on the server when the module gets disabled +]] +MSync.modules[info.ModuleIdentifier].disable = function() + hook.Remove("CheckPassword", "msync."..info.ModuleIdentifier..".banCheck") + hook.Remove("PlayerDisconnected", "msync."..info.ModuleIdentifier..".saveDisconnects") + hook.Remove("ULibPlayerBanned", "msync.mbsync.ulxban") + hook.Remove("ULibPlayerUnBanned", "msync.mbsync.ulxunban") + timer.Remove("msync."..info.ModuleIdentifier..".getActiveBans") end --[[ diff --git a/lua/msync/server/modules/sv_mrsync.lua b/lua/msync/server/modules/sv_mrsync.lua index e1ca581..3915de8 100644 --- a/lua/msync/server/modules/sv_mrsync.lua +++ b/lua/msync/server/modules/sv_mrsync.lua @@ -1,12 +1,13 @@ MSync = MSync or {} MSync.modules = MSync.modules or {} MSync.modules.MRSync = MSync.modules.MRSync or {} +local userTransaction = userTransaction or {} --[[ * @file sv_mrsync.lua * @package MySQL Rank Sync * @author Aperture Development * @license root_dir/LICENCE - * @version 2.2.0 + * @version 2.2.3 ]] --[[ @@ -16,13 +17,14 @@ MSync.modules.MRSync.info = { Name = "MySQL Rank Sync", ModuleIdentifier = "MRSync", Description = "Synchronise your ranks across your servers", - Version = "2.2.0" + Version = "2.2.3" } --[[ Define mysql table and additional functions that are later used ]] function MSync.modules.MRSync.init( transaction ) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Exec: MRSync.init") transaction:addQuery( MSync.DBServer:query([[ CREATE TABLE IF NOT EXISTS `tbl_mrsync` ( `p_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, @@ -37,11 +39,18 @@ function MSync.modules.MRSync.init( transaction ) --[[ Description: Function to save a players rank using steamid and group name + Arguments: + - steamid [string] : The steamid of the user to be saved + - group [string] : the group to be saved in combination with the steamid Returns: nothing ]] function MSync.modules.MRSync.saveRankByID(steamid, group) - if MSync.modules.MRSync.settings.nosync[group] then return end; + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Exec: MRSync.saveRankByID Params: " .. steamid .. " " .. group) + + if MSync.modules.MRSync.settings.nosync[group] then MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Group \"" .. group .. "\" is set to No-Sync. Not sending data to the database"); return end; + + if string.len(group) > 15 then MSync.log(MSYNC_DBG_ERROR, "[MRSync] Groupname \"" .. group .. "\" is too long for MRSync! Please use rank names with max. 15 characters instead."); return end; local addUserRankQ = MSync.DBServer:prepare( [[ INSERT INTO `tbl_mrsync` (user_id, rank, server_group) @@ -63,18 +72,11 @@ function MSync.modules.MRSync.init( transaction ) addUserRankQ.onError = function( q, err, sql ) if string.match( err, "^Column 'user_id' cannot be null$" ) then + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] User does not exist, creating user \"" .. steamid .. "\" and repeating"); MSync.mysql.addUserID(steamid) MSync.modules.MRSync.saveRankByID(steamid, group) else - print("------------------------------------") - print("[MRSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MRSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end end @@ -89,12 +91,16 @@ function MSync.modules.MRSync.init( transaction ) ]] function MSync.modules.MRSync.validateData( steamid, group ) - if MSync.modules.MRSync.settings.nosync[group] then return end; + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Exec: MRSync.validateData Param.: " .. steamid .. " " .. group); + + if MSync.modules.MRSync.settings.nosync[group] then MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Group \"" .. group .. "\" is set to No-Sync. Not sending data to the database"); return end; local removeOldRanksQ if not MSync.modules.MRSync.settings.syncall[group] then + MSync.log(MSYNC_DBG_INFO, "[MRSync] Non-Syncall group, removing all \"allservers\" ranks from user"); + removeOldRanksQ = MSync.DBServer:prepare( [[ DELETE FROM `tbl_mrsync` WHERE user_id=(SELECT p_user_id FROM tbl_users WHERE steamid=? AND steamid64=?) AND server_group=(SELECT p_group_id FROM tbl_server_grp WHERE group_name='allservers'); ]] ) @@ -102,10 +108,13 @@ function MSync.modules.MRSync.init( transaction ) removeOldRanksQ:setString(2, util.SteamIDTo64(steamid)) removeOldRanksQ.onSuccess = function( q, data ) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Data validation completed successfully, saving user"); MSync.modules.MRSync.saveRankByID(steamid, group) end else + MSync.log(MSYNC_DBG_INFO, "[MRSync] Syncall group, removing all groups from user \"" .. steamid .. "\" that are not \"allservers\""); + removeOldRanksQ = MSync.DBServer:prepare( [[ DELETE FROM `tbl_mrsync` WHERE user_id=(SELECT p_user_id FROM tbl_users WHERE steamid=? AND steamid64=?) AND server_group<>(SELECT p_group_id FROM tbl_server_grp WHERE group_name='allservers'); ]] ) @@ -113,24 +122,18 @@ function MSync.modules.MRSync.init( transaction ) removeOldRanksQ:setString(2, util.SteamIDTo64(steamid)) removeOldRanksQ.onSuccess = function( q, data ) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Data validation completed successfully, saving user"); MSync.modules.MRSync.saveRankByID(steamid, group) end end removeOldRanksQ.onError = function( q, err, sql ) if string.match( err, "^Column 'user_id' cannot be null$" ) then + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] User not found, creating user before trying again"); MSync.mysql.addUserID(steamid) MSync.modules.MRSync.saveRankByID(steamid, group) else - print("------------------------------------") - print("[MRSync] SQL Error!") - print("------------------------------------") - print("Please include this in a Bug report:\n") - print(err.."\n") - print("------------------------------------") - print("Do not include this, this is for debugging only:\n") - print(sql.."\n") - print("------------------------------------") + MSync.log(MSYNC_DBG_ERROR, MSync.formatString("\n------------------------------------\n[MRSync] SQL Error!\n------------------------------------\nPlease include this in a Bug report:\n\n$err\n\n------------------------------------\nDo not include this, this is for debugging only:\n\n$sql\n\n------------------------------------", {['err'] = err, ['sql'] = sql})) end end @@ -139,9 +142,12 @@ function MSync.modules.MRSync.init( transaction ) --[[ Description: Function to load a players rank + Arguments: + - ply [playerEntity] : The player to be loaded Returns: nothing ]] function MSync.modules.MRSync.loadRank(ply) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Exec: MRSync.loadRank Param.:" .. ply:Nick()); local loadUserQ = MSync.DBServer:prepare( [[ SELECT `rank` FROM `tbl_mrsync` WHERE user_id=( @@ -158,24 +164,66 @@ function MSync.modules.MRSync.init( transaction ) loadUserQ:setString(3, MSync.settings.data.serverGroup) function loadUserQ.onData( q, data ) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Got data for user \"" .. ply:Nick() .. "\". Rank: " .. data.rank); if not ULib.ucl.groups[data.rank] then - print("[MRSync] Could not load rank "..data.rank.." for "..ply:Nick()..". Rank does not exist on this server") + MSync.log(MSYNC_DBG_ERROR, "[MRSync] Could not load rank "..data.rank.." for "..ply:Nick()..". Rank does not exist on this server") return end if data.rank == ply:GetUserGroup() then return end; + MSync.log(MSYNC_DBG_INFO, "[MRSync] Data for user \"" .. ply:Nick() .. "\" valid and user does not have the right group, changing to " .. data.rank); ply:SetUserGroup(data.rank) end + function loadUserQ.onSuccess( q, data ) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] loadUser query executed successfully"); + if not data[1] then + if ply:GetUserGroup() == "user" or MSync.modules.MRSync.settings.nosync[ply:GetUserGroup()] then return end + + MSync.log(MSYNC_DBG_INFO, "[MRSync] Assuming user \"" .. ply:Nick() .. "\" has been removed from rank, setting them to default") + + userTransaction[ply:SteamID64()] = true + ULib.ucl.removeUser(ply:SteamID()) + end + end + loadUserQ:start() end + --[[ + Description: Function to remove a user entirely from MRSync + Arguments: + - steamid [string] : The steamid of the user to be removed + Returns: nothing + ]] + function MSync.modules.MRSync.removeRank(steamid) + + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Exec: MRSync.removeRank Param.: " .. steamid); + + local removeUserRankQ = MSync.DBServer:prepare( [[ + DELETE FROM `tbl_mrsync` WHERE + user_id=(SELECT p_user_id FROM tbl_users WHERE steamid=? AND steamid64=?) AND + ( + server_group=(SELECT p_group_id FROM tbl_server_grp WHERE group_name='allservers') OR + server_group=(SELECT p_group_id FROM tbl_server_grp WHERE group_name=?) + ); + ]] ) + removeUserRankQ:setString(1, steamid) + removeUserRankQ:setString(2, util.SteamIDTo64( steamid )) + removeUserRankQ:setString(3, MSync.settings.data.serverGroup) + + removeUserRankQ:start() + end + --[[ Description: Function to load the MSync settings file Returns: true ]] function MSync.modules.MRSync.loadSettings() + + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Exec: MRSync.loadSettings"); + if not file.Exists("msync/mrsync.txt", "DATA") then MSync.modules.MRSync.settings = { nosync = { @@ -186,8 +234,10 @@ function MSync.modules.MRSync.init( transaction ) } } file.Write("msync/mrsync.txt", util.TableToJSON(MSync.modules.MRSync.settings, true)) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] No config file, creating one now"); else MSync.modules.MRSync.settings = util.JSONToTable(file.Read("msync/mrsync.txt", "DATA")) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Found config file, loading it now"); end return true @@ -198,6 +248,7 @@ function MSync.modules.MRSync.init( transaction ) Returns: true if the settings file exists ]] function MSync.modules.MRSync.saveSettings() + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Exec: MRSync.saveSettings"); file.Write("msync/mrsync.txt", util.TableToJSON(MSync.modules.MRSync.settings, true)) return file.Exists("msync/mrsync.txt", "DATA") end @@ -221,6 +272,7 @@ function MSync.modules.MRSync.net() ]] util.AddNetworkString("msync.mrsync.sendSettingsPly") function MSync.modules.MRSync.sendSettings(ply) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Exec: MRSync.sendSettings Param.: " .. ply:Nick()); net.Start("msync.mrsync.sendSettingsPly") net.WriteTable(MSync.modules.MRSync.settings) net.Send(ply) @@ -232,6 +284,7 @@ function MSync.modules.MRSync.net() ]] util.AddNetworkString("msync.mrsync.getSettings") net.Receive("msync.mrsync.getSettings", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Net: msync.mrsync.getSettings Ply.: " .. ply:Nick()); if not ply:query("msync.getSettings") then return end MSync.modules.MRSync.sendSettings(ply) @@ -243,6 +296,7 @@ function MSync.modules.MRSync.net() ]] util.AddNetworkString("msync.mrsync.sendSettings") net.Receive("msync.mrsync.sendSettings", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "[MRSync] Net: msync.mrsync.sendSettings Ply.: " .. ply:Nick()); if not ply:query("msync.sendSettings") then return end MSync.modules.MRSync.settings = net.ReadTable() @@ -266,20 +320,46 @@ function MSync.modules.MRSync.hooks() -- Load rank on spawn hook.Add("PlayerInitialSpawn", "mrsync.H.loadRank", function(ply) + MSync.log(MSYNC_DBG_INFO, "[MRSync] Loading user rank for \"" .. ply:Nick() .. "\""); MSync.modules.MRSync.loadRank(ply) end) -- Save rank on disconnect hook.Add("PlayerDisconnected", "mrsync.H.saveRank", function(ply) + MSync.log(MSYNC_DBG_INFO, "[MRSync] Saving user rank for \"" .. ply:Nick() .. "\""); --MSync.modules.MRSync.saveRank(ply) MSync.modules.MRSync.validateData(ply:SteamID(), ply:GetUserGroup()) end) -- Save rank on GroupChange hook.Add("ULibUserGroupChange", "mrsync.H.saveRankOnUpdate", function(sid, _, _, new_group, _) + MSync.log(MSYNC_DBG_INFO, "[MRSync] Rank changed for user \"" .. sid .. "\" updating it now"); --MSync.modules.MRSync.saveRankByID(sid, new_group) MSync.modules.MRSync.validateData(sid, new_group) end) + + -- Remove Rank on ULX Remove + hook.Add("ULibUserRemoved", "mrsync.H.saveRankOnUpdate", function(sid) + MSync.log(MSYNC_DBG_INFO, "[MRSync] User deletion detected for \"" .. sid .. "\""); + if userTransaction[util.SteamIDTo64(sid)] then + userTransaction[util.SteamIDTo64(sid)] = nil + MSync.log(MSYNC_DBG_INFO, "[MRSync] User has already been removed from MRSync, aborting removal"); + return + end + + MSync.log(MSYNC_DBG_INFO, "[MRSync] User \"" .. ply:Nick() .. "\" was removed from ULX, removing from MRSync"); + MSync.modules.MRSync.removeRank(sid) + end) +end + +--[[ + Define a function to run on the server when the module gets disabled +]] +MSync.modules.MRSync.disable = function() + hook.Remove("PlayerInitialSpawn", "mrsync.H.loadRank") + hook.Remove("PlayerDisconnected", "mrsync.H.saveRank") + hook.Remove("ULibUserGroupChange", "mrsync.H.saveRankOnUpdate") + hook.Remove("ULibUserRemoved", "mrsync.H.saveRankOnUpdate") end --[[ diff --git a/lua/msync/server/modules/sv_samplemodule.lua b/lua/msync/server/modules/sv_samplemodule.lua index f523b32..a20b8b0 100644 --- a/lua/msync/server/modules/sv_samplemodule.lua +++ b/lua/msync/server/modules/sv_samplemodule.lua @@ -69,6 +69,13 @@ MSync.modules[info.ModuleIdentifier].hooks = function() end) end +--[[ + Define a function to run on the server when the module gets disabled +]] +MSync.modules[info.ModuleIdentifier].disable = function() + +end + --[[ Return info ( Just for single module loading ) ]] diff --git a/lua/msync/server/sv_hooks.lua b/lua/msync/server/sv_hooks.lua index 5f479a2..706a31d 100644 --- a/lua/msync/server/sv_hooks.lua +++ b/lua/msync/server/sv_hooks.lua @@ -1,3 +1,7 @@ +MSync = MSync or {} +MSync.mysql = MSync.mysql or {} +MSync.net = MSync.net or {} +MSync.loadedModules = MSync.loadedModules or {} --[[ Description: hook to initialize MSync 2 Returns: nothing @@ -27,7 +31,7 @@ end) ]] hook.Add("PlayerInitialSpawn", "msync.createUser", function( ply ) MSync.mysql.addUser(ply) - MSync.net.sendTable(ply, "modulestate", MSync.settings.data.enabledModules) + MSync.net.sendTable(ply, "modulestate", MSync.loadedModules) if ply:query("msync.openAdminGUI") then MSync.net.dbStatus(ply) diff --git a/lua/msync/server/sv_init.lua b/lua/msync/server/sv_init.lua index 15565b6..411d472 100644 --- a/lua/msync/server/sv_init.lua +++ b/lua/msync/server/sv_init.lua @@ -1,10 +1,11 @@ -MSync = MSync or {} -MSync.net = MSync.net or {} -MSync.mysql = MSync.mysql or {} -MSync.modules = MSync.modules or {} -MSync.settings = MSync.settings or {} -MSync.func = MSync.func or {} -MSync.ulx = MSync.ulx or {} +MSync = MSync or {} +MSync.net = MSync.net or {} +MSync.mysql = MSync.mysql or {} +MSync.modules = MSync.modules or {} +MSync.settings = MSync.settings or {} +MSync.func = MSync.func or {} +MSync.ulx = MSync.ulx or {} +MSync.loadedModules = MSync.loadedModules or {} --[[ Description: Function to load the server side files @@ -12,6 +13,8 @@ MSync.ulx = MSync.ulx or {} ]] function MSync.func.loadServer() + MSync.log(MSYNC_DBG_DEBUG, "Loading Server") + include("msync/server/sv_net.lua") include("msync/server/sv_mysql.lua") include("msync/server/sv_modules.lua") @@ -23,6 +26,8 @@ function MSync.func.loadServer() timer.Create("msync.t.checkForULXandULib", 5, 0, function() if not ulx or not ULib then return end; + MSync.log(MSYNC_DBG_DEBUG, "ULX Loaded, continuing startup") + timer.Remove("msync.t.checkForULXandULib") MSync.ulx.createPermissions() MSync.ulx.createCommands() @@ -34,11 +39,13 @@ function MSync.func.loadServer() local files, _ = file.Find("msync/client_gui/*.lua", "LUA") for k, v in pairs(files) do AddCSLuaFile("msync/client_gui/"..v) + MSync.log(MSYNC_DBG_DEBUG, "Added client Lua file: "..v) end local files, _ = file.Find("msync/client_gui/modules/*.lua", "LUA") for k, v in pairs(files) do AddCSLuaFile("msync/client_gui/modules/"..v) + MSync.log(MSYNC_DBG_DEBUG, "Added client module file: "..v) end end @@ -47,6 +54,7 @@ end Returns: true ]] function MSync.func.loadSettings() + MSync.log(MSYNC_DBG_INFO, "Loading Settings") if not file.Exists("msync/settings.txt", "DATA") then MSync.settings.data = { mysql = { @@ -57,14 +65,39 @@ function MSync.func.loadSettings() database = "msync" }, enabledModules = { - ["mrsync"] = true + ["MRSync"] = true }, serverGroup = "allservers" } file.CreateDir("msync") file.Write("msync/settings.txt", util.TableToJSON(MSync.settings.data, true)) + MSync.log(MSYNC_DBG_DEBUG, "Created new configuration") else MSync.settings.data = util.JSONToTable(file.Read("msync/settings.txt", "DATA")) + + if MSync.settings.data.EnabledModules and MSync.settings.data.DisabledModules then + MSync.log(MSYNC_DBG_WARNING, "Old settings file found! Updating to new format") + file.Delete( "msync/settings.txt" ) + + local oldSettings = table.Copy(MSync.settings.data) + MSync.settings.data = { + mysql = { + host = oldSettings.mysql.Host, + port = oldSettings.mysql.Port, + username = oldSettings.mysql.Username, + password = oldSettings.mysql.Password, + database = oldSettings.mysql.Database + }, + enabledModules = { + ["MRSync"] = true + }, + serverGroup = "allservers" + } + + file.Write("msync/settings.txt", util.TableToJSON(MSync.settings.data, true)) + MSync.log(MSYNC_DBG_WARNING, "Settings imported! Module settings cannot be ported and need to be re-done") + end + MSync.log(MSYNC_DBG_DEBUG, "Loaded found configuration") end return true @@ -76,6 +109,9 @@ end ]] function MSync.func.saveSettings() file.Write("msync/settings.txt", util.TableToJSON(MSync.settings.data, true)) + + MSync.log(MSYNC_DBG_INFO, "Saved configuration") + return file.Exists("msync/settings.txt", "DATA") end @@ -86,9 +122,12 @@ end function MSync.func.getModuleInfos() local infoTable = {} + MSync.log(MSYNC_DBG_DEBUG, "Getting module informations...") + for k,v in pairs(MSync.modules) do infoTable[k] = v.info infoTable[k].state = MSync.settings.data.enabledModules[v.info.ModuleIdentifier] or false + MSync.log(MSYNC_DBG_DEBUG, "[getModuleInfos] Got info for "..k) end return infoTable @@ -101,6 +140,9 @@ end ]] function MSync.func.getSafeSettings() local settings = table.Copy(MSync.settings.data) + + MSync.log(MSYNC_DBG_DEBUG, "Generating safe settings to be sent to the player") + settings.mysql.password = nil return settings diff --git a/lua/msync/server/sv_modules.lua b/lua/msync/server/sv_modules.lua index 32bfc1d..17ac4e9 100644 --- a/lua/msync/server/sv_modules.lua +++ b/lua/msync/server/sv_modules.lua @@ -1,16 +1,21 @@ -MSync = MSync or {} -MSync.mysql = MSync.mysql or {} -MSync.settings = MSync.settings or {} -MSync.modules = MSync.modules or {} +MSync = MSync or {} +MSync.mysql = MSync.mysql or {} +MSync.settings = MSync.settings or {} +MSync.modules = MSync.modules or {} +MSync.loadedModules = MSync.loadedModules or {} --[[ Description: Loads all server side modules Returns: nothing ]] function MSync.loadModules() + + MSync.log(MSYNC_DBG_DEBUG, "Loading modules") + local files, _ = file.Find("msync/server/modules/*.lua", "LUA") for k, v in pairs(files) do include("msync/server/modules/"..v) + MSync.log(MSYNC_DBG_DEBUG, "Found module: "..v) end end @@ -19,6 +24,9 @@ end Returns: nothing ]] function MSync.initModules() + + MSync.log(MSYNC_DBG_DEBUG, "Initializing modules") + MSync.mysql.dbstatus = false if MSync.DBServer then local initTransaction = MSync.DBServer:createTransaction() @@ -29,23 +37,25 @@ function MSync.initModules() v["net"]() v["ulx"]() v["hooks"]() - print("["..v["info"]["Name"].."] Module loaded") + MSync.loadedModules[v["info"].ModuleIdentifier] = true + MSync.net.sendModuleEnable( v["info"].ModuleIdentifier ) + MSync.log(MSYNC_DBG_INFO, "["..v["info"]["Name"].."] Module loaded") end end function initTransaction.onSuccess() - print("[MSync] Module querys have been completed successfully") + MSync.log(MSYNC_DBG_INFO, "Module querys have been completed successfully") MSync.mysql.dbstatus = true end function initTransaction.onError(tr, err) - print("[MSync] There has been a error while loading the module querys.\nPlease inform the Developer and send him this:\n"..err) + MSync.log(MSYNC_DBG_ERROR, "There has been a error while loading the module querys.\nPlease inform the Developer and send him this:\n"..err) MSync.mysql.dbstatus = false end initTransaction:start() else - print("[MSync] No MySQL server connected, aborting module loading.") + MSync.log(MSYNC_DBG_ERROR, "No MySQL server connected, aborting module loading.") end end @@ -63,17 +73,82 @@ function MSync.loadModule(path) MSync.modules[info.ModuleIdentifier].ulx() MSync.modules[info.ModuleIdentifier].hooks() - print("["..MSync.modules[info.Name].."] Module loaded") + MSync.log(MSYNC_DBG_INFO, "["..MSync.modules[info.Name].."] Module loaded") function initTransaction.onSuccess() - print("[MSync] Module query has been completed successfully") - MSync.mysql[info.ModuleIdentifier].dbstatus = true + MSync.log(MSYNC_DBG_INFO, "Module query has been completed successfully") + --MSync.mysql[info.ModuleIdentifier].dbstatus = true end function initTransaction.onError(tr, err) - print("[MSync] There has been a error while loading the module querys.\nPlease inform the Developer and send him this:\n"..err) - MSync.mysql[info.ModuleIdentifier].dbstatus = false + MSync.log(MSYNC_DBG_ERROR, "There has been a error while loading the module querys.\nPlease inform the Developer and send him this:\n"..err) + --MSync.mysql[info.ModuleIdentifier].dbstatus = false end initTransaction:start() end + +--[[ + Description: Enables a single already loaded clientside module + Arguments: + - module [string] - the module to be enabled + Returns: nothing +]] +function MSync.enableModule( module ) + if MSync.modules[module] then + MSync.log(MSYNC_DBG_DEBUG, "Module \"" .. module .. "\" enabled?: " .. tostring(MSync.settings.data.enabledModules[module])) + if not MSync.settings.data.enabledModules[module] then + if MSync.DBServer:ping() then + local initTransaction = MSync.DBServer:createTransaction() + + MSync.modules[module].init(initTransaction) + MSync.modules[module].net() + MSync.modules[module].ulx() + MSync.modules[module].hooks() + + function initTransaction.onSuccess() + MSync.log(MSYNC_DBG_INFO, "["..MSync.modules[module]["info"]["Name"].."] Module loaded") + MSync.net.sendModuleEnable( module ) + --MSync.mysql[module].dbstatus = true + end + + function initTransaction.onError(tr, err) + MSync.log(MSYNC_DBG_ERROR, "There has been a error while loading the module querys.\nPlease inform the Developer and send him this:\n"..err) + --MSync.mysql[module].dbstatus = false + end + + initTransaction:start() + else + MSync.log(MSYNC_DBG_WARNING, "Cannot enable \"" .. module .. "\". Database isn't connected") + end + else + MSync.log(MSYNC_DBG_WARNING, "Module \"" .. module .. "\" is already enabled") + end + else + MSync.log(MSYNC_DBG_WARNING, "Cannot enable non-existant module \"" .. module .. "\"") + end +end + +--[[ + Description: Disabled a single already loaded clientside module + Arguments: + - module [string] - the module to be disabled + Returns: nothing +]] +function MSync.disableModule( module ) + if MSync.modules[module] then + if MSync.settings.data.enabledModules[module] then + if MSync.modules[module].disable then + MSync.modules[module].disable() + MSync.log(MSYNC_DBG_INFO, "["..MSync.modules[module]["info"]["Name"].."] Module disabled") + MSync.net.sendModuleDisable( module ) + else + MSync.log(MSYNC_DBG_WARNING, "Cannot disable outdated module \"" .. module .. "\"") + end + else + MSync.log(MSYNC_DBG_WARNING, "Module \"" .. module .. "\" is already disabled") + end + else + MSync.log(MSYNC_DBG_WARNING, "Cannot disable non-existant module \"" .. module .. "\"") + end +end \ No newline at end of file diff --git a/lua/msync/server/sv_mysql.lua b/lua/msync/server/sv_mysql.lua index b75b6d5..3b1db2b 100644 --- a/lua/msync/server/sv_mysql.lua +++ b/lua/msync/server/sv_mysql.lua @@ -9,9 +9,11 @@ MSync.func = MSync.func or {} Returns: nothing ]] function MSync.mysql.initialize() - if (file.Exists( "bin/gmsv_mysqloo_linux.dll", "LUA" ) or file.Exists( "bin/gmsv_mysqloo_win32.dll", "LUA" )) and MSync.settings.data.mysql then + if (file.Exists( "bin/gmsv_mysqloo_linux.dll", "LUA" ) or file.Exists( "bin/gmsv_mysqloo_win32.dll", "LUA" ) or file.Exists( "bin/gmsv_mysqloo_linux64.dll", "LUA" ) or file.Exists( "bin/gmsv_mysqloo_win64.dll", "LUA" )) and MSync.settings.data.mysql then require("mysqloo") + MSync.log(MSYNC_DBG_INFO, "Initializing database") + MSync.DBServer = mysqloo.connect( MSync.settings.data.mysql.host, MSync.settings.data.mysql.username, @@ -23,6 +25,8 @@ function MSync.mysql.initialize() function MSync.DBServer.onConnected( db ) local initDatabase = MSync.DBServer:createTransaction() + MSync.log(MSYNC_DBG_INFO, "Connected to database, running database preparation ( this is done at each startup )") + initDatabase:addQuery(MSync.DBServer:query([[ CREATE TABLE IF NOT EXISTS `tbl_msyncdb_version` ( `version` INT UNSIGNED NOT NULL, @@ -77,10 +81,11 @@ function MSync.mysql.initialize() function initDatabase.onSuccess() MSync.mysql.saveServer() MSync.initModules() + MSync.log(MSYNC_DBG_INFO, "Database prepared") end function initDatabase.onError(tr, err) - print("[MSync] There has been a error while initializing the database.\nPlease inform the Developer and send him this:\n"..err) + MSync.log(MSYNC_DBG_ERROR, "There has been a error while initializing the database.\nPlease inform the Developer and send him this:\n"..err) end initDatabase:start() @@ -88,14 +93,14 @@ function MSync.mysql.initialize() end function MSync.DBServer.onConnectionFailed( db, err ) - print("[MSync] There has been a error while loading the module querys.\nPlease inform the Developer and send him this:\n"..err) + MSync.log(MSYNC_DBG_ERROR, "There has been a error while loading the module querys.\nPlease inform the Developer and send him this:\n"..err) end MSync.DBServer:connect() elseif not MSync.settings then - print("[MSync] Settings not found") + MSync.log(MSYNC_DBG_ERROR, "Settings not found") else - print("[MSync] Could not locate MySQLoo") + MSync.log(MSYNC_DBG_ERROR, "Could not locate MySQLoo") end end @@ -105,7 +110,8 @@ end Returns: nothing ]] function MSync.mysql.addUser(ply) - if not MSync.DBServer then print("[MSync] No Database connected yet. Please connect to a Database to be able to create users."); return end; + MSync.log(MSYNC_DBG_DEBUG, "Exec: addUser. Param.: " .. tostring(ply)) + if not MSync.DBServer then MSync.log(MSYNC_DBG_DEBUG, "No Database connected yet. Please connect to a Database to be able to create users."); return end; local addUserQ = MSync.DBServer:prepare( [[ INSERT INTO `tbl_users` (steamid, steamid64, nickname, joined) @@ -124,11 +130,11 @@ function MSync.mysql.addUser(ply) addUserQ:setString(4, os.date("%Y-%m-%d %H:%M:%S", os.time())) function addUserQ.onSuccess() - print("[MSync] User "..ply:Nick().." successfully created") + MSync.log(MSYNC_DBG_INFO, "User "..ply:Nick().." successfully created") end function addUserQ.onError(q, err, sql) - print("[MSync] Failed to create user "..ply:Nick().." !\nPlease report this to the developer: "..err) + MSync.log(MSYNC_DBG_ERROR, "Failed to create user "..ply:Nick().." !\nPlease report this to the developer: "..err) end addUserQ:start() @@ -142,7 +148,8 @@ end Returns: nothing ]] function MSync.mysql.addUserID(steamid, nickname) - if not MSync.DBServer then print("[MSync] No Database connected yet. Please connect to a Database to be able to create users."); return end; + MSync.log(MSYNC_DBG_DEBUG, "Exec: addUserID. Param.: " .. steamid .. " " .. nickname) + if not MSync.DBServer then MSync.log(MSYNC_DBG_DEBUG, "No Database connected yet. Please connect to a Database to be able to create users."); return end; if not string.match( steamid, "^STEAM_[0-1]:[0-1]:[0-9]+$" ) then return end; nickname = nickname or "None Given" @@ -163,11 +170,11 @@ function MSync.mysql.addUserID(steamid, nickname) addUserQ:setString(4, os.date("%Y-%m-%d %H:%M:%S", os.time())) function addUserQ.onSuccess() - print("[MSync] User "..steamid.." successfully created") + MSync.log(MSYNC_DBG_INFO, "User "..steamid.." successfully created") end function addUserQ.onError(q, err, sql) - print("[MSync] Failed to create user "..steamid.." !\nPlease report this to the developer: "..err) + MSync.log(MSYNC_DBG_ERROR, "Failed to create user "..steamid.." !\nPlease report this to the developer: "..err) end addUserQ:start() @@ -210,6 +217,7 @@ function MSync.mysql.saveServer() if string.len(hostname) > 75 then hostname = string.sub( hostname, 1, 75 ) + MSync.log(MSYNC_DBG_WARNING, "Hostname too long, shorting it down to a max of 75 letters") end addServer:setString(1, hostname) addServer:setString(2, gameAddress[1]) @@ -217,18 +225,18 @@ function MSync.mysql.saveServer() addServer:setString(4, MSync.settings.data.serverGroup) function addServer.onSuccess() - print("[MSync] Server saved to database") + MSync.log(MSYNC_DBG_INFO, "Server saved to database") end function addServer.onError(q, err, sql) - print("[MSync] Failed to create server !\nPlease report this to the developer: "..err) + MSync.log(MSYNC_DBG_ERROR, "Failed to create server !\nPlease report this to the developer: "..err) end addServer:start() end function addServerGroup.onError(q, err, sql) - print("[MSync] Failed to create server !\nPlease report this to the developer: "..err) + MSync.log(MSYNC_DBG_ERROR, "Failed to create server !\nPlease report this to the developer: "..err) end addServerGroup:start() diff --git a/lua/msync/server/sv_net.lua b/lua/msync/server/sv_net.lua index 467c029..431c053 100644 --- a/lua/msync/server/sv_net.lua +++ b/lua/msync/server/sv_net.lua @@ -10,6 +10,7 @@ MSync.net = MSync.net or {} Returns: nothing ]] function MSync.net.sendTable(ply, identifier, table) + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.sendTable. Param.: " .. tostring(ply) .. " " .. identifier .. " " .. tostring(table)) local identifier = identifier or "settings" net.Start("msync.sendTable") @@ -28,6 +29,7 @@ util.AddNetworkString("msync.sendTable") Returns: nothing ]] function MSync.net.sendMessage(ply, state, string) + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.sendMessage. Param.: " .. tostring(ply) .. " " .. state .. " " .. tostring(string)) local state = state or "info" net.Start("msync.sendMessage") @@ -44,6 +46,7 @@ util.AddNetworkString("msync.sendMessage") Returns: nothing ]] function MSync.net.openAdminGUI(ply) + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.openAdminGUI. Param.: " .. tostring(ply)) net.Start("msync.openAdminGUI") net.Send(ply) end @@ -56,9 +59,10 @@ util.AddNetworkString("msync.openAdminGUI") Returns: nothing ]] function MSync.net.dbStatus(ply) + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.dbStatus. Param.: " .. tostring(ply)) net.Start("msync.dbStatus") if MSync.DBServer then - net.WriteBool(true) + net.WriteBool(MSync.DBServer:ping()) else net.WriteBool(false) end @@ -66,12 +70,41 @@ function MSync.net.dbStatus(ply) end util.AddNetworkString("msync.dbStatus") +--[[ + Description: Function to enable a module on all clients + Arguments: + module [string] - the module to be enabled + Returns: nothing +]] +function MSync.net.sendModuleEnable( module ) + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.enableModule. Param.: " .. module) + net.Start("msync.enableModule") + net.WriteString(module) + net.Broadcast() +end +util.AddNetworkString("msync.enableModule") + +--[[ + Description: Function to disable a module on all clients + Arguments: + module [string] - the module to be disabled + Returns: nothing +]] +function MSync.net.sendModuleDisable( module ) + MSync.log(MSYNC_DBG_DEBUG, "Exec: net.disableModule. Param.: " .. module) + net.Start("msync.disableModule") + net.WriteString(module) + net.Broadcast() +end +util.AddNetworkString("msync.disableModule") + --[[ Description: Net Receiver - Gets called when the client requests a table Returns: nothing ]] util.AddNetworkString("msync.getTable") net.Receive("msync.getTable", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.getTable. Ply.: " .. ply:Nick()) if not ply:query("msync.getTable") then return end local identifier = net.ReadString() @@ -84,6 +117,7 @@ end ) ]] util.AddNetworkString("msync.sendSettings") net.Receive("msync.sendSettings", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.sendSettings. Ply.: " .. ply:Nick()) if not ply:query("msync.sendSettings") then return end local password = MSync.settings.data.mysql.password @@ -106,6 +140,7 @@ end ) ]] util.AddNetworkString("msync.getSettings") net.Receive("msync.getSettings", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.getSettings. Ply.: " .. ply:Nick()) if not ply:query("msync.getSettings") then return end MSync.net.sendTable(ply, "settings", MSync.func.getSafeSettings()) end ) @@ -116,6 +151,7 @@ end ) ]] util.AddNetworkString("msync.getModules") net.Receive("msync.getModules", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.getModules. Ply.: " .. ply:Nick()) if not ply:query("msync.getModules") then return end MSync.net.sendTable(ply, "modules", MSync.func.getModuleInfos()) @@ -127,14 +163,17 @@ end ) ]] util.AddNetworkString("msync.toggleModule") net.Receive("msync.toggleModule", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.toggleModule. Ply.: " .. ply:Nick()) if not ply:query("msync.toggleModule") then return end local ident = net.ReadString() local state = net.ReadString() if state == "Enable" then + MSync.enableModule( ident ) MSync.settings.data.enabledModules[ident] = true elseif state == "Disable" then + MSync.disableModule( ident ) MSync.settings.data.enabledModules[ident] = nil end MSync.func.saveSettings() @@ -147,7 +186,28 @@ end ) ]] util.AddNetworkString("msync.connectDB") net.Receive("msync.connectDB", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.connectDB. Ply.: " .. ply:Nick()) if not ply:query("msync.connectDB") then return end - MSync.mysql.initialize() + if MSync.DBServer then + if not MSync.DBServer:ping() then + MSync.mysql.initialize() + else + MSync.net.sendMessage(ply, "error", "The database is already connected!") + MSync.log(MSYNC_DBG_DEBUG, "Aborted database connect because connection is already established") + end + else + MSync.mysql.initialize() + end +end ) + +--[[ + Description: Net Receiver - Gets called when the client requests the database status + Returns: nothing +]] +util.AddNetworkString("msync.connectionStatus") +net.Receive("msync.connectionStatus", function(len, ply) + MSync.log(MSYNC_DBG_DEBUG, "Net: msync.connectionStatus. Ply.: " .. ply:Nick()) + if not ply:query("msync.getSettings") then return end + MSync.net.dbStatus(ply) end ) diff --git a/lua/msync/server/sv_ulx.lua b/lua/msync/server/sv_ulx.lua index f8acdb1..59bf021 100644 --- a/lua/msync/server/sv_ulx.lua +++ b/lua/msync/server/sv_ulx.lua @@ -6,6 +6,7 @@ MSync.ulx = MSync.ulx or {} Returns: nothing ]] function MSync.ulx.createPermissions() + MSync.log(MSYNC_DBG_DEBUG, "Creating ULX permissions") ULib.ucl.registerAccess("msync.getTable", "superadmin", "Allows player to get MSync tables", "MSync") ULib.ucl.registerAccess("msync.sendSettings", "superadmin", "Allows player to send settings to server", "MSync") ULib.ucl.registerAccess("msync.connectDB", "superadmin", "Allows player to connect the server to the database server", "MSync") @@ -22,6 +23,7 @@ end Returns: nothing ]] function MSync.ulx.createCommands() + MSync.log(MSYNC_DBG_DEBUG, "Creating ULX commands") function MSync.func.openAdminGUI(calling_ply) if not calling_ply:query("msync.openAdminGUI") then return end; if not IsValid(calling_ply) then return end; diff --git a/lua/msync/sh_init.lua b/lua/msync/sh_init.lua index df7e2ed..0f7a831 100644 --- a/lua/msync/sh_init.lua +++ b/lua/msync/sh_init.lua @@ -1,2 +1,75 @@ MSync = MSync or {} MSync.net = MSync.net or {} + +MSYNC_DBG_ERROR = 0 +MSYNC_DBG_WARNING = 1 +MSYNC_DBG_INFO = 2 +MSYNC_DBG_DEBUG = 3 + +local debugLevels = { + [MSYNC_DBG_ERROR] = { + color = Color(255, 44, 44), + prefix = "[MSync_ERROR]" + }, + [MSYNC_DBG_WARNING] = { + color = Color(255, 160, 17), + prefix = "[MSync_WARN]" + }, + [MSYNC_DBG_INFO] = { + color = Color(255, 255, 255), + prefix = "[MSync_INFO]" + }, + [MSYNC_DBG_DEBUG] = { + color = Color(180, 180, 180), + prefix = "[MSync_DEGUB]" + } +} + +--[[ + MSync debug level convar +]] +MSync.DebugCVar = CreateConVar( "msync_debug", 0, FCVAR_REPLICATED+FCVAR_ARCHIVE, "Set the MSync debug level. 0 = Error, 1 = Warning, 2 = Info, 3 = Debug", 0, 3 ) + +--[[ + Description: MSync logging function, allowing log levels and formated console logs + Arguments: + - logLevel [number] - The log level this message requires to be sent. Valid Globals are: MSYNC_DBG_ERROR, MSYNC_DBG_WARNING, MSYNC_DBG_INFO and MSYNC_DBG_DEBUG + - logMessage [string] - The log message to be sent into the console ( should Prefix the module like "[MBSync]...") + Returns: + - Nothing +]] +MSync.log = function(logLevel, logMessage) + if not type(logLevel) == "number" then return end + + local DebugCvarValue = MSync.DebugCVar:GetInt() + + if DebugCvarValue >= logLevel then + if type(logMessage) == "function" then + logMessage = logMessage() + end + if debugLevels[logLevel] then + MsgC(debugLevels[logLevel].color, debugLevels[logLevel].prefix.." "..logMessage.."\n") + else + MsgC(Color(255, 255, 255), "[MSync_LOG] "..logMessage.."\n") + end + -- Feature(?): Log files? Client GUI log viewer? + --file.Append( "msync/logs/msync_"..os.date("[]")..".log", os.date("[]") ) + end +end + +--[[ + Description: MSync string format function, allows easy and fast formating of long strings + Arguments: + - str [string] - The string containing the variables formated like $ + - tbl [table] - The table containing the variable name as key and the value as key value ( e.g. {["variable"] = "My first variable"}) + Returns: + - formatedString [string] - the string with all variables replaced +]] +MSync.formatString = function(str, tbl) + return function() + for k,v in pairs(tbl) do + str = string.Replace(str, "$"..k, tostring(v)) + end + return str + end +end \ No newline at end of file