-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
executable file
·201 lines (180 loc) · 5.74 KB
/
server.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
'use strict';
/**
* Created by rogerk on 10/20/15.
*/
var childProcess = require('child_process');
var fs = require('fs');
var Path = require('path');
var timers = require('timers');
var extend = require('util')._extend;
var config = require('./config');
var timerPending = false;
var parseState = {};
var running = {};
var pending = {};
function findCommand(values) {
var keys = Object.keys(config.commands);
for (var ix = 0; ix < keys.length; ix++) {
var cmd = config.commands[keys[ix]];
if (cmd.match) {
var m = values.command.match(cmd.match);
if (m) {
return extend({ name: keys[ix] }, cmd);
}
}
}
return null;
}
function replaceValues(txt, values) {
var keys = Object.keys(values);
for (var ix = 0; ix < keys.length; ix++) {
txt = txt.replace('{' + keys[ix] + '}', values[keys[ix]]);
}
return txt;
}
function executeCommand(cmd, values, completed) {
cmd = extend({
cwd: Path.resolve(cmd.cwd || __dirname),
timeout: config.minecraft.timeout || 10000
}, cmd);
if (!cmd.hasOwnProperty('file')) {
cmd.file = config.mcexec.file;
cmd.args = config.mcexec.args.concat(cmd.args || config.mcrunas.args);
}
if (running[cmd.file]) {
pending[cmd.file] = (pending[cmd.file] || []).concat([{command: cmd, values: values, cb: completed}]);
return;
}
running[cmd.file] = true;
var finished = function() {
running[cmd.file] = false;
try {
if (cmd.complete) {
timers.setTimeout(function() { executeCommand(config.mctell, {username: values.username, command: cmd.complete}); }, 1);
}
if (completed) {
completed();
}
}
catch(ex) { console.error(ex); }
if (pending[cmd.file] && pending[cmd.file].length > 0) {
var next = pending[cmd.file][0];
pending[cmd.file] = pending[cmd.file].splice(1);
timers.setTimeout(function() { executeCommand(next.command, next.values, next.cb); }, 10);
}
};
try {
var args = (cmd.args || []).map(function(arg) { return replaceValues(arg, values); });
console.log('> ' + cmd.file + ' ' + args.join(' '));
if (cmd.file !== config.mcexec.file && cmd.started) {
timers.setTimeout(function() { executeCommand(config.mctell, {username: values.username, command: cmd.started}); }, 1);
}
if (cmd.action) {
cmd.action(
function rawMcCommand(cmdText, callback) {
var scmd = extend({}, config.mcexec);
scmd.args = scmd.args.concat([cmdText]);
executeCommand(scmd, {username: values.username, command: ''}, callback);
},
extend({}, values),
finished
);
}
else {
childProcess.execFile(cmd.file, args, {
cwd: Path.resolve(cmd.cwd || __dirname),
timeout: config.minecraft.timeout || 10000
},
function (err, stdOut, stdErr) {
if (err) {
console.log(err.message);
}
if (stdErr.length) {
console.log('! ' + stdErr.toString());
}
if (stdOut.length) {
console.log('< ' + stdOut.toString());
}
finished();
});
}
}
catch(ex) {
console.log('ERR: ' + ex.message);
finished();
}
}
function parseLogLine(line) {
if (!line) {
return null;
}
var match = line.match(config.minecraft.logFormat);
if(match) {
var values = {
username: match[1],
command: match[2]
};
var cmd = findCommand(values);
if (cmd) {
return executeCommand(cmd, values);
}
else if (values.command.match(/^\/help/i)) {
return executeCommand(config.mctell, {
username: values.username,
command: 'Available commands: ' + Object.keys(config.commands).join(', ')
});
}
else {
values.command = 'Unknown command: ' + values.command;
return executeCommand(config.mctell, values);
}
}
match = line.match(config.minecraft.badCommand);
if (match) {
values = {
username: match[2],
command: match[1].replace(/["']+/g, '')
};
return executeCommand(config.mctell, values);
}
return null;
}
function parseLogFile() {
timerPending = false;
var content;
try {
content = fs.readFileSync(config.minecraft.log);
} catch(ex) {
console.log(ex.message);
return;
}
var lines = content.toString().split('\n');
lines.pop();
if (!parseState.lines) {
parseState.lines = lines.length;
return;
}
if (parseState.lines > lines.length) {
parseState.lines = 0;
}
for (parseState.lines; parseState.lines < lines.length; parseState.lines++) {
var line = lines[parseState.lines].replace('\r', '');
try {
parseLogLine(line);
}
catch (ex) {
console.log('ERR: ' + ex.toString());
}
}
//console.log(JSON.stringify(parseState));
}
parseLogFile();
var notify = new (require('fs.notify'))([config.minecraft.log]);
notify.on('change', function(path, op) {
console.log('Changed: %s %s ', op, path);
if (!timerPending) {
timerPending = true;
timers.setTimeout(parseLogFile, 100);
}
});
console.log('Started OK.');