diff --git a/LICENSE b/LICENSE index 22fbe5d..27b4e5e 100644 --- a/LICENSE +++ b/LICENSE @@ -291,7 +291,7 @@ convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. {description} - Copyright (C) {year} {fullname} + Copyright (C) 2015 Yago PĂ©rez This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -336,4 +336,4 @@ This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. \ No newline at end of file +Public License instead of this License. diff --git a/bot/bot.lua b/bot/bot.lua index b162ffa..3822f04 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -1,11 +1,6 @@ -http = require("socket.http") -https = require("ssl.https") -URL = require("socket.url") -json = (loadfile "./libs/JSON.lua")() -serpent = (loadfile "./libs/serpent.lua")() require("./bot/utils") -VERSION = '0.9.3' +VERSION = '0.9.5' function on_msg_receive (msg) vardump(msg) @@ -91,7 +86,7 @@ function do_action(msg) -- print("Trying", text, "against", pattern) matches = { string.match(text, pattern) } if matches[1] then - mark_read(get_receiver(msg), ok_cb, false) + mark_read(receiver, ok_cb, false) print(" matches", pattern) if desc.run ~= nil then -- If plugin is for privileged user @@ -230,4 +225,4 @@ end -- Start and load values our_id = 0 now = os.time() -math.randomseed(now) \ No newline at end of file +math.randomseed(now) diff --git a/bot/utils.lua b/bot/utils.lua index 74d2da4..6b83656 100644 --- a/bot/utils.lua +++ b/bot/utils.lua @@ -1,5 +1,10 @@ -local mimetype = (loadfile "./libs/mimetype.lua")() -local ltn12 = require "ltn12" +http = require "socket.http" +https = require "ssl.https" +ltn12 = require "ltn12" +URL = require "socket.url" +json = (loadfile "./libs/JSON.lua")() +serpent = (loadfile "./libs/serpent.lua")() +mimetype = (loadfile "./libs/mimetype.lua")() function get_receiver(msg) if msg.to.type == 'user' then @@ -8,6 +13,9 @@ function get_receiver(msg) if msg.to.type == 'chat' then return 'chat#id'..msg.to.id end + if msg.to.type == 'encr_chat' then + return msg.to.print_name + end end function is_chat_msg( msg ) @@ -33,11 +41,17 @@ function string:split(sep) return fields end --- Removes spaces +-- DEPRECATED function string.trim(s) + print("string.trim(s) is DEPRECATED use string:trim() instead") return s:gsub("^%s*(.-)%s*$", "%1") end +-- Removes spaces +function string:trim() + return self:gsub("^%s*(.-)%s*$", "%1") +end + function get_http_file_name(url, headers) -- Everything after the last / local file_name = url:match("([^/]+)$") @@ -46,7 +60,10 @@ function get_http_file_name(url, headers) content_type = content_type or headers["Content-type"] content_type = content_type or h["Content-Type"] - local extension = mimetype.get_mime_extension(content_type) + local extension = nil + if content_type then + extension = mimetype.get_mime_extension(content_type) + end if extension then file_name = file_name.."."..extension end @@ -66,9 +83,24 @@ function download_to_file(url, file_name) redirect = true } - local one, c, h = http.request(options) + -- nil, code, headers, status + local response = nil - local file_name = get_http_file_name(url, h) + if url:starts('https') then + options.redirect = false + response = {https.request(options)} + else + response = {http.request(options)} + end + + local code = response[2] + local headers = response[3] + local status = response[4] + + if code ~= 200 then return nil end + + file_name = file_name or get_http_file_name(url, headers) + local file_path = "/tmp/"..file_name print("Saved to: "..file_path) @@ -135,15 +167,16 @@ function run_command(str) return result end +-- User has priviledges function is_sudo(msg) - 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 + 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 is_disabled(msg) @@ -162,11 +195,11 @@ end -- Returns the name of the sender function get_name(msg) - local name = msg.from.first_name - if name == nil then - name = msg.from.id - end - return name + local name = msg.from.first_name + if name == nil then + name = msg.from.id + end + return name end -- Returns at table of lua files inside plugins @@ -214,9 +247,15 @@ function string:isblank() return self:isempty() end --- Returns true if String starts with Start +-- DEPRECATED!!!!! function string.starts(String, Start) - return Start == string.sub(String,1,string.len(Start)) + print("string.starts(String, Start) is DEPRECATED use string:starts(text) instead") + return Start == string.sub(String,1,string.len(Start)) +end + +-- Returns true if String starts with Start +function string:starts(text) + return text == string.sub(self,1,string.len(text)) end -- Send image to user and delete it when finished. @@ -234,9 +273,18 @@ end -- Download the image and send to receiver, it will be deleted. -- cb_function and cb_extra are optionals callback function send_photo_from_url(receiver, url, cb_function, cb_extra) + -- If callback not provided + cb_function = cb_function or ok_cb + cb_extra = cb_extra or false + local file_path = download_to_file(url, false) - print("File path: "..file_path) - _send_photo(receiver, file_path, cb_function, cb_extra) + if not file_path then -- Error + local text = 'Error downloading the image' + send_msg(receiver, text, cb_function, cb_extra) + else + print("File path: "..file_path) + _send_photo(receiver, file_path, cb_function, cb_extra) + end end -- Same as send_photo_from_url but as callback function @@ -245,8 +293,13 @@ function send_photo_from_url_callback(cb_extra, success, result) local url = cb_extra.url local file_path = download_to_file(url, false) - print("File path: "..file_path) - _send_photo(receiver, file_path, cb_function, cb_extra) + if not file_path then -- Error + local text = 'Error downloading the image' + send_msg(receiver, text, ok_cb, false) + else + print("File path: "..file_path) + _send_photo(receiver, file_path, ok_cb, false) + end end -- Send multimple images asynchronous. @@ -325,4 +378,4 @@ function send_document_from_url(receiver, url, cb_function, cb_extra) local file_path = download_to_file(url, false) print("File path: "..file_path) _send_document(receiver, file_path, cb_function, cb_extra) -end \ No newline at end of file +end diff --git a/plugins/boobs.lua b/plugins/boobs.lua new file mode 100644 index 0000000..ba4edab --- /dev/null +++ b/plugins/boobs.lua @@ -0,0 +1,71 @@ +do + +function getRandomButts(attempt) + attempt = attempt or 0 + attempt = attempt + 1 + + local res,status = http.request("http://api.obutts.ru/noise/1") + + if status ~= 200 then return nil end + local data = json:decode(res)[1] + + -- The OpenBoobs API sometimes returns an empty array + if not data and attempt < 10 then + print('Cannot get that butts, trying another ones...') + return getRandomButts(attempt) + end + + return 'http://media.obutts.ru/' .. data.preview +end + +function getRandomBoobs(attempt) + attempt = attempt or 0 + attempt = attempt + 1 + + local res,status = http.request("http://api.oboobs.ru/noise/1") + + if status ~= 200 then return nil end + local data = json:decode(res)[1] + + -- The OpenBoobs API sometimes returns an empty array + if not data and attempt < 10 then + print('Cannot get that boobs, trying another ones...') + return getRandomBoobs(attempt) + end + + return 'http://media.oboobs.ru/' .. data.preview +end + +function run(msg, matches) + local url = nil + + if matches[1] == "!boobs" then + url = getRandomBoobs() + end + + if matches[1] == "!butts" then + url = getRandomButts() + end + + if url ~= nil then + local receiver = get_receiver(msg) + send_photo_from_url(receiver, url) + else + return 'Error getting boobs/butts for you, please try again later.' + end +end + +return { + description = "Gets a random boobs or butts pic", + usage = { + "!boobs", + "!butts" + }, + patterns = { + "^!boobs$", + "^!butts$" + }, + run = run +} + +end diff --git a/plugins/giphy.lua b/plugins/giphy.lua index b10e72f..6d684fd 100644 --- a/plugins/giphy.lua +++ b/plugins/giphy.lua @@ -6,24 +6,40 @@ do local BASE_URL = 'http://api.giphy.com/v1' local API_KEY = 'dc6zaTOxFJmzC' -- public beta key -function get_random_top() - local res, code = http.request(BASE_URL.."/gifs/trending?api_key="..API_KEY) - if code ~= 200 then return nil end - local images = json:decode(res).data +function get_image(response) + local images = json:decode(response).data + if #images == 0 then return nil end -- No images local i = math.random(0,#images) - return images[i].images.downsized.url + local image = images[i] -- A random one + + if image.images.downsized then + return image.images.downsized.url + end + + if image.images.original then + return image.original.url + end + + return nil +end + +function get_random_top() + local url = BASE_URL.."/gifs/trending?api_key="..API_KEY + local response, code = http.request(url) + if code ~= 200 then return nil end + return get_image(response) end function search(text) - local res, code = http.request(BASE_URL.."/gifs/search?q="..text.."&api_key="..API_KEY) - local images = json:decode(res).data - if #images == 0 then return nil end -- No images - local i = math.random(0,#images) - return images[i].images.downsized.url + text = URL.escape(text) + local url = BASE_URL.."/gifs/search?q="..text.."&api_key="..API_KEY + local response, code = http.request(url) + if code ~= 200 then return nil end + return get_image(response) end function run(msg, matches) - local gif_url = '' + local gif_url = nil -- If no search data, a random trending GIF will be sended if matches[1] == "!gif" or matches[1] == "!giphy" then diff --git a/plugins/google.lua b/plugins/google.lua index 61f6b2e..5e3434b 100644 --- a/plugins/google.lua +++ b/plugins/google.lua @@ -22,17 +22,17 @@ function stringlinks(results) end function run(msg, matches) - vardump(matches) - local results = googlethat(matches[1]) - return stringlinks(results) + vardump(matches) + local results = googlethat(matches[1]) + return stringlinks(results) end return { - description = "Searches Google and send results", - usage = "!google [terms]: Searches Google and send results", - patterns = { - "^!google (.*)$", - "^%.[g|G]oogle (.*)$" - }, - run = run + description = "Searches Google and send results", + usage = "!google [terms]: Searches Google and send results", + patterns = { + "^!google (.*)$", + "^%.[g|G]oogle (.*)$" + }, + run = run } diff --git a/plugins/hello.lua b/plugins/hello.lua index f9b13ba..706b3d1 100644 --- a/plugins/hello.lua +++ b/plugins/hello.lua @@ -1,15 +1,17 @@ +do function run(msg, matches) return "Hello, " .. matches[1] end return { - description = "Says hello to someone", - usage = "say hello to [name]", - patterns = { - "^say hello to (.*)$", - "^Say hello to (.*)$" - }, - run = run + description = "Says hello to someone", + usage = "say hello to [name]", + patterns = { + "^say hello to (.*)$", + "^Say hello to (.*)$" + }, + run = run } +end \ No newline at end of file diff --git a/plugins/help.lua b/plugins/help.lua index c671ff1..186a2af 100644 --- a/plugins/help.lua +++ b/plugins/help.lua @@ -61,14 +61,14 @@ function run(msg, matches) end return { - description = "Help plugin. Get info from other plugins. ", - usage = { - "!help: Show all the help", - "!help md: Generate a GitHub Markdown table" - }, - patterns = { - "^!help$", - "^!help md$" - }, - run = run + description = "Help plugin. Get info from other plugins. ", + usage = { + "!help: Show all the help", + "!help md: Generate a GitHub Markdown table" + }, + patterns = { + "^!help$", + "^!help md$" + }, + run = run } \ No newline at end of file diff --git a/plugins/images.lua b/plugins/images.lua index bf342f6..1b89ee2 100644 --- a/plugins/images.lua +++ b/plugins/images.lua @@ -10,11 +10,11 @@ return { description = "When user sends image URL (ends with png, jpg, jpeg) download and send it to origin.", usage = "", patterns = { - "(https?://[%w-_%.%?%.:/%+=&]+.png)$", - "(https?://[%w-_%.%?%.:/%+=&]+.jpg)$", - "(https?://[%w-_%.%?%.:/%+=&]+.jpeg)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.png)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.jpg)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.jpeg)$", }, run = run } -end \ No newline at end of file +end diff --git a/plugins/img_google.lua b/plugins/img_google.lua index 5335082..3e7c552 100644 --- a/plugins/img_google.lua +++ b/plugins/img_google.lua @@ -26,10 +26,10 @@ function run(msg, matches) end return { - description = "Search image with Google API and sends it.", - usage = "!img [term]: Random search an image with Google API.", - patterns = {"^!img (.*)$"}, - run = run + description = "Search image with Google API and sends it.", + usage = "!img [term]: Random search an image with Google API.", + patterns = {"^!img (.*)$"}, + run = run } end \ No newline at end of file diff --git a/plugins/media.lua b/plugins/media.lua index 64c8175..ce2c3fb 100644 --- a/plugins/media.lua +++ b/plugins/media.lua @@ -1,23 +1,26 @@ +do function run(msg, matches) - file = download_to_file(matches[1]) - send_document(get_receiver(msg), file, ok_cb, false) + file = download_to_file(matches[1]) + send_document(get_receiver(msg), file, ok_cb, false) end return { - description = "When user sends media URL (ends with gif, mp4, pdf, etc.) download and send it to origin.", - usage = "", - patterns = { - "(https?://[%w-_%.%?%.:/%+=&]+.gif)$", - "(https?://[%w-_%.%?%.:/%+=&]+.mp4)$", - "(https?://[%w-_%.%?%.:/%+=&]+.pdf)$", - "(https?://[%w-_%.%?%.:/%+=&]+.ogg)$", - "(https?://[%w-_%.%?%.:/%+=&]+.zip)$", - "(https?://[%w-_%.%?%.:/%+=&]+.mp3)$", - "(https?://[%w-_%.%?%.:/%+=&]+.rar)$", - "(https?://[%w-_%.%?%.:/%+=&]+.wmv)$", - "(https?://[%w-_%.%?%.:/%+=&]+.doc)$", - "(https?://[%w-_%.%?%.:/%+=&]+.avi)$" - }, - run = run -} \ No newline at end of file + description = "When user sends media URL (ends with gif, mp4, pdf, etc.) download and send it to origin.", + usage = "", + patterns = { + "(https?://[%w-_%.%?%.:/%+=&]+%.gif)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.mp4)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.pdf)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.ogg)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.zip)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.mp3)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.rar)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.wmv)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.doc)$", + "(https?://[%w-_%.%?%.:/%+=&]+%.avi)$" + }, + run = run +} + +end \ No newline at end of file diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua index d00037b..0354b62 100644 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -44,7 +44,7 @@ end return { description = "Send comic images from xkcd", - usage = {"!xkcd (id): Send an xkcd image and tigle. If not id, send a random one"}, + usage = {"!xkcd (id): Send an xkcd image and title. If not id, send a random one"}, patterns = { "^!xkcd$", "^!xkcd (%d+)",