2016-07-17 13:22:27 +02:00
|
|
|
local plugin_manager = {}
|
|
|
|
|
|
|
|
local bot = require('miku.bot')
|
|
|
|
|
|
|
|
function plugin_manager:init(config)
|
|
|
|
plugin_manager.triggers = {
|
|
|
|
"^/plugins$",
|
2017-09-12 19:12:06 +02:00
|
|
|
"^/plugins? (enable) ([%w_%.%-]+) (chat) (-?%d+)$",
|
2016-08-15 23:14:28 +02:00
|
|
|
"^/plugins? (enable) ([%w_%.%-]+) (chat)$",
|
2017-09-12 19:12:06 +02:00
|
|
|
"^/plugins? (disable) ([%w_%.%-]+) (chat) (-?%d+)$",
|
2016-08-15 23:14:28 +02:00
|
|
|
"^/plugins? (disable) ([%w_%.%-]+) (chat)$",
|
2016-07-17 13:22:27 +02:00
|
|
|
"^/plugins? (enable) ([%w_%.%-]+)$",
|
|
|
|
"^/plugins? (disable) ([%w_%.%-]+)$",
|
|
|
|
"^/plugins? (reload)$",
|
|
|
|
"^/(reload)$"
|
|
|
|
}
|
|
|
|
plugin_manager.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[plugins*: Listet alle Plugins auf
|
|
|
|
*]]..config.cmd_pat..[[plugins* _enable/disable_ _<Plugin>_: Aktiviert/deaktiviert Plugin
|
|
|
|
*]]..config.cmd_pat..[[plugins* _enable/disable_ _<Plugin>_ chat: Aktiviert/deaktiviert Plugin im aktuellen Chat
|
|
|
|
*]]..config.cmd_pat..[[plugins* _enable/disable_ _<Plugin>_ _<chat#id>_: Aktiviert/deaktiviert Plugin in diesem Chat
|
|
|
|
*]]..config.cmd_pat..[[reload*: Lädt Plugins neu]]
|
|
|
|
end
|
|
|
|
|
|
|
|
plugin_manager.command = 'plugins <nur für Superuser>'
|
|
|
|
|
2016-09-08 01:35:10 +02:00
|
|
|
-- Returns at table of lua files inside plugins
|
|
|
|
function plugin_manager:plugins_names()
|
|
|
|
local files = {}
|
2016-09-11 14:07:55 +02:00
|
|
|
for k, v in pairs(scandir("miku/plugins")) do
|
2016-09-08 01:35:10 +02:00
|
|
|
-- Ends with .lua
|
|
|
|
if (v:match(".lua$")) then
|
|
|
|
files[#files+1] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return files
|
|
|
|
end
|
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
-- Returns the key (index) in the config.enabled_plugins table
|
|
|
|
function plugin_manager:plugin_enabled(name, chat)
|
|
|
|
for k,v in pairs(enabled_plugins) do
|
|
|
|
if name == v then
|
|
|
|
return k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- If not found
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns true if file exists in plugins folder
|
|
|
|
function plugin_manager:plugin_exists(name)
|
2016-09-08 01:35:10 +02:00
|
|
|
for k,v in pairs(plugin_manager:plugins_names()) do
|
2016-07-17 13:22:27 +02:00
|
|
|
if name..'.lua' == v then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function plugin_manager:list_plugins()
|
|
|
|
local text = ''
|
2016-09-08 01:35:10 +02:00
|
|
|
for k, v in pairs(plugin_manager:plugins_names()) do
|
2016-07-17 13:22:27 +02:00
|
|
|
-- ✔ enabled, ❌ disabled
|
|
|
|
local status = '❌'
|
|
|
|
-- Check if is enabled
|
|
|
|
for k2, v2 in pairs(enabled_plugins) do
|
|
|
|
if v == v2..'.lua' then
|
|
|
|
status = '✔'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not only_enabled or status == '✔' then
|
|
|
|
-- get the name
|
|
|
|
v = string.match (v, "(.*)%.lua")
|
|
|
|
text = text..v..' '..status..'\n'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
|
|
|
function plugin_manager:reload_plugins(self, config, plugin_name, status)
|
|
|
|
for pac, _ in pairs(package.loaded) do
|
|
|
|
if pac:match('^miku%.plugins%.') then
|
|
|
|
package.loaded[pac] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
package.loaded['miku.bindings'] = nil
|
|
|
|
package.loaded['miku.utilities'] = nil
|
|
|
|
package.loaded['config'] = nil
|
|
|
|
bot.init(self, config)
|
|
|
|
if plugin_name then
|
|
|
|
return 'Plugin '..plugin_name..' wurde '..status
|
|
|
|
else
|
2016-10-03 21:56:32 +02:00
|
|
|
return 'Plugins neu geladen ✔️'
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function plugin_manager:enable_plugin(self, config, plugin_name)
|
|
|
|
print('checking if '..plugin_name..' exists')
|
|
|
|
-- Check if plugin is enabled
|
|
|
|
if plugin_manager:plugin_enabled(plugin_name) then
|
|
|
|
return 'Plugin '..plugin_name..' ist schon aktiviert'
|
|
|
|
end
|
|
|
|
-- Checks if plugin exists
|
|
|
|
if plugin_manager:plugin_exists(plugin_name) then
|
|
|
|
-- Add to redis set
|
|
|
|
redis:sadd('telegram:enabled_plugins', plugin_name)
|
|
|
|
print(plugin_name..' saved to redis set telegram:enabled_plugins')
|
|
|
|
-- Reload the plugins
|
2016-10-03 21:56:32 +02:00
|
|
|
return plugin_manager:reload_plugins(self, config, plugin_name, 'aktiviert ✔️')
|
2016-07-17 13:22:27 +02:00
|
|
|
else
|
2016-10-03 21:56:32 +02:00
|
|
|
return 'Plugin '..plugin_name..' existiert nicht ❌'
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function plugin_manager:disable_plugin(self, config, name, chat)
|
|
|
|
-- Check if plugins exists
|
|
|
|
if not plugin_manager:plugin_exists(name) then
|
2016-10-03 21:56:32 +02:00
|
|
|
return 'Plugin '..name..' existiert nicht ❌'
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
local k = plugin_manager:plugin_enabled(name)
|
|
|
|
-- Check if plugin is enabled
|
|
|
|
if not k then
|
2016-10-03 21:56:32 +02:00
|
|
|
return 'Plugin '..name..' ist nicht aktiviert ❌'
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
-- Disable and reload
|
|
|
|
redis:srem('telegram:enabled_plugins', name)
|
|
|
|
print(name..' saved to redis set telegram:enabled_plugins')
|
2016-10-03 21:56:32 +02:00
|
|
|
return plugin_manager:reload_plugins(self, config, name, 'deaktiviert ✔️')
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function plugin_manager:disable_plugin_on_chat(msg, plugin)
|
|
|
|
if not plugin_manager:plugin_exists(plugin) then
|
2016-10-03 21:56:32 +02:00
|
|
|
return "Plugin existiert nicht! ❌"
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if not msg.chat then
|
|
|
|
hash = 'chat:'..msg..':disabled_plugins'
|
|
|
|
else
|
|
|
|
hash = get_redis_hash(msg, 'disabled_plugins')
|
|
|
|
end
|
|
|
|
local disabled = redis:hget(hash, plugin)
|
|
|
|
|
|
|
|
if disabled ~= 'true' then
|
|
|
|
print('Setting '..plugin..' in redis hash '..hash..' to true')
|
|
|
|
redis:hset(hash, plugin, true)
|
2016-10-03 21:56:32 +02:00
|
|
|
return 'Plugin '..plugin..' für diesen Chat deaktiviert. ✔️'
|
2016-07-17 13:22:27 +02:00
|
|
|
else
|
2016-10-03 21:56:32 +02:00
|
|
|
return 'Plugin '..plugin..' wurde für diesen Chat bereits deaktiviert. ✔️'
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function plugin_manager:reenable_plugin_on_chat(msg, plugin)
|
|
|
|
if not plugin_manager:plugin_exists(plugin) then
|
2016-10-03 21:56:32 +02:00
|
|
|
return "Plugin existiert nicht! ❌"
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if not msg.chat then
|
|
|
|
hash = 'chat:'..msg..':disabled_plugins'
|
|
|
|
else
|
|
|
|
hash = get_redis_hash(msg, 'disabled_plugins')
|
|
|
|
end
|
|
|
|
local disabled = redis:hget(hash, plugin)
|
|
|
|
|
2016-10-03 21:56:32 +02:00
|
|
|
if disabled == nil then return 'Es gibt keine deaktivierten Plugins für disen Chat. ❌' end
|
2016-07-17 13:22:27 +02:00
|
|
|
|
|
|
|
if disabled == 'true' then
|
|
|
|
print('Setting '..plugin..' in redis hash '..hash..' to false')
|
|
|
|
redis:hset(hash, plugin, false)
|
2016-10-03 21:56:32 +02:00
|
|
|
return 'Plugin '..plugin..' wurde für diesen Chat reaktiviert. ✔️'
|
2016-07-17 13:22:27 +02:00
|
|
|
else
|
2016-10-03 21:56:32 +02:00
|
|
|
return 'Plugin '..plugin..' ist nicht deaktiviert. ✔️'
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function plugin_manager:action(msg, config, matches)
|
2016-09-08 01:35:10 +02:00
|
|
|
if not is_sudo(msg, config) then
|
2016-08-26 17:42:31 +02:00
|
|
|
utilities.send_reply(msg, config.errors.sudo, true)
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Show the available plugins
|
|
|
|
if matches[1] == '/plugins' then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, plugin_manager:list_plugins())
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Reenable a plugin for this chat
|
|
|
|
if matches[1] == 'enable' and matches[3] == 'chat' then
|
|
|
|
local plugin = matches[2]
|
|
|
|
if matches[4] then
|
|
|
|
local id = matches[4]
|
|
|
|
print("enable "..plugin..' on chat#id'..id)
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, plugin_manager:reenable_plugin_on_chat(id, plugin))
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
else
|
|
|
|
print("enable "..plugin..' on this chat')
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, plugin_manager:reenable_plugin_on_chat(msg, plugin))
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Enable a plugin
|
|
|
|
if matches[1] == 'enable' then
|
|
|
|
local plugin_name = matches[2]
|
|
|
|
print("enable: "..matches[2])
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, plugin_manager:enable_plugin(self, config, plugin_name))
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Disable a plugin on a chat
|
|
|
|
if matches[1] == 'disable' and matches[3] == 'chat' then
|
|
|
|
local plugin = matches[2]
|
|
|
|
if matches[4] then
|
|
|
|
local id = matches[4]
|
|
|
|
print("disable "..plugin..' on chat#id'..id)
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, plugin_manager:disable_plugin_on_chat(id, plugin))
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
else
|
|
|
|
print("disable "..plugin..' on this chat')
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, plugin_manager:disable_plugin_on_chat(msg, plugin))
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Disable a plugin
|
|
|
|
if matches[1] == 'disable' then
|
|
|
|
print("disable: "..matches[2])
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, plugin_manager:disable_plugin(self, config, matches[2]))
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Reload all the plugins!
|
|
|
|
if matches[1] == 'reload' then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, plugin_manager:reload_plugins(self, config))
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return plugin_manager
|