Merge pull request #1 from yagop/master

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

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