Common functions moved to utils.lua. Indentation ...

This commit is contained in:
yago 2014-12-14 20:52:48 +01:00
parent 495bad27ee
commit fe05645a39
2 changed files with 272 additions and 297 deletions

View File

@ -1,332 +1,210 @@
http = require("socket.http") http = require("socket.http")
https = require("ssl.https") https = require("ssl.https")
URL = require("socket.url") URL = require("socket.url")
json = (loadfile "./bot/JSON.lua")() json = (loadfile "./bot/JSON.lua")()
require("./bot/utils")
VERSION = 'v0.7.4' VERSION = 'v0.7.6'
-- taken from http://stackoverflow.com/a/11130774/3163199
function scandir(directory)
local i, t, popen = 0, {}, io.popen
for filename in popen('ls -a "'..directory..'"'):lines() do
i = i + 1
t[i] = filename
end
return t
end
function on_msg_receive (msg)
vardump(msg)
if msg_valid(msg) == false then function on_msg_receive (msg)
return vardump(msg)
end
update_user_stats(msg) if msg_valid(msg) == false then
do_action(msg) return
mark_read(get_receiver(msg), ok_cb, false)
end end
function ok_cb(extra, success, result) update_user_stats(msg)
end do_action(msg)
-- Callback to remove tmp files mark_read(get_receiver(msg), ok_cb, false)
function rmtmp_cb(file_path, success, result) end
os.remove(file_path)
end
function msg_valid(msg) function ok_cb(extra, success, result)
--if msg.from.id == our_id then end
-- return false
--end
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 -- Callback to remove tmp files
function do_action(msg) function rmtmp_cb(file_path, success, result)
local receiver = get_receiver(msg) os.remove(file_path)
local text = msg.text end
if msg.text == nil then
-- Not a text message, make text the same as what tg shows so function msg_valid(msg)
-- we can match on it. The plugin is resposible for handling --if msg.from.id == our_id then
text = '['..msg.media.type..']' -- return false
end --end
-- print("Received msg", text) if msg.out then
for name, desc in pairs(plugins) do return false
-- print("Trying module", name) end
for k, pattern in pairs(desc.patterns) do if msg.date < now then
-- print("Trying", text, "against", pattern) return false
matches = { string.match(text, pattern) } end
if matches[1] then if msg.unread == 0 then
print(" matches",pattern) return false
result = desc.run(msg, matches) end
print(" sending", result) end
if (result) then
_send_msg(receiver, result) -- Where magic happens
return function do_action(msg)
end 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)
result = desc.run(msg, matches)
print(" sending", result)
if (result) then
_send_msg(receiver, result)
return
end end
end end
end end
end end
end
-- If text is longer than 4096 chars, send multiple msg. -- If text is longer than 4096 chars, send multiple msg.
-- https://core.telegram.org/method/messages.sendMessage -- https://core.telegram.org/method/messages.sendMessage
function _send_msg( destination, text) function _send_msg( destination, text)
local msg_text_max = 4096 local msg_text_max = 4096
local len = string.len(text) local len = string.len(text)
local iterations = math.ceil(len / msg_text_max) local iterations = math.ceil(len / msg_text_max)
for i = 1, iterations, 1 do for i = 1, iterations, 1 do
print ("iteracion: "..i) print ("iteracion: "..i)
local inital_c = i * msg_text_max - msg_text_max local inital_c = i * msg_text_max - msg_text_max
local final_c = i * msg_text_max local final_c = i * msg_text_max
-- dont worry about if text length < msg_text_max -- dont worry about if text length < msg_text_max
local text_msg = string.sub(text,inital_c,final_c) local text_msg = string.sub(text,inital_c,final_c)
send_msg(destination, text_msg, ok_cb, false) send_msg(destination, text_msg, ok_cb, false)
end
end end
end
function load_config() function load_config()
local f = assert(io.open('./bot/config.json', "r")) local f = assert(io.open('./bot/config.json', "r"))
local c = f:read "*a" local c = f:read "*a"
local config = json:decode(c) local config = json:decode(c)
if config.sh_enabled then if config.sh_enabled then
print ("!sh command is enabled") print ("!sh command is enabled")
for v,user in pairs(config.sudo_users) do for v,user in pairs(config.sudo_users) do
print("Allowed user: " .. user) print("Allowed user: " .. user)
end
end
-- print("Torrent path: " .. config.torrent_path)
f:close()
return config
end
function is_sudo(msg)
local var = false
-- Check users id in config
for v,user in pairs(config.sudo_users) do
if user == msg.from.id then
var = true
end
end
return var
end
function get_name(msg)
local name = msg.from.first_name
if name == nil then
name = msg.from.id
end
return name
end
function run_sh(msg)
name = get_name(msg)
text = ''
if config.sh_enabled == false then
text = '!sh command is disabled'
else
if is_sudo(msg) then
bash = msg.text:sub(4,-1)
text = run_bash(bash)
else
text = name .. ' you have no power here!'
end
end
return text
end
function run_bash(str)
local cmd = io.popen(str)
local result = cmd:read('*all')
cmd:close()
return result
end
function download_to_file( url , noremove )
print("url to download: "..url)
req, c, h = http.request(url)
htype = h["content-type"]
vardump(c)
print("content-type: "..htype)
if htype == "image/jpeg" then
file_name = string.random(5)..".jpg"
file_path = "/tmp/"..file_name
else
if htype == "image/gif" then
file_name = string.random(5)..".gif"
file_path = "/tmp/"..file_name
else
if htype == "image/png" then
file_name = string.random(5)..".png"
file_path = "/tmp/"..file_name
else
file_name = url:match("([^/]+)$")
file_path = "/tmp/"..file_name
end
end end
end end
file = io.open(file_path, "w+") -- print("Torrent path: " .. config.torrent_path)
file:write(req) f:close()
file:close() return config
end
if noremove == nil then function is_sudo(msg)
postpone(rmtmp_cb, file_path, config.rmtmp_delay) local var = false
end -- Check users id in config
for v,user in pairs(config.sudo_users) do
return file_path if user == msg.from.id then
end var = true
function string.random(length)
math.randomseed(os.time())
local str = "";
for i = 1, length do
math.random(97, 122)
str = str..string.char(math.random(97, 122));
end
return str;
end
function string:split(sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
function vardump(value, depth, key)
local linePrefix = ""
local spaces = ""
if key ~= nil then
linePrefix = "["..key.."] = "
end
if depth == nil then
depth = 0
else
depth = depth + 1
for i=1, depth do spaces = spaces .. " " end
end
if type(value) == 'table' then
mTable = getmetatable(value)
if mTable == nil then
print(spaces ..linePrefix.."(table) ")
else
print(spaces .."(metatable) ")
value = mTable
end
for tableKey, tableValue in pairs(value) do
vardump(tableValue, depth, tableKey)
end end
elseif type(value) == 'function' or end
type(value) == 'thread' or return var
type(value) == 'userdata' or end
value == nil
then function get_name(msg)
print(spaces..tostring(value)) local name = msg.from.first_name
else if name == nil then
print(spaces..linePrefix.."("..type(value)..") "..tostring(value)) name = msg.from.id
end end
return name
end
function update_user_stats(msg)
-- Save user to _users table
local from_id = tostring(msg.from.id)
local user_name = get_name(msg)
-- If last name is nil dont save last_name.
local user_last_name = msg.from.last_name
local user_print_name = msg.from.print_name
if _users[from_id] == nil then
_users[from_id] = {
name = user_name,
last_name = user_last_name,
print_name = user_print_name,
msg_num = 1
}
else
local actual_num = _users[from_id].msg_num
_users[from_id].msg_num = actual_num + 1
-- And update last_name
_users[from_id].last_name = user_last_name
end end
end
function update_user_stats(msg) function load_user_stats()
-- Save user to _users table local f = io.open('res/users.json', "r+")
local from_id = tostring(msg.from.id) -- If file doesn't exists
local user_name = get_name(msg) if f == nil then
-- If last name is nil dont save last_name. f = io.open('res/users.json', "w+")
local user_last_name = msg.from.last_name f:write("{}") -- Write empty table
local user_print_name = msg.from.print_name f:close()
print ("user_last_name", user_last_name) return {}
if _users[from_id] == nil then else
_users[from_id] = { local c = f:read "*a"
name = user_name, f:close()
last_name = user_last_name, return json:decode(c)
print_name = user_print_name,
msg_num = 1
}
else
local actual_num = _users[from_id].msg_num
_users[from_id].msg_num = actual_num + 1
-- And update last_name
_users[from_id].last_name = user_last_name
end
end end
end
function load_user_stats() function get_receiver(msg)
local f = io.open('res/users.json', "r+") if msg.to.type == 'user' then
-- If file doesn't exists return 'user#id'..msg.from.id
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 end
if msg.to.type == 'chat' then
function get_receiver(msg) return 'chat#id'..msg.to.id
if msg.to.type == 'user' then
return 'user#id'..msg.from.id
end
if msg.to.type == 'chat' then
return 'chat#id'..msg.to.id
end
end end
end
function on_our_id (id) function on_our_id (id)
our_id = id our_id = id
end end
function on_user_update (user, what) function on_user_update (user, what)
--vardump (user) --vardump (user)
end end
function on_chat_update (chat, what) function on_chat_update (chat, what)
--vardump (chat) --vardump (chat)
end end
function on_secret_chat_update (schat, what) function on_secret_chat_update (schat, what)
--vardump (schat) --vardump (schat)
end end
function on_get_difference_end () function on_get_difference_end ()
end end
function on_binlog_replay_end () function on_binlog_replay_end ()
started = 1 started = 1
end end
-- Start and load values -- Start and load values
config = load_config() config = load_config()
_users = load_user_stats() _users = load_user_stats()
our_id = 0 our_id = 0
now = os.time() now = os.time()
-- load plugins -- load plugins
plugins = {} plugins = {}
-- load all plugins in the plugins/ directory
for k, v in pairs(scandir("plugins")) do
if not (v:sub(0, 1) == ".") then
print("Loading plugin", v)
t = loadfile("plugins/" .. v)()
table.insert(plugins, t)
end
end
-- load all plugins in the plugins/ directory
for k, v in pairs(scandir("plugins")) do
if not (v:sub(0, 1) == ".") then
print("Loading plugin", v)
t = loadfile("plugins/" .. v)()
table.insert(plugins, t)
end
end

97
bot/utils.lua Normal file
View File

@ -0,0 +1,97 @@
function string.random(length)
math.randomseed(os.time())
local str = "";
for i = 1, length do
math.random(97, 122)
str = str..string.char(math.random(97, 122));
end
return str;
end
function string:split(sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
function download_to_file( url , noremove )
print("url to download: "..url)
req, c, h = http.request(url)
htype = h["content-type"]
vardump(c)
print("content-type: "..htype)
if htype == "image/jpeg" then
file_name = string.random(5)..".jpg"
file_path = "/tmp/"..file_name
else
if htype == "image/gif" then
file_name = string.random(5)..".gif"
file_path = "/tmp/"..file_name
else
if htype == "image/png" then
file_name = string.random(5)..".png"
file_path = "/tmp/"..file_name
else
file_name = url:match("([^/]+)$")
file_path = "/tmp/"..file_name
end
end
end
file = io.open(file_path, "w+")
file:write(req)
file:close()
if noremove == nil then
postpone(rmtmp_cb, file_path, config.rmtmp_delay)
end
return file_path
end
function vardump(value, depth, key)
local linePrefix = ""
local spaces = ""
if key ~= nil then
linePrefix = "["..key.."] = "
end
if depth == nil then
depth = 0
else
depth = depth + 1
for i=1, depth do spaces = spaces .. " " end
end
if type(value) == 'table' then
mTable = getmetatable(value)
if mTable == nil then
print(spaces ..linePrefix.."(table) ")
else
print(spaces .."(metatable) ")
value = mTable
end
for tableKey, tableValue in pairs(value) do
vardump(tableValue, depth, tableKey)
end
elseif type(value) == 'function' or
type(value) == 'thread' or
type(value) == 'userdata' or
value == nil
then
print(spaces..tostring(value))
else
print(spaces..linePrefix.."("..type(value)..") "..tostring(value))
end
end
-- taken from http://stackoverflow.com/a/11130774/3163199
function scandir(directory)
local i, t, popen = 0, {}, io.popen
for filename in popen('ls -a "'..directory..'"'):lines() do
i = i + 1
t[i] = filename
end
return t
end