- Banhammer und Bann-System stark überararbeitet:

- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
  - /leave hinzugefügt: Bot verlässt die Gruppe
  - Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
  - Geblockte User können keine InlineQuerys mehr benutzen
  - Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
This commit is contained in:
Andreas Bielawski 2016-08-11 14:08:21 +02:00
parent c931404452
commit 30feea5238
3 changed files with 184 additions and 28 deletions

View File

@ -70,6 +70,7 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec
msg.text_lower = msg.text:lower()
end
msg = pre_process_msg(self, msg, config)
if not msg then return end -- deleted by banning
if is_service_msg(msg) then
msg = service_modify_msg(msg)
@ -94,6 +95,41 @@ function bot:on_callback_receive(callback, msg, config) -- whenever a new callba
if not callback.data:find(':') or not callback.data:find('@'..self.info.username..' ') then
return
end
-- Check if user is blocked
local user_id = callback.from.id
local chat_id = msg.chat.id
if redis:get('blocked:'..user_id) then
utilities.answer_callback_query(self, callback, 'Du darfst den Bot nicht nutzen!', true)
return
end
-- Check if user is banned
local banned = redis:get('banned:'..chat_id..':'..user_id)
if banned then
utilities.answer_callback_query(self, callback, 'Du darfst den Bot nicht nutzen!', true)
return
end
-- Check if whitelist is enabled and user/chat is whitelisted
local whitelist = redis:get('whitelist:enabled')
if whitelist and not is_sudo(msg, config) then
local hash = 'whitelist:user#id'..user_id
local allowed = redis:get(hash) or false
if not allowed then
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
local allowed = redis:get('whitelist:chat#id'.. chat_id)
if not allowed then
utilities.answer_callback_query(self, callback, 'Du darfst den Bot nicht nutzen!', true)
return
end
else
utilities.answer_callback_query(self, callback, 'Du darfst den Bot nicht nutzen!', true)
return
end
end
end
callback.data = string.gsub(callback.data, '@'..self.info.username..' ', "")
local called_plugin = callback.data:match('(.*):.*')
local param = callback.data:sub(callback.data:find(':')+1)
@ -116,9 +152,18 @@ function bot:process_inline_query(inline_query, config) -- When an inline query
-- remove comment to enable debugging
-- vardump(inline_query)
-- PLEASE READ: Blocking every single InlineQuery IS NOT POSSIBLE!
-- When the request is cached, the user can still send this query
-- but he WON'T be able to make new requests.
local user_id = inline_query.from.id
if redis:get('blocked:'..user_id) then
utilities.answer_inline_query(self, inline_query, nil, 0, true)
return
end
if not config.enable_inline_for_everyone then
local is_whitelisted = redis:get('whitelist:user#id'..inline_query.from.id)
if not is_whitelisted then utilities.answer_inline_query(self, inline_query) return end
if not is_whitelisted then utilities.answer_inline_query(self, inline_query, nil, 0, true) return end
end
if inline_query.query:match('"') then
@ -129,6 +174,9 @@ function bot:process_inline_query(inline_query, config) -- When an inline query
local plugin = self.plugins[n]
match_inline_plugins(self, inline_query, config, plugin)
end
-- Stop the spinning circle
utilities.answer_inline_query(self, inline_query, nil, 0, true)
end
function bot:run(config)

View File

