diff --git a/bot/bot.lua b/bot/bot.lua index 719c391..12af316 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -4,10 +4,12 @@ VERSION = '0.10.1' -- This function is called when tg receive a msg function on_msg_receive (msg) + local receiver = get_receiver(msg) -- vardump(msg) if msg_valid(msg) then msg = pre_process_msg(msg) match_plugins(msg) + mark_read(receiver, ok_cb, false) end end @@ -62,42 +64,42 @@ function match_plugins(msg) 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) local receiver = get_receiver(msg) -- Go over patterns. If one matches is enought. for k, pattern in pairs(plugin.patterns) do - -- print(msg.text, pattern) - matches = { string.match(msg.text, pattern) } - if matches[1] then - mark_read(receiver, ok_cb, false) - print(" matches", pattern) + local matches = match_pattern(pattern, msg.text) + if matches then + print("msg matches: ", pattern) -- Function exists - if plugin.run ~= nil then + if plugin.run then -- If plugin is for privileged users only - if not user_allowed(plugin, msg) then - local text = 'This plugin requires privileged user' - send_msg(receiver, text, ok_cb, false) - else - -- Send the returned text by run function. - result = plugin.run(msg, matches) - if result ~= nil then - _send_msg(receiver, result) + if not warns_user_not_allowed(plugin, msg) then + local result = plugin.run(msg, matches) + if result then + send_large_msg(receiver, result) end end end - -- One matches + -- One patterns matches return 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 - return true +-- DEPRECATED, use send_large_msg(destination, text) +function _send_msg(destination, text) + send_large_msg(destination, text) end --Apply lex and other text. @@ -116,22 +118,6 @@ function pre_process_msg(msg) return msg 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 function save_config( ) serialize_to_file(_config, './data/config.lua') diff --git a/bot/utils.lua b/bot/utils.lua index 0cda8b2..bc5df12 100644 --- a/bot/utils.lua +++ b/bot/utils.lua @@ -382,3 +382,59 @@ function format_http_params(params, is_get) end return str 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