added manager plugin

This commit is contained in:
Akamaru 2015-06-15 23:04:20 +02:00
parent c817c774b1
commit f228fea149
2 changed files with 212 additions and 61 deletions

View File

@ -1,61 +0,0 @@
-- Kick an user from the chat group.
-- Use !kick name User_name or !kick id id_number
-- The User_name is the print_name (there are no spaces but _)
--~ TODO:
--~ - !kick all
--~ Get a list of users and kick them all (Useful for delete groups)
--~ - !kick new // !protect
--~ Kick each new user who entered in the group
--~ - !ban user#id
--~ Blacklist an user to enter a group
do
function ban(usr,chat)
print ("Trying to kick: "..usr.." to "..chat)
local success = chat_del_user (chat, usr, ok_cb, false)
if not success then
return "An error happened"
else
local kicked = "Kicked user: "..usr.." from "..chat
return kicked
end
end
function run(msg, matches)
chat_ = 'chat#id'..msg.to.id
-- The message must come from a chat group OR
if msg.to.type ~= 'chat' then
return 'This isn\'t a chat group!'
end
-- User submitted a user name
if matches[1] == "name" then
user_ = matches[2]
user_ = string.gsub(user_," ","_")
ban(user_,chat_)
-- User submitted an id
elseif matches[1] == "id" then
for i=2,#matches do
user_ = 'user#id'..matches[i]
ban(user_,chat_)
end
end
user_ = nil
chat_ = nil
end
return {
description = "Ban an user from the chat group. Credits: @Rutrus",
usage = {
"/kick name [user_name]",
"/kick id [user_id]+" },
patterns = {
"^/kick (name) (.*)",
"^/kick (id) (%d+)"
},
run = run,
privileged = true
}
end

212
plugins/manager.lua Normal file
View File

@ -0,0 +1,212 @@
local function is_user_whitelisted(id)
local hash = 'whitelist:user#id'..id
local white = redis:get(hash) or false
return white
end
local function is_chat_whitelisted(id)
local hash = 'whitelist:chat#id'..id
local white = redis:get(hash) or false
return white
end
local function kick_user(user_id, chat_id)
local chat = 'chat#id'..chat_id
local user = 'user#id'..user_id
chat_del_user(chat, user, ok_cb, true)
end
local function ban_user(user_id, chat_id)
-- Save to redis
local hash = 'banned:'..chat_id..':'..user_id
redis:set(hash, true)
-- Kick from chat
kick_user(user_id, chat_id)
end
local function is_banned(user_id, chat_id)
local hash = 'banned:'..chat_id..':'..user_id
local banned = redis:get(hash)
return banned or false
end
local function pre_process(msg)
-- SERVICE MESSAGE
if msg.action and msg.action.type then
local action = msg.action.type
-- Check if banned user joins chat
if action == 'chat_add_user' or action == 'chat_add_user_link' then
local user_id
if msg.action.link_issuer then
user_id = msg.from.id
else
user_id = msg.action.user.id
end
print('Checking invited user '..user_id)
local banned = is_banned(user_id, msg.to.id)
if banned then
print('User wurde gebannt!')
kick_user(user_id, msg.to.id)
end
end
-- No further checks
return msg
end
-- BANNED USER TALKING
if msg.to.type == 'chat' then
local user_id = msg.from.id
local chat_id = msg.to.id
local banned = is_banned(user_id, chat_id)
if banned then
print('Banned user talking!')
ban_user(user_id, chat_id)
msg.text = ''
end
end
-- WHITELIST
local hash = 'whitelist:enabled'
local whitelist = redis:get(hash)
local issudo = is_sudo(msg)
-- 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 = is_user_whitelisted(msg.from.id)
if not allowed then
print('User '..msg.from.id..' not whitelisted')
if msg.to.type == 'chat' then
allowed = is_chat_whitelisted(msg.to.id)
if not allowed then
print ('Chat '..msg.to.id..' not whitelisted')
else
print ('Chat '..msg.to.id..' whitelisted :)')
end
end
else
print('User '..msg.from.id..' allowed :)')
end
if not allowed then
msg.text = ''
end
else
print('Whitelist not enabled or is sudo')
end
return msg
end
local function run(msg, matches)
-- Silent ignore
if not is_sudo(msg) then
return nil
end
if matches[1] == 'ban' then
local user_id = matches[3]
local chat_id = msg.to.id
if msg.to.type == 'chat' then
if matches[2] == 'user' then
ban_user(user_id, chat_id)
return 'User '..user_id..' banned'
end
if matches[2] == 'delete' then
local hash = 'banned:'..chat_id..':'..user_id
redis:del(hash)
return 'User '..user_id..' unbanned'
end
else
return 'This isn\'t a chat group'
end
end
if matches[1] == 'kick' then
if msg.to.type == 'chat' then
kick_user(matches[2], msg.to.id)
else
return 'This isn\'t a chat group'
end
end
if matches[1] == 'whitelist' then
if matches[2] == 'enable' then
local hash = 'whitelist:enabled'
redis:set(hash, true)
return 'Enabled whitelist'
end
if matches[2] == 'disable' then
local hash = 'whitelist:enabled'
redis:del(hash)
return 'Disabled whitelist'
end
if matches[2] == 'user' then
local hash = 'whitelist:user#id'..matches[3]
redis:set(hash, true)
return 'User "'..matches[3]..'" wurde freigeschalten'
end
if matches[2] == 'chat' then
if msg.to.type ~= 'chat' then
return 'Das hier ist kein Chat!'
end
local hash = 'whitelist:chat#id'..msg.to.id
redis:set(hash, true)
return 'Chat "'..msg.to.id..'" wurde freigeschalten'
end
if matches[2] == 'delete' and matches[3] == 'user' then
local hash = 'whitelist:user#id'..matches[4]
redis:del(hash)
return 'User "'..matches[4]..'" removed from whitelist'
end
if matches[2] == 'delete' and matches[3] == 'chat' then
if msg.to.type ~= 'chat' then
return 'This isn\'t a chat group'
end
local hash = 'whitelist:chat#id'..msg.to.id
redis:del(hash)
return 'Chat '..msg.to.id..' removed from whitelist'
end
end
end
return {
description = "Plugin to manage bans, kicks and white/black lists.",
usage = {
"/whitelist <enable>/<disable>: Enable or disable whitelist mode",
"/whitelist user <user_id>: Allow user to use the bot when whitelist mode is enabled",
"/whitelist chat: Allow everybody on current chat to use the bot when whitelist mode is enabled",
"/whitelist delete user <user_id>: Remove user from whitelist",
"/whitelist delete chat: Remove chat from whitelist",
"/ban user <user_id>: Kick user from chat and kicks it if joins chat again",
"/ban delete <user_id>: Unban user",
"/kick <user_id> Kick user from chat group"
},
patterns = {
"^/(whitelist) (enable)$",
"^/(whitelist) (disable)$",
"^/(whitelist) (user) (%d+)$",
"^/(whitelist) (chat)$",
"^/(whitelist) (delete) (user) (%d+)$",
"^/(whitelist) (delete) (chat)$",
"^/(ban) (user) (%d+)$",
"^/(ban) (delete) (%d+)$",
"^/(kick) (%d+)$",
"^//tgservice (.+)$",
},
run = run,
pre_process = pre_process,
privileged = true
}