-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
103 lines (90 loc) · 3.13 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {
Appservice,
EncryptionAlgorithm,
LogLevel,
LogService,
MessageEvent,
RichConsoleLogger,
RustSdkAppserviceCryptoStorageProvider,
SimpleFsStorageProvider,
SimpleRetryJoinStrategy,
} from "matrix-bot-sdk";
LogService.setLogger(new RichConsoleLogger());
LogService.setLevel(LogLevel.TRACE);
LogService.muteModule("Metrics");
LogService.trace = LogService.debug;
const appservice = new Appservice({
bindAddress: "0.0.0.0",
port: 9994,
homeserverName: "local",
homeserverUrl: "http://synapse:8008",
storage: new SimpleFsStorageProvider("/data/encryption_appservice.json"),
registration: {
"as_token": "change_me",
"hs_token": "change_me",
"sender_localpart": "encbot",
"namespaces": {
users: [{
regex: "@crypto_.*:local",
exclusive: true,
}],
rooms: [],
aliases: [],
},
"de.sorunome.msc2409.push_ephemeral": true,
},
joinStrategy: new SimpleRetryJoinStrategy(),
cryptoStorage: new RustSdkAppserviceCryptoStorageProvider("/data/encryption"),
intentOptions: {
encryption: true,
},
});
const bot = appservice.botIntent;
(async function () {
await bot.enableEncryption();
let privateRoomId: string;
const joinedRooms = await bot.underlyingClient.getJoinedRooms();
for (const roomId of joinedRooms) {
if (await bot.underlyingClient.crypto.isRoomEncrypted(roomId)) {
try {
const roomName = (await bot.underlyingClient.getRoomStateEvent(roomId, "m.room.name", ""))?.["name"];
if (roomName === "Encbot Private") {
privateRoomId = roomId;
break;
}
} catch (e) { }
}
}
if (!privateRoomId) {
privateRoomId = await bot.underlyingClient.createRoom({
visibility: "private",
preset: "private_chat",
initial_state: [
{ type: "m.room.encryption", state_key: "", content: { algorithm: EncryptionAlgorithm.MegolmV1AesSha2 } },
{ type: "m.room.name", state_key: "", content: { name: "Encbot Private" } },
],
});
}
appservice.on("room.invite", async (roomId: string, inviteEvent: any) => {
LogService.info("index", `Received invite for ${inviteEvent["state_key"]} to ${roomId}`);
await bot.joinRoom(roomId);
});
appservice.on("room.join", (roomId: string, joinEvent: any) => {
LogService.info("index", `Joined ${roomId} as ${joinEvent["state_key"]}`);
});
appservice.on("room.failed_decryption", async (roomId: string, event: any, e: Error) => {
LogService.error("index", `Failed to decrypt ${roomId} ${event['event_id']} because `, e);
});
appservice.on("room.message", async (roomId: string, event: any) => {
const message = new MessageEvent(event);
if (message.messageType !== "m.text") return;
if (message.textBody.startsWith("!ping")) {
await bot.underlyingClient.replyNotice(roomId, event, "Pong");
} else if (message.textBody.startsWith("!invite")) {
await bot.underlyingClient.inviteUser(message.sender, privateRoomId);
await bot.underlyingClient.replyNotice(roomId, event, "Invited you into private room");
}
});
LogService.info("index", "Starting appservice...");
await appservice.begin();
})();