diff --git a/config.example.lua b/config.example.lua index 0f78b06..563fe0d 100644 --- a/config.example.lua +++ b/config.example.lua @@ -23,7 +23,7 @@ return { STORAGE_DIR_URL = 'https://image-host.com/myimages/', -- Lua stuff - LUA_EXTRA_PATH = nil, -- Set a string here if you are using libraries located - LUA_EXTRA_CPATH = nil, -- weird location. + LUA_EXTRA_PATH = nil, -- Set a string here if you are using libraries + LUA_EXTRA_CPATH = nil, -- located in weird location. } diff --git a/internet.lua b/internet.lua index 295fc67..3330892 100644 --- a/internet.lua +++ b/internet.lua @@ -57,6 +57,14 @@ local function generic_request (...) return output, code, headers, status 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 -- Contains logoes @@ -136,7 +144,10 @@ local function get_wikipedia_pages (topics, language) local titles_field = escape_url(table.concat(topics, '|')) 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) 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 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) 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 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) diff --git a/main.lua b/main.lua index 1281e5c..07ed506 100644 --- a/main.lua +++ b/main.lua @@ -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.') end 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 if CONFIG.LUA_EXTRA_PATH then package.path = package.path .. CONFIG.LUA_EXTRA_PATH end @@ -45,7 +54,8 @@ local signal do if a then signal = b end end -local memes = require 'memes' +local memes = require 'memes' +local MESSAGES = require 'misc-messages' -------------------------------------------------------------------------------- -- Util @@ -61,13 +71,6 @@ end 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 function join_channel (channel) @@ -104,33 +107,6 @@ local function common_error(channel, text, ...) error(errmsg) 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_TIME_USER_LOGGED_OUT = {} 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. LAST_USER_LOGIN = user.nick human_delay() - bot:sendChat(channel, choose(HILSNER):format(user.nick)) + bot:sendChat(channel, choose(MESSAGES.OTHER_HELLO):format(user.nick)) io.write '[HILS]\n' 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*$' bot:sendChat(channel, "Det kan du tro! Jeg skal nok lige kigge indenom "..tostring(channel)) join_channel(channel) - return 'BOT' + return '!BOT' end - if message:match 'help' then - bot:sendChat(channel, choose { - 'No manual entry for '..CONFIG.IRC_NICK, - '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' + for match_str, responses in pairs(MESSAGES.DIRECT_MSG_RESPONSES) do + if message:match(match_str) then + bot:sendChat(channel, choose(responses)) + return '!BOT' + end end end -- Farvel msg 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 human_delay() local answer = possible_answeres[math.random(#possible_answeres)] @@ -282,7 +246,7 @@ bot:hook("OnChat", function(user, channel, message) -- Handle error if not success 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 status, message = 'ERROR', ('%s\n\n\t%s\n\n'):format(message, status) end @@ -310,15 +274,6 @@ end -------------------------------------------------------------------------------- -- 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 () -- Leave channels for channel in pairs(ACTIVE_CHANNELS) do @@ -349,12 +304,12 @@ local function init_memebot () if signal then signal.signal(signal.SIGINT, function (signum) 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() os.exit(128 + signum) end) --- - local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm' } + local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm', 'misc-messages' } local ORIG_TABLES = {} for _, module_name in ipairs(RELOADABLE_LIBS) do ORIG_TABLES[module_name] = require(module_name) diff --git a/memes.lua b/memes.lua index c74c307..3e9323b 100644 --- a/memes.lua +++ b/memes.lua @@ -42,9 +42,12 @@ end 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 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 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 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') - 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) - brain_img:free() + 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) + flatten_onto (base_img, brain_img, BRAIN_ROW_WIDTH, (i-1) * BRAIN_ROW_HEIGHT) + brain_img:free() + else + io.stderr:write ('[ERROR]: Could not find brain image "'..brain_path..'"\n') + assert(false) + end end -- Droste diff --git a/misc-messages.lua b/misc-messages.lua new file mode 100644 index 0000000..ce698e9 --- /dev/null +++ b/misc-messages.lua @@ -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 +