2014-07-02 14:30:18 +02:00
|
|
|
http = require("socket.http")
|
|
|
|
json = (loadfile "./bot/JSON.lua")()
|
|
|
|
|
2014-09-24 11:12:41 +02:00
|
|
|
VERSION = 'v0.2.1'
|
2014-09-14 00:07:02 +02:00
|
|
|
|
2014-06-30 12:42:28 +02:00
|
|
|
|
|
|
|
function on_msg_receive (msg)
|
2014-09-14 00:07:02 +02:00
|
|
|
|
|
|
|
if msg_valid(msg) == false then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Check if command starts with ! eg !echo
|
|
|
|
if msg.text:sub(0,1) == '!' then
|
|
|
|
do_action(msg)
|
|
|
|
else
|
2014-09-24 15:31:25 +02:00
|
|
|
if is_image_url(msg.text) then
|
2014-09-14 00:07:02 +02:00
|
|
|
send_image_from_url (msg)
|
2014-09-24 15:31:25 +02:00
|
|
|
else
|
|
|
|
if is_file_url(msg.text) then
|
|
|
|
send_file_from_url (msg)
|
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
print('mark_read: '..get_receiver(msg))
|
|
|
|
mark_read(get_receiver(msg), ok_cb, false)
|
|
|
|
-- write_log_file(msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function ok_cb(extra, success, result)
|
|
|
|
end
|
|
|
|
|
|
|
|
function msg_valid(msg)
|
2014-06-30 12:42:28 +02:00
|
|
|
if msg.out then
|
2014-09-14 00:07:02 +02:00
|
|
|
return false
|
2014-06-30 12:42:28 +02:00
|
|
|
end
|
2014-07-01 22:06:40 +02:00
|
|
|
if msg.date < now then
|
2014-09-14 00:07:02 +02:00
|
|
|
return false
|
2014-07-01 22:06:40 +02:00
|
|
|
end
|
2014-06-30 12:42:28 +02:00
|
|
|
if msg.text == nil then
|
2014-09-14 00:07:02 +02:00
|
|
|
return false
|
2014-06-30 12:42:28 +02:00
|
|
|
end
|
|
|
|
if msg.unread == 0 then
|
2014-09-14 00:07:02 +02:00
|
|
|
return false
|
2014-08-27 15:50:35 +02:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
end
|
2014-08-27 15:50:35 +02:00
|
|
|
|
2014-09-24 15:31:25 +02:00
|
|
|
function send_file_from_url (msg)
|
|
|
|
last = string.get_last_word(msg.text)
|
|
|
|
last = last:match("[%w_:/.%%&-]+") -- Lets sanitize!
|
|
|
|
extension = string.get_extension_from_filename(last)
|
|
|
|
|
|
|
|
file_name = string.random(5)
|
|
|
|
file = "/tmp/"..file_name.."."..extension
|
|
|
|
sh = "curl --insecure -o '"..file.."' "..last
|
|
|
|
run_bash(sh)
|
|
|
|
send_document(get_receiver(msg), file, ok_cb, false)
|
|
|
|
end
|
|
|
|
|
2014-09-14 00:07:02 +02:00
|
|
|
function send_image_from_url (msg)
|
|
|
|
last = string.get_last_word(msg.text)
|
|
|
|
last = last:match("[%w_:/.%%&-]+") -- Lets sanitize!
|
|
|
|
extension = string.get_extension_from_filename(last)
|
2014-08-27 15:50:35 +02:00
|
|
|
|
2014-09-14 00:07:02 +02:00
|
|
|
file_name = string.random(5)
|
|
|
|
file = "/tmp/"..file_name.."."..extension
|
|
|
|
sh = "curl --insecure -o '"..file.."' "..last
|
|
|
|
run_bash(sh)
|
|
|
|
send_photo(get_receiver(msg), file, ok_cb, false)
|
2014-06-30 12:42:28 +02:00
|
|
|
end
|
|
|
|
|
2014-08-27 15:50:35 +02:00
|
|
|
function is_image_url(text)
|
|
|
|
print ('IS image ' .. text ..'?')
|
|
|
|
last = string.get_last_word(text)
|
|
|
|
extension = string.get_extension_from_filename(last)
|
2014-09-24 15:31:25 +02:00
|
|
|
if extension == 'jpg' or extension == 'png' then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function is_file_url(text)
|
|
|
|
last = string.get_last_word(text)
|
|
|
|
extension = string.get_extension_from_filename(last)
|
|
|
|
if extension == 'gif' then
|
2014-08-27 15:50:35 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-07-01 22:06:40 +02:00
|
|
|
-- Where magic happens
|
2014-06-30 12:42:28 +02:00
|
|
|
function do_action(msg)
|
2014-09-14 00:07:02 +02:00
|
|
|
local receiver = get_receiver(msg)
|
2014-08-05 14:07:25 +02:00
|
|
|
|
2014-09-14 00:07:02 +02:00
|
|
|
if string.starts(msg.text, '!sh') then
|
2014-07-04 13:41:44 +02:00
|
|
|
text = run_sh(msg)
|
2014-09-14 00:07:02 +02:00
|
|
|
send_msg(receiver, text, ok_cb, false)
|
|
|
|
return
|
2014-07-04 13:41:44 +02:00
|
|
|
end
|
|
|
|
|
2014-09-14 00:07:02 +02:00
|
|
|
if string.starts(msg.text, '!uc3m') then
|
2014-07-03 21:18:07 +02:00
|
|
|
text = get_fortunes_uc3m()
|
2014-09-14 00:07:02 +02:00
|
|
|
send_msg(receiver, text, ok_cb, false)
|
|
|
|
return
|
2014-07-03 21:18:07 +02:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
|
|
|
|
if string.starts(msg.text, '!9gag') then
|
2014-08-06 01:15:54 +02:00
|
|
|
url, title = get_9GAG()
|
2014-08-05 14:07:25 +02:00
|
|
|
file_name = url:match("([^/]+)$")
|
|
|
|
file = "/tmp/"..file_name
|
|
|
|
sh = "curl -o '"..file.."' "..url
|
|
|
|
run_bash(sh)
|
2014-09-14 00:07:02 +02:00
|
|
|
send_photo(receiver, file, ok_cb, false)
|
|
|
|
send_msg(receiver, title, ok_cb, false)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if string.starts(msg.text, '!fortune') then
|
2014-07-01 22:06:40 +02:00
|
|
|
text = run_bash('fortune')
|
2014-09-14 00:07:02 +02:00
|
|
|
send_msg(receiver, text, ok_cb, false)
|
|
|
|
return
|
2014-07-03 21:18:07 +02:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
|
|
|
|
if string.starts(msg.text, '!forni') then
|
|
|
|
text = msg.text:sub(8,-1)
|
|
|
|
send_msg('Fornicio_2.0', text, ok_cb, false)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if string.starts(msg.text, '!fwd') then
|
|
|
|
fwd_msg (receiver, msg.id, ok_cb, false)
|
|
|
|
return
|
2014-07-07 10:30:27 +02:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
|
|
|
|
if string.starts(msg.text, '!cpu') then
|
2014-07-02 14:30:18 +02:00
|
|
|
text = run_bash('uname -snr') .. ' ' .. run_bash('whoami')
|
2014-06-30 12:42:28 +02:00
|
|
|
text = text .. '\n' .. run_bash('top -b |head -2')
|
2014-09-14 00:07:02 +02:00
|
|
|
send_msg(receiver, text, ok_cb, false)
|
|
|
|
return
|
2014-06-30 12:42:28 +02:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
|
|
|
|
if string.starts(msg.text, '!ping') then
|
|
|
|
print('receiver: '..receiver)
|
|
|
|
send_msg (receiver, 'pong', ok_cb, false)
|
|
|
|
return
|
2014-06-30 12:42:28 +02:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
|
|
|
|
if string.starts(msg.text, '!weather') then
|
2014-07-07 10:30:27 +02:00
|
|
|
if string.len(msg.text) <= 9 then
|
|
|
|
city = 'Madrid,ES'
|
|
|
|
else
|
2014-09-14 00:07:02 +02:00
|
|
|
city = msg.text:sub(10,-1)
|
2014-07-07 10:30:27 +02:00
|
|
|
end
|
|
|
|
text = get_weather(city)
|
2014-09-14 00:07:02 +02:00
|
|
|
send_msg(receiver, text, ok_cb, false)
|
|
|
|
return
|
2014-07-02 14:30:18 +02:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
|
|
|
|
if string.starts(msg.text, '!echo') then
|
|
|
|
echo = msg.text:sub(7,-1)
|
|
|
|
send_msg(receiver, echo, ok_cb, false)
|
|
|
|
return
|
2014-06-30 12:42:28 +02:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
|
|
|
|
if string.starts(msg.text, '!version') then
|
2014-07-03 21:18:07 +02:00
|
|
|
text = 'James Bot '.. VERSION
|
2014-09-14 00:07:02 +02:00
|
|
|
send_msg(receiver, text, ok_cb, false)
|
|
|
|
return
|
2014-06-30 12:42:28 +02:00
|
|
|
end
|
2014-09-14 00:07:02 +02:00
|
|
|
|
|
|
|
if string.starts(msg.text, '!help') then
|
2014-07-01 22:06:40 +02:00
|
|
|
text = [[!help : print this help
|
2014-06-30 12:42:28 +02:00
|
|
|
!ping : bot sends pong
|
2014-07-04 13:41:44 +02:00
|
|
|
!sh (text) : send commands to bash (only privileged users)
|
2014-07-02 14:30:18 +02:00
|
|
|
!echo (text) : echo the msg
|
2014-06-30 12:42:28 +02:00
|
|
|
!version : version info
|
2014-07-02 14:30:18 +02:00
|
|
|
!cpu : status (uname + top)
|
|
|
|
!fwd : forward msg
|
|
|
|
!forni : send text to group Fornicio
|
|
|
|
!fortune : print a random adage
|
2014-07-07 10:30:27 +02:00
|
|
|
!weather [city] : weather in that city (Madrid if not city)
|
2014-07-04 13:41:44 +02:00
|
|
|
!9gag : send random url image from 9gag
|
2014-08-05 14:07:25 +02:00
|
|
|
!uc3m : fortunes from Universidad Carlos III]]
|
2014-09-14 00:07:02 +02:00
|
|
|
send_msg(receiver, text, ok_cb, false)
|
|
|
|
return
|
2014-06-30 12:42:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function string.starts(String,Start)
|
|
|
|
return string.sub(String,1,string.len(Start))==Start
|
|
|
|
end
|
|
|
|
|
2014-07-04 13:41:44 +02: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.torrent_path then
|
|
|
|
print ("!sh command is enabled")
|
|
|
|
for v,user in pairs(config.sudo_users) do
|
|
|
|
print("Allowed user: " .. user)
|
|
|
|
end
|
|
|
|
end
|
2014-07-07 10:30:27 +02:00
|
|
|
-- print("Torrent path: " .. config.torrent_path)
|
2014-07-04 13:41:44 +02:00
|
|
|
f:close()
|
|
|
|
return config
|
|
|
|
end
|
|
|
|
|
2014-07-03 21:18:07 +02:00
|
|
|
function is_sudo(msg)
|
2014-07-04 13:41:44 +02:00
|
|
|
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
|
|
|
|
|
2014-07-08 20:55:17 +02:00
|
|
|
function write_log_file(msg)
|
|
|
|
name = get_name(msg)
|
|
|
|
ret = name .. ' > ' .. msg.text
|
|
|
|
write_to_file(config.log_file, ret)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Saves a string to file
|
|
|
|
function write_to_file(filename, value)
|
|
|
|
if (value) then
|
2014-07-08 21:00:52 +02:00
|
|
|
local file = io.open(filename,"a")
|
2014-07-08 21:05:01 +02:00
|
|
|
file:write(value, "\n")
|
2014-07-08 20:55:17 +02:00
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_name(msg)
|
2014-07-04 13:41:44 +02:00
|
|
|
local name = msg.from.first_name
|
|
|
|
if name == nil then
|
2014-07-08 20:55:17 +02:00
|
|
|
name = msg.from.id
|
2014-07-04 13:41:44 +02:00
|
|
|
end
|
2014-07-08 20:55:17 +02:00
|
|
|
return name
|
|
|
|
end
|
|
|
|
|
|
|
|
function run_sh(msg)
|
|
|
|
name = get_name(msg)
|
2014-07-04 13:41:44 +02:00
|
|
|
text = ''
|
|
|
|
if config.sh_enabled == false then
|
|
|
|
text = '!sh command is disabled'
|
2014-07-03 21:18:07 +02:00
|
|
|
else
|
2014-07-04 13:41:44 +02:00
|
|
|
if is_sudo(msg) then
|
|
|
|
bash = msg.text:sub(4,-1)
|
|
|
|
text = run_bash(bash)
|
|
|
|
else
|
|
|
|
text = name .. ' you have no power here!'
|
|
|
|
end
|
2014-07-03 21:18:07 +02:00
|
|
|
end
|
2014-07-04 13:41:44 +02:00
|
|
|
return text
|
2014-07-03 21:18:07 +02:00
|
|
|
end
|
|
|
|
|
2014-06-30 12:42:28 +02:00
|
|
|
function run_bash(str)
|
|
|
|
local cmd = io.popen(str)
|
|
|
|
local result = cmd:read('*all')
|
|
|
|
cmd:close()
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2014-07-03 21:18:07 +02:00
|
|
|
function get_fortunes_uc3m()
|
2014-07-08 20:55:17 +02:00
|
|
|
math.randomseed(os.time())
|
2014-07-04 13:41:44 +02:00
|
|
|
local i = math.random(0,178) -- max 178
|
|
|
|
local web = "http://www.gul.es/fortunes/f"..i
|
|
|
|
b, c, h = http.request(web)
|
2014-07-03 21:18:07 +02:00
|
|
|
return b
|
|
|
|
end
|
|
|
|
|
2014-08-06 01:15:54 +02:00
|
|
|
function get_9GAG()
|
|
|
|
b = http.request("http://api-9gag.herokuapp.com/")
|
2014-07-03 21:18:07 +02:00
|
|
|
local gag = json:decode(b)
|
2014-08-05 14:20:53 +02:00
|
|
|
math.randomseed(os.time())
|
2014-08-06 01:15:54 +02:00
|
|
|
i = math.random(#gag) -- random max json table size (# is an operator o.O)
|
|
|
|
local link_image = gag[i].src
|
|
|
|
local title = gag[i].title
|
|
|
|
if link_image:sub(0,2) == '//' then
|
|
|
|
link_image = msg.text:sub(3,-1)
|
|
|
|
end
|
|
|
|
return link_image, title
|
2014-07-03 21:18:07 +02:00
|
|
|
end
|
|
|
|
|
2014-07-02 14:30:18 +02:00
|
|
|
function get_weather(location)
|
|
|
|
b, c, h = http.request("http://api.openweathermap.org/data/2.5/weather?q=" .. location .. "&units=metric")
|
|
|
|
weather = json:decode(b)
|
2014-07-07 10:30:27 +02:00
|
|
|
local city = weather.name
|
|
|
|
local country = weather.sys.country
|
|
|
|
temp = 'The temperature in ' .. city .. ' (' .. country .. ')'
|
|
|
|
temp = temp .. ' is ' .. weather.main.temp .. '°C'
|
2014-07-02 14:30:18 +02:00
|
|
|
conditions = 'Current conditions are: ' .. weather.weather[1].description
|
|
|
|
if weather.weather[1].main == 'Clear' then
|
|
|
|
conditions = conditions .. ' ☀'
|
|
|
|
elseif weather.weather[1].main == 'Clouds' then
|
2014-07-03 21:18:07 +02:00
|
|
|
conditions = conditions .. ' ☁☁'
|
|
|
|
elseif weather.weather[1].main == 'Rain' then
|
|
|
|
conditions = conditions .. ' ☔'
|
|
|
|
elseif weather.weather[1].main == 'Thunderstorm' then
|
|
|
|
conditions = conditions .. ' ☔☔☔☔'
|
2014-07-02 14:30:18 +02:00
|
|
|
end
|
|
|
|
return temp .. '\n' .. conditions
|
|
|
|
end
|
|
|
|
|
2014-07-04 13:41:44 +02:00
|
|
|
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
|
|
|
|
|
2014-08-27 15:50:35 +02:00
|
|
|
function string.get_extension_from_filename( filename )
|
|
|
|
return filename:match( "%.([^%.]+)$" )
|
|
|
|
end
|
|
|
|
|
|
|
|
function string.get_last_word( words )
|
|
|
|
print ('Last word of: ' .. words)
|
|
|
|
local splitted = split_by_space ( words )
|
|
|
|
return splitted[#splitted]
|
|
|
|
end
|
|
|
|
|
|
|
|
function split_by_space ( text )
|
|
|
|
print ('Split: ' .. text)
|
|
|
|
words = {}
|
|
|
|
for word in string.gmatch(text, "[^%s]+") do
|
|
|
|
table.insert(words, word)
|
|
|
|
end
|
|
|
|
return words
|
|
|
|
end
|
|
|
|
|
2014-06-30 12:42:28 +02:00
|
|
|
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
|
2014-07-04 13:41:44 +02:00
|
|
|
|
|
|
|
-- Start and load values
|
|
|
|
config = load_config()
|
|
|
|
our_id = 0
|
|
|
|
now = os.time()
|
2014-09-14 00:07:02 +02:00
|
|
|
|
|
|
|
function get_receiver(msg)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|