Through the Testing-Glass, & What Drew Found There

This commit is contained in:
Brayden Banks 2016-04-15 19:07:23 +00:00
parent 9b4604daa2
commit 56abfab0b4
27 changed files with 65 additions and 51 deletions

View File

@ -17,7 +17,7 @@ otouto is an independently-developed Telegram API bot written in Lua. Originally
| [Contributors](#contributors) | | [Contributors](#contributors) |
## Setup ## Setup
You _must_ have Lua (5.2+), lua-socket, lua-sec, and lua-cjson installed. To upload files, you must have curl installed. To use fortune.lua, you must have fortune installed. You _must_ have Lua (5.2+), lua-socket, lua-sec, and dkjson installed. It is recommended you install these with LuaRocks. To upload files, you must have curl installed. To use fortune.lua, you must have fortune installed.
Clone the repository and set the following values in `config.lua`: Clone the repository and set the following values in `config.lua`:
@ -26,7 +26,6 @@ Clone the repository and set the following values in `config.lua`:
Optionally: Optionally:
- `time_offset` as the difference, in seconds, of your system clock to UTC.
- `lang` as the two-letter code representing your language. - `lang` as the two-letter code representing your language.
Some plugins are not enabled by default. If you wish to enable them, add them to the `plugins` array. Some plugins are not enabled by default. If you wish to enable them, add them to the `plugins` array.

View File

@ -5,7 +5,7 @@
local bindings = {} local bindings = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local JSON = require('cjson') local JSON = require('dkjson')
local URL = require('socket.url') local URL = require('socket.url')
function bindings.sendRequest(url) function bindings.sendRequest(url)

View File

@ -25,7 +25,7 @@
]]-- ]]--
local JSON = require('cjson') local JSON = require('dkjson')
local drua = dofile('drua-tg/drua-tg.lua') local drua = dofile('drua-tg/drua-tg.lua')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')
@ -196,7 +196,7 @@ function administration:get_desc(chat_id)
for i,v in ipairs(group.rules) do for i,v in ipairs(group.rules) do
rulelist = rulelist .. '*' .. i .. '.* ' .. v .. '\n' rulelist = rulelist .. '*' .. i .. '.* ' .. v .. '\n'
end end
table.insert(t, rulelist:trim()) table.insert(t, utilities.trim(rulelist))
end end
local flaglist = '' local flaglist = ''
for i = 1, #administration.flags do for i = 1, #administration.flags do
@ -205,7 +205,7 @@ function administration:get_desc(chat_id)
end end
end end
if flaglist ~= '' then if flaglist ~= '' then
table.insert(t, '*Flags:*\n' .. flaglist:trim()) table.insert(t, '*Flags:*\n' .. utilities.trim(flaglist))
end end
if group.governor then if group.governor then
local gov = self.database.users[tostring(group.governor)] local gov = self.database.users[tostring(group.governor)]
@ -217,7 +217,7 @@ function administration:get_desc(chat_id)
modstring = modstring .. administration.mod_format(self, k) modstring = modstring .. administration.mod_format(self, k)
end end
if modstring ~= '' then if modstring ~= '' then
table.insert(t, '*Moderators:*\n' .. modstring:trim()) table.insert(t, '*Moderators:*\n' .. utilities.trim(modstring))
end end
return table.concat(t, '\n\n') return table.concat(t, '\n\n')
@ -491,7 +491,7 @@ function administration.init_command(self_)
local gov = self.database.users[tostring(group.governor)] local gov = self.database.users[tostring(group.governor)]
govstring = '*Governor:* ' .. utilities.md_escape(utilities.build_name(gov.first_name, gov.last_name)) .. ' `[' .. gov.id .. ']`' govstring = '*Governor:* ' .. utilities.md_escape(utilities.build_name(gov.first_name, gov.last_name)) .. ' `[' .. gov.id .. ']`'
end end
local output = modstring:trim() ..'\n\n' .. govstring:trim() local output = utilities.trim(modstring) ..'\n\n' .. utilities.trim(govstring)
if output == '\n\n' then if output == '\n\n' then
output = 'There are currently no moderators for this group.' output = 'There are currently no moderators for this group.'
end end
@ -695,13 +695,13 @@ function administration.init_command(self_)
return return
end end
group.rules = {} group.rules = {}
input = input:trim() .. '\n' input = utilities.trim(input) .. '\n'
local output = '*Rules for* _' .. msg.chat.title .. '_ *:*\n' local output = '*Rules for* _' .. msg.chat.title .. '_ *:*\n'
local i = 1 local i = 1
for l in input:gmatch('(.-)\n') do for l in input:gmatch('(.-)\n') do
output = output .. '*' .. i .. '.* ' .. l .. '\n' output = output .. '*' .. i .. '.* ' .. l .. '\n'
i = i + 1 i = i + 1
table.insert(group.rules, l:trim()) table.insert(group.rules, utilities.trim(l))
end end
bindings.sendMessage(self, msg.chat.id, output, true, nil, true) bindings.sendMessage(self, msg.chat.id, output, true, nil, true)
end end
@ -728,7 +728,7 @@ function administration.init_command(self_)
group.motd = nil group.motd = nil
bindings.sendReply(self, msg, 'The MOTD has been cleared.') bindings.sendReply(self, msg, 'The MOTD has been cleared.')
else else
input = input:trim() input = utilities.trim(input)
group.motd = input group.motd = input
local output = '*MOTD for* _' .. msg.chat.title .. '_ *:*\n' .. input local output = '*MOTD for* _' .. msg.chat.title .. '_ *:*\n' .. input
bindings.sendMessage(self, msg.chat.id, output, true, nil, true) bindings.sendMessage(self, msg.chat.id, output, true, nil, true)

