2014-12-14 20:52:48 +01:00
|
|
|
require("./bot/utils")
|
|
|
|
|
2015-04-05 13:58:07 +02:00
|
|
|
VERSION = '0.10.1'
|
2014-12-14 20:52:48 +01:00
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
-- This function is called when tg receive a msg
|
2014-12-14 20:52:48 +01:00
|
|
|
function on_msg_receive (msg)
|
2015-03-14 17:21:20 +01:00
|
|
|
-- vardump(msg)
|
|
|
|
if msg_valid(msg) then
|
|
|
|
msg = pre_process_msg(msg)
|
|
|
|
match_plugins(msg)
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ok_cb(extra, success, result)
|
|
|
|
end
|
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
function on_binlog_replay_end()
|
2014-12-31 17:14:48 +01:00
|
|
|
started = 1
|
2015-01-12 20:38:36 +01:00
|
|
|
postpone (cron_plugins, false, 60*5.0)
|
2014-12-31 17:14:48 +01:00
|
|
|
-- See plugins/ping.lua as an example for cron
|
|
|
|
|
|
|
|
_config = load_config()
|
|
|
|
|
|
|
|
-- load plugins
|
|
|
|
plugins = {}
|
|
|
|
load_plugins()
|
|
|
|
end
|
|
|
|
|
2014-12-14 20:52:48 +01:00
|
|
|
function msg_valid(msg)
|
2014-12-31 17:14:48 +01:00
|
|
|
-- Dont process outgoing messages
|
2014-12-14 20:52:48 +01:00
|
|
|
if msg.out then
|
2015-02-15 14:02:26 +01:00
|
|
|
print("Not valid, msg from us")
|
2014-12-14 20:52:48 +01:00
|
|
|
return false
|
|
|
|
end
|
2015-03-10 21:06:15 +01:00
|
|
|
if msg.date < now then
|
2015-02-15 14:02:26 +01:00
|
|
|
print("Not valid, old msg")
|
2014-12-14 20:52:48 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
if msg.unread == 0 then
|
2015-02-15 14:02:26 +01:00
|
|
|
print("Not valid, readed")
|
2014-12-14 20:52:48 +01:00
|
|
|
return false
|
|
|
|
end
|
2015-03-14 17:21:20 +01:00
|
|
|
return true
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
|
|
|
|
function do_lex(msg)
|
2015-02-15 12:00:33 +01:00
|
|
|
-- Plugins which implements lex.
|
2015-03-14 17:21:20 +01:00
|
|
|
for name, plugin in pairs(plugins) do
|
|
|
|
if plugin.lex ~= nil then
|
|
|
|
msg = plugin.lex(msg)
|
2014-12-26 12:13:27 +01:00
|
|
|
end
|
|
|
|
end
|
2015-03-14 17:21:20 +01:00
|
|
|
|
|
|
|
return msg
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Go over enabled plugins patterns.
|
|
|
|
function match_plugins(msg)
|
|
|
|
for name, plugin in pairs(plugins) do
|
|
|
|
match_plugin(plugin, msg)
|
|
|
|
end
|
2014-12-26 12:13:27 +01:00
|
|
|
end
|
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
function match_plugin(plugin, msg)
|
2014-12-14 20:52:48 +01:00
|
|
|
local receiver = get_receiver(msg)
|
2015-02-11 22:01:31 +01:00
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
-- 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)
|
|
|
|
-- Function exists
|
|
|
|
if plugin.run ~= nil 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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- One 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
|
|
|
|
end
|
|
|
|
|
|
|
|
--Apply lex and other text.
|
|
|
|
function pre_process_msg(msg)
|
|
|
|
|
|
|
|
if msg.text == nil then
|
2015-02-11 22:01:31 +01:00
|
|
|
-- Not a text message, make text the same as what tg shows so
|
2015-02-15 12:00:33 +01:00
|
|
|
-- we can match on it. Maybe a plugin activated my media type.
|
2015-02-11 22:01:31 +01:00
|
|
|
if msg.media ~= nil then
|
2015-03-14 17:21:20 +01:00
|
|
|
msg.text = '['..msg.media.type..']'
|
2015-02-11 22:01:31 +01:00
|
|
|
end
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
2014-12-26 12:13:27 +01:00
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
msg = do_lex(msg)
|
|
|
|
|
|
|
|
return msg
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
|
2014-12-14 20:52:48 +01:00
|
|
|
-- 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)
|
2014-12-11 22:12:12 +01:00
|
|
|
|
2014-12-14 20:52:48 +01:00
|
|
|
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)
|
2014-11-03 11:54:03 +01:00
|
|
|
end
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
|
2014-12-31 17:14:48 +01:00
|
|
|
-- Save the content of _config to config.lua
|
|
|
|
function save_config( )
|
2015-01-06 21:08:18 +01:00
|
|
|
serialize_to_file(_config, './data/config.lua')
|
|
|
|
print ('saved config into ./data/config.lua')
|
2014-12-31 17:14:48 +01:00
|
|
|
end
|
2014-10-09 15:09:06 +02:00
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
-- Returns the config from config.lua file.
|
|
|
|
-- If file doesnt exists, create it.
|
2014-12-31 17:14:48 +01:00
|
|
|
function load_config( )
|
2015-01-06 21:08:18 +01:00
|
|
|
local f = io.open('./data/config.lua', "r")
|
2014-12-31 17:14:48 +01:00
|
|
|
-- If config.lua doesnt exists
|
|
|
|
if not f then
|
2015-01-06 21:08:18 +01:00
|
|
|
print ("Created new config file: data/config.lua")
|
2014-12-31 17:14:48 +01:00
|
|
|
create_config()
|
2015-01-06 21:08:18 +01:00
|
|
|
else
|
|
|
|
f:close()
|
2014-12-31 17:14:48 +01:00
|
|
|
end
|
2015-01-06 21:08:18 +01:00
|
|
|
local config = loadfile ("./data/config.lua")()
|
2014-12-31 17:14:48 +01:00
|
|
|
for v,user in pairs(config.sudo_users) do
|
|
|
|
print("Allowed user: " .. user)
|
|
|
|
end
|
|
|
|
return config
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Create a basic config.json file and saves it.
|
|
|
|
function create_config( )
|
|
|
|
-- A simple config with basic plugins and ourserves as priviled user
|
|
|
|
config = {
|
|
|
|
enabled_plugins = {
|
2015-01-12 20:29:18 +01:00
|
|
|
"9gag",
|
2015-02-15 12:00:33 +01:00
|
|
|
"eur",
|
2015-01-12 20:29:18 +01:00
|
|
|
"echo",
|
2015-02-15 12:00:33 +01:00
|
|
|
"btc",
|
2014-12-31 17:14:48 +01:00
|
|
|
"get",
|
2015-01-24 16:54:59 +01:00
|
|
|
"giphy",
|
|
|
|
"google",
|
2015-02-15 12:00:33 +01:00
|
|
|
"gps",
|
2015-01-12 20:50:22 +01:00
|
|
|
"help",
|
2014-12-31 17:14:48 +01:00
|
|
|
"images",
|
|
|
|
"img_google",
|
|
|
|
"location",
|
|
|
|
"media",
|
|
|
|
"plugins",
|
2015-01-12 20:29:18 +01:00
|
|
|
"set",
|
2014-12-31 17:14:48 +01:00
|
|
|
"stats",
|
|
|
|
"time",
|
|
|
|
"version",
|
2015-01-24 16:54:59 +01:00
|
|
|
"weather",
|
2015-02-15 12:00:33 +01:00
|
|
|
"xkcd",
|
2014-12-31 17:14:48 +01:00
|
|
|
"youtube" },
|
|
|
|
sudo_users = {our_id}
|
|
|
|
}
|
2015-01-06 21:08:18 +01:00
|
|
|
serialize_to_file(config, './data/config.lua')
|
|
|
|
print ('saved config into ./data/config.lua')
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function on_our_id (id)
|
|
|
|
our_id = id
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_user_update (user, what)
|
|
|
|
--vardump (user)
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_chat_update (chat, what)
|
|
|
|
--vardump (chat)
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_secret_chat_update (schat, what)
|
|
|
|
--vardump (schat)
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_get_difference_end ()
|
|
|
|
end
|
|
|
|
|
2014-12-24 01:38:41 +01:00
|
|
|
-- Enable plugins in config.json
|
2014-12-16 15:05:42 +01:00
|
|
|
function load_plugins()
|
2014-12-31 17:14:48 +01:00
|
|
|
for k, v in pairs(_config.enabled_plugins) do
|
2014-12-24 01:38:41 +01:00
|
|
|
print("Loading plugin", v)
|
2015-03-14 17:21:20 +01:00
|
|
|
local t = loadfile("plugins/"..v..'.lua')()
|
2015-04-05 13:57:10 +02:00
|
|
|
plugins[v] = t
|
2014-12-16 15:05:42 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
-- Call and postpone execution for cron plugins
|
2014-12-16 21:50:19 +01:00
|
|
|
function cron_plugins()
|
2014-12-16 15:05:42 +01:00
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
for name, plugin in pairs(plugins) do
|
|
|
|
-- Only plugins with cron function
|
|
|
|
if plugin.cron ~= nil then
|
|
|
|
plugin.cron()
|
2014-12-16 21:50:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Called again in 5 mins
|
|
|
|
postpone (cron_plugins, false, 5*60.0)
|
2014-12-22 22:05:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Start and load values
|
|
|
|
our_id = 0
|
2015-01-24 17:18:13 +01:00
|
|
|
now = os.time()
|
2015-03-10 21:06:15 +01:00
|
|
|
math.randomseed(now)
|