forked from timklge/node-teamspeak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
214 lines (190 loc) · 5.95 KB
/
index.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
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return - Tim Kluge
* ----------------------------------------------------------------------------
*/
var net = require("net"),
LineInputStream = require("line-input-stream"),
events = require("events"),
util = require("util");
var TS3 = {
Clients: {},
};
function TeamSpeakClient(host, port, name, call){
events.EventEmitter.call(this);
if (TS3.Clients[name]) {
TS3.Clients[name].destroy();
TS3.Clients[name] = null;
}
var self = this,
socket = net.connect(port || 10011, host || 'localhost'),
reader = null,
status = -2,
queue = [],
executing = null;
TS3.Clients[name] = socket;
function tsescape(s){
var r = String(s);
r = r.replace(/\\/g, "\\\\"); // Backslash
r = r.replace(/\//g, "\\/"); // Slash
r = r.replace(/\|/g, "\\p"); // Pipe
r = r.replace(/\n/g, "\\n"); // Newline
r = r.replace(/\r/g, "\\r"); // Carriage Return
r = r.replace(/\t/g, "\\t"); // Tab
r = r.replace(/\v/g, "\\v"); // Vertical Tab
r = r.replace(/\f/g, "\\f"); // Formfeed
r = r.replace(/ /g, "\\s"); // Whitespace
return r;
}
function tsunescape(s){
var r = String(s);
r = r.replace(/\\s/g, " "); // Whitespace
r = r.replace(/\\p/g, "|"); // Pipe
r = r.replace(/\\n/g, "\n"); // Newline
r = r.replace(/\\f/g, "\f"); // Formfeed
r = r.replace(/\\r/g, "\r"); // Carriage Return
r = r.replace(/\\t/g, "\t"); // Tab
r = r.replace(/\\v/g, "\v"); // Vertical Tab
r = r.replace(/\\\//g, "\/"); // Slash
r = r.replace(/\\\\/g, "\\"); // Backslash
return r;
}
function checkQueue(){
if(!executing && queue.length >= 1){
executing = queue.shift();
TS3.Clients[name].write(executing.text + "\n");
}
}
function parseResponse(s){
var response = [];
var records = s.split("|");
response = records.map(function(k){
var args = k.split(" ");
var thisrec = {};
args.forEach(function(v){
if(v.indexOf("=") > -1){
var key = tsunescape(v.substr(0, v.indexOf("=")));
var value = tsunescape(v.substr(v.indexOf("=")+1));
if(parseInt(value, 10) == value) value = parseInt(value, 10);
thisrec[key] = value;
}else thisrec[v] = "";
});
return thisrec;
});
if(response.length === 0){
response = null;
} else if(response.length === 1){
response = response.shift();
}
return response;
}
// Return pending commands that are going to be sent to the server.
// Note that they have been parsed - Access getPending()[0].text to get
// the full text representation of the command.
TeamSpeakClient.prototype.getPending = function(){
return queue.slice(0);
};
// Clear the queue of pending commands so that any command that is currently queued won't be executed.
// The old queue is returned.
TeamSpeakClient.prototype.clearPending = function(){
var q = queue;
queue = [];
return q;
};
// Check if there is an active connection on the server, before loggin
TeamSpeakClient.prototype.checkClient = function(name) {
return TS3.Clients[name];
}
// Send a command to the server
TeamSpeakClient.prototype.send = function(){
var args = Array.prototype.slice.call(arguments);
var options = [], params = {};
var callback = undefined;
var cmd = args.shift();
args.forEach(function(v){
if(util.isArray(v)){
options = v;
} else if(typeof v === "function"){
callback = v;
} else {
params = v;
}
});
var tosend = tsescape(cmd);
options.forEach(function(v){
tosend += " -" + tsescape(v);
});
for(var k in params){
var v = params[k];
if(util.isArray(v)){ // Multiple values for the same key - concatenate all
var doptions = v.map(function(val){
return tsescape(k) + "=" + tsescape(val);
});
tosend += " " + doptions.join("|");
} else {
tosend += " " + tsescape(k.toString()) + "=" + tsescape(v.toString());
}
}
queue.push({cmd: cmd, options: options, parameters: params, text: tosend, cb: callback});
if(status === 0) checkQueue();
};
// SetTimeout function
TeamSpeakClient.prototype.setTimeout = function(time) {
socket.setTimeout(time || 60000, function() {
socket.destroy();
self.emit("timeout");
TS3.Clients[name] = null;
});
};
socket.on("error", function(err){
self.emit("error", err);
});
socket.on("lookup", function(err){
self.emit("lookup", err);
});
socket.on("close", function(){
self.emit("close", queue);
});
socket.on('end', function() {
self.emit("end");
});
socket.on("connect", function(){
reader = LineInputStream(socket);
reader.on("line", function(line){
var s = line.trim();
// Ignore two first lines sent by server ("TS3" and information message)
if(status < 0){
status++;
if(status === 0) checkQueue();
return;
}
// Server answers with:
// [- One line containing the answer ]
// - "error id=XX msg=YY". ID is zero if command was executed successfully.
var response = undefined;
if(s.indexOf("error") === 0){
response = parseResponse(s.substr("error ".length).trim());
executing.error = response;
if(executing.error.id === 0) delete executing.error;
if(executing.cb) executing.cb.call(executing, executing.error, executing.response,
executing.rawResponse);
executing = null;
checkQueue();
} else if(s.indexOf("notify") === 0){
s = s.substr("notify".length);
response = parseResponse(s);
self.emit(s.substr(0, s.indexOf(" ")), response);
} else if(executing) {
response = parseResponse(s);
executing.rawResponse = s;
executing.response = response;
}
});
self.emit("connect");
});
}
util.inherits(TeamSpeakClient, events.EventEmitter);
module.exports = TeamSpeakClient;