@ -12,7 +12,17 @@ function banhammer:init(config)
"^/(whitelist) (delete) (chat)$",
"^/(ban) (user) (%d+)$",
"^/(ban) (delete) (%d+)$",
"^/(kick) (%d+)$"
"^/(block) (user) (%d+)$",
"^/(block) (delete) (%d+)$",
"^/(whitelist)$",
"^/(whitelist) (delete)$",
"^/(ban)$",
"^/(ban) (delete)$",
"^/(block)$",
"^/(block) (delete)$",
"^/(kick) (%d+)$",
"^/(kick)$",
"^/(leave)$"
}
banhammer.doc = [[*
]]..config.cmd_pat..[[whitelist* _<enable>_/_<disable>_: Aktiviert/deaktiviert Whitelist
@ -22,10 +32,15 @@ function banhammer:init(config)
*]]..config.cmd_pat..[[whitelist* delete chat: Lösche ganze Gruppe von der Whitelist
*]]..config.cmd_pat..[[ban* user _<user#id>_: Kicke User vom Chat und kicke ihn, wenn er erneut beitritt
*]]..config.cmd_pat..[[ban* delete _<user#id>_: Entbanne User
*]]..config.cmd_pat..[[kick* _<user#id>_: Kicke User aus dem Chat]]
*]]..config.cmd_pat..[[block* user _<user#id>_: Blocke User vom Bot
*]]..config.cmd_pat..[[block* delete _<user#id>_: Entblocke User
*]]..config.cmd_pat..[[kick* _<user#id>_: Kicke User aus dem Chat
*]]..config.cmd_pat..[[leave*: Bot verlässt die Gruppe
Alternativ kann auch auf die Nachricht des Users geantwortet werden, die Befehle sind dnn die obrigen ohne `user` bzw.`delete`.]]
end
function banhammer:kick_user(user_id, chat_id, self, onlykick)
function banhammer:kick_user(user_id, chat_id, self, onlykick, chat_type)
if user_id == tostring(our_id) then
return "Ich werde mich nicht selbst kicken!"
else
@ -33,6 +48,12 @@ function banhammer:kick_user(user_id, chat_id, self, onlykick)
chat_id = chat_id,
user_id = user_id
} )
if chat_type == 'supergroup' then -- directly unban the user if /kick
bindings.request(self, 'unbanChatMember', {
chat_id = chat_id,
user_id = user_id
} )
end
if onlykick then return end
if not request then return 'User gebannt, aber kicken war nicht erfolgreich. Bin ich Administrator oder ist der User hier überhaupt?' end
return 'User '..user_id..' gebannt!'
@ -54,8 +75,8 @@ end
function banhammer:unban_user(user_id, chat_id, self, chat_type)
local hash = 'banned:'..chat_id..':'..user_id
redis:del(hash)
if chat_type == 'supergroup' then -- how can bots be admins anyway?
local request = bindings.request(self, 'unbanChatMember', {
if chat_type == 'supergroup' then
bindings.request(self, 'unbanChatMember', {
chat_id = chat_id,
user_id = user_id
} )
@ -96,76 +117,94 @@ function banhammer:pre_process(msg, self, config)
end
-- BANNED USER TALKING
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
local user_id = msg.from.id
local chat_id = msg.chat.id
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
local banned = banhammer:is_banned(user_id, chat_id)
if banned then
print('Banned user talking!')
banhammer:ban_user(user_id, chat_id, self)
msg.text = ''
return
end
end
-- BLOCKED USER TALKING (block = user can't use bot, but won't be kicked from group)
local hash = 'blocked:'..user_id
local issudo = is_sudo(msg, config)
local blocked = redis:get(hash)
if blocked and not issudo then
print('User '..user_id..' blocked')
return
end
-- WHITELIST
local hash = 'whitelist:enabled'
local whitelist = redis:get(hash)
local issudo = is_sudo(msg, config)
-- Allow all sudo users even if whitelist is allowed
if whitelist and not issudo then
print('Whitelist enabled and not sudo')
-- Check if user or chat is whitelisted
local allowed = banhammer:is_user_whitelisted(msg.from.id)
local has_been_warned = redis:hget('user:'..msg.from.id, 'has_been_warned')
local allowed = banhammer:is_user_whitelisted(user_id)
local has_been_warned = redis:hget('user:'..user_id, 'has_been_warned')
if not allowed then
print('User '..msg.from.id..' not whitelisted')
print('User '..user_id..' not whitelisted')
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
allowed = banhammer:is_chat_whitelisted(msg.chat.id)
allowed = banhammer:is_chat_whitelisted(chat_id)
if not allowed then
print ('Chat '..msg.chat.id..' not whitelisted')
print ('Chat '..chat_id..' not whitelisted')
else
print ('Chat '..msg.chat.id..' whitelisted :)')
print ('Chat '..chat_id..' whitelisted :)')
end
else
if not has_been_warned then
utilities.send_reply(self, msg, "Dies ist ein privater Bot, der erst nach einer Freischaltung benutzt werden kann.\nThis is a private bot, which can only be after an approval.")
redis:hset('user:'..msg.from.id, 'has_been_warned', true)
redis:hset('user:'..user_id, 'has_been_warned', true)
else
print('User has already been warned!')
end
end
else
print('User '..msg.from.id..' allowed :)')
print('User '..user_id..' allowed :)')
end
if not allowed then
msg.text = ''
msg.text_lower = ''
msg.entities = ''
return
end
-- else
-- print('Whitelist not enabled or is sudo')
end
return msg
end
function banhammer:action(msg, config, matches)
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(self, msg, config.errors.sudo)
return
end
if matches[1] == 'leave' then
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
bindings.request(self, 'leaveChat', {
chat_id = msg.chat.id
} )
return
end
end
if matches[1] == 'ban' then
local user_id = matches[3]
local chat_id = msg.chat.id
if not user_id then
if not msg.reply_to_message then
return
end
user_id = msg.reply_to_message.from.id
end
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
if matches[2] == 'user' then
if matches[2] == 'user' or not matches[2] then
local text = banhammer:ban_user(user_id, chat_id, self)
utilities.send_reply(self, msg, text)
return
@ -183,7 +222,14 @@ function banhammer:action(msg, config, matches)
if matches[1] == 'kick' then
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
banhammer:kick_user(matches[2], msg.chat.id, self, true)
local user_id = matches[2]
if not user_id then
if not msg.reply_to_message then
return
end
user_id = msg.reply_to_message.from.id
end
banhammer:kick_user(user_id, msg.chat.id, self, true, msg.chat.type)
return
else
utilities.send_reply(self, msg, 'Das ist keine Chat-Gruppe')
@ -206,6 +252,28 @@ function banhammer:action(msg, config, matches)
return
end
if not matches[2] then
if not msg.reply_to_message then
return
end
local user_id = msg.reply_to_message.from.id
local hash = 'whitelist:user#id'..user_id
redis:set(hash, true)
utilities.send_reply(self, msg, 'User '..user_id..' whitelisted')
return
end
if matches[2] == 'delete' and not matches[3] then
if not msg.reply_to_message then
return
end
local user_id = msg.reply_to_message.from.id
local hash = 'whitelist:user#id'..user_id
redis:del(hash)
utilities.send_reply(self, msg, 'User '..user_id..' von der Whitelist entfernt!')
return
end
if matches[2] == 'user' then
local hash = 'whitelist:user#id'..matches[3]
redis:set(hash, true)
@ -244,6 +312,46 @@ function banhammer:action(msg, config, matches)
end
end
end
if matches[1] == 'block' then
if matches[2] == 'user' and matches[3] then
local hash = 'blocked:'..matches[3]
redis:set(hash, true)
utilities.send_reply(self, msg, 'User '..matches[3]..' darf den Bot nun nicht mehr nutzen.')
return
end
if matches[2] == 'delete' and matches[3] then
local hash = 'blocked:'..matches[3]
redis:del(hash)
utilities.send_reply(self, msg, 'User '..matches[3]..' darf den Bot wieder nutzen.')
return
end
if not matches[2] then
if not msg.reply_to_message then
return
end
local user_id = msg.reply_to_message.from.id
local hash = 'blocked:'..user_id
redis:set(hash, true)
utilities.send_reply(self, msg, 'User '..user_id..' darf den Bot nun nicht mehr nutzen.')
return
end
if matches[2] == 'delete' and not matches[3] then
if not msg.reply_to_message then
return
end
local user_id = msg.reply_to_message.from.id
local hash = 'blocked:'..user_id
redis:del(hash)
utilities.send_reply(self, msg, 'User '..user_id..' darf den Bot wieder nutzen.')
return
end
end
end
return banhammer

View File

@ -199,7 +199,7 @@ function wikipedia:inline_callback(inline_query, config, matches)
end
end
local results = results..']'
utilities.answer_inline_query(self, inline_query, results, 10)
utilities.answer_inline_query(self, inline_query, results, 3600)
end
function wikipedia:action(msg, config, matches)