Skip to content

Commit ea8edf7

Browse files
committed
feat(plugins/registration): request capabilities on connect
1 parent 5f82c3b commit ea8edf7

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

core/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export type AnyCommand =
22
| "ADMIN"
3+
| "CAP"
34
| "CONNECT"
45
| "ERROR"
56
| "INFO"

plugins/registration.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,41 @@ export interface RegistrationParams {
2424
password?: string;
2525
};
2626
state: {
27+
capabilities: string[];
2728
user: User;
2829
};
2930
}
3031

32+
type AnyCapabilityCommand =
33+
| "LS"
34+
| "LIST"
35+
| "REQ"
36+
| "ACK"
37+
| "NAK"
38+
| "NEW"
39+
| "DEL"
40+
| "END";
41+
3142
export const registrationPlugin: Plugin<
3243
& NickParams
3344
& RegisterParams
3445
& RegistrationParams
3546
> = (client, options) => {
47+
const requestCapability = (
48+
command: AnyCapabilityCommand,
49+
...params: string[]
50+
) => client.send("CAP", command, ...params);
51+
3652
const register = () => {
53+
const { capabilities } = client.state;
54+
55+
if (capabilities.length > 0) {
56+
for (const capability of capabilities) {
57+
requestCapability("REQ", capability);
58+
}
59+
requestCapability("END");
60+
}
61+
3762
if (password !== undefined) {
3863
client.pass(password);
3964
}
@@ -61,6 +86,7 @@ export const registrationPlugin: Plugin<
6186
};
6287

6388
const { nick, username = nick, realname = nick, password } = options;
89+
client.state.capabilities ??= []; // TODO depends of plugins loading order
6490
client.state.user = { nick, username, realname };
6591

6692
client.on("connected", register);

plugins/registration_test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertEquals } from "../deps.ts";
1+
import { assertArrayIncludes, assertEquals } from "../deps.ts";
22
import { describe } from "../testing/helpers.ts";
33
import { mock } from "../testing/mock.ts";
44
import { nickPlugin } from "./nick.ts";
@@ -51,6 +51,29 @@ describe("plugins/registration", (test) => {
5151
]);
5252
});
5353

54+
test("request capabilities on connect", async () => {
55+
const { client, server } = await mock(
56+
plugins,
57+
options,
58+
{ withConnection: false },
59+
);
60+
const { capabilities } = client.state;
61+
62+
capabilities.push("cap1");
63+
capabilities.push("cap2");
64+
capabilities.push("cap3");
65+
66+
await client.connect("");
67+
const raw = server.receive();
68+
69+
assertArrayIncludes(raw, [
70+
"CAP REQ cap1",
71+
"CAP REQ cap2",
72+
"CAP REQ cap3",
73+
"CAP END",
74+
]);
75+
});
76+
5477
test("initialize user state", async () => {
5578
const { client } = await mock(plugins, options);
5679
const { user } = client.state;

0 commit comments

Comments
 (0)