2015-07-03 00:15:52 +02:00
|
|
|
|
-- utilities.lua
|
|
|
|
|
-- Functions shared among plugins.
|
|
|
|
|
|
2016-02-14 09:47:02 +01:00
|
|
|
|
-- you're welcome, brayden :^)
|
2016-02-21 06:21:48 +01:00
|
|
|
|
HTTP = HTTP or require('socket.http')
|
|
|
|
|
HTTPS = HTTPS or require('ssl.https')
|
|
|
|
|
JSON = JSON or require('cjson')
|
2016-01-15 04:39:24 +01:00
|
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
|
-- get the indexed word in a string
|
|
|
|
|
get_word = function(s, i)
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
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
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
2016-03-31 13:53:12 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Like get_word(), but better.
|
|
|
|
|
-- Returns the actual index.
|
|
|
|
|
function string:index()
|
|
|
|
|
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.
|
|
|
|
|
function string:input()
|
2015-11-25 03:22:04 +01:00
|
|
|
|
if not self:find(' ') then
|
2015-07-29 00:13:46 +02:00
|
|
|
|
return false
|
|
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
|
return self:sub(self: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! :)
|
|
|
|
|
function string:trim() -- Trims whitespace from a string.
|
|
|
|
|
local s = self:gsub('^%s*(.-)%s*$', '%1')
|
|
|
|
|
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.
|
|
|
|
|
latcyr = function(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.
|
|
|
|
|
load_data = function(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.
|
|
|
|
|
save_data = function(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-01-12 11:22:28 +01:00
|
|
|
|
get_coords = function(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
|
|
|
|
|
return config.errors.connection
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
|
if jdat.status == 'ZERO_RESULTS' then
|
|
|
|
|
return config.errors.results
|
|
|
|
|
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.
|
|
|
|
|
table_size = function(tab)
|
|
|
|
|
|
|
|
|
|
local i = 0
|
|
|
|
|
for k,v in pairs(tab) do
|
|
|
|
|
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.
|
|
|
|
|
build_name = function(first, last)
|
|
|
|
|
if last then
|
|
|
|
|
return first .. ' ' .. last
|
|
|
|
|
else
|
|
|
|
|
return first
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-03-22 11:16:26 +01:00
|
|
|
|
resolve_username = function(input)
|
|
|
|
|
|
|
|
|
|
input = input:gsub('^@', '')
|
|
|
|
|
for k,v in pairs(database.users) do
|
|
|
|
|
if v.username and v.username:lower() == input:lower() then
|
|
|
|
|
return v
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
user_from_message = function(msg)
|
|
|
|
|
|
|
|
|
|
local input = msg.text_lower:input()
|
|
|
|
|
local target = {}
|
|
|
|
|
if msg.reply_to_message then
|
|
|
|
|
target = msg.reply_to_message.from
|
|
|
|
|
elseif input and tonumber(input) then
|
|
|
|
|
target.id = input
|
|
|
|
|
if database.users[input] then
|
|
|
|
|
for k,v in pairs(database.users[input]) do
|
|
|
|
|
target[k] = v
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
elseif input and input:match('^@') then
|
2016-01-12 11:22:28 +01:00
|
|
|
|
local uname = input:gsub('^@', '')
|
2016-03-22 11:16:26 +01:00
|
|
|
|
for k,v in pairs(database.users) do
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
if target.id then
|
|
|
|
|
target.id_str = tostring(target.id)
|
2016-01-12 11:22:28 +01:00
|
|
|
|
end
|
|
|
|
|
|
2016-03-22 11:16:26 +01:00
|
|
|
|
if not target.first_name then target.first_name = 'User' end
|
|
|
|
|
|
2016-04-03 01:20:28 +02:00
|
|
|
|
target.name = build_name(target.first_name, target.last_name)
|
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
|
|
|
|
|
|
|
|
|
handle_exception = function(err, message)
|
|
|
|
|
|
2016-02-20 11:07:20 +01:00
|
|
|
|
if not err then err = '' end
|
|
|
|
|
|
2016-01-13 19:00:17 +01:00
|
|
|
|
local output = '\n[' .. os.date('%F %T', os.time()) .. ']\n' .. bot.username .. ': ' .. err .. '\n' .. message .. '\n'
|
|
|
|
|
|
|
|
|
|
if config.log_chat then
|
|
|
|
|
output = '```' .. output .. '```'
|
|
|
|
|
sendMessage(config.log_chat, output, true, nil, true)
|
|
|
|
|
else
|
|
|
|
|
print(output)
|
|
|
|
|
end
|
|
|
|
|
|
2016-01-15 04:39:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Okay, this one I actually did copy from yagop.
|
|
|
|
|
-- https://github.com/yagop/telegram-bot/blob/master/bot/utils.lua
|
|
|
|
|
download_file = function(url, filename)
|
|
|
|
|
|
|
|
|
|
local respbody = {}
|
|
|
|
|
local options = {
|
|
|
|
|
url = url,
|
|
|
|
|
sink = ltn12.sink.table(respbody),
|
|
|
|
|
redirect = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local response = nil
|
|
|
|
|
|
|
|
|
|
if url:match('^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 false end
|
|
|
|
|
|
2016-02-25 10:42:13 +01:00
|
|
|
|
filename = filename or '/tmp/' .. os.time()
|
2016-01-15 04:39:24 +01:00
|
|
|
|
|
2016-02-25 10:42:13 +01:00
|
|
|
|
local file = io.open(filename, 'w+')
|
2016-01-15 04:39:24 +01:00
|
|
|
|
file:write(table.concat(respbody))
|
|
|
|
|
file:close()
|
|
|
|
|
|
2016-02-25 10:42:13 +01:00
|
|
|
|
return filename
|
2016-01-15 04:39:24 +01:00
|
|
|
|
|
2016-01-13 19:00:17 +01:00
|
|
|
|
end
|
2016-03-22 11:16:26 +01:00
|
|
|
|
|
|
|
|
|
markdown_escape = function(text)
|
|
|
|
|
|
|
|
|
|
text = text:gsub('_', '\\_')
|
|
|
|
|
text = text:gsub('%[', '\\[')
|
|
|
|
|
text = text:gsub('%*', '\\*')
|
|
|
|
|
text = text:gsub('`', '\\`')
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
end
|
2016-04-01 19:29:00 +02:00
|
|
|
|
|
|
|
|
|
function string:md_escape()
|
|
|
|
|
local text = self
|
|
|
|
|
text = text:gsub('_', '\\_')
|
|
|
|
|
text = text:gsub('%[', '\\[')
|
|
|
|
|
text = text:gsub('%*', '\\*')
|
|
|
|
|
text = text:gsub('`', '\\`')
|
|
|
|
|
return text
|
|
|
|
|
end
|