Unverified Commit f9fb8220 authored by Maximilian Grüttemeier's avatar Maximilian Grüttemeier Committed by GitHub
Browse files

Merge pull request #18 from Aperture-Development/MBSync

Bugfixes and Export command
parents 1d074448 7a3da71f
Loading
Loading
Loading
Loading
+10 −41
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ MSync.modules = MSync.modules or {}
 * @package    MySQL Ban Sync
 * @author     Aperture Development
 * @license    root_dir/LICENCE
 * @version    0.0.5
 * @version    0.0.6
]]

--[[
@@ -15,7 +15,7 @@ local info = {
    Name = "MySQL Ban Sync",
    ModuleIdentifier = "MBSync",
    Description = "Synchronise bans across your servers",
    Version = "0.0.5"
    Version = "0.0.6"
}

--[[
@@ -164,14 +164,6 @@ MSync.modules[info.ModuleIdentifier].init = function()
        steamid_textentry:SetPos( 125, 35 )
        steamid_textentry:SetSize( 210, 20 )
        steamid_textentry:SetPlaceholderText( "SteamID/SteamID64" )
        --steamid_textentry:SetUpdateOnType(true)
        --steamid_textentry.OnValueChange = function( pnl, value )
        --    if string.len(value) == 0 then
        --        ban_button:SetDisabled(true)
        --    else
        --        ban_button:SetDisabled(false)
        --    end
        --end

        local length_text = vgui.Create( "DLabel", panel )
        length_text:SetPos( 15, 60 )
@@ -197,9 +189,6 @@ MSync.modules[info.ModuleIdentifier].init = function()
        allservers_dropdown:AddChoice( "True" )
        allservers_dropdown:AddChoice( "False" )
        allservers_dropdown:SetSortItems( false )
        allservers_dropdown.OnSelect = function( self, index, value )
            --
        end

        local reason_text = vgui.Create( "DLabel", panel )
        reason_text:SetPos( 15, 110 )
@@ -221,7 +210,6 @@ MSync.modules[info.ModuleIdentifier].init = function()
        reason_textentry:SetMultiline(true)
        reason_textentry:SetUpdateOnType(true)
        reason_textentry.OnValueChange = function( pnl, value )
            print(value)
            reasonMaxLen_text:SetText(string.len( value ).."/100")

            if string.len( value ) > 100 then
@@ -231,17 +219,6 @@ MSync.modules[info.ModuleIdentifier].init = function()
            end
        end

        --local bantype_dropdown = vgui.Create( "DComboBox", panel )
        --bantype_dropdown:SetPos( 125, 35 )
        --bantype_dropdown:SetSize( 210, 20 )
        --bantype_dropdown:SetValue( "Recently Disconnected" )
        --bantype_dropdown:AddChoice( "Recently Disconnected" )
        --bantype_dropdown:AddChoice( "SteamID" )
        --bantype_dropdown:SetSortItems( false )
        --bantype_dropdown.OnSelect = function( self, index, value )
            --
        --end

        local reasonMaxLen_text = vgui.Create( "DLabel", panel )
        reasonMaxLen_text:SetPos( 15, 205 )
        reasonMaxLen_text:SetColor( Color( 255, 255, 255 ) )
@@ -269,7 +246,6 @@ MSync.modules[info.ModuleIdentifier].init = function()
        ban_button:SetText( "Ban User" )
        ban_button:SetPos( 15, 425 )
        ban_button:SetSize( 320, 30 )
        ban_button:SetDisabled(true)
        ban_button.DoClick = function()
            local banConfirm_panel = vgui.Create( "DFrame" )
            banConfirm_panel:SetSize( 350, 100 )
@@ -289,7 +265,7 @@ MSync.modules[info.ModuleIdentifier].init = function()
            accept_button:SetPos( 15, 70 )
            accept_button:SetSize( 160, 20 )
            accept_button.DoClick = function()
                -- Ban user and close panel
                RunConsoleCommand("msync.MBSync.banSteamID", steamid_textentry:GetValue(), length_textentry:GetValue(), allservers_dropdown:GetValue(), reason_textentry:GetValue())
                panel:Close()
                banConfirm_panel:Close()
            end
@@ -380,7 +356,6 @@ MSync.modules[info.ModuleIdentifier].init = function()
        local nickname_textentry = vgui.Create( "DTextEntry", panel )
        nickname_textentry:SetPos( 125, 35 )
        nickname_textentry:SetSize( 210, 20 )
        --nickname_textentry:SetText( "[ApDev] Rainbow Dash" )
        nickname_textentry:SetDisabled(true)

        local steamid_text = vgui.Create( "DLabel", panel )
@@ -392,7 +367,6 @@ MSync.modules[info.ModuleIdentifier].init = function()
        local steamid_textentry = vgui.Create( "DTextEntry", panel )
        steamid_textentry:SetPos( 125, 60 )
        steamid_textentry:SetSize( 210, 20 )
        --steamid_textentry:SetText( "STEAM_0:0:0" )
        steamid_textentry:SetDisabled(true)

        local steamid64_text = vgui.Create( "DLabel", panel )
@@ -404,17 +378,9 @@ MSync.modules[info.ModuleIdentifier].init = function()
        local steamid64_textentry = vgui.Create( "DTextEntry", panel )
        steamid64_textentry:SetPos( 125, 85 )
        steamid64_textentry:SetSize( 210, 20 )
        --steamid64_textentry:SetText( "7600000000" )
        steamid64_textentry:SetDisabled(true)

        --local adminheader_text = vgui.Create( "DLabel", panel )
        --adminheader_text:SetPos( 15, 110 )
        --adminheader_text:SetColor( Color( 255, 255, 255 ) )
        --adminheader_text:SetText( "Admin" )
        --adminheader_text:SetSize(320, 15)
        --adminheader_text:SetContentAlignment( 5 )

        --[[ (i*30)+1
        --[[
            Info about the banning Admin
        ]]

@@ -1319,7 +1285,7 @@ MSync.modules[info.ModuleIdentifier].net = function()
        end

        net.Start("msync."..info.ModuleIdentifier..".unban")
            net.WriteInt(userid)
            net.WriteFloat(userid)
        net.SendToServer()
    end
    --[[
@@ -1327,7 +1293,10 @@ MSync.modules[info.ModuleIdentifier].net = function()
        Returns: nothing
    ]]
    net.Receive( "msync."..info.ModuleIdentifier..".sendMessage", function( len, ply )
        local type = net.ReadFloat()
        if type == 0 then
            chat.AddText( Color( 237, 135, 26 ), "[MBSync] ", Color( 255, 255, 255), net.ReadString())
        end
    end )

    --[[
@@ -1381,7 +1350,7 @@ MSync.modules[info.ModuleIdentifier].net = function()
    ]]
    net.Receive( "msync."..info.ModuleIdentifier..".recieveData", function( len, ply )
        MSync.modules[info.ModuleIdentifier].explodeTable(MSync.modules[info.ModuleIdentifier].banTable, net.ReadTable())
        print(MSync.modules[info.ModuleIdentifier].temporary['recieved'])

        MSync.modules[info.ModuleIdentifier].temporary["recieved"] = MSync.modules[info.ModuleIdentifier].temporary["recieved"] + 1

        if MSync.modules[info.ModuleIdentifier].temporary["recieved"] == MSync.modules[info.ModuleIdentifier].temporary["count"] then
+134 −26
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ MSync.modules = MSync.modules or {}
 * @package    MySQL Ban Sync
 * @author     Aperture Development
 * @license    root_dir/LICENSE
 * @version    0.0.5
 * @version    0.0.6
]]

--[[
@@ -15,7 +15,7 @@ local info = {
    Name = "MySQL Ban Sync",
    ModuleIdentifier = "MBSync",
    Description = "Synchronise bans across your servers",
    Version = "0.0.5"
    Version = "0.0.6"
}

--[[
@@ -45,7 +45,6 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction )
        );
    ]] ))


    --[[
        Description: Function to ban a player
        Returns: nothing
@@ -60,12 +59,13 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction )
                (SELECT p_group_id FROM tbl_server_grp WHERE group_name=?)
            );
        ]] )
        local timestamp = os.time()
        banUserQ:setString(1, ply:SteamID())
        banUserQ:setString(2, ply:SteamID64())
        banUserQ:setString(3, calling_ply:SteamID())
        banUserQ:setString(4, calling_ply:SteamID64())
        banUserQ:setString(5, reason)
        banUserQ:setNumber(6, os.time())
        banUserQ:setNumber(6, timestamp)
        banUserQ:setNumber(7, length*60)
        if not allserver then
            banUserQ:setString(8, MSync.settings.data.serverGroup)
