From 91aac0c138d73d76371ef7304be55f0b6beb1c8e Mon Sep 17 00:00:00 2001 From: yago Date: Sun, 12 Apr 2015 21:55:01 +0200 Subject: [PATCH] Disable plugins only on specific chats (current chat) --- bot/bot.lua | 31 ++++++++++++++++--- plugins/plugins.lua | 72 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 92 insertions(+), 11 deletions(-) diff --git a/bot/bot.lua b/bot/bot.lua index a0e5641..f417eea 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -1,6 +1,6 @@ require("./bot/utils") -VERSION = '0.11.0' +VERSION = '0.11.1' -- This function is called when tg receive a msg function on_msg_receive (msg) @@ -59,11 +59,11 @@ end -- Go over enabled plugins patterns. function match_plugins(msg) for name, plugin in pairs(plugins) do - match_plugin(plugin, msg) + match_plugin(plugin, name, msg) end end --- Returns a table whith matches or nil +-- Returns a table with matches or nil function match_pattern(pattern, text) local matches = { string.match(text, pattern) } if next(matches) then @@ -72,7 +72,26 @@ function match_pattern(pattern, text) -- nil end -function match_plugin(plugin, msg) +-- Check if plugin is on _config.disabled_plugin_on_chat table +local function is_plugin_disabled_on_chat(plugin_name, receiver) + local disabled_chats = _config.disabled_plugin_on_chat + -- Table exists and chat has disabled plugins + if disabled_chats and disabled_chats[receiver] then + -- Checks if plugin is disabled on this chat + for disabled_plugin,disabled in pairs(disabled_chats[receiver]) do + print(disabled_plugin) + if disabled_plugin == plugin_name and disabled then + local warning = 'Plugin '..disabled_plugin..' is disabled on this chat' + print(warning) + send_msg(receiver, warning, ok_cb, false) + return true + end + end + end + return false +end + +function match_plugin(plugin, plugin_name, msg) local receiver = get_receiver(msg) -- Go over patterns. If one matches is enought. @@ -80,6 +99,10 @@ function match_plugin(plugin, msg) local matches = match_pattern(pattern, msg.text) if matches then print("msg matches: ", pattern) + + if is_plugin_disabled_on_chat(plugin_name, receiver) then + return nil + end -- Function exists if plugin.run then -- If plugin is for privileged users only diff --git a/plugins/plugins.lua b/plugins/plugins.lua index 4b6a6ac..fc8f931 100644 --- a/plugins/plugins.lua +++ b/plugins/plugins.lua @@ -65,7 +65,7 @@ local function enable_plugin( filename ) end end -local function disable_plugin( name ) +local function disable_plugin( name, chat ) -- Check if plugins exists if not plugin_exists(name) then return 'Plugin '..name..' does not exists' @@ -81,21 +81,76 @@ local function disable_plugin( name ) return reload_plugins(true) end +local function disable_plugin_on_chat(receiver, plugin) + if not plugin_exists(plugin) then + return "Plugin doesn't exists" + end + + if not _config.disabled_plugin_on_chat then + _config.disabled_plugin_on_chat = {} + end + + if not _config.disabled_plugin_on_chat[receiver] then + _config.disabled_plugin_on_chat[receiver] = {} + end + + _config.disabled_plugin_on_chat[receiver][plugin] = true + + save_config() + return 'Plugin '..plugin..' disabled on this chat' +end + +local function reenable_plugin_on_chat(receiver, plugin) + if not _config.disabled_plugin_on_chat then + return 'There aren\'t any disabled plugin.' + end + + if not _config.disabled_plugin_on_chat[receiver] then + return 'There aren\'t any disabled plugin for this chat.' + end + + if not _config.disabled_plugin_on_chat[receiver][plugin] then + return 'This plugin is not disabled' + end + + _config.disabled_plugin_on_chat[receiver][plugin] = false + return 'Plugin '..plugin..' is enabled again' +end + local function run(msg, matches) -- Show the available plugins if matches[1] == '!plugins' then return list_plugins() end + + -- Reenable a plugin for this chat + if matches[1] == 'enable' and matches[3] == 'chat' then + local receiver = get_receiver(msg) + local plugin = matches[2] + print("enable "..plugin..' on this chat') + return reenable_plugin_on_chat(receiver, plugin) + end + -- Enable a plugin if matches[1] == 'enable' then print("enable: "..matches[2]) return enable_plugin(matches[2]) end + + -- Disable a plugin on a chat + if matches[1] == 'disable' and matches[3] == 'chat' then + local plugin = matches[2] + local receiver = get_receiver(msg) + print("disable "..plugin..' on this chat') + return disable_plugin_on_chat(receiver, plugin) + end + -- Disable a plugin if matches[1] == 'disable' then print("disable: "..matches[2]) return disable_plugin(matches[2]) end + -- Reload all the plugins! if matches[1] == 'reload' then return reload_plugins(true) @@ -105,14 +160,17 @@ end return { description = "Plugin to manage other plugins. Enable, disable or reload.", usage = { - "!plugins: list all plugins", - "!plugins enable [plugin]: enable plugin", - "!plugins disable [plugin]: disable plugin", - "!plugins reload: reloads all plugins" }, + "!plugins: list all plugins.", + "!plugins enable [plugin]: enable plugin.", + "!plugins disable [plugin]: disable plugin.", + "!plugins disable [plugin] chat: disable plugin only this chat.", + "!plugins reload: reloads all plugins." }, patterns = { "^!plugins$", - "^!plugins? (enable) (.*)$", - "^!plugins? (disable) (.*)$", + "^!plugins? (enable) ([%w_%.%-]+)$", + "^!plugins? (disable) ([%w_%.%-]+)$", + "^!plugins? (disable) ([%w_%.%-]+) (chat)", + "^!plugins? (enable) ([%w_%.%-]+) (chat)", "^!plugins? (reload)$" }, run = run, privileged = true