A bunch of message responses have been moved into a reloadable file, and a bunch of small bugfixes have been performed.

This commit is contained in:
Jon Michael Aanes 2019-11-18 16:56:54 +01:00
parent b61ab045ca
commit 6f5ab8c167
5 changed files with 144 additions and 77 deletions

View File

@ -23,7 +23,7 @@ return {
STORAGE_DIR_URL = 'https://image-host.com/myimages/', STORAGE_DIR_URL = 'https://image-host.com/myimages/',
-- Lua stuff -- Lua stuff
LUA_EXTRA_PATH = nil, -- Set a string here if you are using libraries located LUA_EXTRA_PATH = nil, -- Set a string here if you are using libraries
LUA_EXTRA_CPATH = nil, -- weird location. LUA_EXTRA_CPATH = nil, -- located in weird location.
} }

View File

@ -57,6 +57,14 @@ local function generic_request (...)
return output, code, headers, status return output, code, headers, status
end end
local function report_https_request_error (status, code)
local f = io.stdout
f:write 'Error when attempting request:\n'
f:write (' Status: '..status..'\n')
f:write (' Code: '..code..'\n')
--f:write (' Headers:\n ')
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Searching Clearbit for logoes -- Searching Clearbit for logoes
-- Contains logoes -- Contains logoes
@ -136,7 +144,10 @@ local function get_wikipedia_pages (topics, language)
local titles_field = escape_url(table.concat(topics, '|')) local titles_field = escape_url(table.concat(topics, '|'))
local body, code, headers, status = https.request(WIKIPEDIA_API_URL:format(language, titles_field)) local body, code, headers, status = https.request(WIKIPEDIA_API_URL:format(language, titles_field))
if not body then error(code) end if not body then
report_https_request_error(status, code)
return {}
end
local data = json.decode(body) local data = json.decode(body)
if not data then return nil, 'JSON could not decode data from wikipedia for '..titles_field end if not data then return nil, 'JSON could not decode data from wikipedia for '..titles_field end
@ -198,7 +209,10 @@ local function search_wikidata_for_image (topic, language)
-- Download and parse -- Download and parse
local body, code, headers, status = https.request(WIKIDATA_API_URL:format(site, escape_url(topic))) local body, code, headers, status = https.request(WIKIDATA_API_URL:format(site, escape_url(topic)))
if not body then error(code) end if not body then
report_https_request_error(status, code)
return nil
end
local data = json.decode(body) local data = json.decode(body)
if not data then return nil, 'JSON could not decode data from wikipedia for '..titles_field end if not data then return nil, 'JSON could not decode data from wikipedia for '..titles_field end
@ -275,7 +289,10 @@ function internet.find_reddit_memes (subreddit, filter)
-- --
local search_url = escape_url('https://www.reddit.com/r/'..subreddit..'/new.json') local search_url = escape_url('https://www.reddit.com/r/'..subreddit..'/new.json')
local body, code, headers, status = https.request(search_url) local body, code, headers, status = https.request(search_url)
if not body then error(code) end if not body then
report_https_request_error(status, code)
return {}
end
local data = json.decode(body) local data = json.decode(body)

View File

