Skip to content

Commit dc0bc57

Browse files
committed
fix(irc): prevent ghost nick loops on collision recovery
- Change buildFallbackNick to accept attempt number for unique suffixes - Use numeric suffix (1, 2, 3...) instead of always '_' - Limit fallback attempts to 10 per connection to prevent infinite loops - Fixes issue where Nick_ ghosts would repeatedly connect/disconnect Fixes #96064
1 parent 04575a9 commit dc0bc57

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

‎extensions/irc/src/client.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,18 @@ function toError(err: unknown): Error {
6666
return new Error(typeof err === "string" ? err : JSON.stringify(err));
6767
}
6868

69-
function buildFallbackNick(nick: string): string {
69+
function buildFallbackNick(nick: string, attempt: number = 0): string {
7070
const normalized = nick.replace(/\s+/g, "");
7171
const safe = normalized.replace(/[^A-Za-z0-9_\-[\]\\`^{}|]/g, "");
7272
const base = safe || "openclaw";
73-
const suffix = "_";
73+
// Use numeric suffix for uniqueness when attempt > 0, otherwise just "_"
74+
const suffix = attempt > 0 ? `${attempt}` : "_";
7475
const maxNickLen = 30;
75-
if (base.length >= maxNickLen) {
76-
return `${base.slice(0, maxNickLen - suffix.length)}${suffix}`;
76+
const result = base + suffix;
77+
if (result.length > maxNickLen) {
78+
return base.slice(0, maxNickLen - suffix.length) + suffix;
7779
}
78-
return `${base}${suffix}`;
80+
return result;
7981
}
8082

8183
function normalizeIrcNick(value: string): string {
@@ -119,7 +121,7 @@ export async function connectIrcClient(options: IrcClientOptions): Promise<IrcCl
119121
let ready = false;
120122
let closed = false;
121123
let nickServRecoverAttempted = false;
122-
let fallbackNickAttempted = false;
124+
let fallbackNickAttemptCount = 0;
123125
let removeAbortListener: (() => void) | null = null;
124126

125127
const socket = options.tls
@@ -179,9 +181,9 @@ export async function connectIrcClient(options: IrcClientOptions): Promise<IrcCl
179181
}
180182
}
181183

182-
if (!fallbackNickAttempted) {
183-
fallbackNickAttempted = true;
184-
const fallbackNick = buildFallbackNick(desiredNick);
184+
if (fallbackNickAttemptCount < 10) {
185+
fallbackNickAttemptCount++;
186+
const fallbackNick = buildFallbackNick(desiredNick, fallbackNickAttemptCount);
185187
if (normalizeIrcNick(fallbackNick) !== normalizeIrcNick(currentNick)) {
186188
try {
187189
sendRaw(`NICK ${fallbackNick}`);

0 commit comments

Comments
 (0)