Page MenuHomePhabricator (Chris)

unfban.lua
No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None

unfban.lua

--[[
mattata v2.0 - Federation: unfban
Unbans a user from the federation and all its chats.
Only the federation owner or a federation admin can unfban.
]]
local tools = require('telegram-bot-lua.tools')
local permissions = require('src.core.permissions')
local plugin = {}
plugin.name = 'unfban'
plugin.category = 'admin'
plugin.description = 'Unban a user from the federation.'
plugin.commands = { 'unfban' }
plugin.help = '/unfban [user] - Unban a user from all chats in this federation.'
plugin.group_only = true
plugin.admin_only = false
local function resolve_user(message, ctx)
if message.reply and message.reply.from then
return message.reply.from.id, message.reply.from.first_name
end
if message.args and message.args ~= '' then
local input = message.args:match('^(%S+)')
if tonumber(input) then
return tonumber(input), input
end
local username = input:gsub('^@', ''):lower()
local user_id = ctx.redis.get('username:' .. username)
if user_id then
return tonumber(user_id), '@' .. username
end
end
return nil, nil
end
local function get_chat_federation(db, chat_id)
local result = db.execute(
'SELECT f.id, f.name, f.owner_id FROM federations f JOIN federation_chats fc ON f.id = fc.federation_id WHERE fc.chat_id = $1',
{ chat_id }
)
if result and #result > 0 then return result[1] end
return nil
end
local function is_fed_admin(db, fed_id, user_id)
local result = db.execute(
'SELECT 1 FROM federation_admins WHERE federation_id = $1 AND user_id = $2',
{ fed_id, user_id }
)
return result and #result > 0
end
function plugin.on_message(api, message, ctx)
if message.chat.type ~= 'private' and not permissions.can_restrict(api, message.chat.id) then
return api.send_message(message.chat.id, 'I need the "Ban Users" admin permission to enforce federation unbans.')
end
local fed = get_chat_federation(ctx.db, message.chat.id)
if not fed then
return api.send_message(
message.chat.id,
'This chat is not part of any federation.',
'html'
)
end
local from_id = message.from.id
if fed.owner_id ~= from_id and not is_fed_admin(ctx.db, fed.id, from_id) then
return api.send_message(
message.chat.id,
'Only the federation owner or a federation admin can use this command.',
'html'
)
end
local target_id, target_name = resolve_user(message, ctx)
if not target_id then
return api.send_message(
message.chat.id,
'Please specify a user to unban by replying to their message or providing a user ID/username.\nUsage: <code>/unfban [user]</code>',
'html'
)
end
-- Check if the user is actually banned
local ban = ctx.db.execute(
'SELECT 1 FROM federation_bans WHERE federation_id = $1 AND user_id = $2',
{ fed.id, target_id }
)
if not ban or #ban == 0 then
return api.send_message(
message.chat.id,
string.format(
'<b>%s</b> (<code>%s</code>) is not banned in this federation.',
tools.escape_html(target_name),
target_id
),
'html'
)
end
-- Remove the ban record
ctx.db.execute(
'DELETE FROM federation_bans WHERE federation_id = $1 AND user_id = $2',
{ fed.id, target_id }
)
-- Invalidate Redis cache
ctx.redis.del(string.format('fban:%s:%s', fed.id, target_id))
-- Unban in all federation chats
local chats = ctx.db.execute(
'SELECT chat_id FROM federation_chats WHERE federation_id = $1',
{ fed.id }
)
local success_count = 0
local fail_count = 0
if chats then
for _, chat in ipairs(chats) do
local ok = api.unban_chat_member(chat.chat_id, target_id)
if ok then
success_count = success_count + 1
else
fail_count = fail_count + 1
end
end
end
local output = string.format(
'<b>Federation Unban</b>\nFederation: <b>%s</b>\nUser: <b>%s</b> (<code>%s</code>)\nUnbanned by: %s\nUnbanned in %d/%d chats.',
tools.escape_html(fed.name),
tools.escape_html(target_name),
target_id,
tools.escape_html(message.from.first_name),
success_count,
success_count + fail_count
)
return api.send_message(message.chat.id, output, 'html')
end
return plugin

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 15, 1:27 AM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
127221
Default Alt Text
unfban.lua (4 KB)

Event Timeline