diff --git a/irc.py b/irc.py index 7b8b037..a35f9f2 100644 --- a/irc.py +++ b/irc.py @@ -27,7 +27,8 @@ class IRC(object): # Public methods def start(self, nick, password, sasl_nick=None): - """ Start the IRC layer. Manage authentication as well """ + """ Start the IRC layer. Manage authentication as well + Return True if authentication succeed, False if failed""" sasl_nick = sasl_nick or nick self.nick = nick self.running = True @@ -37,10 +38,19 @@ class IRC(object): self._send(f"USER {nick} * * :{nick}") self._send(f"NICK {nick}") self._waitfor(lambda m: "NOTICE" in m and "/AUTH" in m) - self._send(f"AUTH {sasl_nick}:{password}") - self._waitfor(lambda m: "You are now logged in" in m) - self.connected = True + for i in range(3): + self._send(f"AUTH {sasl_nick}:{password}") + msg = self._waitfor(lambda m: "You are now logged in" in m or "Authentication failed" in m) + + if "You are now logged in" in msg: + self.connected = True + return True + + logging.info(f"Authentication for {nick} ({sasl_nick}) failed") + self.connected = False + self._handler.stop() + return False def stop(self): """ Stop the IRC layer """ diff --git a/shoutbox.py b/shoutbox.py index c7f2597..d690693 100644 --- a/shoutbox.py +++ b/shoutbox.py @@ -106,8 +106,8 @@ class Shoutbox(object): datetime.datetime.now() ] # Start a thread for new client - self.irc_clients[author][0].start(f"{author}[s]", password, nick) - logging.debug(f"{author} has joined IRC") + if self.irc_clients[author][0].start(f"{author}[s]", password, nick): + logging.debug(f"{author} has joined IRC") # client is known but AFK else: self.irc_clients[author][1] = datetime.datetime.now()