pluginCommand: type it and guard against bad input

This commit is contained in:
Reto Brunner 2023-03-04 17:00:53 +01:00
parent c2e7390127
commit 402332340b

View File

@ -3,6 +3,7 @@ import log from "../../log";
import Chan, {Channel} from "../../models/chan"; import Chan, {Channel} from "../../models/chan";
import Network, {NetworkWithIrcFramework} from "../../models/network"; import Network, {NetworkWithIrcFramework} from "../../models/network";
import {PackageInfo} from "../packages"; import {PackageInfo} from "../packages";
import PublicClient from "../packages/publicClient";
export type PluginInputHandler = ( export type PluginInputHandler = (
this: Client, this: Client,
@ -15,7 +16,18 @@ export type PluginInputHandler = (
type Plugin = { type Plugin = {
commands: string[]; commands: string[];
input: (network: Network, chan: Chan, cmd: string, args: string[]) => void; input: (network: Network, chan: Chan, cmd: string, args: string[]) => void;
allowDisconnected?: boolean | undefined; allowDisconnected?: boolean;
};
type ExternalPluginCommand = {
packageInfo: PackageInfo;
input: (
pub: PublicClient,
netChan: {network: Network; chan: Chan},
cmd: string,
args: string[]
) => void;
allowDisconnected?: boolean;
}; };
const clientSideCommands = ["/collapse", "/expand", "/search"]; const clientSideCommands = ["/collapse", "/expand", "/search"];
@ -79,7 +91,7 @@ for (const input of builtInInputs) {
}); });
} }
const pluginCommands = new Map(); const pluginCommands = new Map<string, ExternalPluginCommand>();
const getCommands = () => const getCommands = () =>
Array.from(userInputs.keys()) Array.from(userInputs.keys())
@ -89,9 +101,22 @@ const getCommands = () =>
.concat(passThroughCommands) .concat(passThroughCommands)
.sort(); .sort();
const addPluginCommand = (packageInfo: PackageInfo, command, func) => { const addPluginCommand = (packageInfo: PackageInfo, command: any, obj: any) => {
func.packageInfo = packageInfo; if (typeof command !== "string") {
pluginCommands.set(command, func); log.error(`plugin {packageInfo.packageName} tried to register a bad command`);
return;
} else if (!obj || typeof obj.input !== "function") {
log.error(
`plugin ${packageInfo.packageName} tried to register command "${command} without a callback"`
);
return;
}
pluginCommands.set(command, {
packageInfo: packageInfo,
input: obj.input,
allowDisconnected: obj.allowDisconnected,
});
}; };
export default { export default {