@@ -76,16 +76,28 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction )
        banUserQ.onSuccess = function( q, data )
            -- Notify the user about the ban and add it to ULib to prevent data loss on Addon Remove
            -- Also, kick the user from the server
            local msgLength
            local msgReason
            local banData = {
                admin = calling_ply:Nick(),
                reason = reason,
                unban = os.time()+(length*60),
                time = os.time()
                unban = timestamp+(length*60),
                time = timestamp
            }
            if length == 0 then
                banData["unban"] = length
                msgLength = "Permanent"
            else
                msgLength = ULib.secondsToStringTime(length)
            end

            if reason == "" then
                msgReason = "(None given)"
            end

            ply:Kick(ULib.getBanMessage( ply:SteamID(), banData))
            ply:Kick("\n"..ULib.getBanMessage( ply:SteamID(), banData))
            MSync.modules[info.ModuleIdentifier].getActiveBans()
            MSync.modules[info.ModuleIdentifier].msg(calling_ply, "Banned "..ply:Nick().." for "..ULib.secondsToStringTime(length).." with reason "..reason)
            MSync.modules[info.ModuleIdentifier].msg(calling_ply, "Banned "..ply:Nick().." for "..msgLength.." with reason "..msgReason)
        end

        banUserQ.onError = function( q, err, sql )
