2014-12-14 20:52:48 +01:00
|
|
|
http = require("socket.http")
|
|
|
|
https = require("ssl.https")
|
|
|
|
URL = require("socket.url")
|
|
|
|
json = (loadfile "./bot/JSON.lua")()
|
|
|
|
require("./bot/utils")
|
|
|
|
|
2014-12-22 21:42:58 +01:00
|
|
|
VERSION = 'v0.7.7'
|
2014-12-14 20:52:48 +01:00
|
|
|
|
|
|
|
function on_msg_receive (msg)
|
|
|
|
vardump(msg)
|
|
|
|
|
|
|
|
if msg_valid(msg) == false then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
update_user_stats(msg)
|
|
|
|
do_action(msg)
|
|
|
|
|
|
|
|
mark_read(get_receiver(msg), ok_cb, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ok_cb(extra, success, result)
|
|
|
|
end
|
|
|
|
|
|
|
|
function msg_valid(msg)
|
2014-12-16 15:06:49 +01:00
|
|
|
-- if msg.from.id == our_id then
|
|
|
|
-- return true
|
|
|
|
-- end
|
2014-12-14 20:52:48 +01:00
|
|
|
if msg.out then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if msg.date < now then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if msg.unread == 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Where magic happens
|
|
|
|
function do_action(msg)
|
|
|
|
local receiver = get_receiver(msg)
|
|
|
|
local text = msg.text
|
|
|
|
if msg.text == nil then
|
|
|
|
-- Not a text message, make text the same as what tg shows so
|
|
|
|
-- we can match on it. The plugin is resposible for handling
|
|
|
|
text = '['..msg.media.type..']'
|
|
|
|
end
|
|
|
|
-- print("Received 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
|
|
|
|
print(" matches",pattern)
|
2014-12-16 21:50:19 +01:00
|
|
|
if desc.run ~= nil then
|
|
|
|
result = desc.run(msg, matches)
|
|
|
|
print(" sending", result)
|
|
|
|
if (result) then
|
|
|
|
_send_msg(receiver, result)
|
|
|
|
return
|
|
|
|
end
|
2014-11-04 16:09:08 +01:00
|
|
|
end
|
2014-11-04 22:06:59 +01:00
|
|
|
end
|
2014-11-03 11:54:03 +01:00
|
|
|
end
|
|
|
|
end
|
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-10-09 15:09:06 +02:00
|
|
|
|
2014-12-14 20:52:48 +01:00
|
|
|
function load_config()
|
|
|
|
local f = assert(io.open('./bot/config.json', "r"))
|
|
|
|
local c = f:read "*a"
|
|
|
|
local config = json:decode(c)
|
|
|
|
if config.sh_enabled then
|
|
|
|
print ("!sh command is enabled")
|
|
|
|
for v,user in pairs(config.sudo_users) do
|
|
|
|
print("Allowed user: " .. user)
|
2014-09-25 23:28:04 +02:00
|
|
|
end
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
|
|
|
f:close()
|
|
|
|
return config
|
|
|
|
end
|
|
|
|
|
|
|
|
function update_user_stats(msg)
|
|
|
|
-- Save user to _users table
|
|
|
|
local from_id = tostring(msg.from.id)
|
2014-12-14 21:28:32 +01:00
|
|
|
local to_id = tostring(msg.to.id)
|
2014-12-14 20:52:48 +01:00
|
|
|
local user_name = get_name(msg)
|
|
|
|
-- If last name is nil dont save last_name.
|
|
|
|
local user_last_name = msg.from.last_name
|
2014-12-14 21:28:32 +01:00
|
|
|
local user_print_name = msg.from.print_name
|
|
|
|
if _users[to_id] == nil then
|
|
|
|
_users[to_id] = {}
|
|
|
|
end
|
|
|
|
if _users[to_id][from_id] == nil then
|
|
|
|
_users[to_id][from_id] = {
|
2014-12-14 20:52:48 +01:00
|
|
|
name = user_name,
|
|
|
|
last_name = user_last_name,
|
|
|
|
print_name = user_print_name,
|
|
|
|
msg_num = 1
|
|
|
|
}
|
|
|
|
else
|
2014-12-14 21:28:32 +01:00
|
|
|
local actual_num = _users[to_id][from_id].msg_num
|
|
|
|
_users[to_id][from_id].msg_num = actual_num + 1
|
2014-12-14 20:52:48 +01:00
|
|
|
-- And update last_name
|
2014-12-14 21:28:32 +01:00
|
|
|
_users[to_id][from_id].last_name = user_last_name
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function load_user_stats()
|
|
|
|
local f = io.open('res/users.json', "r+")
|
|
|
|
-- If file doesn't exists
|
|
|
|
if f == nil then
|
|
|
|
f = io.open('res/users.json', "w+")
|
|
|
|
f:write("{}") -- Write empty table
|
|
|
|
f:close()
|
|
|
|
return {}
|
|
|
|
else
|
|
|
|
local c = f:read "*a"
|
|
|
|
f:close()
|
|
|
|
return json:decode(c)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
function on_binlog_replay_end ()
|
|
|
|
started = 1
|
2014-12-16 21:50:19 +01:00
|
|
|
-- Uncomment the line to enable cron plugins.
|
2014-12-22 22:12:28 +01:00
|
|
|
-- postpone (cron_plugins, false, 5.0)
|
2014-12-16 21:50:19 +01:00
|
|
|
-- See plugins/ping.lua as an example for cron
|
2014-12-14 20:52:48 +01:00
|
|
|
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-24 01:38:41 +01:00
|
|
|
for k, v in pairs(config.enabled_plugins) do
|
|
|
|
print("Loading plugin", v)
|
|
|
|
t = loadfile("plugins/" .. v)()
|
|
|
|
table.insert(plugins, t)
|
2014-12-16 15:05:42 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-16 21:50:19 +01:00
|
|
|
-- Cron all the enabled plugins
|
|
|
|
function cron_plugins()
|
2014-12-16 15:05:42 +01:00
|
|
|
|
2014-12-16 21:50:19 +01:00
|
|
|
for name, desc in pairs(plugins) do
|
|
|
|
if desc.cron ~= nil then
|
2014-12-22 22:12:28 +01:00
|
|
|
print(desc.description)
|
2014-12-16 21:50:19 +01:00
|
|
|
desc.cron()
|
|
|
|
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
|
|
|
|
now = os.time()
|
|
|
|
|
|
|
|
config = load_config()
|
|
|
|
_users = load_user_stats()
|
|
|
|
|
|
|
|
-- load plugins
|
|
|
|
plugins = {}
|
|
|
|
load_plugins()
|