View File

@ -1,7 +1,7 @@
local apod = {} local apod = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local JSON = require('cjson') local JSON = require('dkjson')
local URL = require('socket.url') local URL = require('socket.url')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -4,7 +4,7 @@ local chatter = {}
local HTTP = require('socket.http') local HTTP = require('socket.http')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
function chatter:init() function chatter:init()

View File

@ -32,8 +32,12 @@ function dilbert:action(msg)
return return
end end
local strip_file = io.open('/tmp/' .. input .. '.gif') local strip_filename = '/tmp/' .. input .. '.gif'
if not strip_file then local strip_file = io.open(strip_filename)
if strip_file then
strip_file:close()
strip_file = strip_filename
else
local strip_url = str:match('<meta property="og:image" content="(.-)"/>') local strip_url = str:match('<meta property="og:image" content="(.-)"/>')
strip_file = utilities.download_file(strip_url, '/tmp/' .. input .. '.gif') strip_file = utilities.download_file(strip_url, '/tmp/' .. input .. '.gif')
end end
@ -42,8 +46,6 @@ function dilbert:action(msg)
bindings.sendPhoto(self, msg.chat.id, strip_file, strip_title) bindings.sendPhoto(self, msg.chat.id, strip_file, strip_title)
strip_file:close()
end end
return dilbert return dilbert

View File

@ -3,7 +3,7 @@
local floodcontrol = {} local floodcontrol = {}
local JSON = require('cjson') local JSON = require('dkjson')
local utilities = require('utilities') local utilities = require('utilities')
function floodcontrol:init() function floodcontrol:init()

View File

@ -5,7 +5,7 @@ local gImages = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -2,7 +2,7 @@ local gSearch = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -1,7 +1,7 @@
local hackernews = {} local hackernews = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -3,7 +3,7 @@
local hearthstone = {} local hearthstone = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')
@ -118,7 +118,7 @@ function hearthstone:action(msg)
end end
end end
output = output:trim() output = utilities.trim(output)
if output:len() == 0 then if output:len() == 0 then
bindings.sendReply(self, msg, self.config.errors.results) bindings.sendReply(self, msg, self.config.errors.results)
return return

View File

@ -2,7 +2,7 @@ local imdb = {}
local HTTP = require('socket.http') local HTTP = require('socket.http')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -2,7 +2,7 @@ local lastfm = {}
local HTTP = require('socket.http') local HTTP = require('socket.http')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -2,7 +2,7 @@ local librefm = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -19,7 +19,7 @@ function luarun:action(msg)
return return
end end
local output = loadstring('local utilities = require(\'utilities\'); return function (instance) '..input..' end')()(self) local output = loadstring('local bindings = require(\'bindings\'); local utilities = require(\'utilities\'); return function (instance, msg) '..input..' end')()(self, msg)
if output == nil then if output == nil then
output = 'Done!' output = 'Done!'
elseif type(output) == 'table' then elseif type(output) == 'table' then

View File

@ -1,7 +1,7 @@
local pokedex = {} local pokedex = {}
local HTTP = require('socket.http') local HTTP = require('socket.http')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -29,7 +29,7 @@ function reactions:init()
for trigger,reaction in pairs(mapping) do for trigger,reaction in pairs(mapping) do
help = help .. '' .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n' help = help .. '' .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n'
table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger) table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger)
table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger..'@'..self.username) table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger..'@'..self.username:lower())
end end
end end

View File

@ -2,7 +2,7 @@ local reddit = {}
local HTTP = require('socket.http') local HTTP = require('socket.http')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -21,7 +21,7 @@ function shout:action(msg)
bindings.sendMessage(self, msg.chat.id, shout.doc, true, msg.message_id, true) bindings.sendMessage(self, msg.chat.id, shout.doc, true, msg.message_id, true)
return return
end end
input = input:trim() input = utilities.trim(input)
if input:len() > 20 then if input:len() > 20 then
input = input:sub(1,20) input = input:sub(1,20)
@ -42,7 +42,7 @@ function shout:action(msg)
inc = inc + 1 inc = inc + 1
output = output .. match .. ' ' .. spacing .. match .. '\n' output = output .. match .. ' ' .. spacing .. match .. '\n'
end end
output = '```\n' .. output:trim() .. '\n```' output = '```\n' .. utilities.trim(output) .. '\n```'
bindings.sendMessage(self, msg.chat.id, output, true, false, true) bindings.sendMessage(self, msg.chat.id, output, true, false, true)
end end

