-
Notifications
You must be signed in to change notification settings - Fork 1
/
client-setup.ts
84 lines (71 loc) · 2.96 KB
/
client-setup.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
import { readFileSync, writeFileSync } from 'fs';
import { MatrixClient, MatrixAuth, RustSdkCryptoStorageProvider, SimpleFsStorageProvider, AutojoinRoomsMixin } from 'matrix-bot-sdk';
import { getFromEnv, loadConfig } from '../utils/env';
import '../utils/globals';
globalThis.homeserverUrl = getFromEnv('HOMESERVER_URL');
/**
* A function that checks for a PASSWORD environment variable,
* If it exists, it...
* - uses that and LOGINNAME to generate an access token
* - saves the newly generated access token into .env
* - removes PASSWORD and LOGINNAME from .env
* - reloads the environment variables
* Otherwise, it simply passes
*/
export async function generateAccessToken() : Promise<void> {
if (getFromEnv('PASSWORD', true) != '') {
console.log("Deteced password. Generating access_token...");
// Based on https://github.com/turt2live/matrix-bot-sdk/blob/13ce618976446ac4c8d325acf7aab80a9f5e8d2c/examples/login_register.ts
let auth = await new MatrixAuth(homeserverUrl).passwordLogin(getFromEnv('LOGINNAME'), getFromEnv('PASSWORD'));
let data = readFileSync('.env', { encoding: 'utf-8' });
data += `\nACCESS_TOKEN="${auth.accessToken}"`;
data = data.replace(/LOGINNAME=.*(\n|)/gi, '').replace(/PASSWORD=.*(\n|)/gi, '');
writeFileSync('.env', data, {
encoding: 'utf-8'
});
loadConfig();
}
}
/**
* Sets up...
* - Storage
* - Encryption
* - MatrixClient object
* - globals.clientId, the userId of the MatrixClient
* - globals.regexSelfMention, a Regex to check whether the client has been mentioned in a message
* - Mixins (as of writing, only the AutoJoinRoomsMixin)
*
* @returns {MatrixClient} - A Matrix client for the bot, ready to be started
*/
export async function setupClient() {
const storage = new SimpleFsStorageProvider('./data/bot.json');
const crypto = new RustSdkCryptoStorageProvider('./data/crypto');
const client = new MatrixClient(homeserverUrl, getFromEnv('ACCESS_TOKEN'), storage, crypto);
globalThis.clientId = await client.getUserId();
AutojoinRoomsMixin.setupOnClient(client);
await client.crypto.prepare(await client.getJoinedRooms());
return client;
}
/**
* Starts the passed client, logging its clientId & homeserverUrl
*
* @param {MatrixClient} client - The Matrix client/bot to start
* @returns {MatrixClient} - The started Matrix client/bot
*/
export async function startClient(client: MatrixClient) {
await client.start();
console.log('Client started!');
console.log(`Logged in as ${clientId} on ${homeserverUrl}`);
return client;
}
/**
* Combines the above functions to fully create & start the Matrix Client
* - @see generateAccessToken
* - @see matrixLogin
* - @see startClient
*
* @returns {Promise<MatrixClient>} - The promise for the Matrix client/bot
*/
export async function newClient() : Promise<MatrixClient> {
return generateAccessToken().then(setupClient).then(startClient);
}