From 5a1432c1e604e59d13a15f39c543de44e02a9d35 Mon Sep 17 00:00:00 2001 From: adator <85586985+adator85@users.noreply.github.com> Date: Sun, 2 Nov 2025 21:17:15 +0100 Subject: [PATCH] Update parse_uid, now it returns MUser object. --- core/classes/protocols/inspircd.py | 30 +++++------------------------ core/classes/protocols/interface.py | 4 ++-- core/classes/protocols/unreal6.py | 29 ++++++---------------------- mods/defender/utils.py | 6 ++---- 4 files changed, 15 insertions(+), 54 deletions(-) diff --git a/core/classes/protocols/inspircd.py b/core/classes/protocols/inspircd.py index 09d4bf1..d59055d 100644 --- a/core/classes/protocols/inspircd.py +++ b/core/classes/protocols/inspircd.py @@ -1186,35 +1186,15 @@ class Inspircd(IProtocol): # COMMON IRC PARSER # ------------------------------------------------------------------------ - def parse_uid(self, server_msg: list[str]) -> dict[str, str]: + def parse_uid(self, server_msg: list[str]) -> Optional['MUser']: """Parse UID and return dictionary. - + >>> [':97K', 'UID', '97KAAAAAC', '1762113659', 'adator_', '172.18.128.1', '172.18.128.1', '...', '...', '172.18.128.1', '1762113659', '+', ':...'] Args: server_msg (list[str]): _description_ """ - umodes = str(server_msg[11]) - remote_ip = server_msg[9] if 'S' not in umodes else '127.0.0.1' - - # Extract Geoip information - pattern = r'^.*geoip=cc=(\S{2}).*$' - geoip_match = match(pattern, server_msg[0]) - geoip = geoip_match.group(1) if geoip_match else None - - response = { - 'uid': str(server_msg[2]), - 'nickname': str(server_msg[4]), - 'username': str(server_msg[7]), - 'hostname': str(server_msg[5]), - 'umodes': umodes, - 'vhost': str(server_msg[6]), - 'ip': remote_ip, - 'realname': ' '.join(server_msg[12:]).lstrip(':'), - 'geoip': geoip, - 'reputation_score': 0, - 'iswebirc': True if 'webirc' in server_msg[0] else False, - 'iswebsocket': True if 'websocket' in server_msg[0] else False - } - return response + scopy = server_msg.copy() + uid = scopy[2] + return self._User.get_user(uid) def parse_quit(self, server_msg: list[str]) -> dict[str, str]: """Parse quit and return dictionary. diff --git a/core/classes/protocols/interface.py b/core/classes/protocols/interface.py index 9de8f54..9be3b84 100644 --- a/core/classes/protocols/interface.py +++ b/core/classes/protocols/interface.py @@ -316,14 +316,14 @@ class IProtocol(ABC): # ------------------------------------------------------------------------ @abstractmethod - def parse_uid(self, serverMsg: list[str]) -> dict[str, str]: + def parse_uid(self, serverMsg: list[str]) -> Optional['MUser']: """Parse UID and return dictionary. Args: serverMsg (list[str]): The UID IRCD message Returns: - dict[str, str]: The response as dictionary. + Optional[MUser]: The MUser object or None """ @abstractmethod diff --git a/core/classes/protocols/unreal6.py b/core/classes/protocols/unreal6.py index 3658188..222a218 100644 --- a/core/classes/protocols/unreal6.py +++ b/core/classes/protocols/unreal6.py @@ -630,35 +630,18 @@ class Unrealircd6(IProtocol): # COMMON IRC PARSER # ------------------------------------------------------------------------ - def parse_uid(self, serverMsg: list[str]) -> dict[str, str]: + def parse_uid(self, serverMsg: list[str]) -> Optional['MUser']: """Parse UID and return dictionary. >>> ['@s2s-md/geoip=cc=GBtag...', ':001', 'UID', 'albatros', '0', '1721564597', 'albatros', 'hostname...', '001HB8G04', '0', '+iwxz', 'hostname-vhost', 'hostname-vhost', 'MyZBwg==', ':...'] Args: serverMsg (list[str]): The UID ircd response """ - umodes = str(serverMsg[10]) - remote_ip = self._Base.decode_ip(str(serverMsg[13])) if 'S' not in umodes else '127.0.0.1' + scopy = serverMsg.copy() + if '@' in scopy[0]: + scopy.pop(0) - # Extract Geoip information - pattern = r'^.*geoip=cc=(\S{2}).*$' - geoip_match = match(pattern, serverMsg[0]) - geoip = geoip_match.group(1) if geoip_match else None - - response = { - 'uid': str(serverMsg[8]), - 'nickname': str(serverMsg[3]), - 'username': str(serverMsg[6]), - 'hostname': str(serverMsg[7]), - 'umodes': umodes, - 'vhost': str(serverMsg[11]), - 'ip': remote_ip, - 'realname': ' '.join(serverMsg[12:]).lstrip(':'), - 'geoip': geoip, - 'reputation_score': 0, - 'iswebirc': True if 'webirc' in serverMsg[0] else False, - 'iswebsocket': True if 'websocket' in serverMsg[0] else False - } - return response + uid = scopy[7] + return self._User.get_user(uid) def parse_quit(self, serverMsg: list[str]) -> dict[str, str]: """Parse quit and return dictionary. diff --git a/mods/defender/utils.py b/mods/defender/utils.py index d9e9be4..03f481b 100644 --- a/mods/defender/utils.py +++ b/mods/defender/utils.py @@ -212,7 +212,7 @@ def handle_on_uid(uplink: 'Defender', srvmsg: list[str]): uplink (Defender): The Defender instance srvmsg (list[str]): The Server MSG """ - parser_uid = uplink.Protocol.parse_uid(srvmsg) + _User = uplink.Protocol.parse_uid(srvmsg) gconfig = uplink.Config irc = uplink.Irc confmodel = uplink.ModConfig @@ -222,10 +222,8 @@ def handle_on_uid(uplink: 'Defender', srvmsg: list[str]): return None # Get User information - _User = irc.User.get_user(parser_uid.get('uid', None)) - if _User is None: - irc.Logs.warning(f'This UID: [{parser_uid.get("uid", None)}] is not available please check why') + irc.Logs.warning(f'Error when parsing UID', exc_info=True) return # If user is not service or IrcOp then scan them