home · contact · privacy
Improve numerics handling with own class Numerics.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 14 Aug 2025 10:21:34 +0000 (12:21 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 14 Aug 2025 10:21:34 +0000 (12:21 +0200)
ircplom/client.py

index 65232c6f870754f4295a0fcbd1a7561a2fcaa6fc..abdc611680003673e87aa0ba49b44ab32055694d 100644 (file)
@@ -14,8 +14,17 @@ from ircplom.irc_conn import (BaseIrcConnection, IrcConnAbortException,
 
 ClientsDb = dict[str, 'Client']
 _NAMES_DESIRED_SERVER_CAPS = ('server-time', 'account-tag', 'sasl')
-_NUMERICS_TO_IGNORE = (  # tuples define (inclusive) ranges
-    (2, 4),      # nothing in this login info we're interested in
+
+# NB: in below numerics accounting, tuples define inclusive ranges
+_NUMERICS_TO_CONFIRM_NICKNAME = (
+    (1, 5),
+    (251, 255),
+    (265, 266),
+    372,
+    (375, 376),
+)
+_NUMERICS_TO_IGNORE = (
+    (1, 4),      # nothing in this login info we're interested in
     (251, 255),  # same
     (265, 266),  # same
     375,  # RPL_MOTDSTART already implied to us by first appearance of 372
@@ -31,6 +40,25 @@ class LogScope(Enum):
     SAME = auto()
 
 
+class _Numerics:
+    'To easen dealing with numeric replies.'
+
+    def __init__(self, numerics: tuple[int | tuple[int, int], ...]) -> None:
+        as_ints = []
+        for item in numerics:
+            as_ints += ([item] if isinstance(item, int)
+                        else list(range(item[0], item[1] + 1)))
+        self._numerics = tuple(as_ints)
+
+    def contain(self, msg_verb: str) -> bool:
+        'Is int(msg_verb) within the listed numbers?'
+        return msg_verb.isdigit() and int(msg_verb) in self._numerics
+
+
+_NumericsToIgnore = _Numerics(_NUMERICS_TO_IGNORE)
+_NumericsToConfirmNickname = _Numerics(_NUMERICS_TO_CONFIRM_NICKNAME)
+
+
 @dataclass
 class ClientIdMixin:
     'Collects a Client\'s ID at .client_id.'
@@ -330,16 +358,12 @@ class Client(ABC, ClientQueueMixin):
         if msg.verb != self._prev_verb and self._prev_verb == '005':
             self._update_db('isupports', None, True)
         self._prev_verb = msg.verb
-        numerics_to_ignore = []
-        for item in _NUMERICS_TO_IGNORE:
-            if isinstance(item, tuple):
-                numerics_to_ignore += list(range(item[0], item[1] + 1))
-            else:
-                numerics_to_ignore += [item]
-        if msg.verb.isdigit() and int(msg.verb) in numerics_to_ignore:
+        if _NumericsToConfirmNickname.contain(msg.verb):
+            self._update_db('nickname', value=msg.params[0], confirm=True)
+        if _NumericsToIgnore.contain(msg.verb):
             return
         match msg.verb:
-            case '001' | 'NICK':
+            case 'NICK':
                 self._update_db('nickname', value=msg.params[0], confirm=True)
             case '005':
                 for param in msg.params[1:-1]: