- 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() msg.text_lower = msg.text:lower()
end end
msg = pre_process_msg(self, msg, config) msg = pre_process_msg(self, msg, config)
if not msg then return end -- deleted by banning
if is_service_msg(msg) then if is_service_msg(msg) then
msg = service_modify_msg(msg) 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 if not callback.data:find(':') or not callback.data:find('@'..self.info.username..' ') then
return return
end 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..' ', "") callback.data = string.gsub(callback.data, '@'..self.info.username..' ', "")
local called_plugin = callback.data:match('(.*):.*') local called_plugin = callback.data:match('(.*):.*')
local param = callback.data:sub(callback.data:find(':')+1) 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 -- remove comment to enable debugging
-- vardump(inline_query) -- 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 if not config.enable_inline_for_everyone then
local is_whitelisted = redis:get('whitelist:user#id'..inline_query.from.id) 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 end
if inline_query.query:match('"') then 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] local plugin = self.plugins[n]
match_inline_plugins(self, inline_query, config, plugin) match_inline_plugins(self, inline_query, config, plugin)
end end
-- Stop the spinning circle
utilities.answer_inline_query(self, inline_query, nil, 0, true)
end end
function bot:run(config) function bot:run(config)

View File

@ -12,7 +12,17 @@ function banhammer:init(config)
"^/(whitelist) (delete) (chat)$", "^/(whitelist) (delete) (chat)$",
"^/(ban) (user) (%d+)$", "^/(ban) (user) (%d+)$",
"^/(ban) (delete) (%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 = [[* banhammer.doc = [[*
]]..config.cmd_pat..[[whitelist* _<enable>_/_<disable>_: Aktiviert/deaktiviert Whitelist ]]..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..[[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* 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..[[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 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 if user_id == tostring(our_id) then
return "Ich werde mich nicht selbst kicken!" return "Ich werde mich nicht selbst kicken!"
else else
@ -33,6 +48,12 @@ function banhammer:kick_user(user_id, chat_id, self, onlykick)
chat_id = chat_id, chat_id = chat_id,
user_id = user_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 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 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!' return 'User '..user_id..' gebannt!'
@ -54,8 +75,8 @@ end
function banhammer:unban_user(user_id, chat_id, self, chat_type) function banhammer:unban_user(user_id, chat_id, self, chat_type)
local hash = 'banned:'..chat_id..':'..user_id local hash = 'banned:'..chat_id..':'..user_id
redis:del(hash) redis:del(hash)
if chat_type == 'supergroup' then -- how can bots be admins anyway? if chat_type == 'supergroup' then
local request = bindings.request(self, 'unbanChatMember', { bindings.request(self, 'unbanChatMember', {
chat_id = chat_id, chat_id = chat_id,
user_id = user_id user_id = user_id
} ) } )
@ -96,76 +117,94 @@ function banhammer:pre_process(msg, self, config)
end end
-- BANNED USER TALKING -- BANNED USER TALKING
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
local user_id = msg.from.id local user_id = msg.from.id
local chat_id = msg.chat.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) local banned = banhammer:is_banned(user_id, chat_id)
if banned then if banned then
print('Banned user talking!') print('Banned user talking!')
banhammer:ban_user(user_id, chat_id, self) banhammer:ban_user(user_id, chat_id, self)
msg.text = '' return
end end
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 -- WHITELIST
local hash = 'whitelist:enabled' local hash = 'whitelist:enabled'
local whitelist = redis:get(hash) local whitelist = redis:get(hash)
local issudo = is_sudo(msg, config)
-- Allow all sudo users even if whitelist is allowed -- Allow all sudo users even if whitelist is allowed
if whitelist and not issudo then if whitelist and not issudo then
print('Whitelist enabled and not sudo') print('Whitelist enabled and not sudo')
-- Check if user or chat is whitelisted -- Check if user or chat is whitelisted
local allowed = banhammer:is_user_whitelisted(msg.from.id) local allowed = banhammer:is_user_whitelisted(user_id)
local has_been_warned = redis:hget('user:'..msg.from.id, 'has_been_warned') local has_been_warned = redis:hget('user:'..user_id, 'has_been_warned')
if not allowed then 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 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 if not allowed then
print ('Chat '..msg.chat.id..' not whitelisted') print ('Chat '..chat_id..' not whitelisted')
else else
print ('Chat '..msg.chat.id..' whitelisted :)') print ('Chat '..chat_id..' whitelisted :)')
end end
else else
if not has_been_warned then 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.") 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 else
print('User has already been warned!') print('User has already been warned!')
end end
end end
else else
print('User '..msg.from.id..' allowed :)') print('User '..user_id..' allowed :)')
end end
if not allowed then if not allowed then
msg.text = '' return
msg.text_lower = ''
msg.entities = ''
end end
-- else
-- print('Whitelist not enabled or is sudo')
end end
return msg return msg
end end
function banhammer:action(msg, config, matches) 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) utilities.send_reply(self, msg, config.errors.sudo)
return return
end 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 if matches[1] == 'ban' then
local user_id = matches[3] local user_id = matches[3]
local chat_id = msg.chat.id 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 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) local text = banhammer:ban_user(user_id, chat_id, self)
utilities.send_reply(self, msg, text) utilities.send_reply(self, msg, text)
return return
@ -183,7 +222,14 @@ function banhammer:action(msg, config, matches)
if matches[1] == 'kick' then if matches[1] == 'kick' then
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' 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 return
else else
utilities.send_reply(self, msg, 'Das ist keine Chat-Gruppe') utilities.send_reply(self, msg, 'Das ist keine Chat-Gruppe')
@ -206,6 +252,28 @@ function banhammer:action(msg, config, matches)
return return
end 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 if matches[2] == 'user' then
local hash = 'whitelist:user#id'..matches[3] local hash = 'whitelist:user#id'..matches[3]
redis:set(hash, true) redis:set(hash, true)
@ -244,6 +312,46 @@ function banhammer:action(msg, config, matches)
end end
end 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 end
return banhammer return banhammer

View File

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