Skip to content

Commit 87696ae

Browse files
committed
feat(plugins): add on_connect plugin
Add following options: - channels - oper
1 parent 9304889 commit 87696ae

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as Motd from "./plugins/motd.ts";
1212
import * as Msg from "./plugins/msg.ts";
1313
import * as Nick from "./plugins/nick.ts";
1414
import * as Notice from "./plugins/notice.ts";
15+
import * as OnConnect from "./plugins/on_connect.ts";
1516
import * as Oper from "./plugins/oper.ts";
1617
import * as Part from "./plugins/part.ts";
1718
import * as Ping from "./plugins/ping.ts";
@@ -29,6 +30,7 @@ export type Options =
2930
& Debug.Options
3031
& Invite.Options
3132
& Nick.Options
33+
& OnConnect.Options
3234
& Ping.Options
3335
& Register.Options
3436
& Time.Options

plugins/on_connect.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { ExtendedClient } from "../core/mod.ts";
2+
import { createPlugin } from "../core/mod.ts";
3+
import type { JoinPluginParams } from "./join.ts";
4+
import type { OperPluginParams } from "./oper.ts";
5+
import type { RegisterPluginParams } from "./register.ts";
6+
7+
export interface Options {
8+
/** Channels to join on connect. */
9+
channels?: string[];
10+
11+
/** Sets as operator on connect. */
12+
oper?: {
13+
/** Username operator. */
14+
user: string;
15+
/** Password operator. */
16+
pass: string;
17+
};
18+
}
19+
20+
export interface OnConnectPluginParams {
21+
options: Options;
22+
}
23+
24+
function autoJoin(
25+
client: ExtendedClient<
26+
OnConnectPluginParams & JoinPluginParams & RegisterPluginParams
27+
>,
28+
) {
29+
const { channels } = client.options;
30+
31+
if (channels === undefined || channels.length === 0) {
32+
return;
33+
}
34+
35+
client.on("register", () => {
36+
client.join(...channels);
37+
});
38+
}
39+
40+
function autoOper(
41+
client: ExtendedClient<
42+
OnConnectPluginParams & OperPluginParams & RegisterPluginParams
43+
>,
44+
) {
45+
if (client.options.oper === undefined) {
46+
return;
47+
}
48+
49+
const { user, pass } = client.options.oper;
50+
51+
client.on("register", () => {
52+
client.oper(user, pass);
53+
});
54+
}
55+
56+
export const plugin = createPlugin<
57+
& OnConnectPluginParams
58+
& JoinPluginParams
59+
& OperPluginParams
60+
& RegisterPluginParams
61+
>(autoOper, autoJoin);

plugins/on_connect_test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { assertEquals } from "../core/test_deps.ts";
2+
import { arrange } from "../core/test_helpers.ts";
3+
import { plugin as join } from "./join.ts";
4+
import { plugin as nick } from "./nick.ts";
5+
import { plugin as onConnect } from "./on_connect.ts";
6+
import { plugin as oper } from "./oper.ts";
7+
import { plugin as register } from "./register.ts";
8+
9+
Deno.test("on_connect channels", async () => {
10+
const { server, client, sanitize } = arrange(
11+
[onConnect, join, nick, register],
12+
{ nick: "nick", channels: ["#channel1", "#channel2"] },
13+
);
14+
15+
server.listen();
16+
client.connect(server.host, server.port);
17+
await server.waitClient();
18+
19+
server.send(":serverhost 001 nick :Welcome to the server");
20+
const raw = await server.once("JOIN");
21+
assertEquals(raw, "JOIN #channel1,#channel2");
22+
23+
await sanitize();
24+
});
25+
26+
Deno.test("on_connect oper", async () => {
27+
const { server, client, sanitize } = arrange(
28+
[onConnect, oper, nick, register],
29+
{ nick: "nick", oper: { user: "user", pass: "pass" } },
30+
);
31+
32+
server.listen();
33+
client.connect(server.host, server.port);
34+
await server.waitClient();
35+
36+
server.send(":serverhost 001 nick :Welcome to the server");
37+
const raw = await server.once("OPER");
38+
assertEquals(raw, "OPER user pass");
39+
40+
await sanitize();
41+
});

0 commit comments

Comments
 (0)