2015-06-23 16:42:40 +02:00
|
|
|
URL = require "socket.url"
|
2015-03-10 20:40:08 +01:00
|
|
|
http = require "socket.http"
|
|
|
|
https = require "ssl.https"
|
|
|
|
ltn12 = require "ltn12"
|
2015-06-23 16:42:40 +02:00
|
|
|
serpent = require "serpent"
|
2015-06-02 21:52:21 +02:00
|
|
|
feedparser = require "feedparser"
|
2015-03-10 20:40:08 +01:00
|
|
|
json = (loadfile "./libs/JSON.lua")()
|
|
|
|
mimetype = (loadfile "./libs/mimetype.lua")()
|
2015-05-28 16:47:30 +02:00
|
|
|
redis = (loadfile "./libs/redis.lua")()
|
2015-02-15 14:06:01 +01:00
|
|
|
|
2015-04-16 20:16:37 +02:00
|
|
|
http.TIMEOUT = 10
|
|
|
|
|
2014-12-14 21:28:32 +01:00
|
|
|
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
|
2015-03-10 20:35:19 +01:00
|
|
|
if msg.to.type == 'encr_chat' then
|
|
|
|
return msg.to.print_name
|
|
|
|
end
|
2014-12-14 21:28:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function is_chat_msg( msg )
|
|
|
|
if msg.to.type == 'chat' then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-12-14 20:52:48 +01:00
|
|
|
function string.random(length)
|
|
|
|
local str = "";
|
|
|
|
for i = 1, length do
|
|
|
|
math.random(97, 122)
|
|
|
|
str = str..string.char(math.random(97, 122));
|
|
|
|
end
|
|
|
|
return str;
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-02-28 14:53:39 +01:00
|
|
|
-- DEPRECATED
|
2015-01-31 16:46:30 +01:00
|
|
|
function string.trim(s)
|
2015-02-28 14:53:39 +01:00
|
|
|
print("string.trim(s) is DEPRECATED use string:trim() instead")
|
2015-01-31 16:46:30 +01:00
|
|
|
return s:gsub("^%s*(.-)%s*$", "%1")
|
|
|
|
end
|
|
|
|
|
2015-02-28 14:53:39 +01:00
|
|
|
-- Removes spaces
|
|
|
|
function string:trim()
|
|
|
|
return self:gsub("^%s*(.-)%s*$", "%1")
|
|
|
|
end
|
|
|
|
|
2015-02-15 16:52:08 +01:00
|
|
|
function get_http_file_name(url, headers)
|
2015-05-28 16:47:30 +02:00
|
|
|
-- Eg: foo.var
|
2015-04-16 20:16:37 +02:00
|
|
|
local file_name = url:match("[^%w]+([%.%w]+)$")
|
2015-05-28 16:47:30 +02:00
|
|
|
-- Any delimited alphanumeric on the url
|
2015-04-16 20:16:37 +02:00
|
|
|
file_name = file_name or url:match("[^%w]+(%w+)[^%w]+$")
|
|
|
|
-- Random name, hope content-type works
|
|
|
|
file_name = file_name or str:random(5)
|
2015-06-23 16:42:40 +02:00
|
|
|
|
|
|
|
local content_type = headers["content-type"]
|
2015-02-15 16:52:08 +01:00
|
|
|
|
2015-02-27 22:20:05 +01:00
|
|
|
local extension = nil
|
|
|
|
if content_type then
|
|
|
|
extension = mimetype.get_mime_extension(content_type)
|
|
|
|
end
|
2015-02-15 16:52:08 +01:00
|
|
|
if extension then
|
|
|
|
file_name = file_name.."."..extension
|
|
|
|
end
|
2015-06-23 16:42:40 +02:00
|
|
|
|
|
|
|
local disposition = headers["content-disposition"]
|
|
|
|
if disposition then
|
|
|
|
-- attachment; filename=CodeCogsEqn.png
|
|
|
|
file_name = disposition:match('filename=([^;]+)') or file_name
|
2015-07-21 22:08:58 +02:00
|
|
|
file_name = string.gsub(file_name, "\"", "")
|
2015-06-23 16:42:40 +02:00
|
|
|
end
|
|
|
|
|
2015-02-15 16:52:08 +01:00
|
|
|
return file_name
|
|
|
|
end
|
|
|
|
|
2015-06-23 16:42:40 +02:00
|
|
|
-- Saves file to tmp/. If file_name isn't provided,
|
|
|
|
-- will get the text after the last "/" for filename
|
2015-02-15 16:52:08 +01:00
|
|
|
-- and content-type for extension
|
2015-02-15 14:06:01 +01:00
|
|
|
function download_to_file(url, file_name)
|
2015-05-09 16:35:22 +02:00
|
|
|
print("Download URL: "..url)
|
2015-02-15 14:06:01 +01:00
|
|
|
|
2014-12-21 18:19:33 +01:00
|
|
|
local respbody = {}
|
2015-02-15 14:06:01 +01:00
|
|
|
local options = {
|
|
|
|
url = url,
|
|
|
|
sink = ltn12.sink.table(respbody),
|
|
|
|
redirect = true
|
|
|
|
}
|
|
|
|
|
2015-02-28 14:09:25 +01:00
|
|
|
-- 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]
|
|
|
|
|
2015-02-28 14:22:53 +01:00
|
|
|
if code ~= 200 then return nil end
|
|
|
|
|
2015-02-28 14:09:25 +01:00
|
|
|
file_name = file_name or get_http_file_name(url, headers)
|
|
|
|
|
2015-05-28 16:47:30 +02:00
|
|
|
local file_path = "tmp/"..file_name
|
2015-05-09 16:35:22 +02:00
|
|
|
print("Gespeichert in: "..file_path)
|
2015-02-02 22:22:05 +01:00
|
|
|
|
2014-12-14 20:52:48 +01:00
|
|
|
file = io.open(file_path, "w+")
|
2014-12-21 18:19:33 +01:00
|
|
|
file:write(table.concat(respbody))
|
2014-12-14 20:52:48 +01:00
|
|
|
file:close()
|
|
|
|
|
|
|
|
return file_path
|
|
|
|
end
|
|
|
|
|
2015-04-26 19:16:44 +02:00
|
|
|
function vardump(value)
|
|
|
|
print(serpent.block(value, {comment=false}))
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- taken from http://stackoverflow.com/a/11130774/3163199
|
|
|
|
function scandir(directory)
|
|
|
|
local i, t, popen = 0, {}, io.popen
|
|
|
|
for filename in popen('ls -a "'..directory..'"'):lines() do
|
2015-04-26 19:16:44 +02:00
|
|
|
i = i + 1
|
|
|
|
t[i] = filename
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
|
|
|
return t
|
2014-12-17 13:01:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- http://www.lua.org/manual/5.2/manual.html#pdf-io.popen
|
|
|
|
function run_command(str)
|
|
|
|
local cmd = io.popen(str)
|
|
|
|
local result = cmd:read('*all')
|
|
|
|
cmd:close()
|
|
|
|
return result
|
|
|
|
end
|
2014-12-22 21:52:11 +01:00
|
|
|
|
2015-05-28 16:47:30 +02:00
|
|
|
-- User has privileges
|
2014-12-22 21:52:11 +01:00
|
|
|
function is_sudo(msg)
|
2015-02-28 14:53:39 +01:00
|
|
|
local var = false
|
2015-06-23 16:42:40 +02:00
|
|
|
-- Check users id in config
|
|
|
|
for v,user in pairs(_config.sudo_users) do
|
|
|
|
if user == msg.from.id then
|
|
|
|
var = true
|
2015-02-28 14:53:39 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return var
|
2014-12-22 21:52:11 +01:00
|
|
|
end
|
|
|
|
|
2015-05-03 13:51:33 +02:00
|
|
|
function can_use_bot(msg)
|
|
|
|
local var = false
|
|
|
|
-- Check users id in config
|
|
|
|
for v,user in pairs(_config.can_use_bot) do
|
|
|
|
if user == msg.from.id then
|
|
|
|
var = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return var
|
|
|
|
end
|
|
|
|
|
2014-12-24 01:38:41 +01:00
|
|
|
-- Returns the name of the sender
|
2014-12-22 21:52:11 +01:00
|
|
|
function get_name(msg)
|
2015-02-28 14:53:39 +01:00
|
|
|
local name = msg.from.first_name
|
|
|
|
if name == nil then
|
|
|
|
name = msg.from.id
|
|
|
|
end
|
|
|
|
return name
|
2014-12-24 01:38:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns at table of lua files inside plugins
|
|
|
|
function plugins_names( )
|
|
|
|
local files = {}
|
|
|
|
for k, v in pairs(scandir("plugins")) do
|
|
|
|
-- Ends with .lua
|
|
|
|
if (v:match(".lua$")) then
|
|
|
|
table.insert(files, v)
|
2015-06-23 16:42:40 +02:00
|
|
|
end
|
2014-12-24 01:38:41 +01:00
|
|
|
end
|
|
|
|
return files
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Function name explains what it does.
|
|
|
|
function file_exists(name)
|
|
|
|
local f = io.open(name,"r")
|
2015-06-23 16:42:40 +02:00
|
|
|
if f ~= nil then
|
|
|
|
io.close(f)
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
2014-12-24 01:38:41 +01:00
|
|
|
end
|
2015-01-01 16:06:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Save into file the data serialized for lua.
|
2015-04-16 20:16:37 +02:00
|
|
|
-- Set uglify true to minify the file.
|
|
|
|
function serialize_to_file(data, file, uglify)
|
2015-01-01 16:06:24 +01:00
|
|
|
file = io.open(file, 'w+')
|
2015-04-16 20:16:37 +02:00
|
|
|
local serialized
|
|
|
|
if not uglify then
|
|
|
|
serialized = serpent.block(data, {
|
|
|
|
comment = false,
|
|
|
|
name = '_'
|
|
|
|
})
|
|
|
|
else
|
|
|
|
serialized = serpent.dump(data)
|
|
|
|
end
|
2015-01-01 16:06:24 +01:00
|
|
|
file:write(serialized)
|
|
|
|
file:close()
|
2015-01-06 14:10:26 +01:00
|
|
|
end
|
|
|
|
|
2015-05-28 16:47:30 +02:00
|
|
|
-- Returns true if the string is empty
|
2015-01-06 14:10:26 +01:00
|
|
|
function string:isempty()
|
|
|
|
return self == nil or self == ''
|
2015-02-02 22:22:05 +01:00
|
|
|
end
|
|
|
|
|
2015-05-28 16:47:30 +02:00
|
|
|
-- Returns true if the string is blank
|
2015-02-22 03:31:30 +01:00
|
|
|
function string:isblank()
|
|
|
|
self = self:trim()
|
|
|
|
return self:isempty()
|
|
|
|
end
|
2015-02-02 22:22:05 +01:00
|
|
|
|
2015-02-28 14:09:25 +01:00
|
|
|
-- DEPRECATED!!!!!
|
2015-02-02 22:22:05 +01:00
|
|
|
function string.starts(String, Start)
|
2015-02-28 14:53:39 +01:00
|
|
|
print("string.starts(String, Start) is DEPRECATED use string:starts(text) instead")
|
2015-02-28 14:09:25 +01:00
|
|
|
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))
|
2015-02-15 14:06:01 +01:00
|
|
|
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)
|
|
|
|
local cb_extra = {
|
|
|
|
file_path = file_path,
|
|
|
|
cb_function = cb_function,
|
|
|
|
cb_extra = cb_extra
|
|
|
|
}
|
|
|
|
-- Call to remove with optional callback
|
|
|
|
send_photo(receiver, file_path, rmtmp_cb, cb_extra)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Download the image and send to receiver, it will be deleted.
|
2015-02-15 14:37:23 +01:00
|
|
|
-- cb_function and cb_extra are optionals callback
|
|
|
|
function send_photo_from_url(receiver, url, cb_function, cb_extra)
|
2015-02-28 14:22:53 +01:00
|
|
|
-- If callback not provided
|
|
|
|
cb_function = cb_function or ok_cb
|
|
|
|
cb_extra = cb_extra or false
|
2015-02-15 14:06:01 +01:00
|
|
|
local file_path = download_to_file(url, false)
|
2015-02-28 14:22:53 +01:00
|
|
|
if not file_path then -- Error
|
2015-05-09 16:35:22 +02:00
|
|
|
local text = 'Fehler beim laden des Bildes'
|
2015-02-28 14:22:53 +01:00
|
|
|
send_msg(receiver, text, cb_function, cb_extra)
|
|
|
|
else
|
2015-05-09 16:35:22 +02:00
|
|
|
print("Datei Pfad: "..file_path)
|
2015-02-28 14:22:53 +01:00
|
|
|
_send_photo(receiver, file_path, cb_function, cb_extra)
|
|
|
|
end
|
2015-02-15 14:06:01 +01:00
|
|
|
end
|
|
|
|
|
2015-02-15 20:26:00 +01:00
|
|
|
-- Same as send_photo_from_url but as callback function
|
|
|
|
function send_photo_from_url_callback(cb_extra, success, result)
|
|
|
|
local receiver = cb_extra.receiver
|
|
|
|
local url = cb_extra.url
|
|
|
|
local file_path = download_to_file(url, false)
|
2015-02-28 14:22:53 +01:00
|
|
|
if not file_path then -- Error
|
2015-05-09 16:35:22 +02:00
|
|
|
local text = 'Fehler beim laden des Bildes'
|
2015-02-28 14:22:53 +01:00
|
|
|
send_msg(receiver, text, ok_cb, false)
|
|
|
|
else
|
2015-05-09 16:35:22 +02:00
|
|
|
print("Datei Pfad: "..file_path)
|
2015-02-28 14:22:53 +01:00
|
|
|
_send_photo(receiver, file_path, ok_cb, false)
|
|
|
|
end
|
2015-02-15 20:26:00 +01:00
|
|
|
end
|
|
|
|
|
2015-05-28 16:47:30 +02:00
|
|
|
-- Send multiple images asynchronous.
|
2015-02-15 14:06:01 +01:00
|
|
|
-- param urls must be a table.
|
|
|
|
function send_photos_from_url(receiver, urls)
|
|
|
|
local cb_extra = {
|
|
|
|
receiver = receiver,
|
|
|
|
urls = urls,
|
|
|
|
remove_path = nil
|
|
|
|
}
|
|
|
|
send_photos_from_url_callback(cb_extra)
|
|
|
|
end
|
|
|
|
|
2015-06-23 16:42:40 +02:00
|
|
|
-- Use send_photos_from_url.
|
2015-05-28 16:47:30 +02:00
|
|
|
-- This function might be difficult to understand.
|
2015-02-15 14:06:01 +01:00
|
|
|
function send_photos_from_url_callback(cb_extra, success, result)
|
|
|
|
-- cb_extra is a table containing receiver, urls and remove_path
|
|
|
|
local receiver = cb_extra.receiver
|
|
|
|
local urls = cb_extra.urls
|
|
|
|
local remove_path = cb_extra.remove_path
|
|
|
|
|
|
|
|
-- The previously image to remove
|
|
|
|
if remove_path ~= nil then
|
|
|
|
os.remove(remove_path)
|
2015-05-09 16:35:22 +02:00
|
|
|
print(remove_path.." gelöscht!")
|
2015-02-15 14:06:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Nil or empty, exit case (no more urls)
|
|
|
|
if urls == nil or #urls == 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Take the head and remove from urls table
|
|
|
|
local head = table.remove(urls, 1)
|
|
|
|
|
|
|
|
local file_path = download_to_file(head, false)
|
|
|
|
local cb_extra = {
|
|
|
|
receiver = receiver,
|
|
|
|
urls = urls,
|
|
|
|
remove_path = file_path
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Send first and postpone the others as callback
|
|
|
|
send_photo(receiver, file_path, send_photos_from_url_callback, cb_extra)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Callback to remove a file
|
|
|
|
function rmtmp_cb(cb_extra, success, result)
|
|
|
|
local file_path = cb_extra.file_path
|
2015-02-15 16:52:08 +01:00
|
|
|
local cb_function = cb_extra.cb_function or ok_cb
|
2015-02-15 14:06:01 +01:00
|
|
|
local cb_extra = cb_extra.cb_extra
|
|
|
|
|
|
|
|
if file_path ~= nil then
|
|
|
|
os.remove(file_path)
|
2015-05-09 16:35:22 +02:00
|
|
|
print(file_path.." gelöscht!")
|
2015-02-15 14:06:01 +01:00
|
|
|
end
|
2015-05-28 16:47:30 +02:00
|
|
|
-- Finally call the callback
|
2015-02-15 14:06:01 +01:00
|
|
|
cb_function(cb_extra, success, result)
|
2015-02-15 14:33:08 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Send document to user and delete it when finished.
|
|
|
|
-- cb_function and cb_extra are optionals callback
|
|
|
|
function _send_document(receiver, file_path, cb_function, cb_extra)
|
|
|
|
local cb_extra = {
|
|
|
|
file_path = file_path,
|
|
|
|
cb_function = cb_function or ok_cb,
|
|
|
|
cb_extra = cb_extra or false
|
|
|
|
}
|
|
|
|
-- Call to remove with optional callback
|
|
|
|
send_document(receiver, file_path, rmtmp_cb, cb_extra)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Download the image and send to receiver, it will be deleted.
|
|
|
|
-- cb_function and cb_extra are optionals callback
|
|
|
|
function send_document_from_url(receiver, url, cb_function, cb_extra)
|
|
|
|
local file_path = download_to_file(url, false)
|
2015-05-09 16:35:22 +02:00
|
|
|
print("Datei Pfad: "..file_path)
|
2015-02-15 14:33:08 +01:00
|
|
|
_send_document(receiver, file_path, cb_function, cb_extra)
|
2015-02-27 21:36:50 +01:00
|
|
|
end
|
2015-03-14 17:22:52 +01:00
|
|
|
|
|
|
|
-- Parameters in ?a=1&b=2 style
|
|
|
|
function format_http_params(params, is_get)
|
|
|
|
local str = ''
|
|
|
|
-- If is get add ? to the beginning
|
|
|
|
if is_get then str = '?' end
|
|
|
|
local first = true -- Frist param
|
|
|
|
for k,v in pairs (params) do
|
|
|
|
if v then -- nil value
|
|
|
|
if first then
|
|
|
|
first = false
|
|
|
|
str = str..k.. "="..v
|
|
|
|
else
|
|
|
|
str = str.."&"..k.. "="..v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return str
|
2015-04-10 12:30:57 +02:00
|
|
|
end
|
2015-04-12 19:16:06 +02:00
|
|
|
|
|
|
|
-- Check if user can use the plugin and warns user
|
|
|
|
-- Returns true if user was warned and false if not warned (is allowed)
|
|
|
|
function warns_user_not_allowed(plugin, msg)
|
|
|
|
if not user_allowed(plugin, msg) then
|
2015-05-09 16:35:22 +02:00
|
|
|
local text = 'Du darfst diesen Befehl nicht nutzen!'
|
2015-04-12 19:16:06 +02:00
|
|
|
local receiver = get_receiver(msg)
|
|
|
|
send_msg(receiver, text, ok_cb, false)
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if user can use the plugin
|
|
|
|
function user_allowed(plugin, msg)
|
|
|
|
if plugin.privileged and not is_sudo(msg) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2015-06-23 16:42:40 +02:00
|
|
|
function send_order_msg(destination, msgs)
|
|
|
|
local cb_extra = {
|
|
|
|
destination = destination,
|
|
|
|
msgs = msgs
|
|
|
|
}
|
|
|
|
send_order_msg_callback(cb_extra, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
function send_order_msg_callback(cb_extra, success, result)
|
|
|
|
local destination = cb_extra.destination
|
|
|
|
local msgs = cb_extra.msgs
|
|
|
|
local file_path = cb_extra.file_path
|
|
|
|
if file_path ~= nil then
|
|
|
|
os.remove(file_path)
|
|
|
|
print("Deleted: " .. file_path)
|
|
|
|
end
|
|
|
|
if type(msgs) == 'string' then
|
|
|
|
send_large_msg(destination, msgs)
|
|
|
|
elseif type(msgs) ~= 'table' then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if #msgs < 1 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local msg = table.remove(msgs, 1)
|
|
|
|
local new_cb_extra = {
|
|
|
|
destination = destination,
|
|
|
|
msgs = msgs
|
|
|
|
}
|
|
|
|
if type(msg) == 'string' then
|
|
|
|
send_msg(destination, msg, send_order_msg_callback, new_cb_extra)
|
|
|
|
elseif type(msg) == 'table' then
|
|
|
|
local typ = msg[1]
|
|
|
|
local nmsg = msg[2]
|
|
|
|
new_cb_extra.file_path = nmsg
|
|
|
|
if typ == 'document' then
|
|
|
|
send_document(destination, nmsg, send_order_msg_callback, new_cb_extra)
|
|
|
|
elseif typ == 'image' or typ == 'photo' then
|
|
|
|
send_photo(destination, nmsg, send_order_msg_callback, new_cb_extra)
|
|
|
|
elseif typ == 'audio' then
|
|
|
|
send_audio(destination, nmsg, send_order_msg_callback, new_cb_extra)
|
|
|
|
elseif typ == 'video' then
|
|
|
|
send_video(destination, nmsg, send_order_msg_callback, new_cb_extra)
|
|
|
|
else
|
|
|
|
send_file(destination, nmsg, send_order_msg_callback, new_cb_extra)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-28 16:47:30 +02:00
|
|
|
-- Same as send_large_msg_callback but friendly params
|
2015-04-12 19:16:06 +02:00
|
|
|
function send_large_msg(destination, text)
|
|
|
|
local cb_extra = {
|
|
|
|
destination = destination,
|
|
|
|
text = text
|
|
|
|
}
|
|
|
|
send_large_msg_callback(cb_extra, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If text is longer than 4096 chars, send multiple msg.
|
|
|
|
-- https://core.telegram.org/method/messages.sendMessage
|
|
|
|
function send_large_msg_callback(cb_extra, success, result)
|
|
|
|
local text_max = 4096
|
|
|
|
|
|
|
|
local destination = cb_extra.destination
|
|
|
|
local text = cb_extra.text
|
|
|
|
local text_len = string.len(text)
|
|
|
|
local num_msg = math.ceil(text_len / text_max)
|
|
|
|
|
|
|
|
if num_msg <= 1 then
|
|
|
|
send_msg(destination, text, ok_cb, false)
|
|
|
|
else
|
|
|
|
|
|
|
|
local my_text = string.sub(text, 1, 4096)
|
|
|
|
local rest = string.sub(text, 4096, text_len)
|
|
|
|
|
|
|
|
local cb_extra = {
|
|
|
|
destination = destination,
|
|
|
|
text = rest
|
|
|
|
}
|
|
|
|
|
|
|
|
send_msg(destination, my_text, send_large_msg_callback, cb_extra)
|
|
|
|
end
|
|
|
|
end
|
2015-04-12 21:56:20 +02:00
|
|
|
|
|
|
|
-- Returns a table with matches or nil
|
2015-05-14 21:37:51 +02:00
|
|
|
function match_pattern(pattern, text, lower_case)
|
2015-04-12 23:26:01 +02:00
|
|
|
if text then
|
2015-05-14 21:37:51 +02:00
|
|
|
local matches = {}
|
|
|
|
if lower_case then
|
|
|
|
matches = { string.match(text:lower(), pattern) }
|
|
|
|
else
|
|
|
|
matches = { string.match(text, pattern) }
|
|
|
|
end
|
|
|
|
if next(matches) then
|
|
|
|
return matches
|
|
|
|
end
|
2015-04-12 21:56:20 +02:00
|
|
|
end
|
|
|
|
-- nil
|
2015-04-12 22:08:56 +02:00
|
|
|
end
|
2015-04-22 16:48:05 +02:00
|
|
|
|
|
|
|
function sleep(n)
|
|
|
|
os.execute("sleep " .. tonumber(n))
|
2015-04-26 19:16:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Function to read data from files
|
2015-05-28 16:47:30 +02:00
|
|
|
function load_from_file(file, default_data)
|
2015-04-26 19:16:44 +02:00
|
|
|
local f = io.open(file, "r+")
|
|
|
|
-- If file doesn't exists
|
|
|
|
if f == nil then
|
|
|
|
-- Create a new empty table
|
2015-05-28 16:47:30 +02:00
|
|
|
default_data = default_data or {}
|
|
|
|
serialize_to_file(default_data, file)
|
2015-05-09 16:35:22 +02:00
|
|
|
print ('Erstelle Datei', file)
|
2015-04-26 19:16:44 +02:00
|
|
|
else
|
2015-05-09 16:35:22 +02:00
|
|
|
print ('Daten geladen von', file)
|
2015-04-26 19:16:44 +02:00
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
return loadfile (file)()
|
2015-05-28 16:47:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function run_bash(str)
|
|
|
|
local cmd = io.popen(str)
|
|
|
|
local result = cmd:read('*all')
|
|
|
|
cmd:close()
|
|
|
|
return result
|
2015-06-23 16:42:40 +02:00
|
|
|
end
|
|
|
|
|
2015-07-21 16:10:51 +02:00
|
|
|
function run_sh(msg)
|
|
|
|
name = get_name(msg)
|
|
|
|
text = ''
|
|
|
|
bash = msg.text:sub(4,-1)
|
|
|
|
text = run_bash(bash)
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2015-06-25 22:10:29 +02:00
|
|
|
function unescape(str)
|
|
|
|
str = string.gsub( str, '<', '<' )
|
|
|
|
str = string.gsub( str, '>', '>' )
|
|
|
|
str = string.gsub( str, '"', '"' )
|
|
|
|
str = string.gsub( str, ''', "'" )
|
|
|
|
str = string.gsub( str, '&#(%d+);', function(n) return string.char(n) end )
|
|
|
|
str = string.gsub( str, '&#x(%d+);', function(n) return string.char(tonumber(n,16)) end )
|
|
|
|
str = string.gsub( str, '&', '&' ) -- Be sure to do this after all others
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
2015-06-23 16:42:40 +02:00
|
|
|
-- See http://stackoverflow.com/a/14899740
|
|
|
|
function unescape_html(str)
|
|
|
|
local map = {
|
|
|
|
["lt"] = "<",
|
|
|
|
["gt"] = ">",
|
|
|
|
["amp"] = "&",
|
|
|
|
["quot"] = '"',
|
|
|
|
["apos"] = "'"
|
|
|
|
}
|
|
|
|
new = string.gsub(str, '(&(#?x?)([%d%a]+);)', function(orig, n, s)
|
|
|
|
var = map[s] or n == "#" and string.char(s)
|
|
|
|
var = var or n == "#x" and string.char(tonumber(s,16))
|
|
|
|
var = var or orig
|
|
|
|
return var
|
|
|
|
end)
|
|
|
|
return new
|
2015-05-28 16:47:30 +02:00
|
|
|
end
|