2017-08-09 11:05:55 +02:00
|
|
|
const async = require('async');
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 11:05:55 +02:00
|
|
|
class Teamspeak3 extends require('./core') {
|
|
|
|
run(state) {
|
2014-10-29 08:02:03 +01:00
|
|
|
async.series([
|
2017-08-09 11:05:55 +02:00
|
|
|
(c) => {
|
|
|
|
this.sendCommand('use port='+this.options.port, (data) => {
|
|
|
|
const split = data.split('\n\r');
|
|
|
|
if(split[0] !== 'TS3') this.fatal('Invalid header');
|
2014-10-29 08:02:03 +01:00
|
|
|
c();
|
|
|
|
}, true);
|
|
|
|
},
|
2017-08-09 11:05:55 +02:00
|
|
|
(c) => {
|
|
|
|
this.sendCommand('serverinfo', (data) => {
|
2014-10-29 08:02:03 +01:00
|
|
|
state.raw = data[0];
|
2015-01-04 18:35:11 +01:00
|
|
|
if('virtualserver_name' in state.raw) state.name = state.raw.virtualserver_name;
|
2015-01-17 12:57:38 +01:00
|
|
|
if('virtualserver_maxclients' in state.raw) state.maxplayers = state.raw.virtualserver_maxclients;
|
2014-10-29 08:02:03 +01:00
|
|
|
c();
|
|
|
|
});
|
|
|
|
},
|
2017-08-09 11:05:55 +02:00
|
|
|
(c) => {
|
|
|
|
this.sendCommand('clientlist', (list) => {
|
|
|
|
for (const client of list) {
|
|
|
|
client.name = client.client_nickname;
|
|
|
|
delete client.client_nickname;
|
|
|
|
if(client.client_type === '0') {
|
|
|
|
state.players.push(client);
|
2016-05-23 00:35:56 +02:00
|
|
|
}
|
2014-10-29 08:02:03 +01:00
|
|
|
}
|
|
|
|
c();
|
|
|
|
});
|
|
|
|
},
|
2017-08-09 11:05:55 +02:00
|
|
|
(c) => {
|
|
|
|
this.sendCommand('channellist -topic', (data) => {
|
2014-10-29 08:02:03 +01:00
|
|
|
state.raw.channels = data;
|
|
|
|
c();
|
|
|
|
});
|
|
|
|
},
|
2017-08-09 11:05:55 +02:00
|
|
|
(c) => {
|
|
|
|
this.finish(state);
|
2014-10-29 08:02:03 +01:00
|
|
|
}
|
|
|
|
]);
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
|
|
|
sendCommand(cmd,c,raw) {
|
|
|
|
this.tcpSend(cmd+'\x0A', (buffer) => {
|
2014-10-29 08:02:03 +01:00
|
|
|
if(buffer.length < 21) return;
|
2017-08-09 11:05:55 +02:00
|
|
|
if(buffer.slice(-21).toString() !== '\n\rerror id=0 msg=ok\n\r') return;
|
|
|
|
const body = buffer.slice(0,-21).toString();
|
2015-01-17 12:57:38 +01:00
|
|
|
|
2017-08-09 11:05:55 +02:00
|
|
|
let out;
|
2014-10-29 08:02:03 +01:00
|
|
|
|
|
|
|
if(raw) {
|
|
|
|
out = body;
|
|
|
|
} else {
|
2017-08-09 11:05:55 +02:00
|
|
|
const segments = body.split('|');
|
2014-10-29 08:02:03 +01:00
|
|
|
out = [];
|
2017-08-09 11:05:55 +02:00
|
|
|
for (const line of segments) {
|
|
|
|
const split = line.split(' ');
|
|
|
|
const unit = {};
|
|
|
|
for (const field of split) {
|
|
|
|
const equals = field.indexOf('=');
|
|
|
|
const key = equals === -1 ? field : field.substr(0,equals);
|
|
|
|
const value = equals === -1 ? '' : field.substr(equals+1)
|
2014-10-29 08:02:03 +01:00
|
|
|
.replace(/\\s/g,' ').replace(/\\\//g,'/');
|
|
|
|
unit[key] = value;
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
2014-10-29 08:02:03 +01:00
|
|
|
out.push(unit);
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
2014-10-29 08:02:03 +01:00
|
|
|
}
|
2015-01-17 12:57:38 +01:00
|
|
|
|
2014-10-29 08:02:03 +01:00
|
|
|
c(out);
|
2015-01-17 12:57:38 +01:00
|
|
|
|
2014-10-29 08:02:03 +01:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Teamspeak3;
|