-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
149 lines (123 loc) · 4.88 KB
/
node_helper.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
/* Magic Mirror
* Node Helper: MMM-teamspeak3
*
* npm : https://www.npmjs.com/package/node-teamspeak
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var TeamSpeakClient = require('node-teamspeak');
//var util = require('util');
module.exports = NodeHelper.create({
cl: null,
connected: null,
consolePrefix: 'MMM-teamspeak3 : ',
start: function function_name () {
"use strict";
var self = this;
self.connected = false;
},
socketNotificationReceived: function(notification, payload){
"use strict";
var self = this;
switch(notification){
case 'TS3-INITIALIZE':
// Retrieving config from MMM-teamspeak3.js
this.config = payload;
console.log(self.consolePrefix + 'Retrieving server settings');
setInterval(function() {
self.getClientList();
}, self.config.refreshInterval * 1000);
break;
case 'TS3-LOGIN':
if(!self.connected){
self.logIn();
}
break;
}
},
logIn: function() {
"use strict";
var self = this;
///////////
// Connecting to host
console.log(self.consolePrefix + 'Connecting to Teamspeak3 server: ' + self.config.host);
self.cl = new TeamSpeakClient(self.config.host, self.config.serverQueryPort);
self.cl.on('error', (err) => {
self.connected = false;
self.sendSocketNotification('TS3-ERROR', err.code);
console.error(self.consolePrefix + err);
});
console.log(self.consolePrefix + 'Login: ' + self.config.login);
console.log(self.consolePrefix + 'Password: *****');
// Login query to host with login/passwd
self.cl.send("login", {client_login_name: self.config.login, client_login_password: self.config.passwd}, function(err, response, rawResponse){
// Checking error
if(typeof err !== 'undefined'){
var msg = 'Connexion failed : ' + err.msg;
console.log(self.consolePrefix + msg);
self.connected = false;
self.sendSocketNotification('TS3-ERROR', msg);
}
else{
switch (true){
case (self.config.serverPort !== ''):
console.log(self.consolePrefix + 'Port: ' + self.config.serverPort);
self.cl.send("use", {port: self.config.serverPort}, function(err, response, rawResponse){
self.connected = true;
});
break;
case (self.config.sid !== ''):
console.log(self.consolePrefix + 'sid: ' + self.config.sid);
self.cl.send("use", {sid: self.config.sid}, function(err, response, rawResponse){
self.connected = true;
});
break;
case (self.config.serverPort === '' && self.config.sid === ''):
console.log(self.consolePrefix + 'Default sid used: 1');
self.cl.send("use", {sid: 1}, function(err, response, rawResponse){
self.connected = true;
});
break;
}
}
});
},
getClientList: function() {
"use strict";
var self = this;
// If successfully connected
if(self.connected){
// clentlist query to Teamspeak3 server
console.log(self.consolePrefix + 'Sending clientlist query to TeamSpeak3 server');
self.cl.send("clientlist", function(err, response, rawResponse){
// Cleaning the client list
var data = self.purgeClientList(response);
// Empty list : nobody's connected
if(data.length === 0){
self.sendSocketNotification('TS3-CLIENT-LIST-EMPTY', data);
}
else{
self.sendSocketNotification('TS3-CLIENT-LIST', data);
}
});
}
else{
self.connected = false;
console.log(self.consolePrefix + 'No connexion to TeamSpeak3 server, trying to reconnect');
self.logIn();
}
},
purgeClientList: function(data){
"use strict";
var list = [];
var client;
for(var i in data){
client = data[i];
// Ignoring serverquery clients
if(client.client_type === 0){
list.push(client.client_nickname);
}
}
return list;
}
});