diff --git a/src/bouncer/network.py b/src/bouncer/network.py index d8173fb..f5d18b1 100644 --- a/src/bouncer/network.py +++ b/src/bouncer/network.py @@ -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]