indentado

This commit is contained in:
yago 2014-11-03 11:54:03 +01:00
parent 8f5777ea18
commit de2a69206f

View File

@ -1,11 +1,11 @@
http = require("socket.http") http = require("socket.http")
URL = require("socket.url") URL = require("socket.url")
json = (loadfile "./bot/JSON.lua")() json = (loadfile "./bot/JSON.lua")()
VERSION = 'v0.5' VERSION = 'v0.5'
function on_msg_receive (msg) function on_msg_receive (msg)
if msg_valid(msg) == false then if msg_valid(msg) == false then
return return
@ -27,13 +27,13 @@ function on_msg_receive (msg)
mark_read(get_receiver(msg), ok_cb, false) mark_read(get_receiver(msg), ok_cb, false)
-- write_log_file(msg) -- write_log_file(msg)
end end
function ok_cb(extra, success, result) function ok_cb(extra, success, result)
end end
function msg_valid(msg) function msg_valid(msg)
if msg.out then if msg.out then
return false return false
end end
@ -46,30 +46,30 @@ function msg_valid(msg)
if msg.unread == 0 then if msg.unread == 0 then
return false return false
end end
end end
function send_file_from_url (msg) function send_file_from_url (msg)
last = string.get_last_word(msg.text) last = string.get_last_word(msg.text)
file = download_to_file(last) file = download_to_file(last)
send_document(get_receiver(msg), file, ok_cb, false) send_document(get_receiver(msg), file, ok_cb, false)
end end
function send_image_from_url (msg) function send_image_from_url (msg)
last = string.get_last_word(msg.text) last = string.get_last_word(msg.text)
file = download_to_file(last) file = download_to_file(last)
send_photo(get_receiver(msg), file, ok_cb, false) send_photo(get_receiver(msg), file, ok_cb, false)
end end
function is_image_url(text) function is_image_url(text)
last = string.get_last_word(text) last = string.get_last_word(text)
extension = string.get_extension_from_filename(last) -- TODO: Change it please extension = string.get_extension_from_filename(last) -- TODO: Change it please
if extension == 'jpg' or extension == 'png' or extension == 'jpeg' then if extension == 'jpg' or extension == 'png' or extension == 'jpeg' then
return true return true
end end
return false return false
end end
function get_youtube_code( text ) function get_youtube_code( text )
local yt_normal = string.match(text, "youtube.com/watch%?v=([A-Za-z0-9-]+)") local yt_normal = string.match(text, "youtube.com/watch%?v=([A-Za-z0-9-]+)")
if yt_normal then if yt_normal then
return yt_normal return yt_normal
@ -79,26 +79,26 @@ function get_youtube_code( text )
return yt_short return yt_short
end end
return false return false
end end
function send_youtube_thumbnail(msg) function send_youtube_thumbnail(msg)
yt_code = get_youtube_code(msg.text) yt_code = get_youtube_code(msg.text)
yt_thumbnail = "http://img.youtube.com/vi/".. yt_code .."/hqdefault.jpg" yt_thumbnail = "http://img.youtube.com/vi/".. yt_code .."/hqdefault.jpg"
file = download_to_file(yt_thumbnail) file = download_to_file(yt_thumbnail)
send_photo(get_receiver(msg), file, ok_cb, false) send_photo(get_receiver(msg), file, ok_cb, false)
end end
function is_file_url(text) function is_file_url(text)
last = string.get_last_word(text) last = string.get_last_word(text)
extension = string.get_extension_from_filename(last) extension = string.get_extension_from_filename(last)
if extension == 'gif' then if extension == 'gif' then
return true return true
end end
return false return false
end end
-- Where magic happens -- Where magic happens
function do_action(msg) function do_action(msg)
local receiver = get_receiver(msg) local receiver = get_receiver(msg)
if string.starts(msg.text, '!sh') then if string.starts(msg.text, '!sh') then
@ -203,41 +203,41 @@ function do_action(msg)
if string.starts(msg.text, '!version') then if string.starts(msg.text, '!version') then
text = 'James Bot '.. VERSION .. [[ text = 'James Bot '.. VERSION .. [[
Licencia GNU v2, código disponible en http://git.io/6jdjGg Licencia GNU v2, código disponible en http://git.io/6jdjGg
Al Bot le gusta la gente solidaria. Al Bot le gusta la gente solidaria.
Puedes hacer una donación a la ONG que decidas y ayudar a otras personas.]] Puedes hacer una donación a la ONG que decidas y ayudar a otras personas.]]
send_msg(receiver, text, ok_cb, false) send_msg(receiver, text, ok_cb, false)
return return
end end
if string.starts(msg.text, '!help') then if string.starts(msg.text, '!help') then
text = [[!help : print this help text = [[!help : print this help
!ping : bot sends pong !ping : bot sends pong
!sh (text) : send commands to bash (only privileged users) !sh (text) : send commands to bash (only privileged users)
!echo (text) : echo the msg !echo (text) : echo the msg
!version : version info !version : version info
!cpu : status (uname + top) !cpu : status (uname + top)
!fwd : forward msg !fwd : forward msg
!forni : send text to group Fornicio !forni : send text to group Fornicio
!fortune : print a random adage !fortune : print a random adage
!weather [city] : weather in that city (Madrid if not city) !weather [city] : weather in that city (Madrid if not city)
!9gag : send random url image from 9gag !9gag : send random url image from 9gag
!rae (word): Spanish dictionary !rae (word): Spanish dictionary
!eur : EURUSD market value !eur : EURUSD market value
!img (text) : search image with Google API and sends it !img (text) : search image with Google API and sends it
!uc3m : fortunes from Universidad Carlos III]] !uc3m : fortunes from Universidad Carlos III]]
send_msg(receiver, text, ok_cb, false) send_msg(receiver, text, ok_cb, false)
return return
end end
end end
function string.starts(String,Start) function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start return string.sub(String,1,string.len(Start))==Start
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)
@ -250,9 +250,9 @@ function load_config()
-- print("Torrent path: " .. config.torrent_path) -- print("Torrent path: " .. config.torrent_path)
f:close() f:close()
return config return config
end end
function save_value( text ) function save_value( text )
local vars = split_by_space(text) local vars = split_by_space(text)
if (#vars < 2) then if (#vars < 2) then
return "Usage: !set var_name value" return "Usage: !set var_name value"
@ -263,17 +263,17 @@ function save_value( text )
file:write(json_text) file:write(json_text)
file:close() file:close()
return "Saved "..vars[1].."="..vars[2] return "Saved "..vars[1].."="..vars[2]
end end
function get_value( value_name ) function get_value( value_name )
local value = config.values[value_name] local value = config.values[value_name]
if ( value == nil) then if ( value == nil) then
return "Cant find "..value_name return "Cant find "..value_name
end end
return value_name.." = "..value return value_name.." = "..value
end end
function is_sudo(msg) function is_sudo(msg)
local var = false local var = false
-- Check users id in config -- Check users id in config
for v,user in pairs(config.sudo_users) do for v,user in pairs(config.sudo_users) do
@ -282,32 +282,32 @@ function is_sudo(msg)
end end
end end
return var return var
end end
function write_log_file(msg) function write_log_file(msg)
name = get_name(msg) name = get_name(msg)
ret = name .. ' > ' .. msg.text ret = name .. ' > ' .. msg.text
write_to_file(config.log_file, ret) write_to_file(config.log_file, ret)
end end
-- Saves a string to file -- Saves a string to file
function write_to_file(filename, value) function write_to_file(filename, value)
if (value) then if (value) then
local file = io.open(filename,"a") local file = io.open(filename,"a")
file:write(value, "\n") file:write(value, "\n")
file:close() file:close()
end end
end end
function get_name(msg) function get_name(msg)
local name = msg.from.first_name local name = msg.from.first_name
if name == nil then if name == nil then
name = msg.from.id name = msg.from.id
end end
return name return name
end end
function run_sh(msg) function run_sh(msg)
name = get_name(msg) name = get_name(msg)
text = '' text = ''
if config.sh_enabled == false then if config.sh_enabled == false then
@ -321,24 +321,24 @@ function run_sh(msg)
end end
end end
return text return text
end end
function run_bash(str) function run_bash(str)
local cmd = io.popen(str) local cmd = io.popen(str)
local result = cmd:read('*all') local result = cmd:read('*all')
cmd:close() cmd:close()
return result return result
end end
function get_fortunes_uc3m() function get_fortunes_uc3m()
math.randomseed(os.time()) math.randomseed(os.time())
local i = math.random(0,178) -- max 178 local i = math.random(0,178) -- max 178
local web = "http://www.gul.es/fortunes/f"..i local web = "http://www.gul.es/fortunes/f"..i
b, c, h = http.request(web) b, c, h = http.request(web)
return b return b
end end
function getDulcinea( text ) function getDulcinea( text )
-- Powered by https://github.com/javierhonduco/dulcinea -- Powered by https://github.com/javierhonduco/dulcinea
local api = "http://dulcinea.herokuapp.com/api/?query=" local api = "http://dulcinea.herokuapp.com/api/?query="
b = http.request(api..text) b = http.request(api..text)
@ -370,9 +370,9 @@ function getDulcinea( text )
end end
return text return text
end end
function getGoogleImage(text) function getGoogleImage(text)
text = URL.escape(text) text = URL.escape(text)
for i = 1, 5, 1 do -- Try 5 times for i = 1, 5, 1 do -- Try 5 times
local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q=" local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q="
@ -385,9 +385,9 @@ function getGoogleImage(text)
return google.responseData.results[i].url return google.responseData.results[i].url
end end
end end
end end
function get_9GAG() function get_9GAG()
b = http.request("http://api-9gag.herokuapp.com/") b = http.request("http://api-9gag.herokuapp.com/")
local gag = json:decode(b) local gag = json:decode(b)
math.randomseed(os.time()) math.randomseed(os.time())
@ -398,9 +398,9 @@ function get_9GAG()
link_image = msg.text:sub(3,-1) link_image = msg.text:sub(3,-1)
end end
return link_image, title return link_image, title
end end
function getEURUSD(text) function getEURUSD(text)
b = http.request("http://webrates.truefx.com/rates/connect.html?c=EUR/USD&f=csv&s=n") b = http.request("http://webrates.truefx.com/rates/connect.html?c=EUR/USD&f=csv&s=n")
local rates = b:split(", ") local rates = b:split(", ")
local symbol = rates[1] local symbol = rates[1]
@ -414,10 +414,10 @@ function getEURUSD(text)
text = text.."\n "..usd.."USD = "..eur.."EUR" text = text.."\n "..usd.."USD = "..eur.."EUR"
end end
return text return text
end end
function download_to_file( url ) function download_to_file( url )
print("url a descargar: "..url) print("url a descargar: "..url)
req, c, h = http.request(url) req, c, h = http.request(url)
htype = h["content-type"] htype = h["content-type"]
@ -444,9 +444,9 @@ function download_to_file( url )
file:write(req) file:write(req)
file:close() file:close()
return file_path return file_path
end end
function get_weather(location) function get_weather(location)
b, c, h = http.request("http://api.openweathermap.org/data/2.5/weather?q=" .. location .. "&units=metric") b, c, h = http.request("http://api.openweathermap.org/data/2.5/weather?q=" .. location .. "&units=metric")
weather = json:decode(b) weather = json:decode(b)
local city = weather.name local city = weather.name
@ -464,9 +464,9 @@ function get_weather(location)
conditions = conditions .. ' ☔☔☔☔' conditions = conditions .. ' ☔☔☔☔'
end end
return temp .. '\n' .. conditions return temp .. '\n' .. conditions
end end
function string.random(length) function string.random(length)
math.randomseed(os.time()) math.randomseed(os.time())
local str = ""; local str = "";
for i = 1, length do for i = 1, length do
@ -474,33 +474,33 @@ function string.random(length)
str = str..string.char(math.random(97, 122)); str = str..string.char(math.random(97, 122));
end end
return str; return str;
end end
function string.get_extension_from_filename( filename ) function string.get_extension_from_filename( filename )
return filename:match( "%.([^%.]+)$" ) return filename:match( "%.([^%.]+)$" )
end end
function string.get_last_word( words ) function string.get_last_word( words )
local splitted = split_by_space ( words ) local splitted = split_by_space ( words )
return splitted[#splitted] return splitted[#splitted]
end end
function split_by_space ( text ) function split_by_space ( text )
words = {} words = {}
for word in string.gmatch(text, "[^%s]+") do for word in string.gmatch(text, "[^%s]+") do
table.insert(words, word) table.insert(words, word)
end end
return words return words
end end
function string:split(sep) function string:split(sep)
local sep, fields = sep or ":", {} local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep) local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end) self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields return fields
end end
function vardump(value, depth, key) function vardump(value, depth, key)
local linePrefix = "" local linePrefix = ""
local spaces = "" local spaces = ""
@ -535,42 +535,42 @@ function vardump(value, depth, key)
else else
print(spaces..linePrefix.."("..type(value)..") "..tostring(value)) print(spaces..linePrefix.."("..type(value)..") "..tostring(value))
end end
end end
-- Start and load values -- Start and load values
config = load_config() config = load_config()
our_id = 0 our_id = 0
now = os.time() now = os.time()
function get_receiver(msg) function get_receiver(msg)
if msg.to.type == 'user' then if msg.to.type == 'user' then
return 'user#id'..msg.from.id return 'user#id'..msg.from.id
end end
if msg.to.type == 'chat' then if msg.to.type == 'chat' then
return 'chat#id'..msg.to.id 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