From e796c96a7e8f2aa8919ae4daf1485d7c82f3aefe Mon Sep 17 00:00:00 2001 From: Darks Date: Fri, 11 Mar 2022 22:26:08 +0100 Subject: [PATCH] Some fixes --- irc.py | 22 ++++++++++++++-------- shoutbox.py | 4 ++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/irc.py b/irc.py index dc66b7f..5e1935f 100644 --- a/irc.py +++ b/irc.py @@ -1,6 +1,6 @@ # Manage the IRC layer of GLaDOS -import logging, re, socket, ssl +import logging, re, socket, ssl, time from functools import wraps from queue import Queue from threading import Thread @@ -10,7 +10,7 @@ class IRC(object): """ Initialize an IRC wrapper """ # Public attributes self.connected = False # Simple lock - self.run = False + self.running = False # Private attributes self._socket = ssl.create_default_context().wrap_socket( @@ -25,7 +25,7 @@ class IRC(object): def start(self, nick, password, sasl_nick=None): """ Start the IRC layer. Manage authentication as well """ sasl_nick = sasl_nick or nick - self._run = True + self.running = True self._handler.start() self._send(f"USER {nick} * * :{nick}") @@ -38,9 +38,9 @@ class IRC(object): def stop(self): """ Stop the IRC layer """ - self._send("QUIT") + self._send("QUIT :Bye bye") logging.debug("STOP: sent QUIT message") - self._run = False + self.running = False self._handler.join() logging.debug("STOP: thread has terminated") self._socket.close() @@ -95,7 +95,7 @@ class IRC(object): def _handle(self, store=True): """ Handle raw messages from irc and manage ping """ - while self._run: + while self.running: # Get incoming messages data = self._socket.recv(4096).decode() @@ -112,11 +112,17 @@ class IRC(object): def _send(self, raw): """ Wrap and encode raw message to send """ - self._socket.send(f"{raw}\r\n".encode()) + try: + self._socket.send(f"{raw}\r\n".encode()) + except OSError as e: + logging.warning(e) logging.debug(f"_send: {raw}") def _recv(self): - m = self._inbox.get() + try: + m = self._inbox.get() + except OSError as e: + logging.warning(e) logging.debug(f"_recv: {m}") return m diff --git a/shoutbox.py b/shoutbox.py index 9338190..5482be3 100644 --- a/shoutbox.py +++ b/shoutbox.py @@ -34,8 +34,8 @@ class Shoutbox(object): for event, callback in self._callbacks: # client is not known or is disconnected author = Shoutbox.normalize(message.author) - if author not in self.irc_clients \ - or self.irc_clients[author][0].run == False: + if author not in self.irc_clients.keys() \ + or self.irc_clients[author][0].running == False: self.irc_clients[author] = [ IRC('irc.planet-casio.com', 6697), datetime.datetime.now()