-
Notifications
You must be signed in to change notification settings - Fork 12
/
slaves.js
71 lines (64 loc) · 1.59 KB
/
slaves.js
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
// abstraction around the client to make thin slaves
'use strict';
const fs = require('fs');
const Client = require('./client.js').Client;
/** @type {{[k: string]: boolean}} */
let NamesUsed = {};
/** @type {{[k: string]: {nick: string, pass: string}}} */
let Credentials = {};
function LoadCredentials() {
Credentials = {};
try {
Credentials = JSON.parse(fs.readFileSync('./config/credentials.json').toString());
} catch (e) {}
}
LoadCredentials();
function CountCredentials() {
return Object.keys(Credentials).length;
}
class SlaveClient {
/**
* @param {AnyObject} credentials
* @param {string[]} rooms
*/
constructor(credentials, rooms) {
this.name = credentials.nick;
this.userid = toId(credentials.nick);
this.client = new Client({
nick: credentials.nick,
pass: credentials.pass,
autojoin: rooms,
reconnectTime: 0,
avatar: Math.floor(Math.random() * 100),
});
this.client.connect();
return this;
}
kill() {
this.client.send('|/logout');
this.client.disconnect();
NamesUsed[this.userid] = false;
}
}
/**
* @param {string} preferredName
*/
function GetCredentials(preferredName = '') {
const prefId = toId(preferredName);
if (prefId && !NamesUsed[prefId] && Credentials[prefId]) return Credentials[prefId];
for (const id of Tools.lazyShuffle(Object.keys(Credentials))) {
if (NamesUsed[id]) continue;
NamesUsed[id] = true;
return Credentials[id];
}
// no credentials available - panic!
debug('Ran out of anon credential accounts!');
return null;
}
module.exports = {
SlaveClient,
GetCredentials,
NamesUsed,
LoadCredentials,
CountCredentials,
};