node-gamedig/games/teamspeak2.js

87 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-02-02 13:25:50 +01:00
var async = require('async');
module.exports = require('./protocols/core').extend({
init: function() {
this._super();
this.pretty = 'Teamspeak 2';
this.options.port = 8767;
this.options.master_port = 51234;
},
run: function(state) {
var self = this;
var port = this.options.port;
this.options.port = this.options.master_port;
async.series([
function(c) {
self.sendCommand('sel '+port, function(data) {
if(data != '[TS]') self.fatal('Invalid header');
c();
});
},
function(c) {
self.sendCommand('si', function(data) {
var split = data.split('\r\n');
split.forEach(function(line) {
var equals = line.indexOf('=');
2014-02-02 13:55:28 +01:00
var key = equals == -1 ? line : line.substr(0,equals);
var value = equals == -1 ? '' : line.substr(equals+1);
2014-02-02 13:25:50 +01:00
state.raw[key] = value;
});
c();
});
},
function(c) {
self.sendCommand('pl', function(data) {
var split = data.split('\r\n');
var fields = split.shift().split('\t');
split.forEach(function(line) {
var split2 = line.split('\t');
var player = {};
split2.forEach(function(value,i) {
var key = fields[i];
if(!key) return;
if(key == 'nick') key = 'name';
if(m = value.match(/^"(.*)"$/)) value = m[1];
player[key] = value;
});
state.players.push(player);
});
c();
});
},
function(c) {
self.sendCommand('cl', function(data) {
var split = data.split('\r\n');
var fields = split.shift().split('\t');
state.raw.channels = [];
split.forEach(function(line) {
var split2 = line.split('\t');
var channel = {};
split2.forEach(function(value,i) {
var key = fields[i];
if(!key) return;
if(m = value.match(/^"(.*)"$/)) value = m[1];
channel[key] = value;
});
state.raw.channels.push(channel);
});
c();
});
},
function(c) {
self.finish(state);
}
]);
},
sendCommand: function(cmd,c) {
this.tcpSend(cmd+'\x0A', function(buffer) {
if(buffer.length < 6) return;
if(buffer.slice(-6).toString() != '\r\nOK\r\n') return;
c(buffer.slice(0,-6).toString());
return true;
});
}
});