From 5a30a983960db9c39caccadfafdf7822176ba7bf Mon Sep 17 00:00:00 2001 From: luk3yx Date: Sat, 7 May 2022 19:35:45 +1200 Subject: Process JOIN events --- miniirc_idc.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/miniirc_idc.py b/miniirc_idc.py index 1044144..726d2e2 100644 --- a/miniirc_idc.py +++ b/miniirc_idc.py @@ -49,7 +49,39 @@ def _get_idc_args(command: str, kwargs: Mapping[str, Optional[str | float]] yield f'{key.upper()}={value}' +def _parse_join(irc: IDC, hostmask: tuple[str, str, str], + tags: Mapping[str, str], args: list[str]) -> None: + users = tags.get('=idc-join-users') + if isinstance(users, str): + irc._dispatch('353', '', [irc.current_nick, '=', args[0], users]) + irc._dispatch('366', '', [irc.current_nick, args[0], + 'End of /NAMES list']) + + class IDC(miniirc.IRC): + if miniirc.ver[0] >= 2: + def _dispatch(self, command: str, user: str, args: list[str]) -> None: + self.handle_msg(miniirc.IRCMessage( + command, + (user, '~u', f'idc/{user}') if user else ('', '', ''), + {}, + args, + )) + else: + def _dispatch(self, command: str, user: str, args: list[str]) -> None: + if args: + args[-1] = _LEADING_COLON + args[-1] + self._handle( + command, + (user, '~u', f'idc/{user}') if user else ('', '', ''), + {}, + args, + ) + + def __init__(self, *args, **kwargs) -> None: + super().__init__(*args, **kwargs) + self.Handler('JOIN', colon=False, ircv3=True)(_parse_join) + def _idc_message_parser_no_exc( self, msg: str ) -> Optional[tuple[str, tuple[str, str, str], dict[str, str], list[str]]]: @@ -57,6 +89,7 @@ class IDC(miniirc.IRC): return self.idc_message_parser(msg) except Exception: traceback.print_exc() + return None def idc_message_parser( self, msg: str @@ -74,6 +107,7 @@ class IDC(miniirc.IRC): idc_cmd = arg # Translate IDC keyword arguments into IRC positional ones + tags = {} if idc_cmd == 'PRIVMSG': msg = idc_args['MESSAGE'] command = 'PRIVMSG' @@ -92,14 +126,23 @@ class IDC(miniirc.IRC): elif idc_cmd == 'PONG': command = 'PONG' args = [self.ip, idc_args.get('COOKIE', '')] + elif idc_cmd == 'JOIN': + command = 'JOIN' + idc_args['SOURCE'] = self.current_nick + args = ['#' + idc_args['CHANNEL']] + + # HACK: Add a message tag and fire other events later rather than + # firing events from the parser function which feels worse. + # The tag name starts with = so that it doesn't conflict with any + # actual IRC tags. + tags['=idc-join-users'] = idc_args['USERS'] else: return None # Add generic parameters - tags = {} if 'SOURCE' in idc_args: user = idc_args['SOURCE'] - hostmask = (user, user, user) + hostmask = (user, '~u', f'idc/{user}') tags['account'] = user else: hostmask = ('', '', '') @@ -116,9 +159,12 @@ class IDC(miniirc.IRC): if 'LABEL' in idc_args: tags['label'] = idc_args['LABEL'] - if args and _LEADING_COLON: - args[-1] = _LEADING_COLON + args[-1] - return command, hostmask, tags, args + if miniirc.ver[0] >= 2: + return miniirc.IRCMessage(command, hostmask, tags, args) + else: + if args: + args[-1] = _LEADING_COLON + args[-1] + return command, hostmask, tags, args # Send raw messages def idc_send(self, command: str, **kwargs: Optional[str | float]): -- cgit v1.2.3