-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.js
322 lines (322 loc) · 10.9 KB
/
Client.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const events_1 = require("events");
const JID = tslib_1.__importStar(require("./JID"));
const JXT = tslib_1.__importStar(require("./jxt"));
const SASL = tslib_1.__importStar(require("./lib/sasl"));
const plugins_1 = require("./plugins");
const protocol_1 = tslib_1.__importDefault(require("./protocol"));
const StreamManagement_1 = tslib_1.__importDefault(require("./StreamManagement"));
const bosh_1 = tslib_1.__importDefault(require("./transports/bosh"));
const websocket_1 = tslib_1.__importDefault(require("./transports/websocket"));
const Utils_1 = require("./Utils");
class Client extends events_1.EventEmitter {
constructor(opts = {}) {
super();
this.setMaxListeners(100);
// Some EventEmitter shims don't include off()
this.off = this.removeListener;
this._initConfig(opts);
this.jid = '';
this.sasl = new SASL.Factory();
this.sasl.register('EXTERNAL', SASL.EXTERNAL, 1000);
this.sasl.register('SCRAM-SHA-256-PLUS', SASL.SCRAM, 350);
this.sasl.register('SCRAM-SHA-256', SASL.SCRAM, 300);
this.sasl.register('SCRAM-SHA-1-PLUS', SASL.SCRAM, 250);
this.sasl.register('SCRAM-SHA-1', SASL.SCRAM, 200);
this.sasl.register('DIGEST-MD5', SASL.DIGEST, 100);
this.sasl.register('X-OAUTH2', SASL.OAUTH, 100);
this.sasl.register('PLAIN', SASL.PLAIN, 1);
this.sasl.register('ANONYMOUS', SASL.ANONYMOUS, 0);
this.stanzas = new JXT.Registry();
this.stanzas.define(protocol_1.default);
this.use(plugins_1.core);
this.sm = new StreamManagement_1.default(this);
this.transports = {
bosh: bosh_1.default,
websocket: websocket_1.default
};
this.on('stream:data', (json, kind) => {
this.emit(kind, json);
if (kind === 'message' || kind === 'presence' || kind === 'iq') {
this.sm.handle();
this.emit('stanza', json);
}
else if (kind === 'sm') {
if (json.type === 'ack') {
this.emit('stream:management:ack', json);
this.sm.process(json);
}
if (json.type === 'request') {
this.sm.ack();
}
return;
}
if (json.id) {
this.emit((kind + ':id:' + json.id), json);
}
});
this.on('disconnected', () => {
if (this.transport) {
delete this.transport;
}
});
this.on('auth:success', () => {
if (this.transport) {
this.transport.authenticated = true;
}
});
this.on('iq', (iq) => {
const iqType = iq.type;
const payloadType = iq.payloadType;
const iqEvent = 'iq:' + iqType + ':' + payloadType;
if (iqType === 'get' || iqType === 'set') {
if (payloadType === 'invalid-payload-count') {
return this.sendIQError(iq, {
error: {
condition: 'bad-request',
type: 'modify'
}
});
}
if (payloadType === 'unknown-payload' || this.listenerCount(iqEvent) === 0) {
return this.sendIQError(iq, {
error: {
condition: 'service-unavailable',
type: 'cancel'
}
});
}
this.emit(iqEvent, iq);
}
});
this.on('message', msg => {
const isChat = (msg.alternateLanguageBodies && msg.alternateLanguageBodies.length) ||
(msg.links && msg.links.length);
const isMarker = msg.marker && msg.marker.type !== 'markable';
if (isChat && !isMarker) {
if (msg.type === 'chat' || msg.type === 'normal') {
this.emit('chat', msg);
}
else if (msg.type === 'groupchat') {
this.emit('groupchat', msg);
}
}
if (msg.type === 'error') {
this.emit('message:error', msg);
}
});
this.on('presence', (pres) => {
let presType = pres.type || 'available';
if (presType === 'error') {
presType = 'presence:error';
}
this.emit(presType, pres);
});
}
get stream() {
return this.transport ? this.transport.stream : undefined;
}
emit(name, ...args) {
// Continue supporting the most common and useful wildcard events
const res = super.emit(name, ...args);
if (name === 'raw') {
super.emit(`raw:${args[0]}`, args[1]);
super.emit('raw:*', `raw:${args[0]}`, args[1]);
super.emit('*', `raw:${args[0]}`, args[1]);
}
else {
super.emit('*', name, ...args);
}
return res;
}
use(pluginInit) {
if (typeof pluginInit !== 'function') {
return;
}
pluginInit(this, this.stanzas, this.config);
}
nextId() {
return Utils_1.uuid();
}
async getCredentials() {
return this._getConfiguredCredentials();
}
async connect() {
const transportPref = ['websocket', 'bosh'];
let endpoints;
for (const name of transportPref) {
let conf = this.config.transports[name];
if (!conf) {
continue;
}
if (typeof conf === 'string') {
conf = { url: conf };
}
else if (conf === true) {
if (!endpoints) {
try {
endpoints = await this.discoverBindings(this.config.server);
}
catch (err) {
console.error(err);
continue;
}
}
endpoints[name] = (endpoints[name] || []).filter(url => url.startsWith('wss:') || url.startsWith('https:'));
if (!endpoints[name] || !endpoints[name].length) {
continue;
}
conf = { url: endpoints[name][0] };
}
this.transport = new this.transports[name](this, this.sm, this.stanzas);
this.transport.connect({
acceptLanguages: this.config.acceptLanguages || ['en'],
jid: this.config.jid,
lang: this.config.lang || 'en',
server: this.config.server,
url: conf.url,
...conf
});
return;
}
console.error('No endpoints found for the requested transports.');
return this.disconnect();
}
disconnect() {
if (this.sessionStarted && !this.sm.started) {
// Only emit session:end if we had a session, and we aren't using
// stream management to keep the session alive.
this.emit('session:end');
}
this.sessionStarted = false;
if (this.transport) {
this.transport.disconnect();
}
else {
this.emit('disconnected');
}
}
send(name, data) {
this.sm.track(name, data);
if (this.transport) {
this.transport.send(name, data);
}
}
sendMessage(data) {
const id = data.id || this.nextId();
const msg = {
id,
originId: id,
...data
};
this.emit('message:sent', msg, false);
this.send('message', msg);
return msg.id;
}
sendPresence(data = {}) {
const pres = {
id: this.nextId(),
...data
};
this.send('presence', pres);
return pres.id;
}
sendIQ(data) {
const iq = {
id: this.nextId(),
...data
};
const allowed = JID.allowedResponders(this.jid, data.to);
const respEvent = 'iq:id:' + iq.id;
const request = new Promise((resolve, reject) => {
const handler = (res) => {
// Only process result from the correct responder
if (!allowed.has(res.from)) {
return;
}
// Only process result or error responses, if the responder
// happened to send us a request using the same ID value at
// the same time.
if (res.type !== 'result' && res.type !== 'error') {
return;
}
this.off(respEvent, handler);
if (res.type === 'result') {
resolve(res);
}
else {
reject(res);
}
};
this.on(respEvent, handler);
});
this.send('iq', iq);
return Utils_1.timeoutPromise(request, (this.config.timeout || 15) * 1000, () => ({
error: {
condition: 'timeout'
},
id: iq.id,
type: 'error'
}));
}
sendIQResult(original, reply) {
this.send('iq', {
...reply,
id: original.id,
to: original.from,
type: 'result'
});
}
sendIQError(original, error) {
this.send('iq', {
...error,
id: original.id,
to: original.from,
type: 'error'
});
}
sendStreamError(error) {
this.emit('stream:error', error);
this.send('error', error);
this.disconnect();
}
_getConfiguredCredentials() {
const creds = this.config.credentials || {};
const requestedJID = JID.parse(this.config.jid || '');
const username = creds.username || requestedJID.local;
const server = creds.host || requestedJID.domain;
return {
host: server,
password: this.config.password,
realm: server,
serviceName: server,
serviceType: 'xmpp',
username,
...creds
};
}
_initConfig(opts = {}) {
const currConfig = this.config || {};
this.config = {
jid: '',
transports: {
bosh: true,
websocket: true
},
useStreamManagement: true,
...currConfig,
...opts
};
if (!this.config.server) {
this.config.server = JID.getDomain(this.config.jid);
}
if (this.config.password) {
this.config.credentials = this.config.credentials || {};
this.config.credentials.password = this.config.password;
delete this.config.password;
}
}
}
exports.default = Client;