verb: str,
source: _MsgTokGuide,
params: tuple[_MsgTokGuide, ...] = tuple(),
- idx_into_list: int = -1,
bonus_tasks: tuple[str, ...] = tuple()
) -> None:
self.verb = verb
self.source = _TokExpectation.from_(source)
self.params = tuple(_TokExpectation.from_(param) for param in params)
- self.idx_into_list = idx_into_list
self.bonus_tasks = tuple(_Code.from_(item) for item in bonus_tasks)
def parse_msg(self,
into_nickuserhost: Callable
) -> Optional[dict[str, Any]]:
'Try parsing msg into informative result dictionary, or None on fail.'
- cmp_params: list[str | tuple[str, ...]]
- if self.idx_into_list < 0:
- cmp_params = list(msg.params)
- else:
- idx_after = len(msg.params) + 1 - (len(self.params)
- - self.idx_into_list)
- cmp_params = (list(msg.params[:self.idx_into_list]) +
- [msg.params[self.idx_into_list:idx_after]] +
- list(msg.params[idx_after:]))
+ cmp_params: list[str | tuple[str, ...]] = []
+ idx_after = 0
+ for idx, param in enumerate(self.params):
+ if param == _MsgTok.LIST or (isinstance(param, tuple)
+ and param[0] == _MsgTok.LIST):
+ idx_after = len(msg.params) + 1 - (len(self.params) - idx)
+ cmp_params += [' '.join(msg.params[idx:idx_after])]
+ cmp_params += list(msg.params[idx_after:])
+ break
+ cmp_params += msg.params[idx:idx + 1]
+ if (not idx_after) and len(cmp_params) != len(msg.params):
+ return None
cmp_fields = tuple([msg.source] + cmp_params)
ex_fields = tuple([self.source] + list(self.params))
if len(ex_fields) != len(cmp_fields):
MSG_EXPECTATIONS: list[_MsgParseExpectation] = [
- # these we ignore except, where possible, for checking our nickname
+ # these we mostly ignore except, where possible, for checking our nickname
_MsgParseExpectation(
'001', # RPL_WELCOME
'005', # RPL_ISUPPORT
_MsgTok.SERVER,
((_MsgTok.NICKNAME, 'setattr_db.users.me:nick'),
- (_MsgTok.ANY, 'do_db.set_isupport_from_rpl:isupport'),
- _MsgTok.ANY), # comment
- idx_into_list=1),
+ (_MsgTok.LIST, 'do_db.set_isupport_from_rpl:isupport'),
+ _MsgTok.ANY)), # comment
_MsgParseExpectation(
'372', # RPL_MOTD
_MsgTok.SERVER,
('*',
('LS', ':subverb'),
+ ('*', ':tbc'),
(_MsgTok.LIST, ':items'))),
_MsgParseExpectation(
'CAP',
_MsgTok.SERVER,
('*',
('LS', ':subverb'),
- ('*', ':tbc'),
(_MsgTok.LIST, ':items'))),
_MsgParseExpectation(
'CAP',
_MsgTok.SERVER,
('*',
('LIST', ':subverb'),
+ ('*', ':tbc'),
(_MsgTok.LIST, ':items'))),
_MsgParseExpectation(
'CAP',
_MsgTok.SERVER,
('*',
('LIST', ':subverb'),
- ('*', ':tbc'),
(_MsgTok.LIST, ':items'))),
_MsgParseExpectation(
'CAP',
_MsgTok.SERVER,
((_MsgTok.NICKNAME, 'setattr_db.users.me:nick'),
('LIST', ':subverb'),
+ ('*', ':tbc'),
(_MsgTok.LIST, ':items'))),
_MsgParseExpectation(
'CAP',
_MsgTok.SERVER,
((_MsgTok.NICKNAME, 'setattr_db.users.me:nick'),
('LIST', ':subverb'),
- ('*', ':tbc'),
(_MsgTok.LIST, ':items'))),
# nickname management