From 67f7d7e12fcd5f1b04bdf35dd91a15c844d42fef Mon Sep 17 00:00:00 2001 From: topkecleon Date: Wed, 13 Jul 2016 21:57:23 -0400 Subject: [PATCH] blacklist.lua rewritten to remove toggle and support multiple arguments. The /reload command no longer has stupid, undocumented behavior (though there is now an opt-in for such stupid, undocumented behavior for those of you who like stupid, undocumented behavior). Slight modification to reactions.lua to prevent accidents. New plugin: rmspic.lua. why --- otouto/bot.lua | 2 +- otouto/plugins/blacklist.lua | 112 +++++++++++++++++++++++++++-------- otouto/plugins/control.lua | 8 ++- otouto/plugins/reactions.lua | 11 +++- otouto/plugins/rmspic.lua | 34 +++++++++++ 5 files changed, 135 insertions(+), 32 deletions(-) create mode 100644 otouto/plugins/rmspic.lua diff --git a/otouto/bot.lua b/otouto/bot.lua index ea83a76..6569714 100755 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -95,7 +95,7 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec end for _, plugin in ipairs(self.plugins) do - for _, trigger in pairs(plugin.triggers or {}) do + for _, trigger in ipairs(plugin.triggers or {}) do if string.match(msg.text_lower, trigger) then local success, result = pcall(function() return plugin.action(self, msg, config) diff --git a/otouto/plugins/blacklist.lua b/otouto/plugins/blacklist.lua index 29a852e..33cebb7 100755 --- a/otouto/plugins/blacklist.lua +++ b/otouto/plugins/blacklist.lua @@ -16,36 +16,96 @@ blacklist.triggers = { } function blacklist:action(msg, config) - - if self.database.blacklist[tostring(msg.from.id)] then return end - if self.database.blacklist[tostring(msg.chat.id)] then return end - if not msg.text:match('^'..config.cmd_pat..'blacklist') then return true end - if msg.from.id ~= config.admin then return end - - local target = utilities.user_from_message(self, msg) - if target.err then - utilities.send_reply(self, msg, target.err) + if self.database.blacklist[tostring(msg.from.id)] or self.database.blacklist[tostring(msg.chat.id)] then return end - - if tonumber(target.id) < 0 then - target.name = 'Group' + if not ( + msg.from.id == config.admin + and ( + msg.text:match('^'..config.cmd_pat..'blacklist') + or msg.text:match('^'..config.cmd_pat..'unblacklist') + ) + ) then + return true end - - if self.database.blacklist[tostring(target.id)] then - self.database.blacklist[tostring(target.id)] = nil - utilities.send_reply(self, msg, target.name .. ' has been removed from the blacklist.') - if config.drua_block_on_blacklist then - require('drua-tg').unblock(target.id) - end + local targets = {} + if msg.reply_to_message then + table.insert(targets, { + id_str = tostring(msg.reply_to_message.from.id), + name = utilities.build_name(msg.reply_to_message.from.first_name, msg.reply_to_message.from.last_name) + }) else - self.database.blacklist[tostring(target.id)] = true - utilities.send_reply(self, msg, target.name .. ' has been added to the blacklist.') - if config.drua_block_on_blacklist then - require('drua-tg').block(target.id) + local input = utilities.input(msg.text) + if input then + for _, user in ipairs(utilities.index(input)) do + if self.database.users[user] then + table.insert(targets, { + id = self.database.users[user].id, + id_str = tostring(self.database.users[user].id), + name = utilities.build_name(self.database.users[user].first_name, self.database.users[user].last_name) + }) + elseif tonumber(user) then + local t = { + id_str = user, + id = tonumber(user) + } + if tonumber(user) < 0 then + t.name = 'Group (' .. user .. ')' + else + t.name = 'Unknown (' .. user .. ')' + end + table.insert(targets, t) + elseif user:match('^@') then + local u = utilities.resolve_username(self, user) + if u then + table.insert(targets, { + id = u.id, + id_str = tostring(u.id), + name = utilities.build_name(u.first_name, u.last_name) + }) + else + table.insert(targets, { err = 'Sorry, I do not recognize that username ('..user..').' }) + end + else + table.insert(targets, { err = 'Invalid username or ID ('..user..').' }) + end + end + else + utilities.send_reply(self, msg, 'Please specify a user or users via reply, username, or ID, or a group or groups via ID.') + return end end + local output = '' + if msg.text:match('^'..config.cmd_pat..'blacklist') then + for _, target in ipairs(targets) do + if target.err then + output = output .. target.err .. '\n' + elseif self.database.blacklist[target.id_str] then + output = output .. target.name .. ' is already blacklisted.\n' + else + self.database.blacklist[target.id_str] = true + output = output .. target.name .. ' is now blacklisted.\n' + if config.drua_block_on_blacklist and target.id > 0 then + require('drua-tg').block(target.id) + end + end + end + elseif msg.text:match('^'..config.cmd_pat..'unblacklist') then + for _, target in ipairs(targets) do + if target.err then + output = output .. target.err .. '\n' + elseif not self.database.blacklist[target.id_str] then + output = output .. target.name .. ' is not blacklisted.\n' + else + self.database.blacklist[target.id_str] = nil + output = output .. target.name .. ' is no longer blacklisted.\n' + if config.drua_block_on_blacklist and target.id > 0 then + require('drua-tg').unblock(target.id) + end + end + end + end + utilities.send_reply(self, msg, output) +end - end - - return blacklist +return blacklist diff --git a/otouto/plugins/control.lua b/otouto/plugins/control.lua index a9bbaaf..1599037 100644 --- a/otouto/plugins/control.lua +++ b/otouto/plugins/control.lua @@ -28,9 +28,11 @@ function control:action(msg, config) package.loaded['otouto.bindings'] = nil package.loaded['otouto.utilities'] = nil package.loaded['config'] = nil - if msg.text_lower:match('%+config') then for k, v in pairs(require('config')) do - config[k] = v - end end + if not msg.text_lower:match('%-config') then + for k, v in pairs(require('config')) do + config[k] = v + end + end bot.init(self, config) utilities.send_reply(self, msg, 'Bot reloaded!') elseif msg.text_lower:match('^'..cmd_pat..'halt') then diff --git a/otouto/plugins/reactions.lua b/otouto/plugins/reactions.lua index e594d50..d75a3c3 100755 --- a/otouto/plugins/reactions.lua +++ b/otouto/plugins/reactions.lua @@ -29,10 +29,17 @@ function reactions:init(config) -- Generate a "help" message triggered by "/reactions". help = 'Reactions:\n' reactions.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('reactions').table + local username = self.info.username:lower() for trigger,reaction in pairs(mapping) do help = help .. '• ' .. config.cmd_pat .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n' - table.insert(reactions.triggers, config.cmd_pat..trigger) - table.insert(reactions.triggers, config.cmd_pat..trigger..'@'..self.info.username:lower()) + table.insert(reactions.triggers, '^'..config.cmd_pat..trigger) + table.insert(reactions.triggers, '^'..config.cmd_pat..trigger..'@'..username) + table.insert(reactions.triggers, config.cmd_pat..trigger..'$') + table.insert(reactions.triggers, config.cmd_pat..trigger..'@'..username..'$') + table.insert(reactions.triggers, '\n'..config.cmd_pat..trigger) + table.insert(reactions.triggers, '\n'..config.cmd_pat..trigger..'@'..username) + table.insert(reactions.triggers, config.cmd_pat..trigger..'\n') + table.insert(reactions.triggers, config.cmd_pat..trigger..'@'..username..'\n') end end diff --git a/otouto/plugins/rmspic.lua b/otouto/plugins/rmspic.lua new file mode 100644 index 0000000..40cf66f --- /dev/null +++ b/otouto/plugins/rmspic.lua @@ -0,0 +1,34 @@ +local https = require('ssl.https') +local utilities = require('otouto.utilities') +local bindings = require('otouto.bindings') + +local rms = {} + +function rms:init(config) + rms.BASE_URL = 'https://rms.sexy/img/' + rms.LIST = {} + local s, r = https.request(rms.BASE_URL) + if r ~= 200 then + print('Error connecting to rms.sexy.\nrmspic.lua will not be enabled.') + return + end + for link in s:gmatch('(.-)') do + table.insert(rms.LIST, link) + end + rms.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('rms').table +end + +function rms:action(msg, config) + bindings.sendChatAction(self, { chat_id = msg.chat.id, action = 'upload_photo' }) + local choice = rms.LIST[math.random(#rms.LIST)] + local filename = '/tmp/' .. choice + local image_file = io.open(filename) + if image_file then + image_file:close() + else + utilities.download_file(rms.BASE_URL .. choice, filename) + end + bindings.sendPhoto(self, { chat_id = msg.chat.id }, { photo = filename }) +end + +return rms