2015-07-03 00:15:52 +02:00
|
|
|
|
-- utilities.lua
|
|
|
|
|
-- Functions shared among plugins.
|
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
local utilities = {}
|
|
|
|
|
|
|
|
|
|
local HTTP = require('socket.http')
|
|
|
|
|
local ltn12 = require('ltn12')
|
|
|
|
|
local HTTPS = require('ssl.https')
|
|
|
|
|
local URL = require('socket.url')
|
2016-04-15 21:07:23 +02:00
|
|
|
|
local JSON = require('dkjson')
|
2016-04-08 23:12:02 +02:00
|
|
|
|
local bindings = require('bindings')
|
2016-01-15 04:39:24 +01:00
|
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
|
-- get the indexed word in a string
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.get_word(s, i)
|
2016-01-08 14:44:37 +01:00
|
|
|
|
s = s or ''
|
|
|
|
|
i = i or 1
|
2015-12-13 22:31:22 +01:00
|
|
|
|
local t = {}
|
|
|
|
|
for w in s:gmatch('%g+') do
|
|
|
|
|
table.insert(t, w)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
end
|
2015-12-13 22:31:22 +01:00
|
|
|
|
return t[i] or false
|
2016-03-31 13:53:12 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Like get_word(), but better.
|
|
|
|
|
-- Returns the actual index.
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.index(s)
|
2016-03-31 13:53:12 +02:00
|
|
|
|
local t = {}
|
|
|
|
|
for w in s:gmatch('%g+') do
|
|
|
|
|
table.insert(t, w)
|
|
|
|
|
end
|
|
|
|
|
return t
|
2015-11-25 03:22:04 +01:00
|
|
|
|
end
|
2015-07-29 00:13:46 +02:00
|
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
|
-- Returns the string after the first space.
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.input(s)
|
|
|
|
|
if not s:find(' ') then
|
2015-07-29 00:13:46 +02:00
|
|
|
|
return false
|
|
|
|
|
end
|
2016-04-08 23:12:02 +02:00
|
|
|
|
return s:sub(s:find(' ')+1)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
|
-- I swear, I copied this from PIL, not yago! :)
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.trim(str) -- Trims whitespace from a string.
|
|
|
|
|
local s = str:gsub('^%s*(.-)%s*$', '%1')
|
2015-11-25 03:22:04 +01:00
|
|
|
|
return s
|
2015-07-03 00:15:52 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local lc_list = {
|
|
|
|
|
-- Latin = 'Cyrillic'
|
|
|
|
|
['A'] = 'А',
|
|
|
|
|
['B'] = 'В',
|
|
|
|
|
['C'] = 'С',
|
|
|
|
|
['E'] = 'Е',
|
|
|
|
|
['I'] = 'І',
|
|
|
|
|
['J'] = 'Ј',
|
|
|
|
|
['K'] = 'К',
|
|
|
|
|
['M'] = 'М',
|
|
|
|
|
['H'] = 'Н',
|
|
|
|
|
['O'] = 'О',
|
|
|
|
|
['P'] = 'Р',
|
|
|
|
|
['S'] = 'Ѕ',
|
|
|
|
|
['T'] = 'Т',
|
|
|
|
|
['X'] = 'Х',
|
|
|
|
|
['Y'] = 'Ү',
|
|
|
|
|
['a'] = 'а',
|
|
|
|
|
['c'] = 'с',
|
|
|
|
|
['e'] = 'е',
|
|
|
|
|
['i'] = 'і',
|
|
|
|
|
['j'] = 'ј',
|
|
|
|
|
['o'] = 'о',
|
|
|
|
|
['s'] = 'ѕ',
|
|
|
|
|
['x'] = 'х',
|
|
|
|
|
['y'] = 'у',
|
|
|
|
|
['!'] = 'ǃ'
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
|
-- Replaces letters with corresponding Cyrillic characters.
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.latcyr(str)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
for k,v in pairs(lc_list) do
|
2016-04-03 01:20:28 +02:00
|
|
|
|
str = str:gsub(k, v)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
end
|
|
|
|
|
return str
|
|
|
|
|
end
|
|
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
|
-- Loads a JSON file as a table.
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.load_data(filename)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
local f = io.open(filename)
|
|
|
|
|
if not f then
|
|
|
|
|
return {}
|
|
|
|
|
end
|
|
|
|
|
local s = f:read('*all')
|
|
|
|
|
f:close()
|
|
|
|
|
local data = JSON.decode(s)
|
|
|
|
|
return data
|
|
|
|
|
end
|
|
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
|
-- Saves a table to a JSON file.
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.save_data(filename, data)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
local s = JSON.encode(data)
|
|
|
|
|
local f = io.open(filename, 'w')
|
|
|
|
|
f:write(s)
|
|
|
|
|
f:close()
|
2015-11-25 03:22:04 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Gets coordinates for a location. Used by gMaps.lua, time.lua, weather.lua.
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities:get_coords(input)
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
|
|
|
|
local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input)
|
|
|
|
|
|
|
|
|
|
local jstr, res = HTTP.request(url)
|
|
|
|
|
if res ~= 200 then
|
2016-04-08 23:12:02 +02:00
|
|
|
|
return self.config.errors.connection
|
2015-11-25 03:22:04 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
|
if jdat.status == 'ZERO_RESULTS' then
|
2016-04-08 23:12:02 +02:00
|
|
|
|
return self.config.errors.results
|
2015-11-25 03:22:04 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
lat = jdat.results[1].geometry.location.lat,
|
|
|
|
|
lon = jdat.results[1].geometry.location.lng
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Get the number of values in a key/value table.
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.table_size(tab)
|
2016-01-12 11:22:28 +01:00
|
|
|
|
local i = 0
|
2016-04-08 23:12:02 +02:00
|
|
|
|
for _,_ in pairs(tab) do
|
2016-01-12 11:22:28 +01:00
|
|
|
|
i = i + 1
|
|
|
|
|
end
|
|
|
|
|
return i
|
|
|
|
|
end
|
|
|
|
|
|
2016-04-03 01:20:28 +02:00
|
|
|
|
-- Just an easy way to get a user's full name.
|
2016-05-13 19:22:10 +02:00
|
|
|
|
-- Alternatively, abuse it to concat two strings like I do.
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.build_name(first, last)
|
2016-04-03 01:20:28 +02:00
|
|
|
|
if last then
|
|
|
|
|
return first .. ' ' .. last
|
|
|
|
|
else
|
|
|
|
|
return first
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities:resolve_username(input)
|
2016-03-22 11:16:26 +01:00
|
|
|
|
input = input:gsub('^@', '')
|
2016-04-08 23:12:02 +02:00
|
|
|
|
for _,v in pairs(self.database.users) do
|
2016-03-22 11:16:26 +01:00
|
|
|
|
if v.username and v.username:lower() == input:lower() then
|
|
|
|
|
return v
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-05-15 14:22:31 +02:00
|
|
|
|
function utilities:user_from_message(msg, no_extra)
|
2016-03-22 11:16:26 +01:00
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
local input = utilities.input(msg.text_lower)
|
2016-03-22 11:16:26 +01:00
|
|
|
|
local target = {}
|
|
|
|
|
if msg.reply_to_message then
|
2016-05-15 14:22:31 +02:00
|
|
|
|
for k,v in pairs(self.database.users[msg.reply_to_message.from.id_str]) do
|
|
|
|
|
target[k] = v
|
|
|
|
|
end
|
2016-03-22 11:16:26 +01:00
|
|
|
|
elseif input and tonumber(input) then
|
2016-04-12 11:24:56 +02:00
|
|
|
|
target.id = tonumber(input)
|
2016-04-08 23:12:02 +02:00
|
|
|
|
if self.database.users[input] then
|
|
|
|
|
for k,v in pairs(self.database.users[input]) do
|
2016-03-22 11:16:26 +01:00
|
|
|
|
target[k] = v
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
elseif input and input:match('^@') then
|
2016-01-12 11:22:28 +01:00
|
|
|
|
local uname = input:gsub('^@', '')
|
2016-04-08 23:12:02 +02:00
|
|
|
|
for _,v in pairs(self.database.users) do
|
2016-03-22 11:16:26 +01:00
|
|
|
|
if v.username and uname == v.username:lower() then
|
|
|
|
|
for key, val in pairs(v) do
|
|
|
|
|
target[key] = val
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if not target.id then
|
|
|
|
|
target.err = 'Sorry, I don\'t recognize that username.'
|
|
|
|
|
end
|
2016-01-12 11:22:28 +01:00
|
|
|
|
else
|
2016-03-22 11:16:26 +01:00
|
|
|
|
target.err = 'Please specify a user via reply, ID, or username.'
|
|
|
|
|
end
|
|
|
|
|
|
2016-05-15 14:22:31 +02:00
|
|
|
|
if not no_extra then
|
|
|
|
|
if target.id then
|
|
|
|
|
target.id_str = tostring(target.id)
|
|
|
|
|
end
|
|
|
|
|
if not target.first_name then
|
|
|
|
|
target.first_name = 'User'
|
|
|
|
|
end
|
|
|
|
|
target.name = utilities.build_name(target.first_name, target.last_name)
|
2016-01-12 11:22:28 +01:00
|
|
|
|
end
|
|
|
|
|
|
2016-03-22 11:16:26 +01:00
|
|
|
|
return target
|
|
|
|
|
|
2015-07-29 00:13:46 +02:00
|
|
|
|
end
|
2016-01-13 19:00:17 +01:00
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities:handle_exception(err, message)
|
2016-01-13 19:00:17 +01:00
|
|
|
|
|
2016-02-20 11:07:20 +01:00
|
|
|
|
if not err then err = '' end
|
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
local output = '\n[' .. os.date('%F %T', os.time()) .. ']\n' .. self.info.username .. ': ' .. err .. '\n' .. message .. '\n'
|
2016-01-13 19:00:17 +01:00
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
if self.config.log_chat then
|
2016-01-13 19:00:17 +01:00
|
|
|
|
output = '```' .. output .. '```'
|
2016-04-08 23:12:02 +02:00
|
|
|
|
bindings.sendMessage(self, self.config.log_chat, output, true, nil, true)
|
2016-01-13 19:00:17 +01:00
|
|
|
|
else
|
|
|
|
|
print(output)
|
|
|
|
|
end
|
|
|
|
|
|
2016-01-15 04:39:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.download_file(url, filename)
|
2016-05-13 19:22:10 +02:00
|
|
|
|
if not filename then
|
|
|
|
|
filename = url:match('.+/(.-)$') or os.time()
|
|
|
|
|
filename = '/tmp/' .. filename
|
|
|
|
|
end
|
|
|
|
|
local body = {}
|
|
|
|
|
local doer = HTTP
|
|
|
|
|
local do_redir = true
|
2016-01-15 04:39:24 +01:00
|
|
|
|
if url:match('^https') then
|
2016-05-13 19:22:10 +02:00
|
|
|
|
doer = HTTPS
|
|
|
|
|
do_redir = false
|
2016-01-15 04:39:24 +01:00
|
|
|
|
end
|
2016-05-13 19:22:10 +02:00
|
|
|
|
local _, res = doer.request{
|
|
|
|
|
url = url,
|
|
|
|
|
sink = ltn12.sink.table(body),
|
|
|
|
|
redirect = do_redir
|
|
|
|
|
}
|
|
|
|
|
if res ~= 200 then return false end
|
2016-02-25 10:42:13 +01:00
|
|
|
|
local file = io.open(filename, 'w+')
|
2016-05-13 19:22:10 +02:00
|
|
|
|
file:write(table.concat(body))
|
2016-01-15 04:39:24 +01:00
|
|
|
|
file:close()
|
2016-02-25 10:42:13 +01:00
|
|
|
|
return filename
|
2016-01-13 19:00:17 +01:00
|
|
|
|
end
|
2016-03-22 11:16:26 +01:00
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.markdown_escape(text)
|
2016-03-22 11:16:26 +01:00
|
|
|
|
text = text:gsub('_', '\\_')
|
|
|
|
|
text = text:gsub('%[', '\\[')
|
2016-04-20 22:43:08 +02:00
|
|
|
|
text = text:gsub('%]', '\\]')
|
2016-03-22 11:16:26 +01:00
|
|
|
|
text = text:gsub('%*', '\\*')
|
|
|
|
|
text = text:gsub('`', '\\`')
|
|
|
|
|
return text
|
|
|
|
|
end
|
2016-04-01 19:29:00 +02:00
|
|
|
|
|
2016-04-26 07:40:31 +02:00
|
|
|
|
utilities.md_escape = utilities.markdown_escape
|
2016-04-08 23:12:02 +02:00
|
|
|
|
|
|
|
|
|
utilities.INVOCATION_PATTERN = '/'
|
|
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
|
utilities.triggers_meta = {}
|
|
|
|
|
utilities.triggers_meta.__index = utilities.triggers_meta
|
|
|
|
|
function utilities.triggers_meta:t(pattern, has_args)
|
2016-04-15 21:07:23 +02:00
|
|
|
|
local username = self.username:lower()
|
2016-04-14 05:48:20 +02:00
|
|
|
|
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'$')
|
2016-04-15 21:07:23 +02:00
|
|
|
|
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..username..'$')
|
2016-04-08 23:12:02 +02:00
|
|
|
|
if has_args then
|
2016-04-14 05:48:20 +02:00
|
|
|
|
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'%s+[^%s]*')
|
2016-04-15 21:07:23 +02:00
|
|
|
|
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..username..'%s+[^%s]*')
|
2016-04-08 23:12:02 +02:00
|
|
|
|
end
|
|
|
|
|
return self
|
|
|
|
|
end
|
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
|
function utilities.triggers(username, trigger_table)
|
2016-04-14 05:48:20 +02:00
|
|
|
|
local self = setmetatable({}, utilities.triggers_meta)
|
2016-04-08 23:12:02 +02:00
|
|
|
|
self.username = username
|
2016-04-11 06:04:47 +02:00
|
|
|
|
self.table = trigger_table or {}
|
2016-04-08 23:12:02 +02:00
|
|
|
|
return self
|
2016-04-01 19:29:00 +02:00
|
|
|
|
end
|
2016-04-11 06:04:47 +02:00
|
|
|
|
|
|
|
|
|
function utilities.with_http_timeout(timeout, fun)
|
|
|
|
|
local original = HTTP.TIMEOUT
|
|
|
|
|
HTTP.TIMEOUT = timeout
|
|
|
|
|
fun()
|
|
|
|
|
HTTP.TIMEOUT = original
|
|
|
|
|
end
|
2016-04-12 11:24:56 +02:00
|
|
|
|
|
2016-04-12 15:47:30 +02:00
|
|
|
|
function utilities.enrich_user(user)
|
2016-04-12 11:24:56 +02:00
|
|
|
|
user.id_str = tostring(user.id)
|
2016-04-12 15:47:30 +02:00
|
|
|
|
user.name = utilities.build_name(user.first_name, user.last_name)
|
2016-04-12 11:24:56 +02:00
|
|
|
|
return user
|
|
|
|
|
end
|
|
|
|
|
|
2016-04-12 15:47:30 +02:00
|
|
|
|
function utilities.enrich_message(msg)
|
2016-04-12 11:24:56 +02:00
|
|
|
|
if not msg.text then msg.text = msg.caption or '' end
|
|
|
|
|
msg.text_lower = msg.text:lower()
|
2016-04-12 15:47:30 +02:00
|
|
|
|
msg.from = utilities.enrich_user(msg.from)
|
2016-04-12 11:24:56 +02:00
|
|
|
|
msg.chat.id_str = tostring(msg.chat.id)
|
|
|
|
|
if msg.reply_to_message then
|
|
|
|
|
if not msg.reply_to_message.text then
|
|
|
|
|
msg.reply_to_message.text = msg.reply_to_message.caption or ''
|
|
|
|
|
end
|
|
|
|
|
msg.reply_to_message.text_lower = msg.reply_to_message.text:lower()
|
2016-04-12 15:47:30 +02:00
|
|
|
|
msg.reply_to_message.from = utilities.enrich_user(msg.reply_to_message.from)
|
2016-04-12 11:24:56 +02:00
|
|
|
|
msg.reply_to_message.chat.id_str = tostring(msg.reply_to_message.chat.id)
|
|
|
|
|
end
|
|
|
|
|
if msg.forward_from then
|
2016-04-12 15:47:30 +02:00
|
|
|
|
msg.forward_from = utilities.enrich_user(msg.forward_from)
|
2016-04-12 11:24:56 +02:00
|
|
|
|
end
|
|
|
|
|
if msg.new_chat_participant then
|
2016-04-12 15:47:30 +02:00
|
|
|
|
msg.new_chat_participant = utilities.enrich_user(msg.new_chat_participant)
|
2016-04-12 11:24:56 +02:00
|
|
|
|
end
|
|
|
|
|
if msg.left_chat_participant then
|
2016-04-12 15:47:30 +02:00
|
|
|
|
msg.left_chat_participant = utilities.enrich_user(msg.left_chat_participant)
|
2016-04-12 11:24:56 +02:00
|
|
|
|
end
|
|
|
|
|
return msg
|
|
|
|
|
end
|
2016-04-14 05:48:20 +02:00
|
|
|
|
|
2016-04-15 21:07:23 +02:00
|
|
|
|
function utilities.pretty_float(x)
|
|
|
|
|
if x % 1 == 0 then
|
|
|
|
|
return tostring(math.floor(x))
|
|
|
|
|
else
|
|
|
|
|
return tostring(x)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-05-15 14:22:31 +02:00
|
|
|
|
function utilities:create_user_entry(user)
|
|
|
|
|
local id = tostring(user.id)
|
|
|
|
|
-- Clear things that may no longer exist, or create a user entry.
|
|
|
|
|
if self.database.users[id] then
|
|
|
|
|
self.database.users[id].username = nil
|
|
|
|
|
self.database.users[id].last_name = nil
|
|
|
|
|
else
|
|
|
|
|
self.database.users[id] = {}
|
|
|
|
|
end
|
|
|
|
|
-- Add all the user info to the entry.
|
|
|
|
|
for k,v in pairs(user) do
|
|
|
|
|
self.database.users[id][k] = v
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-05-20 09:38:43 +02:00
|
|
|
|
-- This table will store unsavory characters that are not properly displayed,
|
|
|
|
|
-- or are just not fun to type.
|
|
|
|
|
utilities.char = {
|
|
|
|
|
zwnj = '',
|
|
|
|
|
arabic = '[\216-\219][\128-\191]',
|
2016-05-22 22:08:45 +02:00
|
|
|
|
rtl_override = '',
|
2016-05-25 15:01:54 +02:00
|
|
|
|
rtl_mark = '',
|
|
|
|
|
em_dash = '—'
|
2016-05-20 09:38:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
|
return utilities
|