This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
simple-xmpp.js
484 lines (415 loc) · 17.1 KB
/
simple-xmpp.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/**
The MIT License
Copyright (c) 2011 Arunoda Susiripala
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var xmpp = require('node-xmpp-client');
var Stanza = xmpp.Stanza; // http://node-xmpp.org/doc/ltx.html
var EventEmitter = require('events').EventEmitter;
var qbox = require('qbox');
var STATUS = {
AWAY: "away",
DND: "dnd",
XA: "xa",
ONLINE: "online",
OFFLINE: "offline"
};
var NS_CHATSTATES = "http://jabber.org/protocol/chatstates";
module.exports = new SimpleXMPP();
function SimpleXMPP() {
//setting status here
this.STATUS = STATUS;
var self = this;
var config;
var conn;
var probeBuddies = {};
var joinedRooms = {};
var capabilities = {};
var capBuddies = {};
var iqCallbacks = {};
var $ = qbox.create();
var events = new EventEmitter();
this.on = function() {
events.on.apply(events, Array.prototype.slice.call(arguments));
};
this.removeListener = function() {
events.removeListener.apply(events, Array.prototype.slice.call(arguments));
};
this.events = events;
this.conn = conn;
this.send = function(to, message, group) {
$.ready(function() {
var stanza = new xmpp.Stanza('message', { to: to, type: (group ? 'groupchat' : 'chat') });
stanza.c('body').t(message);
conn.send(stanza);
});
};
this.join = function(to, password) {
$.ready(function() {
var room = to.split('/')[0];
if(!joinedRooms[room]){
joinedRooms[room] = true;
}
var stanza = new Stanza('presence', { to: to }).
c('x', { xmlns: 'http://jabber.org/protocol/muc' });
// XEP-0045 7.2.6 Password-Protected Rooms
if (password !== null && password !== "")
stanza.c('password').t(password);
conn.send(stanza);
});
};
this.invite = function(to, room, reason) {
$.ready(function() {
var stanza = new Stanza('message', { to: room }).
c('x', { xmlns: 'http://jabber.org/protocol/muc#user' }).
c('invite', {to: to});
if (reason)
stanza.c('reason').t(reason);
conn.send(stanza);
});
}
this.subscribe = function(to) {
$.ready(function() {
var stanza = new Stanza('presence', { to: to, type: 'subscribe' });
conn.send(stanza);
});
};
this.unsubscribe = function(to) {
$.ready(function() {
var stanza = new Stanza('presence', { to: to, type: 'unsubscribe' });
conn.send(stanza);
});
};
this.acceptSubscription = function(to) {
// Send a 'subscribed' notification back to accept the incoming
// subscription request
$.ready(function() {
var stanza = new Stanza('presence', { to: to, type: 'subscribed' });
conn.send(stanza);
});
};
this.acceptUnsubscription = function(to) {
$.ready(function() {
var stanza = new Stanza('presence', { to: to, type: 'unsubscribed' });
conn.send(stanza);
});
};
this.getRoster = function() {
$.ready(function() {
var roster = new Stanza('iq', { id: 'roster_0', type: 'get' });
roster.c('query', { xmlns: 'jabber:iq:roster' });
conn.send(roster);
});
};
this.probe = function(buddy, callback) {
probeBuddies[buddy] = true;
$.ready(function() {
var stanza = new Stanza('presence', {type: 'probe', to: buddy});
events.once('probe_' + buddy, callback);
conn.send(stanza);
});
};
function parseVCard(vcard) {
//it appears, that vcard could be null
//in the case, no vcard is set yet, so to avoid crashing, just return null
if (!vcard) {
return null;
}
return vcard.children.reduce(function(jcard, child) {
jcard[child.name.toLowerCase()] = (
(typeof(child.children[0]) === 'object') ?
parseVCard(child) :
child.children.join('')
);
return jcard;
}, {});
}
this.getVCard = function(buddy, callback) {
$.ready(function() {
var id = 'get-vcard-' + buddy.split('@').join('--');
var stanza = new Stanza('iq', { type: 'get', id: id }).
c('vCard', { xmlns: 'vcard-temp' }).
up();
iqCallbacks[id] = function(response) {
if(response.attrs.type === 'error') {
callback(null);
} else {
callback(parseVCard(response.children[0]));
}
};
conn.send(stanza);
});
};
this.getVCardForUser = function(jid, user, callback) {
$.ready(function() {
var id = 'get-vcard-' + user.split('@').join('-');
var stanza = new Stanza('iq', { from: jid, type: 'get', id: id, to: user }).
c('vCard', { xmlns: 'vcard-temp' }).
up();
iqCallbacks[id] = function(response) {
if(response.attrs.type === 'error') {
callback(null);
} else {
var responseObj = {
vcard: parseVCard(response.children[0]),
jid: jid,
user: user
};
callback(responseObj);
}
};
conn.send(stanza);
});
}
// Method: setPresence
//
// Change presence appearance and set status message.
//
// Parameters:
// show - <show/> value to send. Valid values are: ['away', 'chat', 'dnd', 'xa'].
// See http://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1 for details.
// Pass anything that evaluates to 'false' to skip sending the <show/> element.
// status - (optional) status string. This is free text.
// priority - (optional) priority integer. Ranges from -128 to 127.
// See http://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.3 for details.
//
// TODO:
// * add caps support
this.setPresence = function(show, status) {
$.ready(function() {
var stanza = new Stanza('presence');
if(show && show !== STATUS.ONLINE) {
stanza.c('show').t(show);
}
if(typeof(status) !== 'undefined') {
stanza.c('status').t(status);
}
if(typeof(priority) !== 'undefined') {
if(typeof(priority) !== 'number') {
priority = 0;
} else if(priority < -128) {
priority = -128;
} else if(priority > 127) {
priority = 127;
}
stanza.c('priority').t(parseInt(priority));
}
conn.send(stanza);
});
};
// Method: setChatstate
//
// Send current chatstate to the given recipient. Chatstates are defined in
// <XEP-0085 at http://xmpp.org/extensions/xep-0085.html>.
//
// Parameters:
// to - JID to send the chatstate to
// state - State to publish. One of: active, composing, paused, inactive, gone
//
// See XEP-0085 for details on the meaning of those states.
this.setChatstate = function(to, state) {
$.ready(function() {
var stanza = new Stanza('message', { to: to }).
c(state, { xmlns: NS_CHATSTATES }).
up();
conn.send(stanza);
});
};
// TODO: document!
//
// Options:
// * skipPresence - don't send initial empty <presence/> when connecting
//
this.disconnect = function() {
$.ready(function() {
var stanza = new Stanza('presence', { type: 'unavailable' });
stanza.c('status').t('Logged out');
conn.send(stanza);
});
var ref = this.conn.connection;
if (ref.socket.writable) {
if (ref.streamOpened) {
ref.socket.write('</stream:stream>');
delete ref.streamOpened;
} else {
ref.socket.end();
}
}
};
this.connect = function(params) {
config = params;
conn = new xmpp.Client(params);
self.conn = conn;
conn.on('close', function() {
$.stop();
events.emit('close');
});
conn.on('online', function(data){
if(! config.skipPresence) {
conn.send(new Stanza('presence'));
}
events.emit('online', data);
$.start();
// keepalive
if(self.conn.connection.socket) {
self.conn.connection.socket.setTimeout(0);
self.conn.connection.socket.setKeepAlive(true, 10000);
}
});
conn.on('stanza', function(stanza) {
events.emit('stanza', stanza);
//console.log(stanza);
//looking for message stanza
if (stanza.is('message')) {
//getting the chat message
if(stanza.attrs.type === 'chat') {
var body = stanza.getChild('body');
if(body) {
var message = body.getText();
events.emit('chat', stanza.attrs.from, message);
}
var chatstate = stanza.getChildByAttr('xmlns', NS_CHATSTATES);
if(chatstate) {
// Event: chatstate
//
// Emitted when an incoming <message/> with a chatstate notification
// is received.
//
// Event handler parameters:
// jid - the JID this chatstate noticiation originates from
// state - new chatstate we're being notified about.
//
// See <SimpleXMPP#setChatstate> for details on chatstates.
//
events.emit('chatstate', stanza.attrs.from, chatstate.name);
}
} else if (stanza.attrs.type == 'groupchat') {
var body = stanza.getChild('body');
if (body) {
var message = body.getText();
var from = stanza.attrs.from;
var conference = from.split('/')[0];
var id = from.split('/')[1];
var stamp = null;
var delay = null;
if(stanza.getChild('x') && stanza.getChild('x').attrs.stamp)
stamp = stanza.getChild('x').attrs.stamp;
if(stanza.getChild('delay')) {
delay = {
stamp: stanza.getChild('delay').attrs.stamp,
from_jid: stanza.getChild('delay').attrs.from_jid
};
}
events.emit('groupchat', conference, id, message, stamp, delay);
}
}
} else if (stanza.is('presence')) {
var from = stanza.attrs.from;
if (from) {
if (stanza.attrs.type == 'subscribe') {
//handling incoming subscription requests
events.emit('subscribe', from);
} else if (stanza.attrs.type == 'unsubscribe') {
//handling incoming unsubscription requests
events.emit('unsubscribe', from);
} else {
//looking for presence stanza for availability changes
var id = from.split('/')[0];
var resource = from.split('/')[1];
var statusText = stanza.getChildText('status');
var state = (stanza.getChild('show'))? stanza.getChild('show').getText(): STATUS.ONLINE;
state = (state == 'chat')? STATUS.ONLINE : state;
state = (stanza.attrs.type == 'unavailable')? STATUS.OFFLINE : state;
//checking if this is based on probe
if (probeBuddies[id]) {
events.emit('probe_' + id, state, statusText);
delete probeBuddies[id];
} else {
//specifying roster changes
if (joinedRooms[id]){
var groupBuddy = from.split('/')[1];
events.emit('groupbuddy', id, groupBuddy, state, statusText);
} else {
events.emit('buddy', id, state, statusText,resource);
}
}
// Check if capabilities are provided
var caps = stanza.getChild('c', 'http://jabber.org/protocol/caps');
if (caps) {
var node = caps.attrs.node,
ver = caps.attrs.ver;
if (ver) {
var fullNode = node + '#' + ver;
// Check if it's already been cached
if (capabilities[fullNode]) {
events.emit('buddyCapabilities', id, capabilities[fullNode]);
} else {
// Save this buddy so we can send the capability data when it arrives
if (!capBuddies[fullNode]) {
capBuddies[fullNode] = [];
}
capBuddies[fullNode].push(id);
var getCaps = new Stanza('iq', { id: 'disco1', to: from, type: 'get' });
getCaps.c('query', { xmlns: 'http://jabber.org/protocol/disco#info', node: fullNode });
conn.send(getCaps);
}
}
}
}
}
} else if (stanza.is('iq')) {
if (stanza.getChild('ping', 'urn:xmpp:ping')) {
conn.send(new Stanza('iq', { id: stanza.attrs.id, to: stanza.attrs.from, type: 'result' }));
}
// Response to capabilities request?
else if (stanza.attrs.id === 'disco1') {
var query = stanza.getChild('query', 'http://jabber.org/protocol/disco#info');
// Ignore it if there's no <query> element - Not much we can do in this case!
if (!query) {
return;
}
var node = query.attrs.node,
identity = query.getChild('identity'),
features = query.getChildren('feature');
var result = {
clientName: identity && identity.attrs.name,
features: features.map(function (feature) { return feature.attrs['var']; })
};
capabilities[node] = result;
// Send it to all buddies that were waiting
if (capBuddies[node]) {
capBuddies[node].forEach(function (id) {
events.emit('buddyCapabilities', id, result);
});
delete capBuddies[node];
}
}
var cb = iqCallbacks[stanza.attrs.id];
if(cb) {
cb(stanza);
delete iqCallbacks[stanza.attrs.id];
}
}
});
conn.on('error', function(err) {
events.emit('error', err);
});
};
}
SimpleXMPP.prototype.Element = xmpp.Element
// Allow for multiple connections
module.exports.SimpleXMPP = SimpleXMPP;