This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/bot/bot.lua

331 lines
7.9 KiB
Lua
Raw Normal View History

2014-07-02 14:30:18 +02:00
http = require("socket.http")
json = (loadfile "./bot/JSON.lua")()
2014-08-05 14:07:25 +02:00
VERSION = 'v0.1'
2014-06-30 12:42:28 +02:00
function on_msg_receive (msg)
-- vardump(msg)
2014-08-05 14:07:25 +02:00
-- mark_read(get_receiver(msg))
2014-06-30 12:42:28 +02:00
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-08-05 14:07:25 +02:00
-- write_log_file(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-08-05 14:07:25 +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
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)
send_photo(receiver, file)
2014-08-06 01:15:54 +02:00
send_msg(receiver, title)
2014-07-03 21:18:07 +02:00
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-08-05 14:07:25 +02:00
!uc3m : fortunes from Universidad Carlos III]]
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-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-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-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
2014-08-05 14:07:25 +02:00
print("9gag image"..link_image)
2014-08-06 01:15:54 +02:00
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-08-05 14:07:25 +02:00
function sanitize_html(txt)
2014-07-08 20:55:17 +02:00
local replacements = {
['&' ] = '&amp;',
['<' ] = '&lt;',
['>' ] = '&gt;',
['\n'] = '<br/>'
}
return txt
:gsub('[&<>\n]', replacements)
:gsub(' +', function(s) return ' '..('&nbsp;'):rep(#s-1) end)
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()