fix: wait for nick confirmation before joining channels

Send NICK and wait for server confirmation (up to 10s) before
issuing JOIN commands, ensuring channels are joined under the
correct identity.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
user
2026-02-20 18:26:25 +01:00
parent 280d0c3949
commit ab7603f638

View File

@@ -161,6 +161,8 @@ class Network:
self._connect_nick: str = ""
# Visible hostname reported by server
self.visible_host: str | None = None
# Signals that a NICK change was confirmed by the server
self._nick_confirmed: asyncio.Event = asyncio.Event()
# Channel state: topic + names per channel
self.topics: dict[str, str] = {}
self.names: dict[str, set[str]] = {}
@@ -335,8 +337,14 @@ class Network:
desired = _random_nick()
log.info("[%s] switching nick: %s -> %s (host=%s)", self.cfg.name, self.nick, desired,
self.visible_host or "unknown")
self._nick_confirmed.clear()
await self.send_raw("NICK", desired)
# nick will be updated when we get the NICK confirmation from server
# Wait for server to confirm the nick change before joining
try:
await asyncio.wait_for(self._nick_confirmed.wait(), timeout=10)
except asyncio.TimeoutError:
log.warning("[%s] nick change not confirmed in 10s, joining anyway", self.cfg.name)
# Join configured channels
if self.cfg.autojoin and self.cfg.channels:
@@ -392,6 +400,7 @@ class Network:
if old_nick == self.nick:
log.info("[%s] nick changed: %s -> %s", self.cfg.name, old_nick, new_nick)
self.nick = new_nick
self._nick_confirmed.set()
elif msg.command == "JOIN" and msg.prefix:
nick = msg.prefix.split("!")[0]