From 29f64dd9283d1179242261b41a02ba64aeb9d8e0 Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Tue, 28 Aug 2018 19:33:20 +0200 Subject: [PATCH] Use proper deferarals, endless improvements - 4 tab indents - Commands reworked - now supports source 0 (console) - toLower, and duplicate checks in db - Fixed error messages - added seperate error message when `WhiteList` table is empty - fixed steam id check --- README.md | 43 ++++++++++++++++---- __resource.lua | 19 +++++---- esx_whitelist.sql | 5 ++- locales/en.lua | 4 +- locales/fr.lua | 2 + locales/sv.lua | 8 ++++ server/commands.lua | 55 ++++++++++++++++--------- server/main.lua | 99 ++++++++++++++++++++++++++++----------------- 8 files changed, 161 insertions(+), 74 deletions(-) create mode 100644 locales/sv.lua diff --git a/README.md b/README.md index af36ac10..a1c2d105 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,38 @@ -# ESX Whitelist +# esx_whitelist + +## Download & Installation + +### Using [fvm](https://github.com/qlaffont/fvm-installer) +``` +fvm install --save --folder=esx esx-org/esx_whitelist +``` + +### Using Git +``` +cd resources +git clone https://github.com/ESX-Org/esx_whitelist [esx]/esx_whitelist +``` + +### Manually +- Download https://github.com/ESX-Org/esx_whitelist/archive/master.zip +- Put it in the `[esx]` directory ## Installation +- Import `esx_whitelist.sql` to your database +- Add this in your `server.cfg`: -1. CD in your resources/[esx] folder. -2. Clone the repository. - ```bash - git clone https://github.com/FXServer-ESX/fxserver-esx_whitelist.git esx_whitelist - ``` -3. Import esx_whitelist.sql in your database. -4. Add `start esx_whitelist` in your server.cfg. +``` +start esx_whitelist +``` + +# Legal +### License +esx_whitelist - Whitelist script + +Copyright (C) 2015-2018 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. + +This program Is distributed In the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty Of MERCHANTABILITY Or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License For more details. + +You should have received a copy Of the GNU General Public License along with this program. If Not, see http://www.gnu.org/licenses/. \ No newline at end of file diff --git a/__resource.lua b/__resource.lua index b0fc096a..35ea7766 100644 --- a/__resource.lua +++ b/__resource.lua @@ -1,13 +1,16 @@ +resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937' + description 'ESX Whitelist' -version '1.0.2' +version '1.1.0' server_scripts { - '@mysql-async/lib/MySQL.lua', - '@es_extended/locale.lua', - 'config.lua', - 'locales/en.lua', - 'locales/fr.lua', - 'server/main.lua', - 'server/commands.lua', + '@mysql-async/lib/MySQL.lua', + '@es_extended/locale.lua', + 'config.lua', + 'locales/en.lua', + 'locales/fr.lua', + 'locales/sv.lua', + 'server/main.lua', + 'server/commands.lua' } diff --git a/esx_whitelist.sql b/esx_whitelist.sql index f0cf5821..2798b56a 100644 --- a/esx_whitelist.sql +++ b/esx_whitelist.sql @@ -1,6 +1,7 @@ USE `essentialmode`; CREATE TABLE `whitelist` ( - `identifier` varchar(60) NOT NULL, - PRIMARY KEY (`identifier`) + `identifier` varchar(60) NOT NULL, + + PRIMARY KEY (`identifier`) ); diff --git a/locales/en.lua b/locales/en.lua index b4795312..ad20b913 100644 --- a/locales/en.lua +++ b/locales/en.lua @@ -1,6 +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'] = 'there seems to be a problem with your SteamID', + ['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!', ['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 27673b68..59a3f71e 100644 --- a/locales/fr.lua +++ b/locales/fr.lua @@ -1,6 +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!', ['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 new file mode 100644 index 00000000..918f1461 --- /dev/null +++ b/locales/sv.lua @@ -0,0 +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!', + ['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 11daa53b..c05873bb 100644 --- a/server/commands.lua +++ b/server/commands.lua @@ -1,23 +1,42 @@ -function displayPermissionIssue () - TriggerClientEvent('chatMessage', source, 'SYSTEM', { 255, 0, 0 }, 'Insufficienct permissions!') -end - -TriggerEvent('es:addGroupCommand', 'whitelist:load', 'admin', function (source, args, user) - loadWhiteList() +TriggerEvent('es:addGroupCommand', 'wlrefresh', 'admin', function (source, args, user) + loadWhiteList(function() + TriggerEvent('esx_whitelist:sendMessage', source, 'Whitelist', 'Whitelist reloaded') + end) end, function (source, args, user) - displayPermissionIssue(source) + TriggerClientEvent('chat:addMessage', source, { args = { '^1SYSTEM', 'Insufficienct permissions!' } }) end, { help = _U('help_whitelist_load') }) -TriggerEvent('es:addGroupCommand', 'whitelist:add', 'admin', function (source, args, user) - local steamID = 'steam:' .. args[1] +TriggerEvent('es:addGroupCommand', 'wladd', 'admin', function (source, args, user) + local steamID = 'steam:' .. args[1]:lower() - MySQL.Async.execute( - 'INSERT INTO whitelist (identifier) VALUES (@identifier)', - { ['@identifier'] = tostring(steamID) }, - function () - loadWhiteList() - end - ) + 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!') + 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!') + end) + end + end) end, function (source, args, user) - displayPermissionIssue(source) -end, { help = _U('help_whitelist_add'), params = { steam = 'SteamID', help = 'SteamID formated to hex' }}) + 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) + end +end) \ No newline at end of file diff --git a/server/main.lua b/server/main.lua index 1f36f6df..247966aa 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,45 +1,70 @@ -WhiteList = {} +WhiteList = {} +local hasSqlRun = false -function loadWhiteList () - MySQL.Async.fetchAll( - 'SELECT * FROM whitelist', - {}, - function (identifiers) - Whitelist = {} +function loadWhiteList(cb) + Whitelist = {} - for i=1, #identifiers, 1 do - table.insert(WhiteList, tostring(identifiers[i].identifier)) - end - end - ) + MySQL.Async.fetchAll('SELECT * FROM whitelist', {}, function (identifiers) + for i=1, #identifiers, 1 do + table.insert(WhiteList, tostring(identifiers[i].identifier):lower()) + end + + hasSqlRun = true + + if cb ~= nil then + cb() + end + end) end -MySQL.ready(function () - loadWhiteList() +AddEventHandler('onMySQLReady', function() + loadWhiteList() end) -AddEventHandler('playerConnecting', function (playerName, setKickReason) - if (WhiteList == {}) then - Citizen.Wait(1000) - end +Citizen.CreateThread(function() + Citizen.Wait(10000) - local whitelisted = false - local steamID = GetPlayerIdentifiers(source)[1] or false - - if steamID == false then - setKickReason(_U('steamid_error')) - CancelEvent() - end - - for i = 1, #WhiteList, 1 do - if (tostring(WhiteList[i]) == tostring(steamID)) then - whitelisted = true - break - end - end - - if whitelisted == false then - setKickReason(_U('not_whitelisted')) - CancelEvent() - end + if not hasSqlRun then + loadWhiteList() + end end) + +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 + + -- Letting the user know what's going on. + deferrals.update(_U('whitelist_check')) + + -- 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 + end + + if not whitelisted then + kickReason = _U('not_whitelisted') + end + + end + + if whitelisted then + deferrals.done() + else + deferrals.done(kickReason) + end +end) \ No newline at end of file