From c73b222a1e3d04f8f1b87a79e7a6e006190d601c Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Mon, 17 Feb 2020 22:12:33 +0100 Subject: [PATCH] Code cleanup --- README.md | 2 +- esx_whitelist.sql | 2 +- locales/en.lua | 2 +- locales/fr.lua | 2 +- locales/sv.lua | 2 +- server/commands.lua | 48 +++++++++++++++------------------------------ server/main.lua | 48 ++++++++++++++++++++------------------------- 7 files changed, 42 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index a1c2d105..c4acb4fb 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ start esx_whitelist ### License esx_whitelist - Whitelist script -Copyright (C) 2015-2018 Jérémie N'gadi +Copyright (C) 2015-2020 Jérémie N'gadi This program Is free software: you can redistribute it And/Or modify it under the terms Of the GNU General Public License As published by the Free Software Foundation, either version 3 Of the License, Or (at your option) any later version. diff --git a/esx_whitelist.sql b/esx_whitelist.sql index 2798b56a..bcdddb77 100644 --- a/esx_whitelist.sql +++ b/esx_whitelist.sql @@ -1,7 +1,7 @@ USE `essentialmode`; CREATE TABLE `whitelist` ( - `identifier` varchar(60) NOT NULL, + `identifier` varchar(40) NOT NULL, PRIMARY KEY (`identifier`) ); diff --git a/locales/en.lua b/locales/en.lua index ad20b913..8b7443c6 100644 --- a/locales/en.lua +++ b/locales/en.lua @@ -1,8 +1,8 @@ Locales['en'] = { ['whitelist_check'] = 'making sure you\'re whitelisted on this server . . .', ['not_whitelisted'] = 'you are not whitelisted on this server', - ['steamid_error'] = 'your Steam ID was not found, is Steam running?', ['whitelist_empty'] = 'the whitelist hasn\'t been loaded yet, or alternatively no one has been whitelisted!', + ['license_missing'] = 'your license could not be found', ['help_whitelist_add'] = 'add someone to the whitelist', ['help_whitelist_load'] = 'reload the whitelist', } diff --git a/locales/fr.lua b/locales/fr.lua index 59a3f71e..028ba663 100644 --- a/locales/fr.lua +++ b/locales/fr.lua @@ -1,8 +1,8 @@ Locales['fr'] = { ['whitelist_check'] = 'making sure you\'re whitelisted on this server . . .', ['not_whitelisted'] = 'ceci est un serveur avec whitelist', - ['steamid_error'] = 'nous n\'arrivons pas à lire votre SteamID', ['whitelist_empty'] = 'the whitelist hasn\'t been loaded yet, or alternatively no one has been whitelisted!', + ['license_missing'] = 'your license could not be found', ['help_whitelist_add'] = 'ajouter quelqu\'un dans la Whitelist', ['help_whitelist_load'] = 'recharger la Whitelist', } diff --git a/locales/sv.lua b/locales/sv.lua index 918f1461..6270ffb0 100644 --- a/locales/sv.lua +++ b/locales/sv.lua @@ -1,8 +1,8 @@ Locales['sv'] = { ['whitelist_check'] = 'making sure you\'re whitelisted on this server . . .', ['not_whitelisted'] = 'du är inte whitelistad på denna server!', - ['steamid_error'] = 'ditt Steam ID kunde ej hittas, är Steam påslaget?', ['whitelist_empty'] = 'whitelisten har ännu inte laddats in, eller så är ingen whitelitad!', + ['license_missing'] = 'din licens kunde ej hittas', ['help_whitelist_add'] = 'lägg till någon till whitelisten', ['help_whitelist_load'] = 'ladda om whitelist', } diff --git a/server/commands.lua b/server/commands.lua index c05873bb..ef87f319 100644 --- a/server/commands.lua +++ b/server/commands.lua @@ -1,42 +1,26 @@ -TriggerEvent('es:addGroupCommand', 'wlrefresh', 'admin', function (source, args, user) +ESX.RegisterCommand('wlrefresh', 'admin', function(xPlayer, args, showError) loadWhiteList(function() - TriggerEvent('esx_whitelist:sendMessage', source, 'Whitelist', 'Whitelist reloaded') + showError('Whitelist reloaded') end) -end, function (source, args, user) - TriggerClientEvent('chat:addMessage', source, { args = { '^1SYSTEM', 'Insufficienct permissions!' } }) -end, { help = _U('help_whitelist_load') }) +end, true, {help = _U('help_whitelist_load')}) -TriggerEvent('es:addGroupCommand', 'wladd', 'admin', function (source, args, user) - local steamID = 'steam:' .. args[1]:lower() +ESX.RegisterCommand('wladd', 'admin', function(xPlayer, args, showError) + args.license = args.license:lower() - if string.len(steamID) ~= 21 then - TriggerEvent('esx_whitelist:sendMessage', source, '^1SYSTEM', 'Invalid steam ID length!') - return - end - - MySQL.Async.fetchAll('SELECT * FROM whitelist WHERE identifier = @identifier', { - ['@identifier'] = steamID - }, function(result) - if result[1] ~= nil then - TriggerEvent('esx_whitelist:sendMessage', source, '^1SYSTEM', 'The player is already whitelisted on this server!') + if string.len(args.license) == 40 then + if WhiteList[args.license] then + showError('The player is already whitelisted on this server!') else MySQL.Async.execute('INSERT INTO whitelist (identifier) VALUES (@identifier)', { - ['@identifier'] = steamID - }, function (rowsChanged) - table.insert(WhiteList, steamID) - TriggerEvent('esx_whitelist:sendMessage', source, 'Whitelist', 'The player has been whitelisted!') + ['@identifier'] = args.license + }, function(rowsChanged) + WhiteList[args.license] = true + showError('The player has been whitelisted!') end) end - end) -end, function (source, args, user) - TriggerClientEvent('chat:addMessage', source, { args = { '^1SYSTEM', 'Insufficienct permissions!' } }) -end, { help = _U('help_whitelist_add'), params = { steam = 'SteamID', help = 'SteamID formated to hex, begins with 11' }}) - --- console / rcon can also utilize es:command events, but breaks since the source isn't a connected player, ending up in error messages -AddEventHandler('esx_whitelist:sendMessage', function(source, title, message) - if source ~= 0 then - TriggerClientEvent('chat:addMessage', source, { args = { title, message } }) else - print('esx_whitelist: ' .. message) + showError('Invalid steam ID length!') end -end) \ No newline at end of file +end, true, {help = _U('help_whitelist_add'), validate = true, arguments = { + {name = 'license', help = 'the player license', type = 'string'} +}}) diff --git a/server/main.lua b/server/main.lua index 33a6c76c..d239c7c6 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,14 +1,14 @@ -WhiteList = {} +WhiteList = {} function loadWhiteList(cb) Whitelist = {} - MySQL.Async.fetchAll('SELECT * FROM whitelist', {}, function (identifiers) - for i=1, #identifiers, 1 do - table.insert(WhiteList, tostring(identifiers[i].identifier):lower()) + MySQL.Async.fetchAll('SELECT identifier FROM whitelist', {}, function(result) + for k,v in ipairs(result) do + WhiteList[v.identifier] = true end - if cb ~= nil then + if cb then cb() end end) @@ -22,7 +22,7 @@ AddEventHandler('playerConnecting', function(name, setCallback, deferrals) -- Mark this connection as deferred, this is to prevent problems while checking player identifiers. deferrals.defer() - local _source = source + local playerId, kickReason, identifier = source -- Letting the user know what's going on. deferrals.update(_U('whitelist_check')) @@ -30,30 +30,24 @@ AddEventHandler('playerConnecting', function(name, setCallback, deferrals) -- Needed, not sure why. Citizen.Wait(100) - local whitelisted, kickReason, steamID = false, nil, GetPlayerIdentifiers(_source)[1] - - if #WhiteList == 0 then - kickReason = _U('whitelist_empty') - elseif not string.match(steamID, 'steam:1') then - kickReason = _U('steamid_error') - else - - for i = 1, #WhiteList, 1 do - if tostring(WhiteList[i]) == tostring(steamID) then - whitelisted = true - break - end + for k,v in ipairs(GetPlayerIdentifiers(playerId)) do + if string.match(v, 'license:') then + identifier = string.sub(v, 9) + break end - - if not whitelisted then - kickReason = _U('not_whitelisted') - end - end - if whitelisted then - deferrals.done() - else + if ESX.Table.SizeOf(WhiteList) == 0 then + kickReason = _U('whitelist_empty') + elseif not identifier then + kickReason = _U('license_missing') + elseif not WhiteList[identifier] then + kickReason = _U('not_whitelisted') + end + + if kickReason then deferrals.done(kickReason) + else + deferrals.done() end end)