send_large_msg_callback, send_large_msg, user_allowed, warns_user_not_allowed on utils.lua. match_pattern on bot.lua

This commit is contained in:
yago 2015-04-12 19:16:06 +02:00
parent e269a00ba2
commit e399e669cf
2 changed files with 79 additions and 37 deletions

View File

@ -4,10 +4,12 @@ VERSION = '0.10.1'
-- This function is called when tg receive a msg -- This function is called when tg receive a msg
function on_msg_receive (msg) function on_msg_receive (msg)
local receiver = get_receiver(msg)
-- vardump(msg) -- vardump(msg)
if msg_valid(msg) then if msg_valid(msg) then
msg = pre_process_msg(msg) msg = pre_process_msg(msg)
match_plugins(msg) match_plugins(msg)
mark_read(receiver, ok_cb, false)
end end
end end
@ -62,42 +64,42 @@ function match_plugins(msg)
end end
end end
-- Returns a table whith matches or nil
function match_pattern(pattern, text)
local matches = { string.match(text, pattern) }
if next(matches) then
return matches
end
-- nil
end
function match_plugin(plugin, msg) function match_plugin(plugin, msg)
local receiver = get_receiver(msg) local receiver = get_receiver(msg)
-- Go over patterns. If one matches is enought. -- Go over patterns. If one matches is enought.
for k, pattern in pairs(plugin.patterns) do for k, pattern in pairs(plugin.patterns) do
-- print(msg.text, pattern) local matches = match_pattern(pattern, msg.text)
matches = { string.match(msg.text, pattern) } if matches then
if matches[1] then print("msg matches: ", pattern)
mark_read(receiver, ok_cb, false)
print(" matches", pattern)
-- Function exists -- Function exists
if plugin.run ~= nil then if plugin.run then
-- If plugin is for privileged users only -- If plugin is for privileged users only
if not user_allowed(plugin, msg) then if not warns_user_not_allowed(plugin, msg) then
local text = 'This plugin requires privileged user' local result = plugin.run(msg, matches)
send_msg(receiver, text, ok_cb, false) if result then
else send_large_msg(receiver, result)
-- Send the returned text by run function.
result = plugin.run(msg, matches)
if result ~= nil then
_send_msg(receiver, result)
end end
end end
end end
-- One matches -- One patterns matches
return return
end end
end end
end end
-- Check if user can use the plugin -- DEPRECATED, use send_large_msg(destination, text)
function user_allowed(plugin, msg) function _send_msg(destination, text)
if plugin.privileged and not is_sudo(msg) then send_large_msg(destination, text)
return false
end
return true
end end
--Apply lex and other text. --Apply lex and other text.
@ -116,22 +118,6 @@ function pre_process_msg(msg)
return msg return msg
end end
-- If text is longer than 4096 chars, send multiple msg.
-- https://core.telegram.org/method/messages.sendMessage
function _send_msg( destination, text)
local msg_text_max = 4096
local len = string.len(text)
local iterations = math.ceil(len / msg_text_max)
for i = 1, iterations, 1 do
local inital_c = i * msg_text_max - msg_text_max
local final_c = i * msg_text_max
-- dont worry about if text length < msg_text_max
local text_msg = string.sub(text,inital_c,final_c)
send_msg(destination, text_msg, ok_cb, false)
end
end
-- Save the content of _config to config.lua -- Save the content of _config to config.lua
function save_config( ) function save_config( )
serialize_to_file(_config, './data/config.lua') serialize_to_file(_config, './data/config.lua')

View File

@ -382,3 +382,59 @@ function format_http_params(params, is_get)
end end
return str return str
end end
-- Check if user can use the plugin and warns user
-- Returns true if user was warned and false if not warned (is allowed)
function warns_user_not_allowed(plugin, msg)
if not user_allowed(plugin, msg) then
local text = 'This plugin requires privileged user'
local receiver = get_receiver(msg)
send_msg(receiver, text, ok_cb, false)
return true
else
return false
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
return true
end
-- Same as send_large_msg_callback but frienly params
function send_large_msg(destination, text)
local cb_extra = {
destination = destination,
text = text
}
send_large_msg_callback(cb_extra, true)
end
-- If text is longer than 4096 chars, send multiple msg.
-- https://core.telegram.org/method/messages.sendMessage
function send_large_msg_callback(cb_extra, success, result)
local text_max = 4096
local destination = cb_extra.destination
local text = cb_extra.text
local text_len = string.len(text)
local num_msg = math.ceil(text_len / text_max)
if num_msg <= 1 then
send_msg(destination, text, ok_cb, false)
else
local my_text = string.sub(text, 1, 4096)
local rest = string.sub(text, 4096, text_len)
local cb_extra = {
destination = destination,
text = rest
}
send_msg(destination, my_text, send_large_msg_callback, cb_extra)
end
end