forked from c3subtitles/L2S2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.js
More file actions
693 lines (562 loc) · 19.2 KB
/
backend.js
File metadata and controls
693 lines (562 loc) · 19.2 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
var
fs = require('fs'),
path = require('path'),
config = require('./config'),
users = require('./users'),
express = require('express'),
serveIndex = require('serve-index'),
serveStatic = require('serve-static'),
socketio = require('socket.io'),
http = require('http'),
less = require('less-middleware'),
_ = require('lodash'),
extend = require('util')._extend,
clone = function(o) { return extend({}, o); },
app = express(),
server = http.Server(app),
io = socketio(server),
// per-room information. see 'connection'-event for per-room structure
rooms = {},
fahrplan = null;
// load some small helpers, like an oject-map call
require('./lib/helper');
// enable http requests logging
//app.use(morgan('dev'))
app.set('json spaces', '\t');
// enable less-compiler for all less-files in public
app.use(less('./public', {
compiler: {
sourceMap: true
}
}));
// serve ui files
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'public/read.html'));
});
app.get('/beamer', function(req, res) {
res.sendFile(path.join(__dirname, 'public/beamer.html'));
});
app.get('/write', function(req, res) {
res.sendFile(path.join(__dirname, 'public/write.html'));
});
// enable directory-indexes for the logs-folder
app.use('/logs', serveIndex('./public/logs', {
view: 'details'
}));
// serve a generic /status call
app.get('/status', function(req, res) {
res.json(rooms.map(function(room) {
return {
// number of connected sockets per room
'publicSockets': room.publicSockets.length,
'writerSockets': room.writerSockets.length,
// names of writers per room
'writerNames': room.writerSockets.map(function(socket) { return socket.username; }),
// locked-state
'adminlock': room.adminlock,
// room with speech recognition
'speechlock': room.speechlock,
// receiced number of lines per roomm
'statistics': room.statistics,
};
}));
});
// serve a per-room status call
app.get('/status/:room', function(req, res) {
var room = req.params.room;
res.jsonp(
(room in rooms) && (rooms[room].writerSockets.length > 0)
);
});
app.get('/current-talk/:room', function(req, res) {
if(!fahrplan) {
return res.end('early bird');
}
var
now = config.fahrplanSimulate ? new Date(config.fahrplanSimulate) : new Date(),
days = fahrplan.schedule.conference.days;
for (var i = 0; i < days.length; i++) {
var
day = days[i],
dayStart = new Date(day.day_start),
dayEnd = new Date(day.day_end);
if(dayStart > now || dayEnd < now) {
continue;
}
if(!day.rooms[req.params.room]) {
continue;
}
var talks = day.rooms[req.params.room];
for (var j = 0; j < talks.length; j++) {
var
talk = talks[j],
parts = talk.duration.split(':'),
duration = parseInt(parts[0]) * 60 + parseInt(parts[1]),
start = new Date(talk.date),
end = new Date(start.getTime() + duration*60*1000);
if(start > now || end < now) {
continue;
}
talk.now = now.getTime();
return res.json(talk);
}
}
return res.json(null);
});
// enable serving the public filder
app.use(serveStatic('./public'));
function fetchFahrplan() {
console.log('updating fahrplan from %s', config.fahrplan);
http.get(config.fahrplan, function(res) {
var data = '';
res.setEncoding('utf-8');
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
setTimeout(fetchFahrplan, config.fahrplanTTL);
console.log('got updated version of %s bytes', data.length);
var newFahrplan = JSON.parse(data);
if(!newFahrplan) {
return console.log('updated is not valid JSON, resheduling - keeping old version');
}
fahrplan = newFahrplan;
});
}).on('error', function(e) {
setTimeout(fetchFahrplan, config.fahrplanTTL);
return console.log('fahrplan-update returned error: %s - keeping old version', e.message);
});
}
fetchFahrplan();
// format an object consisting of all writers in the keys and
// the settings from users.json (minus password) in the value
// the result-object does only contain one item per user, even
// if the user has joined multiple times. its perfectly suitable
// for submitting it to the client for displaying
function aggregateWritersSettings(room) {
var writersSettings = {};
if(rooms[room]) {
rooms[room].writerSockets.forEach(function(socket) {
var
writer = socket.username,
settings = clone(users[writer]);
delete settings.password;
settings.cnt = writersSettings[writer] ? writersSettings[writer].cnt+1 : 1;
writersSettings[writer] = settings;
});
}
return writersSettings;
}
// serve socket-connections ;)
io.sockets.on('connection', function (socket) {
// hint: this is run once per-connection
var
// if set, the current socket has joined a room and can be found in
// the publicSockets- or the writerSockets-array for that room.
// on connection-close it must be removed from the respective array.
// it takes part in the broadcasting of lines for this room
joinedRoom,
// if set, the socket has successfully authorized itsself as the user
// named by joinedName. the socket can be found in the writerSockets-array
// for the room it has joined. on connection-close it must be removed from
// the respective array. it takes part in the broadcasting of partitial lines
// join and leave-events for this room. it is then also allowed to emit its
// own line and partline events
joinedName;
var partlineStamp = null;
console.log('connection %s from %s', socket.id, socket.conn.remoteAddress);
// join a room, possibly identify as a user is
socket.on('join', function(room, username, password, cb) {
// clean name - to be sure we're not hacked that easy
room = room.replace(/[^a-zA-Z0-9 ]/g, '-');
if(!rooms[room]) {
rooms[room] = {
publicSockets: [],
writerSockets: [],
logfile: null,
adminlock: null,
speechlock: null,
speechDelay: 5,
lastTwenty: [],
statistics: {
linesWritten: 0,
linesServed: 0
}
};
}
// if username and password is provided
if(username && password) {
console.log('socket %s tries to authenticate as %s', socket.id, username);
// check if the password correct
if(users[username] && users[username].password === password) {
console.log('socket %s is now authenticated as %s', socket.id, username);
// register that the socket successfully authenticated as user
joinedName = username;
socket.username = username;
// register username as a writer
rooms[room].writerSockets.push(socket);
joinedRoom = room;
console.log('now %s read-only sockets in room %s (%s writer sockets)', rooms[room].publicSockets.length, joinedRoom, rooms[room].writerSockets.length);
// craft a version of the users settings, suitable for sending
// to the client as initial statement
var writersSettings = aggregateWritersSettings(room);
// if a callback was requested, call back :)
if(cb) {
cb(true, writersSettings);
}
socket.emit('speechDelay', rooms[room].speechDelay);
if(rooms[room].adminlock) {
socket.emit('adminlock', rooms[room].adminlock.name);
}
if(rooms[room].speechlock) {
socket.emit('speechlock', rooms[room].speechlock.name);
}
console.log('informing all writersockets about new writers');
rooms[room].writerSockets.forEach(function(itersocket) {
itersocket.emit('writers', writersSettings);
});
// if no logfile for the room is opened yet
if(!rooms[room].logfile) {
// open a room-logfile
console.log('opening logfile for room %s', room);
rooms[room].logfile = fs.createWriteStream(
path.join('./public/logs/', room+'.txt'),
{ flags: 'a', encoding: 'utf8' }
);
}
return;
}
// the user wanted to authenticate but didn't succeed
else {
console.log('socket %s failed to authenticate as %s', socket.id, username);
// callback with a fail-flag
if(cb) {
cb(false);
}
// and don't change anything on our state
return;
}
}
// register that the socket successfully joined the room
console.log('socket %s joined room %s read-only', socket.id, room);
joinedRoom = room;
// add the socket to the per-room distribution list
rooms[room].publicSockets.push(socket);
joinedRoom = room;
console.log('now %s read-only sockets in room %s (%s writer sockets)', rooms[room].publicSockets.length, joinedRoom, rooms[room].writerSockets.length);
});
// on socket-disconnect
socket.on('disconnect', function() {
// if the user has not yet joined a room
if(!joinedRoom) {
// just let it go
console.log('disconnection of %s', socket.id);
return;
}
console.log('disconnection of %s from room %s', socket.id, joinedRoom);
// if the user was not authenticated
if(!joinedName) {
// remove the socket from the public list of that room
rooms[joinedRoom].publicSockets.splice(rooms[joinedRoom].publicSockets.indexOf(socket), 1);
console.log('now %s read-only sockets in room %s (%s writer sockets)', rooms[joinedRoom].publicSockets.length, joinedRoom, rooms[joinedRoom].writerSockets.length);
return;
}
// remove the writer-socket from the list of that room
rooms[joinedRoom].writerSockets.splice(rooms[joinedRoom].writerSockets.indexOf(socket), 1);
console.log('now %s read-only sockets in room %s (%s writer sockets)', rooms[joinedRoom].publicSockets.length, joinedRoom, rooms[joinedRoom].writerSockets.length);
// craft a version of the users settings, suitable for sending
// to the client as initial statement
var writersSettings = aggregateWritersSettings(joinedRoom);
// emit an event with text & stamp to the remaining sockets in that room
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
itersocket.emit('writers', writersSettings);
});
// if there is no writer for that room left
if(rooms[joinedRoom].logfile && rooms[joinedRoom].writerSockets.length === 0) {
// close the logfile
console.log('closing logfile for room %s', joinedRoom);
rooms[joinedRoom].logfile.close();
rooms[joinedRoom].logfile = null;
}
// if this user had locked the room - unlock it
if(rooms[joinedRoom].adminlock && rooms[joinedRoom].adminlock.id === socket.id) {
console.log('this user had locked the room %s - unlocking it', joinedRoom);
rooms[joinedRoom].adminlock = null;
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
itersocket.emit('adminunlock');
});
}
// if this user had locked the room - unlock it
if(rooms[joinedRoom].speechlock && rooms[joinedRoom].speechlock.id === socket.id)
{
console.log('this user had locked the room', joinedRoom, '- unlocking it');
rooms[joinedRoom].speechlock = null;
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
itersocket.emit('speechunlock');
});
}
});
function calculateDisplayDuration(line) {
var words = (line.match(/ /g) || []).length;
return 2 + words * 1.5;
}
// received a line from a socket
socket.on('line', function(line, correctionId) {
// sending lines is not allowed for non-authorized users
if(!joinedName) {
return;
}
//If locked only authorized
if (rooms[joinedRoom].adminlock && !users[joinedName].admin) {
return;
}
if (rooms[joinedRoom].speechlock && !correctionId && !(users[joinedName].admin || users[joinedName].speech)) {
return;
}
if (correctionId && _.contains(rooms[joinedRoom].lastTwenty, correctionId)) {
return;
}
if (correctionId) {
rooms[joinedRoom].lastTwenty.push(correctionId);
rooms[joinedRoom].lastTwenty = _.drop(rooms[joinedRoom].lastTwenty, rooms[joinedRoom].lastTwenty.length - 20);
}
console.log('line from %s for room %s: %s', socket.id, joinedRoom, line);
// stamp it
if(partlineStamp) {
console.log('using existing partlineStamp %s to stamp the line', partlineStamp);
}
var stamp = partlineStamp || Date.now();
partlineStamp = null;
// emit a line-event with text & stamp to all sockets in that room
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
itersocket.emit('line', stamp, line, joinedName, socket.id, correctionId);
});
// emit a line-event with text & stamp to all sockets in that room
rooms[joinedRoom].publicSockets.forEach(function(itersocket) {
itersocket.emit('line', stamp, line, calculateDisplayDuration(line));
rooms[joinedRoom].statistics.linesServed++;
});
// increment statistics counter
rooms[joinedRoom].statistics.linesWritten++;
// escape \n and \r in input
line = line.replace('\n', '\\n').replace('\r', '\\r');
// write a line into the logfile
rooms[joinedRoom].logfile.write(stamp+'\t'+line+'\n', 'utf8');
});
socket.on('removeCorrection', function(correctionId) {
if (!rooms[joinedRoom].speechlock) {
return;
}
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
itersocket.emit('removeCorrection', correctionId);
});
});
socket.on('correct', function(line) {
if(!joinedName) {
return;
}
if (!rooms[joinedRoom].speechlock) {
return;
}
console.log('line for correction from %s for room %s: %s', socket.id, joinedRoom, line);
// stamp it
if(partlineStamp) {
console.log('using existing partlineStamp %s to stamp the line', partlineStamp);
}
var stamp = partlineStamp || Date.now();
partlineStamp = null;
// emit a line-event with text & stamp to all sockets in that room
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
itersocket.emit('correct', stamp, line, joinedName, socket.id);
});
});
// received a partitial line from a socket
socket.on('partline', function(partline) {
// sending partlines is not allowed for non-authorized users
if(!joinedName) {
return;
}
if (rooms[joinedRoom].adminlock && !users[joinedName].admin) {
return;
}
if (rooms[joinedRoom].speechlock && !(users[joinedName].admin || users[joinedName].speech)) {
return;
}
if(partline.length === 0) {
partlineStamp = null;
console.log('partline got empty from %s - unsetting partlineStamp', socket.id);
} else if(!partlineStamp) {
partlineStamp = Date.now();
console.log('registering partlineStamp %s for %s', partlineStamp, socket.id);
}
// emit a line-event with text & stamp to all sockets in that room
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
itersocket.emit('partline', partline, joinedName, socket.id);
});
});
socket.on('adminlock', function(cb) {
// locking is not allowed for non-authorized users
if(!joinedName || !users[joinedName].admin) {
return;
}
if(rooms[joinedRoom].adminlock) {
console.log('room %s already locked by %s', joinedRoom, rooms[joinedRoom].adminlock.name);
return cb(false);
}
console.log('locking room %s for username %s', joinedRoom, joinedName);
rooms[joinedRoom].adminlock = {
id: socket.id,
name: joinedName
};
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
if(itersocket === socket) {
return;
}
itersocket.emit('adminlock', joinedName);
});
return cb(true);
});
socket.on('adminunlock', function(cb) {
if(!rooms[joinedRoom].adminlock || rooms[joinedRoom].adminlock.id !== socket.id) {
console.log('room %s not locked or locked by a different socket, NOT unlocking', joinedRoom);
return cb(false);
}
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
if(itersocket === socket) {
return;
}
itersocket.emit('adminunlock');
});
rooms[joinedRoom].adminlock = null;
return cb(true);
});
socket.on('speechlock', function(cb) {
// locking is not allowed for non-authorized users
if(!joinedName) {
return;
}
if(!users[joinedName].admin && !users[joinedName].speech) {
return;
}
if(rooms[joinedRoom].speechlock) {
console.log('room', joinedRoom, 'already locked by', rooms[joinedRoom].speechlock.name);
return cb(false);
}
console.log('locking room', joinedRoom, 'for username', joinedName);
rooms[joinedRoom].speechlock = {
id: socket.id,
name: joinedName
};
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
if(itersocket === socket) {
return;
}
itersocket.emit('speechlock', joinedName);
});
return cb(true);
});
socket.on('speechunlock', function(cb) {
if(!rooms[joinedRoom].speechlock || rooms[joinedRoom].speechlock.id !== socket.id) {
console.log('room', joinedRoom, 'not locked or locked by a different socket, NOT unlocking');
return cb(false);
}
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
if(itersocket === socket) {
return;
}
itersocket.emit('speechunlock');
});
rooms[joinedRoom].speechlock = null;
return cb(true);
});
socket.on('speechDelay', function(delay) {
if(!joinedName) {
return;
}
if (!rooms[joinedRoom].speechlock) {
return;
}
rooms[joinedRoom].speechDelay = delay;
console.log(delay);
rooms[joinedRoom].writerSockets.forEach(function(itersocket) {
itersocket.emit('speechDelay', delay);
});
});
function usersWithoutPasswords() {
return users.map(function(user) {
var newUser = clone(user);
delete newUser.password;
return newUser;
});
}
socket.on('usermgmt', function(task, cb) {
// sending lines is not allowed for non-authorized users
if(!joinedName) {
return;
}
// sending lines is not allowed for non-admin users
if(!users[joinedName].admin) {
return;
}
if(!task) {
console.log('sending usermgmt-list to %s', joinedName);
return cb(usersWithoutPasswords());
}
if(task.delete) {
if(task.username === joinedName) {
console.log('NOT allowing user %s to delete her-/himself', task.username);
return cb(false);
}
console.log('deleting user %s on behalf of %s', task.username, joinedName);
delete users[task.username];
} else {
if(users[task.username]) {
var user = users[task.username];
if(joinedName === task.username && user.admin && !task.admin) {
console.log('NOT allowing user %s to revoke her/hos own admin privileges', joinedName);
delete task.admin;
}
if(!task.password) {
delete task.password;
}
user = extend(user, task);
var username = task.username;
delete user.username;
console.log('modifying user %s on behalf of %s to %s', username, joinedName, user);
users[username] = user;
} else {
if(!task.password) {
console.log('NOT creating user without password');
return cb(false);
}
var username = task.username;
delete task.username;
console.log('creating user %s on behalf of %s as %s', username, joinedName, task);
users[username] = task;
}
}
console.log('saving new version of users.js');
var s = fs.createWriteStream('users.json', {encoding: 'utf-8'});
s.write(JSON.stringify(users, null, '\t'));
s.end();
return cb(usersWithoutPasswords());
});
});
// listen for connections
console.log('starting http/socket-server on port %s', config.port);
server.listen(config.port, config.host || '::', null, function() {
try {
console.log('Old User ID: ' + process.getuid() + ', Old Group ID: ' + process.getgid());
/* Throws an error that gid does not exist on my box
process.setgid(config.gid);
process.setuid(config.uid);
console.log('New User ID: ' + process.getuid() + ', New Group ID: ' + process.getgid());
*/
} catch (err) {
console.log(err);
console.log('Cowardly refusing to keep the process alive as root.');
process.exit(1);
}
});
/* vim: ts=4:sw=4:noet
*/