@@ -117,12 +129,13 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction )
                (SELECT p_group_id FROM tbl_server_grp WHERE group_name=?)
            );
        ]] )
        local timestamp = os.time()
        banUserIdQ:setString(1, userid)
        banUserIdQ:setString(2, userid)
        banUserIdQ:setString(3, calling_ply:SteamID())
        banUserIdQ:setString(4, calling_ply:SteamID64())
        banUserIdQ:setString(5, reason)
        banUserIdQ:setNumber(6, os.time())
        banUserIdQ:setNumber(6, timestamp)
        banUserIdQ:setNumber(7, length)
        if not allserver then
            banUserIdQ:setString(8, MSync.settings.data.serverGroup)
@@ -136,15 +149,28 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction )
            local banData = {
                admin = calling_ply:Nick(),
                reason = reason,
                unban = os.time()+(length*60),
                time = os.time()
                unban = timestamp+(length*60),
                time = timestamp
            }
            if length == 0 then
                banData["unban"] = length
                msgLength = "Permanent"
            else
                msgLength = ULib.secondsToStringTime(length)
            end

            if reason == "" then
                msgReason = "(None given)"
            else
                msgReason = reason
            end

            MSync.modules[info.ModuleIdentifier].msg(calling_ply, "Banned "..userid.." for "..msgLength.." with reason "..msgReason)

            if not player.GetBySteamID(userid) then return end

            player.GetBySteamID(userid):Kick(ULib.getBanMessage( userid, banData))
            player.GetBySteamID(userid):Kick("\n"..ULib.getBanMessage( userid, banData))
            MSync.modules[info.ModuleIdentifier].getActiveBans()
            MSync.modules[info.ModuleIdentifier].msg(calling_ply, "Banned "..userid.." for "..ULib.secondsToStringTime(length).." with reason "..reason)
        end

        banUserIdQ.onError = function( q, err, sql )
@@ -219,7 +245,7 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction )
        ]] )
        unBanUserIdQ:setString(1, calling_ply:SteamID())
        unBanUserIdQ:setString(2, calling_ply:SteamID64())
        unBanUserIdQ:setString(3, banId)
        unBanUserIdQ:setNumber(3, banId)

        unBanUserIdQ.onSuccess = function( q, data )
            MSync.modules[info.ModuleIdentifier].getActiveBans()
