2014-07-02 14:30:18 +02:00
|
|
|
http = require("socket.http")
|
|
|
|
json = (loadfile "./bot/JSON.lua")()
|
|
|
|
|
2014-07-07 10:30:27 +02:00
|
|
|
VERSION = 'v0.0.7'
|
2014-06-30 12:42:28 +02:00
|
|
|
|
|
|
|
function on_msg_receive (msg)
|
|
|
|
-- vardump(msg)
|
|
|
|
if msg.out then
|
|
|
|
return
|
|
|
|
end
|
2014-07-01 22:06:40 +02:00
|
|
|
if msg.date < now then
|
|
|
|
return
|
|
|
|
end
|
2014-06-30 12:42:28 +02:00
|
|
|
if msg.text == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if msg.unread == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Check if command starts with ! eg !echo
|
|
|
|
if msg.text:sub(0,1) == '!' then
|
|
|
|
msg.text = msg.text:sub(2,-1)
|
|
|
|
do_action(msg)
|
|
|
|
end
|
2014-07-02 14:30:18 +02:00
|
|
|
mark_read(get_receiver(msg))
|
2014-06-30 12:42:28 +02:00
|
|
|
end
|
|
|
|
|
2014-07-01 22:06:40 +02:00
|
|
|
-- Where magic happens
|
2014-06-30 12:42:28 +02:00
|
|
|
function do_action(msg)
|
|
|
|
receiver = get_receiver(msg)
|
2014-07-04 13:41:44 +02:00
|
|
|
|
2014-07-03 21:18:07 +02:00
|
|
|
if string.starts(msg.text, 'sh') then
|
2014-07-04 13:41:44 +02:00
|
|
|
text = run_sh(msg)
|
2014-07-03 21:18:07 +02:00
|
|
|
send_msg(receiver, text)
|
2014-07-04 13:41:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if string.starts(msg.text, 'torrent') then
|
|
|
|
text = save_torrent(msg)
|
|
|
|
send_msg(receiver, text)
|
|
|
|
end
|
|
|
|
|
2014-07-03 21:18:07 +02:00
|
|
|
if string.starts(msg.text, 'uc3m') then
|
|
|
|
text = get_fortunes_uc3m()
|
|
|
|
send_msg(receiver, text)
|
|
|
|
end
|
|
|
|
if string.starts(msg.text, '9gag') then
|
|
|
|
text = get_infiniGAG()
|
|
|
|
send_msg(receiver, text)
|
|
|
|
end
|
2014-07-01 22:06:40 +02:00
|
|
|
if string.starts(msg.text, 'fortune') then
|
|
|
|
text = run_bash('fortune')
|
|
|
|
send_msg(receiver, text)
|
2014-07-03 21:18:07 +02:00
|
|
|
end
|
2014-06-30 12:42:28 +02:00
|
|
|
if string.starts(msg.text, 'forni') then
|
|
|
|
text = msg.text:sub(7,-1)
|
|
|
|
send_msg('Fornicio_2.0', text)
|
2014-07-07 10:30:27 +02:00
|
|
|
end
|
|
|
|
if string.starts(msg.text, 'hackers') then
|
|
|
|
text = msg.text:sub(9,-1)
|
|
|
|
send_msg('Juankers._Dios_existe_y_es_<span_class=', text)
|
2014-06-30 12:42:28 +02:00
|
|
|
end
|
|
|
|
if string.starts(msg.text, 'fwd') then
|
|
|
|
fwd_msg (receiver, msg.id)
|
|
|
|
end
|
|
|
|
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')
|
|
|
|
send_msg(receiver, text)
|
|
|
|
end
|
|
|
|
if string.starts(msg.text, 'ping') then
|
|
|
|
send_msg(receiver, "pong")
|
|
|
|
end
|
2014-07-02 14:30:18 +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
|
|
|
|
city = msg.text:sub(9,-1)
|
|
|
|
end
|
|
|
|
text = get_weather(city)
|
2014-07-02 14:30:18 +02:00
|
|
|
send_msg(receiver, text)
|
|
|
|
end
|
2014-06-30 12:42:28 +02:00
|
|
|
if string.starts(msg.text, 'echo') then
|
|
|
|
-- Removes echo from the string
|
|
|
|
echo = msg.text:sub(6,-1)
|
|
|
|
send_msg(receiver, echo)
|
|
|
|
end
|
|
|
|
if string.starts(msg.text, 'version') then
|
2014-07-03 21:18:07 +02:00
|
|
|
text = 'James Bot '.. VERSION
|
2014-06-30 12:42:28 +02:00
|
|
|
send_msg(receiver, text)
|
|
|
|
end
|
|
|
|
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-07-07 10:30:27 +02:00
|
|
|
!uc3m : fortunes from Universidad Carlos III
|
|
|
|
!hackers : send text to group Juankers]]
|
2014-06-30 12:42:28 +02:00
|
|
|
send_msg(receiver, text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_receiver(msg)
|
|
|
|
if msg.to.type == 'user' then
|
|
|
|
return msg.from.print_name
|
|
|
|
end
|
|
|
|
if msg.to.type == 'chat' then
|
|
|
|
return msg.to.print_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function on_our_id (id)
|
|
|
|
our_id = id
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_secret_chat_created (peer)
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_user_update (user)
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_chat_update (user)
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_get_difference_end ()
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_binlog_replay_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-07 10:30:27 +02:00
|
|
|
--[[ function save_torrent(msg)
|
2014-07-04 13:41:44 +02:00
|
|
|
if is_sudo(msg) == false then
|
|
|
|
local name = msg.from.first_name
|
|
|
|
if name == nil then
|
|
|
|
name = 'Noob '
|
|
|
|
end
|
|
|
|
text = name .. ' you have no power here!'
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
path = msg.text:sub(9,-1)
|
|
|
|
-- Check this is a torrent
|
|
|
|
pattern = 'http://[%w/%.%%%(%)%[%]&_-]+%.torrent'
|
|
|
|
path = string.match(path, pattern)
|
|
|
|
name = string.random(7) .. '.torrent'
|
|
|
|
filePath = config.torrent_path .. name
|
|
|
|
print ("Download ".. path .." to "..filePath)
|
|
|
|
download_to_file(path, filePath)
|
|
|
|
return "Downloaded ".. path .." to "..filePath
|
2014-07-07 10:30:27 +02:00
|
|
|
end ]]--
|
2014-07-04 13:41:44 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
function run_sh(msg)
|
|
|
|
local name = msg.from.first_name
|
|
|
|
if name == nil then
|
|
|
|
name = 'Noob '
|
|
|
|
end
|
|
|
|
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-01 22:06:40 +02:00
|
|
|
function readAll(file)
|
|
|
|
local f = io.open(file, "rb")
|
|
|
|
local content = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
return content
|
|
|
|
end
|
|
|
|
|
2014-07-03 21:18:07 +02:00
|
|
|
function get_fortunes_uc3m()
|
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
|
|
|
|
|
|
|
|
function get_infiniGAG()
|
|
|
|
b, c, h = http.request("http://infinigag-us.aws.af.cm")
|
|
|
|
local gag = json:decode(b)
|
|
|
|
i = math.random(table.getn(gag.data)) -- random
|
|
|
|
local link_image = gag.data[i].images.normal
|
|
|
|
return link_image
|
|
|
|
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-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()
|