From e0dfbea065d9987f05b44f82de51005bebcef9c9 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 14 Aug 2025 12:21:34 +0200 Subject: [PATCH] Improve numerics handling with own class Numerics. --- ircplom/client.py | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/ircplom/client.py b/ircplom/client.py index 65232c6..abdc611 100644 --- a/ircplom/client.py +++ b/ircplom/client.py @@ -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]: -- 2.30.2