home · contact · privacy
Refactor 353 handler.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 19 Aug 2025 14:09:27 +0000 (16:09 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 19 Aug 2025 14:09:27 +0000 (16:09 +0200)
ircplom/client.py

index e3a9fe2490fc88defcb1c48f2abee0d976e99b6b..85a66ee2f3df5ef11787f0e867a92d73d668caad 100644 (file)
@@ -40,14 +40,15 @@ _NUMERICS_TO_IGNORE = (
 
 
 class _MsgTok(Enum):
-    NONE = auto()
     ANY = auto()
-    USER_ADDRESS = auto()
-    USER_ADDRESS_ME = auto()
-    SERVER = auto()
     CHANNEL = auto()
+    LIST = auto()
     NICKNAME = auto()
     NICKNAME_ME = auto()
+    NONE = auto()
+    SERVER = auto()
+    USER_ADDRESS = auto()
+    USER_ADDRESS_ME = auto()
 
 
 class _MsgParseExpectation(NamedTuple):
@@ -60,7 +61,9 @@ class _MsgParseExpectation(NamedTuple):
 
 _EXPECTATIONS: tuple[_MsgParseExpectation, ...] = (
    _MsgParseExpectation(_MsgTok.SERVER, '005', 3, 15),
-   _MsgParseExpectation(_MsgTok.SERVER, '353', 4),
+   _MsgParseExpectation(_MsgTok.SERVER, '353', params=(_MsgTok.NICKNAME_ME,
+                                                       '=', _MsgTok.CHANNEL,
+                                                       _MsgTok.LIST,)),
    _MsgParseExpectation(_MsgTok.SERVER, '366', 3),
    _MsgParseExpectation(_MsgTok.SERVER, '372', 2),
    _MsgParseExpectation(_MsgTok.SERVER, '376', 2),
@@ -621,6 +624,8 @@ class Client(ABC, ClientQueueMixin):
                     to_return[key_nick] = toks[0]
                 elif ex_tok is _MsgTok.ANY:
                     to_return['any'] = msg_tok
+                elif ex_tok is _MsgTok.LIST:
+                    to_return['list'] = msg_tok.split()
                 if ex_tok in {_MsgTok.NICKNAME_ME, _MsgTok.USER_ADDRESS_ME}:
                     if to_return[key_nick] != self._db.nickname:
                         break
@@ -639,11 +644,9 @@ class Client(ABC, ClientQueueMixin):
             return
         if self._match_msg(msg, '005'):  # RPL_ISUPPORT
             self._db.process_isupport(msg.params[1:-1])
-        elif self._match_msg(msg, '353')\
-                and msg.params[1] == '=':  # RPL_NAMREPLY
-            for user in msg.params[3].split():
-                self._db.chan(msg.params[2]).append_completable(
-                        'users', user.lstrip('~&@%+'))
+        elif (ret := self._match_msg(msg, '353')):  # RPL_NAMREPLY
+            for usr in ret['list']:
+                ret['channel'].append_completable('users', usr.lstrip('~&@%+'))
         elif self._match_msg(msg, '366'):  # RPL_ENDOFNAMES
             self._db.chan(msg.params[1]).declare_complete('users')
         elif self._match_msg(msg, '372'):  # RPL_MOTD
@@ -735,7 +738,7 @@ class Client(ABC, ClientQueueMixin):
             for nom, chan in self._db.chans_of_user(ret['sender']).items():
                 chan.remove_completable('users', ret['sender'], True)
                 self._log(f'{ret["sender"]} quits: {ret["any"]}',
-                          scope=LogScope.CHAT, target=nom)
+                          LogScope.CHAT, target=nom)
         else:
             self._log(f'PLEASE IMPLEMENT HANDLER FOR: {msg.raw}')