This repository has been archived by the owner on May 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.js
executable file
·102 lines (92 loc) · 2.85 KB
/
app.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
#!/usr/bin/env node
GLOBAL.GSBOTVERSION = '2.0.19-RC_2';
var child_process = require('child_process');
var broadcasts = {};
function mergeConfig(bcConfig, masterConfig, depth)
{
var bcKeys = Object.keys(bcConfig);
var msKeys = Object.keys(masterConfig);
for (var i = 0; i < bcKeys.length; ++i)
{
var curr = bcKeys[i];
if (msKeys.indexOf(curr) == -1 || depth == 0)
{
masterConfig[curr] = bcConfig[curr];
}
else
{
mergeConfig(bcConfig[curr], masterConfig[curr], depth - 1);
}
}
}
function printChunk(base, chunk)
{
console.log(base + chunk.toString().replace(/\n$/, '').replace(/\n/g, '\n' + base));
}
function startBroadcast(bcast)
{
var child = child_process.fork('./core/gsbot.js', [], {
cwd: __dirname,
silent:true,
});
child.send(bcast);
child.stdout.on('data', function (chunk) {
printChunk(bcast.user + ': ', chunk);
});
child.stderr.on('data', function (chunk) {
printChunk('[ERR]' + bcast.user + ': ', chunk);
});
child.on('exit', function(code) {
console.log('!!!! User ' + bcast.user + ' exited with code ' + code + (code ? 'Restarting...' : ''));
if (code)
setTimeout(function() {
delete bcast.child;
startBroadcast(bcast);
}, 1000);
});
bcast.child = child;
}
function BroadcastManager(config, cb)
{
var allBroadcast = Object.keys(config.broadcasts);
if (allBroadcast.length == 0)
{
console.log('You need at least one broadcast to start.');
}
for (var i = 0; i < allBroadcast.length; ++i)
{
var user = allBroadcast[i];
var bcastConfig = config.broadcasts[user];
var whiteList = [];
var merged_conf = JSON.parse(JSON.stringify(config.plugins_conf));
mergeConfig(bcastConfig.plugins_conf, merged_conf, 2);
delete bcastConfig.plugins_conf;
bcastConfig.plugins_conf = merged_conf;
if (config.whiteList instanceof Array)
Array.prototype.push.apply(whiteList, config.whiteList);
if (bcastConfig.whiteList instanceof Array)
Array.prototype.push.apply(whiteList, bcastConfig.whiteList);
broadcasts[user] = { user: user, config: bcastConfig, whiteList: whiteList, version: GLOBAL.GSBOTVERSION };
startBroadcast(broadcasts[user]);
}
}
function checkFirstInstall(cb)
{
var fs = require('fs');
fs.exists('config.json', function(exists) {
if (exists)
{
var content = JSON.parse(fs.readFileSync('config.json', 'UTF-8'));
cb(content);
}
else
{
require('./core/reconfigure.js').reconfigure(cb);
}
});
}
checkFirstInstall(function (config) {
BroadcastManager(config, function() {
console.log('Done !');
});
});