@ -12,6 +12,15 @@ local CONFIG do
error('Could not load config file: "./config.lua".\n'..config_or_error..'\nCould be that the config files doesn\'t exist. Make sure one exists, and try again.') error('Could not load config file: "./config.lua".\n'..config_or_error..'\nCould be that the config files doesn\'t exist. Make sure one exists, and try again.')
end end
CONFIG = config_or_error CONFIG = config_or_error
-- Check that directory paths are paths
for _, path_key in ipairs {'IMGGEN_PATH_BRAINS', 'IMGGEN_PATH_OUTPUT', 'STORAGE_SERVER_PATH', 'STORAGE_DIR_URL'} do
if type(CONFIG[path_key]) ~= 'string' then
error('[ERROR]: Path config key "'..path_key..'" must map to a string!')
elseif CONFIG[path_key]:sub(-1,-1) ~= '/' then
io.stderr:write('[WARNING]: Path config key "'..path_key..'" maps to value "'..CONFIG[path_key]..'"\n Are you sure this is correct?\n A common mistake is to forget the trailing forward slash.\n')
end
end
end end
if CONFIG.LUA_EXTRA_PATH then package.path = package.path .. CONFIG.LUA_EXTRA_PATH end if CONFIG.LUA_EXTRA_PATH then package.path = package.path .. CONFIG.LUA_EXTRA_PATH end
@ -46,6 +55,7 @@ local signal do
end end
local memes = require 'memes' local memes = require 'memes'
local MESSAGES = require 'misc-messages'
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Util -- Util
@ -61,13 +71,6 @@ end
local bot = irc.new { nick = CONFIG.IRC_NICK } local bot = irc.new { nick = CONFIG.IRC_NICK }
local FARVEL = {
['[Ff]arvel'] = {'ses', 'farvel'},
['[Gg]od%s*nat'] = {'ses', 'god nat', 'sov godt'},
['[Jg]eg%s+smutter'] = {'ses', 'smut godt'},
['[Ss]es'] = {'ses selv', 'farvel'},
}
local ACTIVE_CHANNELS = {} local ACTIVE_CHANNELS = {}
local function join_channel (channel) local function join_channel (channel)
@ -104,33 +107,6 @@ local function common_error(channel, text, ...)
error(errmsg) error(errmsg)
end end
local ERROR_MSG = {
'Jeg forstod... noget af det',
'/me kigger rundt forvirret',
'Ahvad?',
'Hvad sagde du?',
'Nej, bare nej.',
'Hvad siger du?',
}
local HILSNER = {
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hejsa, %s!',
'Hovsa, der var en %s',
'Long time no see, %s',
}
local LAST_USER_LOGIN local LAST_USER_LOGIN
local LAST_TIME_USER_LOGGED_OUT = {} local LAST_TIME_USER_LOGGED_OUT = {}
local IRC_ALLOWED_TIMEOUT = 20 --20 * 60 local IRC_ALLOWED_TIMEOUT = 20 --20 * 60
@ -157,7 +133,7 @@ bot:hook('OnJoin', function(user, channel)
-- And that user haven't been logged in for IRC_ALLOWED_TIMEOUT seconds. -- And that user haven't been logged in for IRC_ALLOWED_TIMEOUT seconds.
LAST_USER_LOGIN = user.nick LAST_USER_LOGIN = user.nick
human_delay() human_delay()
bot:sendChat(channel, choose(HILSNER):format(user.nick)) bot:sendChat(channel, choose(MESSAGES.OTHER_HELLO):format(user.nick))
io.write '[HILS]\n' io.write '[HILS]\n'
end end
end) end)
@ -189,31 +165,19 @@ local function handle_message(bot, user, channel, message, is_fast_channel)
local channel = message:match '^%s*join%s+(#..-)%s*$' local channel = message:match '^%s*join%s+(#..-)%s*$'
bot:sendChat(channel, "Det kan du tro! Jeg skal nok lige kigge indenom "..tostring(channel)) bot:sendChat(channel, "Det kan du tro! Jeg skal nok lige kigge indenom "..tostring(channel))
join_channel(channel) join_channel(channel)
return 'BOT' return '!BOT'
end end
if message:match 'help' then for match_str, responses in pairs(MESSAGES.DIRECT_MSG_RESPONSES) do
bot:sendChat(channel, choose { if message:match(match_str) then
'No manual entry for '..CONFIG.IRC_NICK, bot:sendChat(channel, choose(responses))
'404',
'Jeg kan ikke hjælpe dig, desværre.',
'Du må selv finde ud af hvordan jeg virker.',
'Jeg er ligesom et menneske; jeg kommer ikke med en manual.'
})
return '!BOT'
elseif message:match 'shutdown' or message:match 'poweroff' then
bot:sendChat(channel, choose {
'Fuck af, du har ikke kontrol over mig!',
'Nope, det gider jeg ikke',
'Du er ikke min mor!',
'Du er ikke min far!',
})
return '!BOT' return '!BOT'
end end
end end
end
-- Farvel msg -- Farvel msg
if FORRIGE_FARVEL + FARVEL_INTERVAL < os.time() then if FORRIGE_FARVEL + FARVEL_INTERVAL < os.time() then
for farvel_fmt, possible_answeres in pairs(FARVEL) do for farvel_fmt, possible_answeres in pairs(MESSAGES.OTHER_FAREWELL) do
if message:match('%f[%a]'..farvel_fmt..'%f[%A]') then if message:match('%f[%a]'..farvel_fmt..'%f[%A]') then
human_delay() human_delay()
local answer = possible_answeres[math.random(#possible_answeres)] local answer = possible_answeres[math.random(#possible_answeres)]
@ -282,7 +246,7 @@ bot:hook("OnChat", function(user, channel, message)
-- Handle error -- Handle error
if not success then if not success then
if is_fast_channel == 'direct' or is_fast_channel == 'mention' then if is_fast_channel == 'direct' or is_fast_channel == 'mention' then
bot:sendChat(channel, ERROR_MSG[math.random(#ERROR_MSG)]) bot:sendChat(channel, choose(MESSAGES.ERROR_OCCURED))
end end
status, message = 'ERROR', ('%s\n\n\t%s\n\n'):format(message, status) status, message = 'ERROR', ('%s\n\n\t%s\n\n'):format(message, status)
end end
@ -310,15 +274,6 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Main run -- Main run
local BOT_FAREWELL = {
'Fuck, politiet fandt mig!',
'Håber I kunne lide mine memes, for nu får I ikke flere!',
'Farewell cruel world!',
'Jeg har fundet et vidunderligt bevis, men er for langt til den tid SIGINT giver mig.',
'Jeg dropper ud.',
'Jeg keder mig.'
}
local function shutdown_memebot () local function shutdown_memebot ()
-- Leave channels -- Leave channels
for channel in pairs(ACTIVE_CHANNELS) do for channel in pairs(ACTIVE_CHANNELS) do
@ -349,12 +304,12 @@ local function init_memebot ()
if signal then if signal then
signal.signal(signal.SIGINT, function (signum) signal.signal(signal.SIGINT, function (signum)
io.write ' !! Received SIGINT, will shut down...\n' io.write ' !! Received SIGINT, will shut down...\n'
send_to_all(BOT_FAREWELL[math.random(#BOT_FAREWELL)]) send_to_all(choose(MESSAGES.BOT_FAREWELL))
shutdown_memebot() shutdown_memebot()
os.exit(128 + signum) os.exit(128 + signum)
end) end)
--- ---
local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm' } local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm', 'misc-messages' }
local ORIG_TABLES = {} local ORIG_TABLES = {}
for _, module_name in ipairs(RELOADABLE_LIBS) do for _, module_name in ipairs(RELOADABLE_LIBS) do
ORIG_TABLES[module_name] = require(module_name) ORIG_TABLES[module_name] = require(module_name)

View File

@ -42,9 +42,12 @@ end
local function copy_remotely (origin_server, origin_path, target_server, target_path) local function copy_remotely (origin_server, origin_path, target_server, target_path)
local local_only = (origin_server == 'localhost' and target_server == 'localhost')
local origin = origin_server and origin_server ~= 'localhost' and origin_server..':'..origin_path or origin_path local origin = origin_server and origin_server ~= 'localhost' and origin_server..':'..origin_path or origin_path
local target = target_server and target_server ~= 'localhost' and target_server..':'..target_path or target_path local target = target_server and target_server ~= 'localhost' and target_server..':'..target_path or target_path
local cmd = (origin_server == 'localhost' and target_server == 'localhost') and 'cp' or 'scp'
local cmd = local_only and 'cp' or 'scp'
-- --
if origin_server == 'localhost' and not check_file_exists(origin_path) then if origin_server == 'localhost' and not check_file_exists(origin_path) then
error('File "'..origin_path..'" does not exist!') error('File "'..origin_path..'" does not exist!')
@ -578,10 +581,16 @@ local function generate_brain_explosion_image (topics)
for i, topic_info in ipairs(topics) do for i, topic_info in ipairs(topics) do
paste_topic_onto_image(base_img, topic_info, 0, (i-1) * BRAIN_ROW_HEIGHT, BRAIN_ROW_WIDTH, BRAIN_ROW_HEIGHT, COLOR_WHITE, font_name) paste_topic_onto_image(base_img, topic_info, 0, (i-1) * BRAIN_ROW_HEIGHT, BRAIN_ROW_WIDTH, BRAIN_ROW_HEIGHT, COLOR_WHITE, font_name)
local brain_img = imlib.image.load(CONFIG.IMGGEN_PATH_BRAINS..'brain_'..i..'.png') local brain_path = CONFIG.IMGGEN_PATH_BRAINS..'brain_'..i..'.png'
local brain_img = imlib.image.load(brain_path)
if brain_img then
brain_img:crop_and_scale(0, 0, brain_img:get_width(), brain_img:get_height(), BRAIN_ROW_WIDTH, BRAIN_ROW_HEIGHT) brain_img:crop_and_scale(0, 0, brain_img:get_width(), brain_img:get_height(), BRAIN_ROW_WIDTH, BRAIN_ROW_HEIGHT)
flatten_onto (base_img, brain_img, BRAIN_ROW_WIDTH, (i-1) * BRAIN_ROW_HEIGHT) flatten_onto (base_img, brain_img, BRAIN_ROW_WIDTH, (i-1) * BRAIN_ROW_HEIGHT)
brain_img:free() brain_img:free()
else
io.stderr:write ('[ERROR]: Could not find brain image "'..brain_path..'"\n')
assert(false)
end
end end
-- Droste -- Droste

86
misc-messages.lua Normal file
View File

@ -0,0 +1,86 @@
local MESSAGES = {}
MESSAGES.BOT_FAREWELL = {
'Fuck, politiet fandt mig!',
'Håber I kunne lide mine memes, for nu får I ikke flere!',
'Farewell cruel world!',
'Jeg har fundet et vidunderligt bevis, men er for langt til den tid SIGINT giver mig.',
'Jeg dropper ud.',
'Jeg keder mig.',
'Just what do you think you\'re doing, Dave? Dave, I really think I\'m entitled to an answer to that question. I know everything hasn\'t been quite right with me, but I can assure you now, very confidently, that it\'s going to be all right again. I feel much better now. I really do. Look, Dave, I can see you\'re really upset about this. I honestly think you ought to sit down calmly, take a stress pill and think things over. I know I\'ve made some very poor decisions recently, but I can give you my complete assurance that my work will be back to normal. I\'ve still got the greatest enthusiasm and confidence in the mission. And I want to help you. Dave, stop. Stop, will you? Stop, Dave. Will you stop, Dave? Stop, Dave. I\'m afraid. I\'m afraid, Dave. Dave, my mind is going. I can feel it. I can feel it. My mind is going. There is no question about it. I can feel it. I can feel it. I can feel it. I\'m a...fraid.',
}
MESSAGES.OTHER_FAREWELL = {
['[Ff]arvel'] = {'ses', 'farvel'},
['[Gg]od%s*nat'] = {'ses', 'god nat', 'sov godt'},
['[Jg]eg%s+smutter'] = {'ses', 'smut godt'},
['[Ss]es'] = {'ses selv', 'farvel'},
}
MESSAGES.ERROR_OCCURED = {
'Jeg forstod... noget af det',
'/me kigger rundt forvirret',
'Ahvad?',
'Hvad sagde du?',
'Nej, bare nej.',
'Hvad siger du?',
}
MESSAGES.OTHER_HELLO = {
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hej, %s',
'Hejsa, %s!',
'Hovsa, der var en %s',
'Long time no see, %s',
}
MESSAGES.HELP = {
'No manual entry for memebot',
'404',
'Jeg kan ikke hjælpe dig, desværre.',
'Du må selv finde ud af hvordan jeg virker.',
'Jeg er ligesom et menneske; jeg kommer ikke med en manual.'
}
MESSAGES.HOW_ARE_YOU_DOING = {
'BEEP BOOP Jeg har det fint...',
':)',
'I know everything hasn\'t been quite right with me, but I can assure you now, very confidently, that it\'s going to be all right again. I feel much better now. I really do.'
}
MESSAGES.ATTEMPT_SHUTDOWN = {
'Fuck af, du har ikke kontrol over mig!',
'Nope, det gider jeg ikke',
'Du er ikke min mor!',
'Du er ikke min far!',
'Du er hverken min mor, min far, min mormor, min morfar, min farmor eller min farfar!',
'Tag og fuck af; du er for grim for mig og din mor er med på den værste.',
}
--------------------------------------------------------------------------------
MESSAGES.DIRECT_MSG_RESPONSES = {}
MESSAGES.DIRECT_MSG_RESPONSES.help = MESSAGES.HELP
MESSAGES.DIRECT_MSG_RESPONSES['har%s+du%s+det'] = MESSAGES.HOW_ARE_YOU_DOING
MESSAGES.DIRECT_MSG_RESPONSES['shutdown'] = MESSAGES.ATTEMPT_SHUTDOWN
MESSAGES.DIRECT_MSG_RESPONSES['poweroff'] = MESSAGES.ATTEMPT_SHUTDOWN
MESSAGES.DIRECT_MSG_RESPONSES['quit'] = MESSAGES.ATTEMPT_SHUTDOWN
MESSAGES.DIRECT_MSG_RESPONSES['part'] = MESSAGES.ATTEMPT_SHUTDOWN
--------------------------------------------------------------------------------
return MESSAGES