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