mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-25 10:09:33 +00:00
87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
// Irc helper module supports normalize behavior.
|
|
import {
|
|
normalizeLowercaseStringOrEmpty,
|
|
normalizeOptionalLowercaseString,
|
|
} from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
import { hasIrcControlChars } from "./control-chars.js";
|
|
import type { IrcInboundMessage } from "./types.js";
|
|
|
|
const IRC_TARGET_PATTERN = /^[^\s:]+$/u;
|
|
|
|
export function isChannelTarget(target: string): boolean {
|
|
return target.startsWith("#") || target.startsWith("&");
|
|
}
|
|
|
|
export function normalizeIrcMessagingTarget(raw: string): string | undefined {
|
|
const trimmed = raw.trim();
|
|
if (!trimmed) {
|
|
return undefined;
|
|
}
|
|
let target = trimmed;
|
|
const lowered = normalizeLowercaseStringOrEmpty(target);
|
|
if (lowered.startsWith("irc:")) {
|
|
target = target.slice("irc:".length).trim();
|
|
}
|
|
if (normalizeLowercaseStringOrEmpty(target).startsWith("channel:")) {
|
|
target = target.slice("channel:".length).trim();
|
|
if (!target.startsWith("#") && !target.startsWith("&")) {
|
|
target = `#${target}`;
|
|
}
|
|
}
|
|
if (normalizeLowercaseStringOrEmpty(target).startsWith("user:")) {
|
|
target = target.slice("user:".length).trim();
|
|
}
|
|
if (!target || !looksLikeIrcTargetId(target)) {
|
|
return undefined;
|
|
}
|
|
return target;
|
|
}
|
|
|
|
export function looksLikeIrcTargetId(raw: string): boolean {
|
|
const trimmed = raw.trim();
|
|
if (!trimmed) {
|
|
return false;
|
|
}
|
|
if (hasIrcControlChars(trimmed)) {
|
|
return false;
|
|
}
|
|
return IRC_TARGET_PATTERN.test(trimmed);
|
|
}
|
|
|
|
export function normalizeIrcAllowEntry(raw: string): string {
|
|
let value = normalizeLowercaseStringOrEmpty(raw);
|
|
if (!value) {
|
|
return "";
|
|
}
|
|
if (value.startsWith("irc:")) {
|
|
value = value.slice("irc:".length);
|
|
}
|
|
if (value.startsWith("user:")) {
|
|
value = value.slice("user:".length);
|
|
}
|
|
return value.trim();
|
|
}
|
|
|
|
export function buildIrcAllowlistCandidates(
|
|
message: IrcInboundMessage,
|
|
params?: { allowNameMatching?: boolean },
|
|
): string[] {
|
|
const nick = normalizeLowercaseStringOrEmpty(message.senderNick);
|
|
const user = normalizeOptionalLowercaseString(message.senderUser);
|
|
const host = normalizeOptionalLowercaseString(message.senderHost);
|
|
const candidates = new Set<string>();
|
|
if (nick && params?.allowNameMatching === true) {
|
|
candidates.add(nick);
|
|
}
|
|
if (nick && user) {
|
|
candidates.add(`${nick}!${user}`);
|
|
}
|
|
if (nick && host) {
|
|
candidates.add(`${nick}@${host}`);
|
|
}
|
|
if (nick && user && host) {
|
|
candidates.add(`${nick}!${user}@${host}`);
|
|
}
|
|
return [...candidates];
|
|
}
|