-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompliance_bot.js
More file actions
186 lines (157 loc) · 4.96 KB
/
compliance_bot.js
File metadata and controls
186 lines (157 loc) · 4.96 KB
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
var fs = require('fs');
const WickrIOBotAPI = require('wickrio-bot-api');
const WickrUser = WickrIOBotAPI.WickrUser;
const bot = new WickrIOBotAPI.WickrIOBot();
const logger = require('./logger');
const WickrIOAPI = bot.apiService().WickrIOAPI;
process.title = "complianceBot";
module.exports = WickrIOAPI;
process.stdin.resume(); //so the program will not close instantly
async function exitHandler(options, err) {
try {
var closed = await bot.close();
if (err || options.exit) {
logger.info("Exit reason:", err);
process.exit();
} else if (options.pid) {
process.kill(process.pid);
}
} catch (err) {
logger.error(err);
}
}
//catches ctrl+c and stop.sh events
process.on('SIGINT', exitHandler.bind(null, { exit: true }));
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, { pid: true }));
process.on('SIGUSR2', exitHandler.bind(null, { pid: true }));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, { exit: true }));
async function main() {
try {
var client;
bot.processesJsonToProcessEnv()
var tokens = JSON.parse(process.env.tokens);
if (tokens.WICKRIO_BOT_NAME !== undefined &&
tokens.WICKRIO_BOT_NAME.value !== undefined) {
client = tokens.WICKRIO_BOT_NAME.value;
} else if (process.argv[2] === undefined) {
client = await fs.readFileSync('client_bot_username.txt', 'utf-8');
client = client.trim();
} else {
client = process.argv[2];
}
var status = await bot.start(client)
if (!status) {
exitHandler(null, {
exit: true,
reason: 'Client not able to start'
});
}
} catch (err) {
logger.error(err);
process.exit();
}
try {
var type='complianceruntime';
var value='true';
await WickrIOAPI.cmdSetControl(type, value);
await WickrIOAPI.cmdSetControl('contactbackup', 'false');
await WickrIOAPI.cmdSetControl('convobackup', 'false');
} catch (err) {
logger.error(err);
process.exit();
}
// Set the duration (seconds)
var rtDuration='1440';
if (tokens.RUNTIME_DURATION !== undefined) {
try {
if (tokens.RUNTIME_DURATION.encrypted) {
rtDuration = await WickrIOAPI.cmdDecryptString(tokens.RUNTIME_DURATION.value);
} else {
rtDuration = tokens.RUNTIME_DURATION.value;
}
rtDurationSeconds = parseInt(rtDuration, 10) * 60;
rtDuration = rtDurationSeconds.toString();
await WickrIOAPI.cmdSetControl('duration', rtDuration);
} catch (err) {
logger.error(err);
process.exit();
}
}
var useStreaming;
// set the streaming, if is turned on
if (tokens.USE_STREAMING !== undefined) {
if (tokens.USE_STREAMING.encrypted) {
useStreaming = await WickrIOAPI.cmdDecryptString(tokens.USE_STREAMING.value);
} else {
useStreaming = tokens.USE_STREAMING.value;
}
if (useStreaming === "yes") {
var dest, basename, maxsize, attachloc;
if (tokens.STREAM_DESTINATION !== undefined) {
if (tokens.STREAM_DESTINATION.encrypted) {
dest = await WickrIOAPI.cmdDecryptString(tokens.STREAM_DESTINATION.value);
} else {
dest = tokens.STREAM_DESTINATION.value;
}
} else {
dest = __dirname + "messages";
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
}
if (tokens.STREAM_BASENAME.encrypted) {
basename = await WickrIOAPI.cmdDecryptString(tokens.STREAM_BASENAME.value);
} else {
basename = tokens.STREAM_BASENAME.value;
}
if (tokens.STREAM_MAXSIZE.encrypted) {
maxsize = await WickrIOAPI.cmdDecryptString(tokens.STREAM_MAXSIZE.value);
} else {
maxsize = tokens.STREAM_MAXSIZE.value;
}
if (tokens.STREAM_DESTINATION !== undefined) {
if (tokens.STREAM_ATTACHLOC.encrypted) {
attachloc = await WickrIOAPI.cmdDecryptString(tokens.STREAM_ATTACHLOC.value);
} else {
attachloc = tokens.STREAM_ATTACHLOC.value;
}
} else {
attachloc = __dirname + "attachments";
if (!fs.existsSync(attachloc)) {
fs.mkdirSync(attachloc);
}
}
var csm = await WickrIOAPI.cmdSetFileStreaming(dest, basename, maxsize, attachloc);
logger.info(csm);
}
} else {
useStreaming = "no";
}
// If not using streaming then start listening for messages
if (useStreaming !== "yes") {
// turn off streaming
try {
var csm = await WickrIOAPI.cmdSetStreamingOff();
logger.info(csm);
} catch (err) {
logger.error(err);
}
// set the callback function that will receive incoming messages into the bot client
try {
await bot.startListening(listen);
} catch (err) {
logger.error(err);
process.exit();
}
}
}
function listen(message) {
try {
fs.appendFileSync('receivedMessages.log', message + '\n', 'utf8');
} catch (err) {
return logger.error(err);
}
}
main();