Skip to content

Commit b845cff

Browse files
author
Peter Steinberger
committed
fix(irc): recognize punctuation in nickname mentions
1 parent 12297b7 commit b845cff

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

‎extensions/irc/src/inbound.behavior.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,70 @@ describe("irc inbound behavior", () => {
334334
expect(ctx?.OriginatingTo).toBe("channel:#ops");
335335
});
336336

337+
it.each([
338+
{ label: "ordinary nick", nick: "OpenClaw", text: "OpenClaw: hello", mentioned: true },
339+
{ label: "ASCII case folding", nick: "OpenClaw", text: "openclaw: hello", mentioned: true },
340+
{ label: "leading bracket", nick: "[Claw]", text: "[Claw]: hello", mentioned: true },
341+
{ label: "trailing bracket", nick: "Claw]", text: "hello Claw],", mentioned: true },
342+
{ label: "leading caret", nick: "^Claw", text: "^Claw, hello", mentioned: true },
343+
{ label: "trailing hyphen", nick: "Claw-", text: "Claw-: hello", mentioned: true },
344+
{ label: "escaped backslash", nick: "\\Claw", text: "\\Claw: hello", mentioned: true },
345+
{ label: "embedded brackets", nick: "Claw[Ops]", text: "Claw[Ops]: hi", mentioned: true },
346+
{ label: "RFC1459 opening bracket", nick: "[Claw", text: "{claw: hello", mentioned: true },
347+
{ label: "RFC1459 opening brace", nick: "{Claw", text: "[claw: hello", mentioned: true },
348+
{ label: "RFC1459 closing bracket", nick: "Claw]", text: "claw}: hello", mentioned: true },
349+
{ label: "RFC1459 closing brace", nick: "Claw}", text: "claw]: hello", mentioned: true },
350+
{ label: "RFC1459 backslash", nick: "\\Claw", text: "|claw: hello", mentioned: true },
351+
{ label: "RFC1459 vertical bar", nick: "|Claw", text: "\\claw: hello", mentioned: true },
352+
{ label: "RFC1459 caret", nick: "^Claw", text: "~claw: hello", mentioned: true },
353+
{ label: "RFC1459 tilde", nick: "~Claw", text: "^claw: hello", mentioned: true },
354+
{ label: "ordinary nick suffix", nick: "Claw", text: "Clawbot: hello", mentioned: false },
355+
{ label: "ordinary nick prefix", nick: "Claw", text: "overClaw: hello", mentioned: false },
356+
{ label: "IRC nick punctuation suffix", nick: "Claw", text: "Claw-bot: hi", mentioned: false },
357+
{ label: "RFC1459 tilde nick suffix", nick: "Claw", text: "Claw~bot: hi", mentioned: false },
358+
{
359+
label: "punctuated nick inside a longer nick",
360+
nick: "[Claw]",
361+
text: "prefix[Claw]: hello",
362+
mentioned: false,
363+
},
364+
])(
365+
"recognizes only complete IRC nickname mentions: $label",
366+
async ({ nick, text, mentioned }) => {
367+
const coreRuntime = createPluginRuntimeMock();
368+
const runtime = createRuntimeEnv();
369+
setIrcRuntime(coreRuntime as never);
370+
371+
await handleIrcInbound({
372+
message: createMessage({
373+
target: "#ops",
374+
isGroup: true,
375+
text,
376+
}),
377+
account: createAccount({
378+
nick,
379+
config: {
380+
dmPolicy: "open",
381+
allowFrom: ["*"],
382+
groupPolicy: "open",
383+
groupAllowFrom: [],
384+
groups: {
385+
"#ops": { enabled: true, requireMention: true },
386+
},
387+
},
388+
}),
389+
config: { channels: { irc: {} } } as CoreConfig,
390+
runtime,
391+
sendReply: vi.fn(async () => {}),
392+
});
393+
394+
expect(coreRuntime.channel.inbound.dispatch).toHaveBeenCalledTimes(mentioned ? 1 : 0);
395+
if (!mentioned) {
396+
expect(runtime.log).toHaveBeenCalledWith("irc: drop channel #ops (missing-mention)");
397+
}
398+
},
399+
);
400+
337401
it("drops a spoofed sender for a host-less nick!user DM allowlist entry", async () => {
338402
const coreRuntime = createPluginRuntimeMock();
339403
const runtime = createRuntimeEnv();

‎extensions/irc/src/inbound.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ const ircIngressIdentity = defineStableChannelIngressIdentity({
8181
});
8282

8383
const escapeIrcRegexLiteral = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
84+
// IRC nicknames permit punctuation, so ASCII word boundaries lose valid leading/trailing chars.
85+
const IRC_NICK_CHARACTER = String.raw`[A-Za-z0-9_\-\[\]\\\x60^{}|~]`;
86+
const IRC_RFC1459_CASE_EQUIVALENTS = new Map([
87+
["[", "{"],
88+
["{", "["],
89+
["]", "}"],
90+
["}", "]"],
91+
["\\", "|"],
92+
["|", "\\"],
93+
["^", "~"],
94+
["~", "^"],
95+
]);
96+
97+
function buildIrcNickMentionPattern(value: string): string {
98+
return Array.from(value, (character) => {
99+
const equivalent = IRC_RFC1459_CASE_EQUIVALENTS.get(character);
100+
return equivalent
101+
? `[${escapeIrcRegexLiteral(character)}${escapeIrcRegexLiteral(equivalent)}]`
102+
: escapeIrcRegexLiteral(character);
103+
}).join("");
104+
}
84105

85106
function isBareNick(value: string): boolean {
86107
return !value.includes("!") && !value.includes("@");
@@ -266,7 +287,10 @@ export async function handleIrcInbound(params: {
266287
const mentionRegexes = core.channel.mentions.buildMentionRegexes(config as OpenClawConfig);
267288
const mentionNick = connectedNick?.trim() || account.nick;
268289
const explicitMentionRegex = mentionNick
269-
? new RegExp(`\\b${escapeIrcRegexLiteral(mentionNick)}\\b[:,]?`, "i")
290+
? new RegExp(
291+
`(?<!${IRC_NICK_CHARACTER})${buildIrcNickMentionPattern(mentionNick)}(?!${IRC_NICK_CHARACTER})[:,]?`,
292+
"i",
293+
)
270294
: null;
271295
const wasMentioned =
272296
core.channel.mentions.matchesMentionPatterns(rawBody, mentionRegexes) ||

0 commit comments

Comments
 (0)