Initial test of plugins handler

This commit is contained in:
yago 2014-12-24 01:38:41 +01:00
parent 8ca367e83b
commit d0a728e214
5 changed files with 97 additions and 27 deletions

View File

@ -22,11 +22,6 @@ end
function ok_cb(extra, success, result) function ok_cb(extra, success, result)
end end
-- Callback to remove tmp files
function rmtmp_cb(file_path, success, result)
os.remove(file_path)
end
function msg_valid(msg) function msg_valid(msg)
-- if msg.from.id == our_id then -- if msg.from.id == our_id then
-- return true -- return true
@ -170,16 +165,13 @@ function on_binlog_replay_end ()
-- See plugins/ping.lua as an example for cron -- See plugins/ping.lua as an example for cron
end end
-- load all plugins in the plugins/ directory -- Enable plugins in config.json
function load_plugins() function load_plugins()
for k, v in pairs(scandir("plugins")) do for k, v in pairs(config.enabled_plugins) do
-- Load only lua files
if (v:match(".lua$")) then
print("Loading plugin", v) print("Loading plugin", v)
t = loadfile("plugins/" .. v)() t = loadfile("plugins/" .. v)()
table.insert(plugins, t) table.insert(plugins, t)
end end
end
end end
-- Cron all the enabled plugins -- Cron all the enabled plugins

View File

@ -1,4 +1,5 @@
{ {
"enabled_plugins": [ "plugins.lua", "echo.lua", "hello.lua" ],
"rmtmp_delay": 20, "rmtmp_delay": 20,
"google_api_key": "", "google_api_key": "",
"log_file": "/var/www/html/log.txt", "log_file": "/var/www/html/log.txt",

View File

@ -67,6 +67,11 @@ function download_to_file( url , noremove )
return file_path return file_path
end end
-- Callback to remove a file
function rmtmp_cb(file_path, success, result)
os.remove(file_path)
end
function vardump(value, depth, key) function vardump(value, depth, key)
local linePrefix = "" local linePrefix = ""
local spaces = "" local spaces = ""
@ -133,6 +138,7 @@ function is_sudo(msg)
return var return var
end end
-- Returns the name of the sender
function get_name(msg) function get_name(msg)
local name = msg.from.first_name local name = msg.from.first_name
if name == nil then if name == nil then
@ -140,3 +146,26 @@ function get_name(msg)
end end
return name return name
end end
-- Returns at table of lua files inside plugins
function plugins_names( )
local files = {}
for k, v in pairs(scandir("plugins")) do
-- Ends with .lua
if (v:match(".lua$")) then
table.insert(files, v)
end
end
return files
end
-- Function name explains what it does.
function file_exists(name)
local f = io.open(name,"r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end

62
plugins/plugins.lua Normal file
View File

@ -0,0 +1,62 @@
function enable_plugin( filename )
-- Checks if file exists
if file_exists('plugins/'..filename) then
-- Add to the config table
table.insert(config.enabled_plugins, filename)
-- Reload the plugins
reload_plugins( )
else
return 'Plugin does not exists'
end
end
function reload_plugins( )
plugins = {}
load_plugins()
return list_plugins(true)
end
function list_plugins(only_enabled)
local text = ''
for k, v in pairs( plugins_names( )) do
-- ✔ enabled, ❌ disabled
local status = ''
-- Check if is enabled
for k2, v2 in pairs(config.enabled_plugins) do
if v == v2 then
status = ''
end
end
if not only_enabled or status == '' then
text = text..v..' '..status..'\n'
end
end
return text
end
function run(msg, matches)
-- Show the available plugins
if matches[1] == '!plugins' then
return list_plugins()
end
-- Reload all the plugins!
if matches[1] == 'reload' then
return reload_plugins(true)
end
-- Enable a plugin
if matches[1] == 'enable' then
print("enable: "..matches[2])
return enable_plugin(matches[2])
end
end
return {
description = "Enable / Disable plugins",
usage = "!plugins",
patterns = {
"^!plugins$",
"^!plugins (enable) ([%w%.]+)$",
"^!plugins (reload)$"
},
run = run
}

View File

@ -1,14 +0,0 @@
function run(msg, matches)
plugins = {}
load_plugins()
return 'Plugins reloaded'
end
return {
description = "Reloads bot plugins",
usage = "!reload",
patterns = {"^!reload$"},
run = run
}