A little restructure and implementation of better CTRL+C handling.

This commit is contained in:
Jon Michael Aanes 2018-06-08 13:37:31 +02:00
parent 1e24a12504
commit 5753c04bcd

101
main.lua
View File

@ -1,4 +1,7 @@
-- TODO: Invite to bogus channels
-- TODO: Make text randomly colored
-- Constants
math.randomseed(os.time())
@ -14,15 +17,33 @@ end
if CONFIG.LUA_EXTRA_PATH then package.path = package.path .. CONFIG.LUA_EXTRA_PATH end
if CONFIG.LUA_EXTRA_CPATH then package.cpath = package.cpath .. CONFIG.LUA_EXTRA_CPATH end
--------------------------------------------------------------------------------
-- Constants
local FARVEL_INTERVAL = 90
-- TODO: Invite to bogus channels
--------------------------------------------------------------------------------
-- Make sure all required modules can be loaded
local imlib = require 'imlib2'
require 'socket'
local https = require 'ssl.https'
local json = require 'json'
require 'irc.init'
local sleep = require 'socket'.sleep
local signal do
local a, b = pcall(require, 'posix.signal')
if a then signal = b end
end
--------------------------------------------------------------------------------
-- Meme utils
local imlib = require 'imlib2'
for _, path in ipairs(CONFIG.IMGGEN_PATH_FONTS) do
imlib.font.add_path(path)
end
@ -40,11 +61,6 @@ local function flatten_onto(target_img, other_img, x0, y0)
return
end
require 'socket'
local https = require 'ssl.https'
local json = require 'json'
local function search_clearbit_for_logo (topic)
if not (type(topic) == 'string' and topic == topic:lower() and #topic > 0) then
return nil, 'Bad topic: '..tostring(topic)
@ -101,12 +117,6 @@ local function generate_bait_link()
return 'https://dcav.pw/jbait'
end
require 'irc.init'
local sleep = require 'socket'.sleep
--require 'errors' 'memebot' . enable_strict_globals()
local function determine_required_font_size (font_name, text, width)
local size_min, size_max = 1, 50
while true do
@ -279,6 +289,20 @@ local FARVEL = {
['[Ss]es'] = {'ses selv', 'farvel'},
}
local ACTIVE_CHANNELS = {}
local function join_channel (channel)
ACTIVE_CHANNELS[channel] = true
bot:join(channel)
end
local function leave_channel (channel)
if ACTIVE_CHANNELS[channel] then
ACTIVE_CHANNELS[channel] = nil
bot:part(channel)
end
end
local LAST_ACTIVE = os.time()
local ACTIVE_PERIOD = 10
@ -405,7 +429,7 @@ local function handle_message(bot, user, channel, message)
if msg:match '%s*join%s+(#%a)' then
local channel = msg:match '^%s*join%s+(#%a+)%s*$'
bot:sendChat(channel, "Will do! I'll join "..tostring(channel))
bot:join(channel)
join_channel(channel)
else
bot:sendChat(channel, ERROR_MSG[math.random(#ERROR_MSG)])
end
@ -483,24 +507,65 @@ bot:hook('OnRaw', function (line)
io.write((" [RAW]: %s\n"):format(line))
end)
local function send_to_all (fmt, ...)
local msg = fmt:format(...)
for channel in pairs(ACTIVE_CHANNELS) do
bot:sendChat(channel, msg)
end
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
leave_channel(channel)
end
-- Leave IRC
bot:disconnect()
end
local function init_memebot ()
-- Connection to IRC
bot:connect {
host = CONFIG.IRC_SERVER,
port = CONFIG.IRC_PORT,
secure = CONFIG.IRC_SECURE,
}
io.write ('Memebot has started up as "'..CONFIG.IRC_NICK..'"\n')
-- Connect to chats, if any
assert(CONFIG.IRC_CHANNELS == nil or type(CONFIG.IRC_CHANNELS) == 'table')
for _, channel in ipairs(CONFIG.IRC_CHANNELS or {}) do
bot:join(channel)
join_channel(channel)
end
while true do
local BOT_SHOULD_CONTINUE_RUNNING = true
-- Install SIGINT handler, if module loaded
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)])
shutdown_memebot()
os.exit(128 + signum)
end)
else
io.write ' !! Module "posix.signal" was missing, so CTRL+C will hard-crash.\n'
end
-- Think loop
while BOT_SHOULD_CONTINUE_RUNNING do
bot:think()
sleep(0.5)
end