Skip to content

Commit

Permalink
refactor: improve /unban command with security and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
omarcopires committed Nov 11, 2024
1 parent 555c233 commit 5a6a851
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
40 changes: 35 additions & 5 deletions data/scripts/talkactions/ban.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local talk = TalkAction("/ban")
local ban = TalkAction("/ban")

function talk.onSay(player, words, param)
function ban.onSay(player, words, param)
if not player:getGroup():getAccess() or player:getAccountType() < ACCOUNT_TYPE_GAMEMASTER then
return true
end
Expand Down Expand Up @@ -45,6 +45,36 @@ function talk.onSay(player, words, param)
return true
end

talk:separator(" ")
talk:groupType("gamemaster")
talk:register()
ban:separator(" ")
ban:register()

local unban = TalkAction("/unban")

function unban.onSay(player, words, param)
if not player:getGroup():getAccess() then
return true
end

if param == "" then
player:sendTextMessage(MESSAGE_STATUS_WARNING, "Command requires 1 parameter: /unban <player name>")
return true
end

local resultId = db.storeQuery("SELECT `account_id`, `lastip` FROM `players` WHERE `name` = " .. db.escapeString(param))
if not resultId then
return true
end

local accountId = result.getNumber(resultId, "account_id")
local lastIp = result.getString(resultId, "lastip")
result.free(resultId)

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.")
return true
end

unban:separator(" ")
unban:register()
16 changes: 0 additions & 16 deletions data/talkactions/scripts/unban.lua

This file was deleted.

1 change: 0 additions & 1 deletion data/talkactions/talkactions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<!-- commands -->
<talkaction words="/attr" separator=" " script="attributes.lua" />
<talkaction words="/ipban" separator=" " script="ip_ban.lua" />
<talkaction words="/unban" separator=" " script="unban.lua" />
<talkaction words="/up" script="up.lua" />
<talkaction words="/down" script="down.lua" />
<talkaction words="/c" separator=" " script="teleport_creature_here.lua" />
Expand Down

0 comments on commit 5a6a851

Please sign in to comment.