mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import type { CommandFlagKey } from "../../config/commands.js";
|
|
import { isCommandFlagEnabled } from "../../config/commands.js";
|
|
import { logVerbose } from "../../globals.js";
|
|
import { isInternalMessageChannel } from "../../utils/message-channel.js";
|
|
import type { ReplyPayload } from "../types.js";
|
|
import type { CommandHandlerResult, HandleCommandsParams } from "./commands-types.js";
|
|
|
|
export function rejectUnauthorizedCommand(
|
|
params: HandleCommandsParams,
|
|
commandLabel: string,
|
|
): CommandHandlerResult | null {
|
|
if (params.command.isAuthorizedSender) {
|
|
return null;
|
|
}
|
|
logVerbose(
|
|
`Ignoring ${commandLabel} from unauthorized sender: ${params.command.senderId || "<unknown>"}`,
|
|
);
|
|
return { shouldContinue: false };
|
|
}
|
|
|
|
export function requireGatewayClientScopeForInternalChannel(
|
|
params: HandleCommandsParams,
|
|
config: {
|
|
label: string;
|
|
allowedScopes: string[];
|
|
missingText: string;
|
|
},
|
|
): CommandHandlerResult | null {
|
|
if (!isInternalMessageChannel(params.command.channel)) {
|
|
return null;
|
|
}
|
|
const scopes = params.ctx.GatewayClientScopes ?? [];
|
|
if (config.allowedScopes.some((scope) => scopes.includes(scope))) {
|
|
return null;
|
|
}
|
|
logVerbose(
|
|
`Ignoring ${config.label} from gateway client missing scope: ${config.allowedScopes.join(" or ")}`,
|
|
);
|
|
return {
|
|
shouldContinue: false,
|
|
reply: { text: config.missingText },
|
|
};
|
|
}
|
|
|
|
export function buildDisabledCommandReply(params: {
|
|
label: string;
|
|
configKey: CommandFlagKey;
|
|
disabledVerb?: "is" | "are";
|
|
docsUrl?: string;
|
|
}): ReplyPayload {
|
|
const disabledVerb = params.disabledVerb ?? "is";
|
|
const docsSuffix = params.docsUrl ? ` Docs: ${params.docsUrl}` : "";
|
|
return {
|
|
text: `⚠️ ${params.label} ${disabledVerb} disabled. Set commands.${params.configKey}=true to enable.${docsSuffix}`,
|
|
};
|
|
}
|
|
|
|
export function requireCommandFlagEnabled(
|
|
cfg: { commands?: unknown } | undefined,
|
|
params: {
|
|
label: string;
|
|
configKey: CommandFlagKey;
|
|
disabledVerb?: "is" | "are";
|
|
docsUrl?: string;
|
|
},
|
|
): CommandHandlerResult | null {
|
|
if (isCommandFlagEnabled(cfg, params.configKey)) {
|
|
return null;
|
|
}
|
|
return {
|
|
shouldContinue: false,
|
|
reply: buildDisabledCommandReply(params),
|
|
};
|
|
}
|