This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.js
290 lines (256 loc) · 8.77 KB
/
main.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const NextcloudTalkBot = require("./src/nextcloudTalkBot");
const Jitsi = require("@pojntfx/jitsi-meet-node-client");
const crypto = require("crypto");
const log = require("pino")();
const util = require("util");
const programName = "nctalkbot";
function parse_args() {
var configData = null;
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
console.time("Parsing args");
// parse arguments
// https://devhints.io/yargs
const yargs = require("yargs");
const argv = yargs
.scriptName(programName)
.usage("$0 <options>")
.example(
"$0 -f nctalkbot-jitsi.yml",
" -> The file location is searched relative from calling path"
)
.alias("h", "help")
.env("NCTALKBOT_")
.help("help")
.epilog("© 2020 Felicitas Pojtinger")
.option("a", {
alias: "jitsiAddr",
description: "Jitsi server address",
nargs: 1,
type: "string",
//demand: true,
//demand: 'Argument jitsi addr required',
default: "meet.jit.si",
})
.option("c", {
alias: "commands",
description: "Bot commands",
nargs: 1,
type: "array",
demand: false,
default: '[ "#meeting", "#chat", "#meetjitsi" ]',
})
.option("f", {
alias: "configfile",
description: "Use config options from given yml file",
nargs: 1,
type: "string",
demand: false,
default: "/etc/nctalkbot-jitsi.yml",
})
.option("n", {
alias: "botName",
description: "Bots username",
nargs: 1,
type: "string",
demand: true,
demand: "Argument username required",
default: "jitsibot",
})
.option("p", {
alias: "jitsiRoomPasswordLength",
description: "Bot listening port",
nargs: 1,
type: "int",
demand: true,
demand: "Argument roomPasswordLength is required",
default: "4",
})
.option("r", {
alias: "nctalkproxydAddr",
description: "Nctalkproxyd server address",
nargs: 1,
type: "string",
demand: true,
demand: "Argument jitsi addr required",
default: "localhost:1969",
})
.option("s", {
alias: "sleepTimer",
description: "Bots looping sleep timeout value",
nargs: 1,
type: "int",
demand: true,
demand: "Argument sleepTimer is required",
default: "5",
})
.strict().argv;
//if (argv._.includes('file')) {
// const configFile = argv.configfile
//}
if (argv.version) {
console.debug(scriptName, " : v", version);
}
if (argv.configfile) {
let ext = path.extname(argv.configfile);
if (ext !== ".yml" && ext !== ".yaml") {
throw new Error(
`configfile '${argv.configfile}' has unsupported extension '${ext}'`
);
}
let configFile = path.normalize(argv.configfile);
if (path.isAbsolute(configFile)) {
console.log("configfile (absolute):", configFile);
} else {
configFile = path.join(__dirname, configFile);
console.log("configfile (relative):", configFile);
}
try {
// try with normalized path
configData = yaml.safeLoad(fs.readFileSync(configFile, "utf8"));
} catch (e) {
// try default places: ./
baseName = path.basename(configFile);
configFile = path.join(__dirname, baseName);
try {
console.debug("searching configfile:", configFile);
configData = yaml.safeLoad(fs.readFileSync(configFile, "utf8"));
} catch (e) {
// handle configfile as relative path
// try default places: ./etc
configFile = path.join(__dirname, "etc", baseName);
try {
console.debug("searching configfile:", configFile);
configData = yaml.safeLoad(fs.readFileSync(configFile, "utf8"));
} catch (e) {
console.warn("No suitable config file found.");
console.warn(" ... creating default structure");
// create default object
configData = {
nctalkbot: {
botName: "jitsibot",
sleepTimer: "20",
commands: ["#chat", "#meeting", "#meetjitsi"],
jitsiAddr: "meet.jit.si",
jitsiRoomPasswordLength: "4",
nctalkproxydAddr: "localhost:1969",
},
};
}
}
}
}
// check that given config file actually describes a nctalkbot environment
let objectName = util.inspect(
configData,
(showHidden = false),
(depth = 0),
(colorize = true)
);
var objectValue = objectName.substring(
objectName.lastIndexOf("{") + 2,
objectName.lastIndexOf(":")
);
if (objectValue !== programName)
throw new Error(
`configfile describes object '${objectValue}'. We do require '${programName}'!`
);
// Ugly: can't we just loop over??
configData.nctalkbot.botName = argv.botName;
configData.nctalkbot.commands = argv.commands;
configData.nctalkbot.sleepTimer = argv.sleepTimer;
configData.nctalkbot.jitsiAddr = argv.jitsiAddr;
configData.nctalkbot.jitsiRoomPasswordLength = argv.jitsiRoomPasswordLength;
configData.nctalkbot.nctalkproxydAddr = argv.nctalkproxydAddr;
if (process.env.NCTALKBOT_BOT_NAME)
configData.nctalkbot.botName = process.env.NCTALKBOT_BOT_NAME;
if (process.env.NCTALKBOT_COMMANDS)
configData.nctalkbot.commands = process.env.NCTALKBOT_COMMANDS.split(",");
if (process.env.NCTALKBOT_SLEEP_TIME)
configData.nctalkbot.sleepTimer = process.env.NCTALKBOT_SLEEP_TIME;
if (process.env.NCTALKBOT_JITSI_ADDR)
configData.nctalkbot.jitsiAddr = process.env.NCTALKBOT_JITSI_ADDR;
if (process.env.NCTALKBOT_JITSI_ROOM_PASSWORD_BYTE_LENGTH)
configData.nctalkbot.jitsiRoomPasswordLength = NCTALKBOT_JITSI_ROOM_PASSWORD_BYTE_LENGTH;
if (process.env.NCTALKBOT_NCTALKPROXYD_ADDR)
configData.nctalkbot.nctalkproxydAddr =
process.env.NCTALKBOT_NCTALKPROXYD_ADDR;
console.timeEnd("Parsing args");
return configData;
}
const main = async () => {
// parse_args returns struct "config" with param values
try {
var config = parse_args();
} catch (e) {
console.error("error:", e);
return;
}
console.debug(
"nctalkbot-jitsi: config structure ...\nconfig:",
util.inspect(config, (showHidden = false), (depth = 2), (colorize = true))
);
/*
console.debug("nctalkbot-jitsi: botName: " + config.nctalkbot.botName);
console.debug("nctalkbot-jitsi: commands: " + config.nctalkbot.commands);
console.debug("nctalkbot-jitsi: sleepTimer: " + config.nctalkbot.sleepTimer);
console.debug("nctalkbot-jitsi: jitsiAddr: " + config.nctalkbot.jitsiAddr);
console.debug("nctalkbot-jitsi: jitsiRoomPasswordByteLength: " + config.nctalkbot.jitsiRoomPasswordByteLength);
console.debug("nctalkbot-jitsi: nctalkproxydAddr: " + config.nctalkbot.nctalkproxydAddr);
*/
log.info(
`starting WebRTC node subsystem (timeout: ${config.nctalkbot.sleepTime} seconds)`
);
await jitsi.open();
const bot = new NextcloudTalkBot(config.nctalkbot.nctalkproxydAddr);
log.info(
`connecting to nctalkproxyd with address ${config.nctalkbot.nctalkproxydAddr}`
);
return await bot.readChats(async (chat) => {
log.info(
`Received message from "${chat.getActordisplayname()}" ("${chat.getActorid()}") in room "${chat.getToken()}." with ID "${chat.getId()}": "${JSON.stringify(
chat.getMessage()
)}`
);
const message = chat.getMessage();
const test = new RegExp(`^(${config.nctalkbot.commands.join("|")})`, "g");
if (!test.test(message)) {
return;
}
const token = chat.getToken();
const actorID = chat.getActorid();
const password = crypto
.randomBytes(parseInt(config.nctalkbot.nctalkbotRoomPasswordLength))
.toString("hex");
log.info(
`"${chat.getActordisplayname()}" ("${chat.getActorid}")
has requested a video call in room "${chat.getToken()}" with ID "${chat.getId()}"; creating video call.`
);
await bot.writeChat(
token,
`@${actorID} started a video call. Tap on https://${config.nctalkbot.jitsiaddr}/${token} and enter ${password}
to join; if no one joins within ${config.nctalkbot.sleepTimer} seconds or the last user leaves the meeting,
the password will be removed and the meeting will be closed.`
);
await jitsi.createRoom(
config.nctalkbot.jitsiAddr,
token,
config.nctalkbot.botName,
password,
config.nctalkbot.sleepTimer
);
return log.info(
`WebRTC subsystem exiting room ${chat.getToken()} after ${sleepTimer} seconds.`
);
});
};
const jitsi = new Jitsi();
process.on("SIGINT", async () => await jitsi.close());
process.on("uncaughtException", async (e) => {
const timeout = 0.125;
log.info(`bot crashed! Restarting in ${timeout} seconds:`, e.stack);
await new Promise((resolve) => setTimeout(resolve), timeout * 1000);
main();
});
main();