home · contact · privacy
Handle 433 with automatic new nickname.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 18 Aug 2025 15:49:02 +0000 (17:49 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 18 Aug 2025 15:49:02 +0000 (17:49 +0200)
ircplom/client.py

index 9f166be442d913210a29bca5175ed9638445c016..05ea0ddc10d926ec1214af48af247412b86c879b 100644 (file)
@@ -412,6 +412,16 @@ class _ClientDb(_Db, IrcConnSetup):
             else:
                 self.isupports[toks[0]] = toks[1] if len(toks) > 1 else ''
 
+    @property
+    def nick_incremented(self) -> str:
+        'Return .nickname with number suffix incremented, or "0" if none yet.'
+        name, digits = ([(self.nickname, '')]
+                        + [(self.nickname[:i], self.nickname[i:])
+                           for i in range(len(self.nickname), 0, -1)
+                           if self.nickname[i:].isdigit()]
+                        )[-1]
+        return name + str(0 if not digits else (int(digits) + 1))
+
 
 class Client(ABC, ClientQueueMixin):
     'Abstracts socket connection, loop over it, and handling messages from it.'
@@ -520,6 +530,10 @@ class Client(ABC, ClientQueueMixin):
             # '@'-split because <https://defs.ircdocs.horse/defs/numerics>
             # claims: "<hostname> can also be in the form <user@hostname>"
             self._db.client_host = msg.params[1].split('@')[-1]
+        elif msg.match('433', 3):  # ERR_NICKNAMEINUSE
+            new_nick = self._db.nick_incremented
+            self._log('nickname already in use, trying {new_nick}', alert=True)
+            self.send(IrcMessage(verb='NICK', params=(new_nick,)))
         elif msg.match('903', 2) or msg.match('904', 2):  # RPL_SUCESS, or …
             self._db.sasl_auth_state = 'WIN' if msg.verb == '903' else 'FAIL'
             self._caps.end_negotiation()                  # … ERR_SASLFAIL