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-06-07 06:31:34 +02:00
|
|
|
|
local bindings = require('otouto.bindings')
|
2016-01-15 04:39:24 +01:00
|
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
|
-- For the sake of ease to new contributors and familiarity to old contributors,
|
|
|
|
|
-- we'll provide a couple of aliases to real bindings here.
|
2016-08-14 04:26:44 +02:00
|
|
|
|
-- Edit: To keep things working and allow for HTML messages, you can now pass a
|
|
|
|
|
-- string for use_markdown and that will be sent as the parse mode.
|
2016-05-29 19:08:39 +02:00
|
|
|
|
function utilities:send_message(chat_id, text, disable_web_page_preview, reply_to_message_id, use_markdown)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
local parse_mode
|
|
|
|
|
if type(use_markdown) == 'string' then
|
|
|
|
|
parse_mode = use_markdown
|
|
|
|
|
elseif use_markdown == true then
|
|
|
|
|
parse_mode = 'markdown'
|
|
|
|
|
end
|
|
|
|
|
return bindings.request(self, 'sendMessage', {
|
|
|
|
|
chat_id = chat_id,
|
|
|
|
|
text = text,
|
|
|
|
|
disable_web_page_preview = disable_web_page_preview,
|
|
|
|
|
reply_to_message_id = reply_to_message_id,
|
|
|
|
|
parse_mode = parse_mode
|
|
|
|
|
} )
|
2016-05-29 19:08:39 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function utilities:send_reply(old_msg, text, use_markdown)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
return utilities.send_message(self, old_msg.chat.id, text, true, old_msg.message_id, use_markdown)
|
2016-05-29 19:08:39 +02:00
|
|
|
|
end
|
|
|
|
|
|
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-08-14 04:46:18 +02:00
|
|
|
|
s = s or ''
|
|
|
|
|
i = i or 1
|
|
|
|
|
local n = 0
|
|
|
|
|
for w in s:gmatch('%g+') do
|
|
|
|
|
n = n + 1
|
|
|
|
|
if n == i then return w end
|
|
|
|
|
end
|
|
|
|
|
return false
|
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)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
if not s:find(' ') then
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
return s:sub(s:find(' ')+1)
|
2015-07-29 00:13:46 +02:00
|
|
|
|
end
|
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
|
function utilities.input_from_msg(msg)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
return utilities.input(msg.text) or (msg.reply_to_message and #msg.reply_to_message.text > 0 and msg.reply_to_message.text) or false
|
2016-08-14 04:26:44 +02:00
|
|
|
|
end
|
|
|
|
|
|
2016-05-27 17:49:58 +02:00
|
|
|
|
-- Calculates the length of the given string as UTF-8 characters
|
|
|
|
|
function utilities.utf8_len(s)
|
|
|
|
|
local chars = 0
|
|
|
|
|
for i = 1, string.len(s) do
|
|
|
|
|
local b = string.byte(s, i)
|
|
|
|
|
if b < 128 or b >= 192 then
|
|
|
|
|
chars = chars + 1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return chars
|
|
|
|
|
end
|
|
|
|
|
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
|
-- Trims whitespace from a string.
|
|
|
|
|
function utilities.trim(str)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
local s = str:gsub('^%s*(.-)%s*$', '%1')
|
|
|
|
|
return s
|
2015-07-03 00:15:52 +02:00
|
|
|
|
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)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
local f = io.open(filename)
|
|
|
|
|
if f then
|
|
|
|
|
local s = f:read('*all')
|
|
|
|
|
f:close()
|
|
|
|
|
return JSON.decode(s)
|
|
|
|
|
else
|
|
|
|
|
return {}
|
|
|
|
|
end
|
2015-07-29 00:13:46 +02:00
|
|
|
|
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)
|
2016-08-14 04:46:18 +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-05-27 02:59:45 +02:00
|
|
|
|
function utilities.get_coords(input, config)
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
|
local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input)
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
|
local jstr, res = HTTP.request(url)
|
|
|
|
|
if res ~= 200 then
|
|
|
|
|
return config.errors.connection
|
|
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
|
if jdat.status == 'ZERO_RESULTS' then
|
|
|
|
|
return config.errors.results
|
|
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
|
return {
|
|
|
|
|
lat = jdat.results[1].geometry.location.lat,
|
|
|
|
|
lon = jdat.results[1].geometry.location.lng
|
|
|
|
|
}
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
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-08-14 04:46:18 +02:00
|
|
|
|
local i = 0
|
|
|
|
|
for _,_ in pairs(tab) do
|
|
|
|
|
i = i + 1
|
|
|
|
|
end
|
|
|
|
|
return i
|
2016-01-12 11:22:28 +01:00
|
|
|
|
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-08-14 04:46:18 +02:00
|
|
|
|
if last then
|
|
|
|
|
return first .. ' ' .. last
|
|
|
|
|
else
|
|
|
|
|
return first
|
|
|
|
|
end
|
2016-04-03 01:20:28 +02:00
|
|
|
|
end
|
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities:resolve_username(input)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
input = input:gsub('^@', '')
|
|
|
|
|
for _, user in pairs(self.database.users) do
|
|
|
|
|
if user.username and user.username:lower() == input:lower() then
|
|
|
|
|
local t = {}
|
|
|
|
|
for key, val in pairs(user) do
|
|
|
|
|
t[key] = val
|
|
|
|
|
end
|
|
|
|
|
return t
|
|
|
|
|
end
|
|
|
|
|
end
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
|
end
|
|
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
|
function utilities:handle_exception(err, message, config)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
local output = string.format(
|
|
|
|
|
'\n[%s]\n%s: %s\n%s\n',
|
|
|
|
|
os.date('%F %T'),
|
|
|
|
|
self.info.username,
|
|
|
|
|
err or '',
|
|
|
|
|
message
|
|
|
|
|
)
|
|
|
|
|
if config.log_chat then
|
|
|
|
|
output = '```' .. output .. '```'
|
|
|
|
|
utilities.send_message(self, config.log_chat, output, true, nil, true)
|
|
|
|
|
else
|
|
|
|
|
print(output)
|
|
|
|
|
end
|
2016-01-13 19:00:17 +01:00
|
|
|
|
|
2016-01-15 04:39:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
|
function utilities.download_file(url, filename)
|
2016-08-14 04:46:18 +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
|
|
|
|
|
if url:match('^https') then
|
|
|
|
|
doer = HTTPS
|
|
|
|
|
do_redir = false
|
|
|
|
|
end
|
|
|
|
|
local _, res = doer.request{
|
|
|
|
|
url = url,
|
|
|
|
|
sink = ltn12.sink.table(body),
|
|
|
|
|
redirect = do_redir
|
|
|
|
|
}
|
|
|
|
|
if res ~= 200 then return false end
|
|
|
|
|
local file = io.open(filename, 'w+')
|
|
|
|
|
file:write(table.concat(body))
|
|
|
|
|
file:close()
|
|
|
|
|
return filename
|
2016-01-13 19:00:17 +01:00
|
|
|
|
end
|
2016-03-22 11:16:26 +01:00
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
|
function utilities.md_escape(text)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
return text:gsub('_', '\\_')
|
|
|
|
|
:gsub('%[', '\\['):gsub('%]', '\\]')
|
|
|
|
|
:gsub('%*', '\\*'):gsub('`', '\\`')
|
2016-03-22 11:16:26 +01:00
|
|
|
|
end
|
2016-04-01 19:29:00 +02:00
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
|
function utilities.html_escape(text)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
return text:gsub('&', '&'):gsub('<', '<'):gsub('>', '>')
|
2016-08-14 04:26:44 +02:00
|
|
|
|
end
|
2016-04-08 23:12:02 +02:00
|
|
|
|
|
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-08-14 04:46:18 +02:00
|
|
|
|
local username = self.username:lower()
|
|
|
|
|
table.insert(self.table, '^'..self.cmd_pat..pattern..'$')
|
|
|
|
|
table.insert(self.table, '^'..self.cmd_pat..pattern..'@'..username..'$')
|
|
|
|
|
if has_args then
|
|
|
|
|
table.insert(self.table, '^'..self.cmd_pat..pattern..'%s+[^%s]*')
|
|
|
|
|
table.insert(self.table, '^'..self.cmd_pat..pattern..'@'..username..'%s+[^%s]*')
|
|
|
|
|
end
|
|
|
|
|
return self
|
2016-04-08 23:12:02 +02:00
|
|
|
|
end
|
|
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
|
function utilities.triggers(username, cmd_pat, trigger_table)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
local self = setmetatable({}, utilities.triggers_meta)
|
|
|
|
|
self.username = username
|
|
|
|
|
self.cmd_pat = cmd_pat
|
|
|
|
|
self.table = trigger_table or {}
|
|
|
|
|
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)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
local original = HTTP.TIMEOUT
|
|
|
|
|
HTTP.TIMEOUT = timeout
|
|
|
|
|
fun()
|
|
|
|
|
HTTP.TIMEOUT = original
|
2016-04-11 06:04:47 +02:00
|
|
|
|
end
|
2016-04-12 11:24:56 +02:00
|
|
|
|
|
2016-04-15 21:07:23 +02:00
|
|
|
|
function utilities.pretty_float(x)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
if x % 1 == 0 then
|
|
|
|
|
return tostring(math.floor(x))
|
|
|
|
|
else
|
|
|
|
|
return tostring(x)
|
|
|
|
|
end
|
2016-04-15 21:07:23 +02:00
|
|
|
|
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 = {
|
2016-08-14 04:46:18 +02:00
|
|
|
|
zwnj = '',
|
|
|
|
|
arabic = '[\216-\219][\128-\191]',
|
|
|
|
|
rtl_override = '',
|
|
|
|
|
rtl_mark = '',
|
|
|
|
|
em_dash = '—',
|
|
|
|
|
utf_8 = '[%z\1-\127\194-\244][\128-\191]',
|
2016-05-20 09:38:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-14 02:00:58 +02:00
|
|
|
|
utilities.set_meta = {}
|
|
|
|
|
utilities.set_meta.__index = utilities.set_meta
|
|
|
|
|
function utilities.new_set()
|
|
|
|
|
return setmetatable({__count = 0}, utilities.set_meta)
|
|
|
|
|
end
|
|
|
|
|
function utilities.set_meta:add(x)
|
|
|
|
|
if x == "__count" then
|
|
|
|
|
return false
|
|
|
|
|
else
|
|
|
|
|
if not self[x] then
|
|
|
|
|
self[x] = true
|
|
|
|
|
self.__count = self.__count + 1
|
|
|
|
|
end
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
function utilities.set_meta:remove(x)
|
|
|
|
|
if x == "__count" then
|
|
|
|
|
return false
|
|
|
|
|
else
|
|
|
|
|
if self[x] then
|
|
|
|
|
self[x] = nil
|
|
|
|
|
self.__count = self.__count - 1
|
|
|
|
|
end
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
function utilities.set_meta:__len()
|
|
|
|
|
return self.__count
|
|
|
|
|
end
|
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
|
-- Styling functions to keep things consistent and easily changeable across plugins.
|
|
|
|
|
-- More to be added.
|
|
|
|
|
utilities.style = {}
|
|
|
|
|
utilities.style.enquote = function(title, body)
|
2016-08-14 04:46:18 +02:00
|
|
|
|
return '*' .. title:gsub('*', '\\*') .. ':*\n"' .. utilities.md_escape(body) .. '"'
|
2016-08-14 04:26:44 +02:00
|
|
|
|
end
|
|
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
|
return utilities
|