forked from hex2f/ts3-clientquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (133 loc) · 4.37 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
const net = require('net');
const Promise = require('promise');
function ClientQuery() {
this.state = 0; /* <0 Not connected | 1 Connecting | 2 Connected no auth | 3 Connected and auth | 4 Connection Failed> */
let actions = this.actions = {};
this.actionsLastCall = {};
this.debug = true;
this.onMessage = function(data){};
this.log = function(msg) {
if(!this.debug) {console.log(msg)}
}
this.parse = function(res) {
let rawStrings = res.toString().split("|")
let parsedObjects = [];
rawStrings.forEach(function(str) {
let tempobj = {};
str.split('\n\r')[0].split(" ").forEach(function(vari) {
if(vari.indexOf("=") > -1) {
tempobj[vari.split("=")[0]] = vari.substring(vari.indexOf('=')+1, vari.length).replaceAll("\\\\s", " ");
}
})
if(rawStrings.length > 1) {
parsedObjects.push(tempobj)
} else {
parsedObjects = tempobj;
}
}, this);
return parsedObjects;
}
this.handleMessage = function(msg) {
if(this.actions.hasOwnProperty(msg.toString().split(" ")[0])) {
if(this.actionsLastCall[msg.toString().split(" ")[0]] != msg.toString()) {
this.actionsLastCall[msg.toString().split(" ")[0]] = msg.toString();
this.actions[msg.toString().split(" ")[0]](msg);
}
} else {
this.onMessage(msg);
}
}.bind(this)
this.notifyOn = function(action, args, callback) {
console.log('Register notify '+action)
this.actions[action] = callback;
this.send(`clientnotifyregister event=${action} ${args}`)
}
this.notifyOff = function(action, args) {
console.log('Unregister notify '+action)
this.send(`clientnotifyunregister event=${action} ${args}`)
.then(()=>{
this.actions[action] = undefined;
})
}
this.request = function (data) {
this.log('Request: ' + data)
return new Promise(function (resolve, reject) {
let received = "";
let func = (data) => {
var datastr = data.toString();
received += datastr;
if ( datastr.includes('error id=') ) {
resolve(received);
this.sock.removeListener('data', func);
this.sock.on('data', this.handleMessage);
}
}
this.sock.removeListener('data', this.handleMessage);
this.sock.on('data', func);
this.sock.write(data + '\n', 'utf8', (res) => {
console.log('Sent: ' + data);
})
}.bind(this));
}
this.send = function(data) {
this.log('Send: ' + data)
this.sock.removeListener('data', this.handleMessage)
this.sock.on('data', this.handleMessage)
return new Promise(function (resolve, reject) {
this.sock.write(data + '\n', 'utf8', (res) => {
this.log('Sent: '+data)
resolve(res)
})
}.bind(this));
}
this.reInitActions = function() {
let act = this.actions;
console.log('actions', Object.keys(act))
Object.keys(act).forEach(function(key) {
console.log('addNotifyMemes Lol', key, 'schandlerid=1', typeof(act[key]))
this.notifyOn(key, 'schandlerid=1', act[key])
}.bind(this));
}
this.reconnects = 0;
setInterval(()=>{
this.reconnects--;
}, 1000);
this.connect = function(host, port, apikey) {
return new Promise(function (resolve, reject) {
if(this.reconnects < 5) {
this.reconnects++;
this.state = 1;
let sock = this.sock = new net.Socket();
sock.connect(port, host);
sock.on('connect', () => {
this.state = 2;
sock.setTimeout(0);
this.log('connected');
this.request(`auth apikey=${apikey}`)
.then((resp) => {
if (resp.includes('id=0')) {
resolve(this);
this.state = 3;
setTimeout(() => {
this.reInitActions(actions);
}, 100);
}else{
reject("Error connecting");
}
});
});
sock.on('close', () => {
console.log('con closed, reopen')
sock.destroy();
this.connect(port, host, apikey);
});
sock.on('data', this.handleMessage);
} else {
setTimeout(() => {this.connect(host, port, apikey)}, 3000);
}
}.bind(this));
}
}
module.exports = {
newQuery: function() {return new ClientQuery()}
}