Defer JOIN until NickServ login confirmed (fixes +r channel 477)

This commit is contained in:
megaproxy 2026-06-04 17:25:18 +01:00
parent 841a152436
commit 52e579e364
2 changed files with 39 additions and 7 deletions

29
bot.py
View file

@ -173,8 +173,13 @@ class IRCBot:
time.sleep(backoff)
backoff = min(backoff * 2, 300)
def _join_channel(self):
log.info("Joining %s", CHANNEL)
self._send_raw(f"JOIN {CHANNEL}")
def _event_loop(self):
registered = False
joined = False
for line in self._read_lines():
log.debug("<< %s", line)
prefix, command, params = parse_message(line)
@ -183,15 +188,33 @@ class IRCBot:
self._send_raw(f"PONG :{params[-1] if params else ''}")
continue
# 376 = end of MOTD, 422 = no MOTD; either means we can join.
# 376 = end of MOTD, 422 = no MOTD; either means we're registered.
if command in ("376", "422") and not registered:
registered = True
if NICKSERV_PASS:
# Identify first, then wait for the login confirmation
# below before joining. +r channels reject us until the
# NickServ login has actually landed, so joining now races.
self.send_privmsg("NickServ", f"IDENTIFY {NICKSERV_PASS}")
log.info("Joining %s", CHANNEL)
self._send_raw(f"JOIN {CHANNEL}")
log.info("Identifying with NickServ; join deferred until login")
else:
self._join_channel()
joined = True
continue
# 900 RPL_LOGGEDIN, or the NickServ "now identified" notice —
# either confirms login is complete, so it's safe to join +r chans.
if not joined and registered and NICKSERV_PASS:
identified = command == "900" or (
command == "NOTICE"
and prefix.lower().startswith("nickserv!")
and "now identified" in (params[-1].lower() if params else "")
)
if identified:
self._join_channel()
joined = True
continue
# Nick already in use — try a variant so we still connect.
if command == "433":
global NICK