-
Notifications
You must be signed in to change notification settings - Fork 4
/
MinisterIN.js
351 lines (319 loc) · 9.33 KB
/
MinisterIN.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*jshint esnext: true */
// Include needed node.js modules
var http = require('http');
var request = require('request');
var Twitter = require('twitter');
var irc = require('irc');
var bot = require('./ircbot.js');
var gh_webhook = require('github-webhook-handler');
var program = require('commander');
var mqtt = require('mqtt');
var fs = require('fs');
var cron = require('node-schedule');
var mattermost = require('node-mattermost');
require('datejs');
var numOfHackers = -1;
var spaceOpen = false;
program
.version('1.3.1')
.usage('space=[open/closed]')
.option(
'-s, --space <state>',
'Whether the space is open or closed (default = closed)',
/^(closed|open)$/i,
'closed'
)
.parse(process.argv);
// Check if "space=open" was given as argument
spaceOpen = program.space.toLowerCase() === 'open';
// Read MQTT configuration file
var mqttConfig;
try {
mqttConfig = require('./mqtt_config.json');
} catch (e) {
console.log('Could not parse MQTT configuration file: ' + e.message);
mqttConfig = {
caFile: 'ca.crt',
port: '8883',
host: 'mqtt.lambdaspace.gr'
};
}
// Create an object with the API keys and access tokens
var APIKeys = require('./apikeys.json');
// If the keys file cannot be parsed, MinisterIN terminates abnormally
// Read tweet messages from the tweets.json file
var tweetMsgs;
var helloMsgs;
try {
tweetMsgs = require('./tweets.json');
} catch (e) {
console.log('Could not parse tweets file: ' + e.message);
tweetMsgs = {
statusOpen: ['Minister is in'],
statusClosed: ['Minister is out']
};
}
try {
helloMsgs = require('./hello.json');
} catch (e) {
console.log('Could not parse hello file: ' + e.message);
helloMsgs = {
hello: ['hello!']
};
}
// Create a twitter client
var twitterClient = new Twitter({
consumer_key: APIKeys.twitter.consumer_key,
consumer_secret: APIKeys.twitter.consumer_secret,
access_token_key: APIKeys.twitter.access_token_key,
access_token_secret: APIKeys.twitter.access_token_secret
});
// Read Mattermost config
try {
var mattermostConfig = require('./mattermost_config.json');
} catch (e) {
console.log('Could not parse mattermost configuration file: ' + e.message);
}
// Create Mattermost client
if (mattermostConfig.hook_url) var mattermostClient = new mattermost(mattermostConfig.hook_url);
var mattermostBroadcast = function(msg, channel = '#off-topic') {
if (!msg) return;
try {
mattermostClient.send({
text: msg,
channel: channel,
icon_url: mattermostConfig.icon,
username: mattermostConfig.username
});
return true;
} catch (e) {
console.log('Could not post to mattermost' + e);
return false;
}
};
// Read IRC configuration file
var ircConfig;
try {
ircConfig = require('./irc_config.json');
} catch (e) {
console.log('Could not parse IRC configuration file: ' + e.message);
ircConfig = {
server: 'irc.freenode.net',
nick: 'Consuela-λspace',
channels: ['#LambdaSpace']
};
}
var CA = fs.readFileSync(__dirname + '/' + mqttConfig.caFile);
var mqttOptions = {
port: mqttConfig.port,
host: mqttConfig.host,
protocol: 'mqtts',
rejectUnauthorized: true,
//The CA list will be used to determine if server is authorized
ca: CA
};
try {
var client = mqtt.connect(mqttOptions);
} catch (e) {
console.log(e);
}
client.on('connect', function() {
console.log('Connected to MQTT broker ' + mqttConfig.host);
});
// Create an IRC client
var ircClient = new irc.Client(ircConfig.server, ircConfig.nick, ircConfig);
ircClient.sayAllChannels = function(message) {
ircConfig.channels.forEach(function(channel) {
try {
ircClient.say(channel, message);
} catch (e) {
console.log(e);
}
});
};
/**
* Tweet the status of the space
*
* @param newStatus true if the space is open,
* false if the space is closed.
*/
var broadcastStatusChange = function(newStatus) {
var msg;
var rand;
if (newStatus) {
rand = Math.floor(Math.random() * tweetMsgs.statusOpen.length);
try {
msg = tweetMsgs.statusOpen[rand] + ' - Space: OPEN';
} catch (e) {
console.log(e);
}
} else {
rand = Math.floor(Math.random() * tweetMsgs.statusClosed.length);
try {
msg = tweetMsgs.statusClosed[rand] + ' - Space: CLOSED';
} catch (e) {
console.log(e);
}
}
twitterClient.post('statuses/update', { status: msg }, function(error, tweet, response) {
if (error) {
console.log('Could not tweet: ' + tweet); // Tweet body.
console.log(response); // Raw response object.
} else {
// IRC bot says the random message in the channel
ircClient.sayAllChannels(msg);
// Update the status of the spaceOpen variable only after the status
// got successfully tweeted
spaceOpen = newStatus;
}
});
if (mattermostBroadcast(msg, '#status-updates')) spaceOpen = newStatus;
};
/**
* Updates the status of the space, given the number
* of the hackers there.
*
* @param numOfHackers the number of hackers in space. or NaN
* in case there was an error while parsing the hackers.txt file.
*/
var updateStatus = function(numOfHackers) {
if (spaceOpen) {
if (numOfHackers === 0) {
// If space status was open and there are no hackers anymore
broadcastStatusChange(false);
}
} else {
if (numOfHackers > 0) {
// If space was closed and there are hackers now
broadcastStatusChange(true);
}
}
};
// Reply to questions directed to Consuela
ircClient.addListener('message#LambdaSpace', function(from, message) {
var reply = bot(message, numOfHackers, helloMsgs);
if (reply !== undefined) {
try {
ircClient.say(ircConfig.channels[0], from + reply);
} catch (e) {
console.log(e);
}
}
});
// Welcome new people joining the LambdaSpace channel
// ircClient.addListener('join#LambdaSpace', function(nick, message) {
// ircClient.say(ircConfig.channels[0], nick + ", " +
// helloMsgs.hello[Math.floor(Math.random() * helloMsgs.hello.length)]);
// });
// Github organization Webhooks
var gh_webhook_handler = gh_webhook({ path: '/lambdaspace', secret: APIKeys.github.webhook });
try {
http
.createServer(function(req, res) {
gh_webhook_handler(req, res, function(err) {
res.statusCode = 404;
res.end('No such location');
});
})
.listen(7777);
} catch (e) {
console.log(e);
}
gh_webhook_handler.on('push', function(event) {
ircClient.sayAllChannels(
event.payload.pusher.name + ' pushed to repository ' + event.payload.repository.name + ':'
);
event.payload.commits.forEach(function(commit) {
var msg = '* ' + commit.author.name + ' - ' + commit.message;
ircClient.sayAllChannels(msg);
mattermostBroadcast(msg);
});
});
gh_webhook_handler.on('error', function(err) {
console.error('Github Webhook error:', err.message);
});
// Add MQTT listener callback
client.on('message', function(topic, message) {
numOfHackers = parseInt(message.toString());
// If message could not be parsed to number
if (isNaN(numOfHackers)) {
console.log('Could not parse number of hackers: ' + message.toString());
return;
}
updateStatus(numOfHackers);
// Uncomment to debug number of hackers
//console.log('Number of Hackers: ' + numOfHackers);
});
try {
client.subscribe('lambdaspace/spacestatus/hackers');
} catch (e) {
console.log(e);
}
/* Disclaimer: follows code that is very specific to the LambdaSpace stack */
// Parse events from discourse
var eventParser = function(topic) {
var event = {};
var tokens = topic.split(' ');
event.day = tokens[0];
if (!event.day.match(/^\d\d\/\d\d\/\d\d\d\d+$/)) {
console.log('Not in expected format');
return;
}
var dateTokens = tokens[0].split('/');
event.date = new Date(dateTokens[2], dateTokens[1] - 1, dateTokens[0], 0, 0);
if (tokens[1].match(/^\d\d:\d\d+$/)) {
event.time = tokens[1];
event.title = topic.substr(17);
} else {
event.time = '';
event.title = topic.substr(11);
}
return event;
};
// Iterate the list of events from discourse and announce forthcoming ones
var parseEvents = function(data) {
data.topic_list.topics.forEach(function(topic) {
var event;
try {
event = eventParser(topic.title);
} catch (e) {
return;
}
if (!event) return;
var pubEvent = null;
if (event.date.equals(Date.parse('today'))) {
pubEvent = 'TODAY ';
} else if (event.date.equals(Date.parse('tomorrow'))) {
pubEvent = 'TOMORROW ';
}
if (!!pubEvent) {
pubEvent = pubEvent + event.time + ' - ' + event.title;
ircClient.sayAllChannels(pubEvent);
pubEvent = pubEvent.substring(0, 139);
twitterClient.post('statuses/update', { status: pubEvent }, function(error, tweet, response) {
if (error) {
console.log('Could not tweet event: ' + pubEvent); // Tweet body.
}
});
mattermostBroadcast(pubEvent);
}
});
};
// Check for events every day at 11:00
cron.scheduleJob('0 0 11 * * * *', function() {
try {
request('https://community.lambdaspace.gr/c/5/l/latest.json', function(error, response, body) {
if (!error && response.statusCode == 200) {
try {
parseEvents(JSON.parse(body));
} catch (e) {
console.log('Response from Discourse could not be parsed to JSON');
}
} else {
console.log('Error while retrieving events.json');
}
});
} catch (e) {
console.log(e);
}
});