Skip to content

Commit 4023323

Browse files
committedMar 4, 2023
pluginCommand: type it and guard against bad input
1 parent c2e7390 commit 4023323

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed
 

‎server/plugins/inputs/index.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import log from "../../log";
33
import Chan, {Channel} from "../../models/chan";
44
import Network, {NetworkWithIrcFramework} from "../../models/network";
55
import {PackageInfo} from "../packages";
6+
import PublicClient from "../packages/publicClient";
67

78
export type PluginInputHandler = (
89
this: Client,
@@ -15,7 +16,18 @@ export type PluginInputHandler = (
1516
type Plugin = {
1617
commands: string[];
1718
input: (network: Network, chan: Chan, cmd: string, args: string[]) => void;
18-
allowDisconnected?: boolean | undefined;
19+
allowDisconnected?: boolean;
20+
};
21+
22+
type ExternalPluginCommand = {
23+
packageInfo: PackageInfo;
24+
input: (
25+
pub: PublicClient,
26+
netChan: {network: Network; chan: Chan},
27+
cmd: string,
28+
args: string[]
29+
) => void;
30+
allowDisconnected?: boolean;
1931
};
2032

2133
const clientSideCommands = ["/collapse", "/expand", "/search"];
@@ -79,7 +91,7 @@ for (const input of builtInInputs) {
7991
});
8092
}
8193

82-
const pluginCommands = new Map();
94+
const pluginCommands = new Map<string, ExternalPluginCommand>();
8395

8496
const getCommands = () =>
8597
Array.from(userInputs.keys())
@@ -89,9 +101,22 @@ const getCommands = () =>
89101
.concat(passThroughCommands)
90102
.sort();
91103

92-
const addPluginCommand = (packageInfo: PackageInfo, command, func) => {
93-
func.packageInfo = packageInfo;
94-
pluginCommands.set(command, func);
104+
const addPluginCommand = (packageInfo: PackageInfo, command: any, obj: any) => {
105+
if (typeof command !== "string") {
106+
log.error(`plugin {packageInfo.packageName} tried to register a bad command`);
107+
return;
108+
} else if (!obj || typeof obj.input !== "function") {
109+
log.error(
110+
`plugin ${packageInfo.packageName} tried to register command "${command} without a callback"`
111+
);
112+
return;
113+
}
114+
115+
pluginCommands.set(command, {
116+
packageInfo: packageInfo,
117+
input: obj.input,
118+
allowDisconnected: obj.allowDisconnected,
119+
});
95120
};
96121

97122
export default {

0 commit comments

Comments
 (0)
Please sign in to comment.