Merge pull request #1 from yagop/master

Updating to latest
This commit is contained in:
soend 2015-03-12 09:46:42 +02:00
commit 061dee2fa2
12 changed files with 241 additions and 101 deletions

View File

@ -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

View File

@ -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

View File

@ -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
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_name = get_http_file_name(url, h)
local file_path = "/tmp/"..file_name
print("Saved to: "..file_path)
@ -135,6 +167,7 @@ function run_command(str)
return result
end
-- User has priviledges
function is_sudo(msg)
local var = false
-- Check users id in config
@ -214,11 +247,17 @@ function string:isblank()
return self:isempty()
end
-- Returns true if String starts with Start
-- DEPRECATED!!!!!
function string.starts(String, 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.
-- cb_function and cb_extra are optionals callback
function _send_photo(receiver, file_path, cb_function, cb_extra)
@ -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)
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)
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, cb_function, cb_extra)
_send_photo(receiver, file_path, ok_cb, false)
end
end
-- Send multimple images asynchronous.

71
plugins/boobs.lua Normal file
View File

@ -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

View File

@ -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

View File

@ -1,3 +1,4 @@
do
function run(msg, matches)
return "Hello, " .. matches[1]
@ -13,3 +14,4 @@ return {
run = run
}
end

View File

@ -10,9 +10,9 @@ 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
}

View File

@ -1,3 +1,4 @@
do
function run(msg, matches)
file = download_to_file(matches[1])
@ -8,16 +9,18 @@ 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)$"
"(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

View File

@ -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+)",