View File

@ -1,7 +1,7 @@
local time = {} local time = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')
@ -33,7 +33,10 @@ function time:action(msg)
return return
end end
local url = 'https://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '&timestamp='..os.time() local now = os.time()
local utc = os.time(os.date("!*t", now))
local url = 'https://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '&timestamp='..utc
local jstr, res = HTTPS.request(url) local jstr, res = HTTPS.request(url)
if res ~= 200 then if res ~= 200 then
@ -43,16 +46,16 @@ function time:action(msg)
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
local now = os.time() local timestamp = now + jdat.rawOffset + jdat.dstOffset
local time_offset = os.difftime(now, os.time(os.date("!*t", now)))
local timestamp = now + jdat.rawOffset + jdat.dstOffset + time_offset
local utcoff = (jdat.rawOffset + jdat.dstOffset) / 3600 local utcoff = (jdat.rawOffset + jdat.dstOffset) / 3600
if utcoff == math.abs(utcoff) then if utcoff == math.abs(utcoff) then
utcoff = '+' .. utcoff utcoff = '+'.. utilities.pretty_float(utcoff)
else
utcoff = utilities.pretty_float(utcoff)
end end
local message = os.date('%I:%M %p\n', timestamp) .. os.date('%A, %B %d, %Y\n', timestamp) .. jdat.timeZoneName .. ' (UTC' .. utcoff .. ')' local message = os.date('!%I:%M %p\n', timestamp) .. os.date('!%A, %B %d, %Y\n', timestamp) .. jdat.timeZoneName .. ' (UTC' .. utcoff .. ')'
bindings.sendReply(self.msg, message) bindings.sendReply(self, msg, message)
end end

View File

@ -2,7 +2,7 @@ local translate = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -2,7 +2,7 @@ local urbandictionary = {}
local HTTP = require('socket.http') local HTTP = require('socket.http')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')
@ -43,9 +43,9 @@ function urbandictionary:action(msg)
return return
end end
local output = '*' .. jdat.list[1].word .. '*\n\n' .. jdat.list[1].definition:trim() local output = '*' .. jdat.list[1].word .. '*\n\n' .. utilities.trim(jdat.list[1].definition)
if string.len(jdat.list[1].example) > 0 then if string.len(jdat.list[1].example) > 0 then
output = output .. '_\n\n' .. jdat.list[1].example:trim() .. '_' output = output .. '_\n\n' .. utilities.trim(jdat.list[1].example) .. '_'
end end
output = output:gsub('%[', ''):gsub('%]', '') output = output:gsub('%[', ''):gsub('%]', '')

View File

@ -1,7 +1,7 @@
local weather = {} local weather = {}
local HTTP = require('socket.http') local HTTP = require('socket.http')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -2,7 +2,7 @@ local wikipedia = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -3,7 +3,7 @@ local xkcd = {}
local HTTP = require('socket.http') local HTTP = require('socket.http')
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -4,7 +4,7 @@ local youtube = {}
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
local utilities = require('utilities') local utilities = require('utilities')

View File

@ -7,7 +7,7 @@ local HTTP = require('socket.http')
local ltn12 = require('ltn12') local ltn12 = require('ltn12')
local HTTPS = require('ssl.https') local HTTPS = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('cjson') local JSON = require('dkjson')
local bindings = require('bindings') local bindings = require('bindings')
-- get the indexed word in a string -- get the indexed word in a string
@ -169,6 +169,7 @@ function utilities:user_from_message(msg)
local input = utilities.input(msg.text_lower) local input = utilities.input(msg.text_lower)
local target = {} local target = {}
if msg.reply_to_message then if msg.reply_to_message then
print('reply')
target = msg.reply_to_message.from target = msg.reply_to_message.from
elseif input and tonumber(input) then elseif input and tonumber(input) then
target.id = tonumber(input) target.id = tonumber(input)
@ -279,11 +280,12 @@ utilities.INVOCATION_PATTERN = '/'
utilities.triggers_meta = {} utilities.triggers_meta = {}
utilities.triggers_meta.__index = utilities.triggers_meta utilities.triggers_meta.__index = utilities.triggers_meta
function utilities.triggers_meta:t(pattern, has_args) function utilities.triggers_meta:t(pattern, has_args)
local username = self.username:lower()
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'$') table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'$')
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..self.username..'$') table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..username..'$')
if has_args then if has_args then
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'%s+[^%s]*') table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'%s+[^%s]*')
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..self.username..'%s+[^%s]*') table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..username..'%s+[^%s]*')
end end
return self return self
end end
@ -333,4 +335,12 @@ function utilities.enrich_message(msg)
return msg return msg
end end
function utilities.pretty_float(x)
if x % 1 == 0 then
return tostring(math.floor(x))
else
return tostring(x)
end
end
return utilities return utilities