From f6fe53385dda40ba530825398d7dc1a58540cc7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sun, 2 May 2021 00:40:22 +0300 Subject: [PATCH] Add built-in authentication to o3-base --- ircbot.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/ircbot.py b/ircbot.py index a5ab9dd..848e1a9 100644 --- a/ircbot.py +++ b/ircbot.py @@ -142,11 +142,12 @@ class API: self.serverthread_object.logging_channel.send((logmessage_types.internal, internal_submessage_types.error, message)) -# ServerThread(server, control_channel, cron_control_channel, logging_channel) +# ServerThread(server, auth, control_channel, cron_control_channel, logging_channel) # Creates a new server main loop thread class ServerThread(threading.Thread): - def __init__(self, server, control_channel, cron_control_channel, logging_channel): + def __init__(self, server, auth, control_channel, cron_control_channel, logging_channel): self.server = server + self.auth = auth self.control_channel = control_channel self.cron_control_channel = cron_control_channel self.logging_channel = logging_channel @@ -330,10 +331,15 @@ class ServerThread(threading.Thread): try: # Run initialization - self.send_line_raw(b'USER %s a a :%s' % (self.server.username.encode('utf-8'), self.server.realname.encode('utf-8'))) - # Set up nick + # Use server-password based authentication if it's set up + user, password = self.auth + if user is not None: + self.send_line_raw(b'PASS %s:%s' % (user.encode(), password.encode())) + + # Set up nick and username self.api.nick(self.server.nick.encode('utf-8')) + self.send_line_raw(b'USER %s a a :%s' % (self.server.username.encode('utf-8'), self.server.realname.encode('utf-8'))) # Run the on_connect hook, to allow further setup botcmd.on_connect(irc = self.api) @@ -377,12 +383,12 @@ class ServerThread(threading.Thread): # Tell cron we're quiting cron.quit(cron_control_channel) -# spawn_serverthread(server, cron_control_channel, logging_channel) → control_channel +# spawn_serverthread(server, auth, cron_control_channel, logging_channel) → control_channel # Creates a ServerThread for given server and returns the channel for controlling it -def spawn_serverthread(server, cron_control_channel, logging_channel): +def spawn_serverthread(server, auth, cron_control_channel, logging_channel): thread_control_socket, spawner_control_socket = socket.socketpair() control_channel = channel.Channel() - ServerThread(server, control_channel, cron_control_channel, logging_channel).start() + ServerThread(server, auth, control_channel, cron_control_channel, logging_channel).start() return control_channel # spawn_loggerthread() → logging_channel, dead_notify_channel @@ -406,18 +412,24 @@ def read_config(): realname = config['server']['realname'] channels = config['server']['channels'].split() + user = None + password = None + if 'auth' in config: + user = config['auth']['user'] + password = config['auth']['password'] + server = Server(host = host, port = port, nick = nick, username = username, realname = realname, channels = channels) - return config, server + return config, server, (user, password) if __name__ == '__main__': - config, server = read_config() + config, server, auth = read_config() botcmd.initialize(config = config) cron_control_channel = cron.start() logging_channel, dead_notify_channel = spawn_loggerthread() - control_channel = spawn_serverthread(server, cron_control_channel, logging_channel) + control_channel = spawn_serverthread(server, auth, cron_control_channel, logging_channel) while True: message = dead_notify_channel.recv(blocking = False)