diff --git a/packages/servers/ircbot/hooks/.gitignore b/packages/servers/ircbot/hooks/.gitignore new file mode 100644 index 0000000..225fc6f --- /dev/null +++ b/packages/servers/ircbot/hooks/.gitignore @@ -0,0 +1 @@ +/__pycache__ diff --git a/packages/servers/ircbot/hooks/fistbump.py b/packages/servers/ircbot/hooks/fistbump.py new file mode 100644 index 0000000..33cea53 --- /dev/null +++ b/packages/servers/ircbot/hooks/fistbump.py @@ -0,0 +1,7 @@ +import main + +class EventHandler(main.EventHandler): + def on_message(bot, e): + if e.message == '.fistbump': + msg = f'vroooooooooooo fiiiist, {e.sender}! :^)' + bot.send_message(e.channel, msg) diff --git a/packages/servers/ircbot/hooks/quit.py b/packages/servers/ircbot/hooks/quit.py new file mode 100644 index 0000000..cc5bd53 --- /dev/null +++ b/packages/servers/ircbot/hooks/quit.py @@ -0,0 +1,7 @@ +import main + +class EventHandler(main.EventHandler): + def on_message(bot, e): + if e.message == '.quit': + bot.send_message(e.channel, 'exiting...') + bot.emit('quit') diff --git a/packages/servers/ircbot/hooks/reload.py b/packages/servers/ircbot/hooks/reload.py new file mode 100644 index 0000000..604337d --- /dev/null +++ b/packages/servers/ircbot/hooks/reload.py @@ -0,0 +1,7 @@ +import main + +class EventHandler(main.EventHandler): + def on_message(bot, e): + if e.message == '.reload': + bot.send_message(e.channel, 'reloading hooks...') + bot.emit('reload-hooks') diff --git a/packages/servers/ircbot/main.py b/packages/servers/ircbot/main.py index 87479bb..f86ca8c 100644 --- a/packages/servers/ircbot/main.py +++ b/packages/servers/ircbot/main.py @@ -1,34 +1,83 @@ +import os +import sys import justirc +import importlib + +class EventHandler(object): + def on_message(bot, event): + ... + + def on_reload(bot): + ... def main(): - config = dict(nick='smith', debug=False) + config = dict( + debug=False, + nick='smith', + channel='#general', + server='irc.privatevoid.net', + port=6697, + tls=True, + ) run_bot(config) +def shutdown_bot_hooks(bot): + for name, hook in bot.hooks: + try: + hook.EventHandler.on_reload(bot) + except Exception as e: + print(f'exception running hook {name}: {e}') + def run_bot(c): bot = justirc.IRCConnection() + bot.db = () # TODO: store a database handle here + bot.hooks = [] # storage for all bot hooks + if c['debug']: @bot.on('packet') def new_packet(e): print(e.packet) + @bot.on('reload-hooks') + def reload_hooks(e): + shutdown_bot_hooks(bot) + bot.hooks.clear() + + for path in filter(lambda h: h[-3:] == '.py', os.listdir('hooks')): + name = '.'.join(['hooks', path.split('.py')[0]]) + if name in sys.modules.keys(): + del sys.modules[name] + try: + mod = importlib.import_module(name, package=name) + bot.hooks.append((name, mod)) + except Exception as e: + print(f'failed to load hook {name}: {e}') + + @bot.on('quit') + def quit(e): + shutdown_bot_hooks(bot) + exit(0) + @bot.on('connect') def connect(e): bot.send_line(f'NICK {c["nick"]}') bot.send_line(f'USER {c["nick"]} 8 * {c["nick"]}') + bot.emit('reload-hooks') @bot.on('welcome') def welcome(e): - bot.join_channel("#general") + bot.join_channel(c['channel']) @bot.on('message') def message(e): - message = e.message.lower() - if message == '.fistbump': - message = f'vroooooooooooo fiiiist, {e.sender} :vvvv)))' - bot.send_message(e.channel, message) + for name, hook in bot.hooks: + try: + hook.EventHandler.on_message(bot, e) + except Exception as e: + print(f'exception running hook {name}: {e}') - bot.connect('irc.privatevoid.net', port=6697, tls=True) + bot.connect(c['server'], port=c['port'], tls=c['tls']) bot.run_loop() if __name__ == '__main__':