From 7d12e96979ae62c8d2c19984126412fee04be0fc Mon Sep 17 00:00:00 2001 From: jmaa Date: Thu, 14 Jun 2018 15:54:11 +0200 Subject: [PATCH 1/5] What?? --- memes.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/memes.lua b/memes.lua index e00e06e..c80847b 100644 --- a/memes.lua +++ b/memes.lua @@ -547,7 +547,8 @@ local function is_image_link (str) if type(str) ~= 'string' then return false end if not str:match '^%a+://.+$' then return false end local headers = internet.download_headers(str) - return headers['content-type']:match '^image/%a+$' + print(headers) + return type(headers) == 'table' and headers['content-type']:match '^image/%a+$' end function memes.generate_for_message (user, message) From a491765d59742513280aa63a8dabac6a6e742b89 Mon Sep 17 00:00:00 2001 From: jmaa Date: Thu, 14 Jun 2018 16:38:05 +0200 Subject: [PATCH 2/5] Removed debug prints --- memes.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/memes.lua b/memes.lua index c2c8938..767909a 100644 --- a/memes.lua +++ b/memes.lua @@ -543,7 +543,6 @@ local curb = require 'curb_your_enthusiasm' -------------------------------------------------------------------------------- local function is_image_link (str) - print(str) if str:match '%.png$' or str:match '%.gif$' or str:match '%.jpg$' then return true end @@ -551,7 +550,6 @@ local function is_image_link (str) if type(str) ~= 'string' then return false end if not str:match '^%a+://.+$' then return false end local headers = internet.download_headers(str) - print(headers) return type(headers) == 'table' and headers['content-type']:match '^image/%a+$' end From 34a24fad4e0fc8362e47dfaf792626067aac7a63 Mon Sep 17 00:00:00 2001 From: jmaa Date: Sat, 16 Jun 2018 13:10:23 +0200 Subject: [PATCH 3/5] Fixed issue with reloadable libraries --- main.lua | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/main.lua b/main.lua index 082d91b..cbc72ba 100644 --- a/main.lua +++ b/main.lua @@ -331,17 +331,30 @@ local function init_memebot () shutdown_memebot() os.exit(128 + signum) end) + --- + local RELOADABLE_LIBS = { 'config', 'internet', 'memes', 'curb_your_enthusiasm' } + local ORIG_TABLES = {} + for _, module_name in ipairs(RELOADABLE_LIBS) do + ORIG_TABLES[module_name] = require(module_name) + end + -- signal.signal(signal.SIGUSR1, function (signum) io.write ' !! Received SIGUSR1, will reload modules...\n' - for _, module_name in ipairs { 'internet', 'memes', 'curb_your_enthusiasm' } do - local old_module = require(module_name) + for _, module_name in ipairs(RELOADABLE_LIBS) do + io.write(' - '..module_name) package.loaded[module_name] = nil local new_module = require(module_name) - for k, v in pairs(new_module) do - old_module[k] = v - end - io.write(' - '..module_name..'\n') + if type(new_module) == 'table' then + local old_module = ORIG_TABLES[module_name] + for k, v in pairs(new_module) do + old_module[k] = v + end + else + io.write(' SKIP: Got '..tostring(new_module)..' but expected table.') + end + io.write '\n' end + io.write ' Done\n' end) else io.write ' !! Module "posix.signal" was missing, so CTRL+C will hard-crash.\n' From e47102ec8c2eae60ea7e2f2777e3f891b4c974e1 Mon Sep 17 00:00:00 2001 From: jmaa Date: Sat, 16 Jun 2018 13:10:49 +0200 Subject: [PATCH 4/5] Improved error handling when attempting to download bad files. --- internet.lua | 24 +++++++++++++++----- memes.lua | 63 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/internet.lua b/internet.lua index 75d6c96..53b51d4 100644 --- a/internet.lua +++ b/internet.lua @@ -46,11 +46,15 @@ local function safe_access (base, path) end local function generic_request (...) + --print('Request', ...) local output, code, headers, status = https.request(...) - if status == nil and code ~= 'connection refused' then + --print('Https', output, code, headers, status) + if code ~= nil and status ~= 'connection refused' then return output, code, headers, status end - return http.request(...) + local output, code, headers, status = http.request(...) + --print('Http', output, code, headers, status) + return output, code, headers, status end -------------------------------------------------------------------------------- @@ -262,14 +266,25 @@ end -- Download file function internet.download_file (url, filename) + assert(type(url) == 'string') + assert(type(filename) == 'string') + -- retrieve the content of a URL - local body, code = https.request(url) - if not body then error(('Https connection to "%s" failed, with error "%s"'):format(url, code)) end + --local body, code, headers, status = generic_request(url) + local body, code, headers, status = https.request(url) + + if code ~= 200 then + return false, code + --error(('Connection to "%s" failed, with error "%s"'):format(url, status)) + end + assert(type(body) == 'string') -- save the content to a file local f = assert(io.open(filename, 'wb')) -- open in "binary" mode f:write(body) f:close() + + return true end function internet.download_video (url) @@ -287,7 +302,6 @@ function internet.download_headers (url) url = url, method = 'HEAD' } - print(_, code, headers, status) -- return headers end diff --git a/memes.lua b/memes.lua index 767909a..b7447b3 100644 --- a/memes.lua +++ b/memes.lua @@ -244,24 +244,16 @@ local function paste_topic_onto_image (target, topic, x, y, w, h, bg_color, font assert(type(font_name) == 'string') -- Download and paste found image if topic.type == 'image' then - local file_extension = topic.url:match '%.(%a+)$' - local url, filename = topic.url, os.tmpname() --CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..file_extension - assert(type(url) == 'string' and #url > 0) - assert(type(filename) == 'string' and #filename > 0) - internet.download_file(url, filename) - -- Convert svg to png - if url:match '%.svg$' then - local filename_2 = CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..'png' - os.execute('convert -density "1200" -resize 400x400 "'..filename..'" "'..filename_2..'" &> /dev/null') - filename = filename_2 - end - -- - local found_img = assert(imlib.image.load(filename)) + assert(type(topic.filename) == 'string') + assert(not topic.url) + + local found_img = assert(imlib.image.load(topic.filename)) found_img:crop_and_scale(0, 0, found_img:get_width(), found_img:get_height(), w, h) flatten_onto (target, found_img, x, y) found_img:free() --os.remove(filename) elseif topic.type == 'text' then + assert(type(topic.text) == 'string') local text = topic.text draw_centered_text_in_box(font_name, target, text, x, y, w, h, bg_color, font_color) elseif topic.type == 'droste' then @@ -317,6 +309,24 @@ local DROSTE_EFFECT_TRIGGERS = { ['loop'] = true, } +local function download_and_standardize_image (image_url) + local file_extension = image_url:match '%.(%a+)$' + local url, filename = image_url, os.tmpname() + assert(type(url) == 'string' and #url > 0) + assert(type(filename) == 'string' and #filename > 0) + local success, errmsg = internet.download_file(url, filename) + + -- Convert svg to png + if success and url:match '%.svg$' then + local filename_2 = CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..'png' + os.execute('convert -density "1200" -resize 400x400 "'..filename..'" "'..filename_2..'" &> /dev/null') + filename = filename_2 + end + -- + + return success and filename, errmsg +end + local function fill_in_topics_information (topics) assert(type(topics) == 'table') -- @@ -328,9 +338,13 @@ local function fill_in_topics_information (topics) local url = topic_to_image_url[topic] - if DROSTE_EFFECT_TRIGGERS[topic] then new_topics[i] = { topic = topic, type = 'droste' } - elseif url then new_topics[i] = { topic = topic, type = 'image', url = url } - else new_topics[i] = { topic = topic, type = 'text', text = topic } + if DROSTE_EFFECT_TRIGGERS[topic] then + new_topics[i] = { topic = topic, type = 'droste' } + elseif url then + local filename = download_and_standardize_image(url) + new_topics[i] = { topic = topic, type = 'image', filename = filename } + else + new_topics[i] = { topic = topic, type = 'text', text = topic } end end return new_topics @@ -610,12 +624,17 @@ function memes.generate_for_message (user, message) -- Is this a rich picture? if is_image_link(message) then - local url = message - local img_link = generate_is_this_a_pidgeon { - { type = 'image', url = url }, - { type = 'text', text = 'Is this a rich picture?' } - } - return img_link, 'KYNG' + local filename, status = download_and_standardize_image(message) + if filename then + local img_link = generate_is_this_a_pidgeon { + { type = 'image', filename = filename }, + { type = 'text', text = 'Is this a rich picture?' } + } + return img_link, 'KYNG' + else + img_link = 'Kunne ikke skaffe det billede. Fik fejlkode '..tostring(status) + return img_link, '!KYNG' + end end -- Comparison memes From e7a4609c3c5f1840cdfa79f4dad227a249c39080 Mon Sep 17 00:00:00 2001 From: jmaa Date: Sun, 17 Jun 2018 23:43:54 +0200 Subject: [PATCH 5/5] Added snarky comments when attempting to get help or perform shutdown. --- main.lua | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/main.lua b/main.lua index cbc72ba..47e15d1 100644 --- a/main.lua +++ b/main.lua @@ -106,15 +106,9 @@ local ERROR_MSG = { 'Jeg forstod... noget af det', '/me kigger rundt forvirret', 'Ahvad?', - 'Kan du ikke lige gentage det, på en bedre måde?', 'Hvad sagde du?', - 'Hvorfor ikke kigge i en bogord?', 'Nej, bare nej.', - 'Hvad tror du jeg er? Et menneske?', - 'Jeg har ingen hjerne og må tænke.', 'Hvad siger du?', - 'Ser jeg ud til at have otte arme? Nej, for jeg har ikke nogle arme!', - 'Do androids dream of electric sheep?', } local HILSNER = { @@ -195,6 +189,24 @@ local function handle_message(bot, user, channel, message, is_fast_channel) join_channel(channel) return 'BOT' end + if message:match 'help' then + bot:sendChat(channel, choose { + 'No manual entry for ro-bot', + '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 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' + end end -- Farvel msg