From de7ae215e0c25d2de46f1646296e2a11af77ba91 Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 11 Nov 2024 09:51:14 -0300 Subject: [PATCH] refactor: ipban to use string formatting in messages --- data/scripts/talkactions/ban.lua | 71 +++++++++++++++++++++++++---- data/talkactions/scripts/ip_ban.lua | 39 ---------------- data/talkactions/talkactions.xml | 1 - 3 files changed, 62 insertions(+), 49 deletions(-) delete mode 100644 data/talkactions/scripts/ip_ban.lua diff --git a/data/scripts/talkactions/ban.lua b/data/scripts/talkactions/ban.lua index 39ff4319a6..0b1a6cfd6d 100644 --- a/data/scripts/talkactions/ban.lua +++ b/data/scripts/talkactions/ban.lua @@ -1,7 +1,7 @@ local ban = TalkAction("/ban") function ban.onSay(player, words, param) - if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GAMEMASTER then + if not player:getGroup():getAccess() then return true end @@ -11,7 +11,7 @@ function ban.onSay(player, words, param) return true end - local name = params[1]:trim() + local targetName = params[1]:trim() local banDuration = tonumber(params[2]:trim()) local banReason = params[3]:trim() @@ -20,7 +20,7 @@ function ban.onSay(player, words, param) return true end - local accountId = Game.getPlayerAccountId(name) + local accountId = Game.getPlayerAccountId(targetName) if accountId == 0 then return true end @@ -35,12 +35,12 @@ function ban.onSay(player, words, param) local expirationTime = currentTime + (banDuration * 24 * 60 * 60) db.query(string.format("INSERT INTO `account_bans` (`account_id`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (%d, %s, %d, %d, %d)", accountId, db.escapeString(banReason), currentTime, expirationTime, player:getGuid())) - local target = Player(name) - if target then - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been banned for %d days.", target:getName(), banDuration)) - target:remove() + local targetPlayer = Player(targetName) + if targetPlayer then + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been banned for %d days.", targetPlayer:getName(), banDuration)) + targetPlayer:remove() else - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been banned for %d days.", name, banDuration)) + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been banned for %d days.", targetName, banDuration)) end return true end @@ -48,6 +48,59 @@ end ban:separator(" ") ban:register() +local ipban = TalkAction("/ipban") + +function ipban.onSay(player, words, param) + if not player:getGroup():getAccess() then + return true + end + + local params = param:split(",") + if #params < 3 then + player:sendCancelMessage("Command requires 3 parameters: /ipban , , ") + return true + end + + local targetName = params[1]:trim() + local ipBanDuration = tonumber(params[2]:trim()) + local ipBanReason = params[3]:trim() + + if not ipBanDuration or ipBanDuration <= 0 then + player:sendCancelMessage("Ban duration must be a positive number.") + return true + end + + local resultId = db.storeQuery("SELECT `name`, `lastip` FROM `players` WHERE `name` = " .. db.escapeString(targetName)) + if not resultId then + return true + end + + local targetIp = result.getString(resultId, "lastip") + result.free(resultId) + + if targetIp == "0" then + player:sendTextMessage(MESSAGE_STATUS_WARNING, string.format("Invalid IP for player %s.", targetName)) + return true + end + + local checkBanQuery = db.storeQuery("SELECT 1 FROM `ip_bans` WHERE `ip` = " .. db.escapeString(targetIp)) + if checkBanQuery then + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s is already IP banned.", targetName)) + result.free(checkBanQuery) + return true + end + + local currentTime = os.time() + local expirationTime = currentTime + (ipBanDuration * 24 * 60 * 60) + db.query(string.format("INSERT INTO `ip_bans` (`ip`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (%s, %s, %d, %d, %d)", db.escapeString(targetIp), db.escapeString(ipBanReason), currentTime, expirationTime, player:getGuid())) + + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been IP banned for %d days.", targetName, ipBanDuration)) + return true +end + +ipban:separator(" ") +ipban:register() + local unban = TalkAction("/unban") function unban.onSay(player, words, param) @@ -72,7 +125,7 @@ function unban.onSay(player, words, param) db.asyncQuery("DELETE FROM `account_bans` WHERE `account_id` = " .. db.escapeString(tostring(accountId))) db.asyncQuery("DELETE FROM `ip_bans` WHERE `ip` = " .. db.escapeString(lastIp)) - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, param .. " has been unbanned.") + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("%s has been unbanned.", param)) return true end diff --git a/data/talkactions/scripts/ip_ban.lua b/data/talkactions/scripts/ip_ban.lua deleted file mode 100644 index 4d920d13a7..0000000000 --- a/data/talkactions/scripts/ip_ban.lua +++ /dev/null @@ -1,39 +0,0 @@ -local ipBanDays = 7 - -function onSay(player, words, param) - if not player:getGroup():getAccess() then - return true - end - - local resultId = db.storeQuery("SELECT `name`, `lastip` FROM `players` WHERE `name` = " .. db.escapeString(param)) - if not resultId then - return false - end - - local targetName = result.getString(resultId, "name") - local targetIp = result.getNumber(resultId, "lastip") - result.free(resultId) - - local targetPlayer = Player(param) - if targetPlayer then - targetIp = targetPlayer:getIp() - targetPlayer:remove() - end - - if targetIp == 0 then - return false - end - - resultId = db.storeQuery("SELECT 1 FROM `ip_bans` WHERE `ip` = " .. targetIp) - if resultId then - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, targetName .. " is already IP banned.") - result.free(resultId) - return false - end - - local timeNow = os.time() - db.query("INSERT INTO `ip_bans` (`ip`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (" .. - targetIp .. ", '', " .. timeNow .. ", " .. timeNow + (ipBanDays * 86400) .. ", " .. player:getGuid() .. ")") - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, targetName .. " has been IP banned.") - return false -end diff --git a/data/talkactions/talkactions.xml b/data/talkactions/talkactions.xml index 4d99b9e221..bad913122b 100644 --- a/data/talkactions/talkactions.xml +++ b/data/talkactions/talkactions.xml @@ -2,7 +2,6 @@ -