@@ -488,6 +514,89 @@ MSync.modules[info.ModuleIdentifier].init = function( transaction )
        getActiveBansQ:start()
    end

    --[[
        Description: This function allows us to export our active bans into ULX
        Returns: nothing
    ]]
    MSync.modules[info.ModuleIdentifier].exportBansToULX = function()
        local exportActiveBans = MSync.DBServer:prepare( [[
            SELECT 
                tbl_mbsync.*,
                banned.steamid,
                banned.steamid64,
                banned.nickname AS 'banned.nickname',
                admin.nickname AS 'admin.nickname',
                admin.steamid AS 'admin.steamid'
            FROM `tbl_mbsync`
            LEFT JOIN tbl_users AS banned
                ON tbl_mbsync.user_id = banned.p_user_id
            LEFT JOIN tbl_users AS admin
                ON tbl_mbsync.admin_id = admin.p_user_id
            WHERE
                ban_lifted IS NULL AND
                (
                    (date_unix+length_unix)>? OR
                     length_unix=0
                ) AND
                (
                    server_group=(SELECT p_group_id FROM tbl_server_grp WHERE group_name=?) OR
                    server_group=(SELECT p_group_id FROM tbl_server_grp WHERE group_name='allservers')
                )
        ]] )
        exportActiveBans:setNumber(1, os.time())
        exportActiveBans:setString(8, MSync.settings.data.serverGroup)

        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")
            for k,v in pairs(data) do
                local unban
                if v.length_unix == 0 then
                    unban = 0
                else
                    unban = v.date_unix + v.length_unix
                end

                ULib.bans[ v["steamid"] ] = {
                    admin = v["admin.nickname"],
                    time = v.date_unix,
                    unban = unban,
                    reason = v.reason,
                    name = v["banned.nickname"]
                }
                hook.Call( ULib.HOOK_USER_BANNED, _, v["steamid"], ULib.bans[ v["steamid"] ] )
            end
            ULib.fileWrite( ULib.BANS_FILE, ULib.makeKeyValues( ULib.bans ) )
            print("[MBSync] Export finished")

        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("------------------------------------")
        end

        exportActiveBans:start()
    end
    concommand.Add("msync."..info.ModuleIdentifier..".export", function( ply, cmd, args )
        MSync.modules[info.ModuleIdentifier].exportBansToULX()
    end)

    --[[
        Description: Function to load the MSync settings file
        Returns: true
@@ -671,7 +780,7 @@ MSync.modules[info.ModuleIdentifier].net = function()
    net.Receive("msync."..info.ModuleIdentifier..".unban", function(len, ply)
        if not ply:query("msync."..info.ModuleIdentifier..".unBanID") then return end

        local banid = net.ReadInt()
        local banid = net.ReadFloat()

        --[[
            Error check and fill in of default data
@@ -729,21 +838,20 @@ MSync.modules[info.ModuleIdentifier].net = function()
    ]]
    util.AddNetworkString("msync."..info.ModuleIdentifier..".openBanGUI")
    MSync.modules[info.ModuleIdentifier].openBanGUI = function(ply)
        local tableLength = #MSync.modules[info.ModuleIdentifier].recentDisconnects
        local tableLength = table.Count(MSync.modules[info.ModuleIdentifier].recentDisconnects)
        local disconnectTable = {}

        if not tableLength == 0 then
            for i = 0, 9, 1 do
                if MSync.modules[info.ModuleIdentifier].recentDisconnects[tableLength-i] then
                    disconnectTable[i] = MSync.modules[info.ModuleIdentifier].recentDisconnects[tableLength-i]
                else
                    break
        if tableLength > 0 then
            local runs = 0
            for k,v in pairs(MSync.modules[info.ModuleIdentifier].recentDisconnects) do
                if runs > (tableLength - 10) then
                    disconnectTable[k] = v
                end
                runs = runs + 1
            end
        else
            disconnectTable = {}
        end

        net.Start("msync."..info.ModuleIdentifier..".openBanGUI")
            net.WriteTable(disconnectTable)
        net.Send(ply)
@@ -956,14 +1064,14 @@ MSync.modules[info.ModuleIdentifier].ulx = function()
                Translate ban length and Date into readable values
            ]]

            banData.timestamp = os.date( "%H:%M:%S - %d/%m/%Y", banData.timestamp)

            if banData.length == 0 then
                banData.length = "permanent"
            else
                banData.length = os.date( "%H:%M:%S - %d/%m/%Y", banData.timestamp + banData.length)
            end

            banData.timestamp = os.date( "%H:%M:%S - %d/%m/%Y", banData.timestamp)

            --[[
                Message the ban informations to the asking player
            ]]
@@ -1099,7 +1207,7 @@ MSync.modules[info.ModuleIdentifier].hooks = function()

    hook.Add("PlayerDisconnected", "msync."..info.ModuleIdentifier..".saveDisconnects", function( ply )
        if ply:IsBot() then return end
        local tableLength = #MSync.modules[info.ModuleIdentifier].recentDisconnects
        local tableLength = table.Count(MSync.modules[info.ModuleIdentifier].recentDisconnects)
        local data = {
            name = ply:Name(),
            steamid = ply:SteamID(),