forked from Tiiffi/mcrcon
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·135 lines (113 loc) · 4.9 KB
/
cli.js
File metadata and controls
executable file
·135 lines (113 loc) · 4.9 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
#!/usr/bin/env node
'use strict';
var debug = require('debug')('mcrcon:cli');
var MCrcon = require('./lib/mcrcon');
var cli = function(argv) {
argv = require('minimist')(argv.slice(2));
debug('presented command-line parameters: %j', argv);
var password = argv._[0];
var host = argv.h || argv.host || process.env.HOST || '127.0.0.1';
var port = parseInt(argv.p || argv.port || process.env.PORT || '25575');
var message = argv.c || argv.command;
debug(' password: %s', password);
debug(' host: %s', host);
debug(' port: %s', port);
debug(' message: %s', message);
if (!port || !host || !password) {
console.log('usage: mcrcon <password> [-h <host>] [-p <port>] [-c <command>]');
process.exit(0);
}
debug('connecting to minecraft server at %s:%d with password %s', host, port, password);
var mcrcon = new MCrcon(password);
mcrcon.connect({ port: port, host: host }, function (err) {
if (err) {
console.error('error connecting to %s:%d [%s]', host, port, err.message);
process.exit(1);
}
if (message) {
mcrcon.command({'id': 1, 'message': message.trim()}, function (err, response) {
if (err) {
console.error('error sending command \'%s\' [%s]', message, err.message);
} else {
console.log(response.message);
}
mcrcon.close();
process.exit(0);
});
} else {
var commands = require('./lib/commands.json');
var completer = function() {
var completions = [];
for (var command in commands) {
if (commands.hasOwnProperty(command)) {
completions.push(command.trim());
}
}
return function (partial) {
return [
completions.filter(function (completion) {
return completion.indexOf(partial) === 0;
}),
partial
];
};
};
var id = 1;
var readline = require('readline').createInterface(process.stdin, process.stdout, completer());
readline.setPrompt('MCrcon> ');
readline.prompt();
readline
.on('line', function (message) {
readline.pause();
message = message.trim();
switch (message) {
case '.help':
console.log('<minecraft command> [args]');
console.log('.commands (or TAB) for list of commands');
console.log('.exit to exit the application.');
readline.prompt();
break;
case '.commands':
for (var command in commands) {
if (commands.hasOwnProperty(command)) {
console.log(command + ': ' + commands[command]);
}
}
readline.prompt();
break;
case '.exit':
readline.question('Are you sure you want to exit? ', function (answer) {
if (answer.match(/^y(es)?$/i)) {
readline.close();
}
});
break;
default:
if (message[0] === '.') {
console.log('unknown command');
readline.prompt();
} else if (message === 'stop') {
console.log('\'stop\' command is disabled');
readline.prompt();
} else {
mcrcon.command({'id': id, 'message': message.trim()}, function (err, response) {
if (err) {
console.error('error sending command \'%s\' [%s]', message, err.message);
} else {
console.log(response.message);
}
id++;
readline.prompt();
});
}
break;
}
})
.on('close', function () {
mcrcon.close();
process.exit(0);
});
}
});
};
cli(process.argv);