Matches one pattern per plugin only. do_lex msg as param.

This commit is contained in:
yago 2015-03-14 17:21:20 +01:00
parent ee5506ae24
commit 29ca79a193

View File

@ -2,22 +2,20 @@ require("./bot/utils")
VERSION = '0.9.5' VERSION = '0.9.5'
-- This function is called when tg receive a msg
function on_msg_receive (msg) function on_msg_receive (msg)
vardump(msg) -- vardump(msg)
if msg_valid(msg) then
if msg_valid(msg) == false then msg = pre_process_msg(msg)
return match_plugins(msg)
end end
do_action(msg)
end end
function ok_cb(extra, success, result) function ok_cb(extra, success, result)
end end
function on_binlog_replay_end () function on_binlog_replay_end()
started = 1 started = 1
-- Uncomment the line to enable cron plugins.
postpone (cron_plugins, false, 60*5.0) postpone (cron_plugins, false, 60*5.0)
-- See plugins/ping.lua as an example for cron -- See plugins/ping.lua as an example for cron
@ -42,65 +40,80 @@ function msg_valid(msg)
print("Not valid, readed") print("Not valid, readed")
return false return false
end end
return true
end end
function do_lex(msg, text)
function do_lex(msg)
-- Plugins which implements lex. -- Plugins which implements lex.
for name, desc in pairs(plugins) do for name, plugin in pairs(plugins) do
if (desc.lex ~= nil) then if plugin.lex ~= nil then
result = desc.lex(msg, text) msg = plugin.lex(msg)
if (result ~= nil) then
print ("Mutating to " .. result)
text = result
end end
end end
end
return text return msg
end end
-- Where magic happens -- Go over enabled plugins patterns.
function do_action(msg) function match_plugins(msg)
for name, plugin in pairs(plugins) do
match_plugin(plugin, msg)
end
end
function match_plugin(plugin, msg)
local receiver = get_receiver(msg) local receiver = get_receiver(msg)
local text = msg.text
if text == nil then -- Go over patterns. If one matches is enought.
-- Not a text message, make text the same as what tg shows so for k, pattern in pairs(plugin.patterns) do
-- we can match on it. Maybe a plugin activated my media type. -- print(msg.text, pattern)
if msg.media ~= nil then matches = { string.match(msg.text, pattern) }
text = '['..msg.media.type..']'
end
end
-- We can't do anything
if msg.text == nil then return false end
msg.text = do_lex(msg, text)
for name, desc in pairs(plugins) do
-- print("Trying module", name)
for k, pattern in pairs(desc.patterns) do
-- print("Trying", text, "against", pattern)
matches = { string.match(text, pattern) }
if matches[1] then if matches[1] then
mark_read(receiver, ok_cb, false) mark_read(receiver, ok_cb, false)
print(" matches", pattern) print(" matches", pattern)
if desc.run ~= nil then -- Function exists
-- If plugin is for privileged user if plugin.run ~= nil then
if desc.privileged and not is_sudo(msg) then -- If plugin is for privileged users only
if not user_allowed(plugin, msg) then
local text = 'This plugin requires privileged user' local text = 'This plugin requires privileged user'
send_msg(receiver, text, ok_cb, false) send_msg(receiver, text, ok_cb, false)
else else
result = desc.run(msg, matches) -- Send the returned text by run function.
-- print(" sending", result) result = plugin.run(msg, matches)
if (result) then if result ~= nil then
result = do_lex(msg, result)
_send_msg(receiver, result) _send_msg(receiver, result)
end end
end end
end end
-- One matches
return
end end
end end
end
-- Check if user can use the plugin
function user_allowed(plugin, msg)
if plugin.privileged and not is_sudo(msg) then
return false
end end
return true
end
--Apply lex and other text.
function pre_process_msg(msg)
if msg.text == nil then
-- Not a text message, make text the same as what tg shows so
-- we can match on it. Maybe a plugin activated my media type.
if msg.media ~= nil then
msg.text = '['..msg.media.type..']'
end
end
msg = do_lex(msg)
return msg
end end
-- If text is longer than 4096 chars, send multiple msg. -- If text is longer than 4096 chars, send multiple msg.
@ -125,7 +138,8 @@ function save_config( )
print ('saved config into ./data/config.lua') print ('saved config into ./data/config.lua')
end end
-- Returns the config from config.lua file.
-- If file doesnt exists, create it.
function load_config( ) function load_config( )
local f = io.open('./data/config.lua', "r") local f = io.open('./data/config.lua', "r")
-- If config.lua doesnt exists -- If config.lua doesnt exists
@ -197,18 +211,18 @@ end
function load_plugins() function load_plugins()
for k, v in pairs(_config.enabled_plugins) do for k, v in pairs(_config.enabled_plugins) do
print("Loading plugin", v) print("Loading plugin", v)
t = loadfile("plugins/"..v..'.lua')() local t = loadfile("plugins/"..v..'.lua')()
table.insert(plugins, t) table.insert(plugins, t)
end end
end end
-- Cron all the enabled plugins -- Call and postpone execution for cron plugins
function cron_plugins() function cron_plugins()
for name, desc in pairs(plugins) do for name, plugin in pairs(plugins) do
if desc.cron ~= nil then -- Only plugins with cron function
print(desc.description) if plugin.cron ~= nil then
desc.cron() plugin.cron()
end end
end end