home · contact · privacy
Ignore messages not matching expected limitations, e.g. number of params.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 14 Aug 2025 18:20:56 +0000 (20:20 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 14 Aug 2025 18:20:56 +0000 (20:20 +0200)
ircplom/client.py

index c71bbfdbff16e1e78fdc2d04cf694a2407c65282..6f46b33d2d7a8b910a827dbe836fbebe62bbae51 100644 (file)
@@ -406,9 +406,11 @@ class Client(ABC, ClientQueueMixin):
             self._update_db('nickname', value=msg.params[0], confirm=True)
         if _NumericsToIgnore.contain(msg.verb):
             return
+        if msg.verb in {'JOIN', 'PART'} and len(msg.params) >= 1:
+            scope = LogScope.CHAT
+            channel = msg.params[0]
+            log_msg = f'{msg.nick_from_source} {msg.verb.lower()}s {channel}'
         match msg.verb:
-            case 'NICK':
-                self._update_db('nickname', value=msg.params[0], confirm=True)
             case '005':
                 for param in msg.params[1:-1]:
                     self._db.append('isupports', param)
@@ -421,22 +423,13 @@ class Client(ABC, ClientQueueMixin):
                 # claims: "<hostname> can also be in the form <user@hostname>"
                 self._update_db('client_host', confirm=True,
                                 value=msg.params[1].split('@')[-1])
-            case 'PING':
-                self.send(IrcMessage(verb='PONG', params=(msg.params[0],)))
-            case 'ERROR':
-                self.close()
-            case 'MODE' if (len(msg.params) == 2
-                            and msg.params[0] == self._db.nickname):
-                self._update_db('user_modes', msg.params[1], True)
-            case 'NOTICE' | 'PRIVMSG':
-                scope = LogScope.CHAT if '!' in msg.source else LogScope.SERVER
-                self._log(msg.params[-1], scope=scope, channel=msg.params[0],
-                          out=False, as_notice=msg.verb == 'NOTICE',
-                          sender=msg.nick_from_source)
-            case 'PRIVMSG':
-                self._log(msg.params[-1], scope=LogScope.CHAT, out=False,
-                          sender=msg.nick_from_source, channel=msg.params[0])
-            case 'CAP':
+            case 'AUTHENTICATE' if msg.params == ('+',):
+                auth = b64encode((self._db.conn_setup.nickname + '\0' +
+                                  self._db.conn_setup.nickname + '\0' +
+                                  self._db.conn_setup.password
+                                  ).encode('utf-8')).decode('utf-8')
+                self.send(IrcMessage('AUTHENTICATE', (auth,)))
+            case 'CAP' if len(msg.params) > 1:
                 if self._caps.process_msg(msg.params[1:]):
                     self._log('', caps=self._db.caps)
                     if (sasl_caps := self._db.caps.get('sasl', None))\
@@ -446,22 +439,32 @@ class Client(ABC, ClientQueueMixin):
                             self.send(IrcMessage('AUTHENTICATE', ('PLAIN',)))
                         else:
                             self._caps.end_negotiation()
-            case 'AUTHENTICATE':
-                if msg.params == ('+',):
-                    auth = b64encode((self._db.conn_setup.nickname + '\0' +
-                                      self._db.conn_setup.nickname + '\0' +
-                                      self._db.conn_setup.password
-                                      ).encode('utf-8')).decode('utf-8')
-                    self.send(IrcMessage('AUTHENTICATE', (auth,)))
+            case 'ERROR':
+                self.close()
+            case 'JOIN' if len(msg.params) == 1:
+                self._log(log_msg, scope=scope, channel=channel)
+            case 'MODE' if (len(msg.params) == 2
+                            and msg.params[0] == self._db.nickname):
+                self._update_db('user_modes', msg.params[1], True)
+            case 'NICK' if (len(msg.params) == 1
+                            and msg.nick_from_source == self._db.nickname):
+                self._update_db('nickname', msg.params[0], confirm=True)
+            case 'NOTICE' | 'PRIVMSG' if len(msg.params) > 1:
+                scope = LogScope.CHAT if '!' in msg.source else LogScope.SERVER
+                self._log(msg.params[-1], scope=scope, channel=msg.params[0],
+                          out=False, as_notice=msg.verb == 'NOTICE',
+                          sender=msg.nick_from_source)
+            case 'PART' if len(msg.params) >= 1:
+                if len(msg.params) > 1:
+                    log_msg += f': {msg.params[1]}'
+                self._log(log_msg, scope=scope, channel=channel)
+            case 'PING' if len(msg.params) == 1:
+                self.send(IrcMessage(verb='PONG', params=(msg.params[0],)))
             case '903' | '904':
                 alert = msg.verb == '904'
                 self._log(f'SASL auth {"failed" if alert else "succeeded"}',
                           alert=alert)
                 self._caps.end_negotiation()
-            case 'JOIN' | 'PART':
-                user, channel = msg.nick_from_source, msg.params[-1]
-                self._log(f'{user} {msg.verb.lower()}s {channel}',
-                          scope=LogScope.CHAT, channel=channel)
             case _:
                 self._log(f'PLEASE IMPLEMENT HANDLER FOR: {msg.raw}')