-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathstrophe.disco.js
More file actions
108 lines (86 loc) · 3.55 KB
/
strophe.disco.js
File metadata and controls
108 lines (86 loc) · 3.55 KB
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
// XMPP plugins for Strophe v1.0.5
// (c) 2012-2013 Yiorgis Gozadinos.
// strophe.plugins is distributed under the MIT license.
// http://github.com/ggozad/strophe.plugins
// A Disco plugin partially implementing
// [XEP-0030 Service Discovery](http://xmpp.org/extensions/xep-0030.html)
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', 'underscore', 'backbone', 'strophe'], function ($, _, Backbone, wrapper) {
// Also create a global in case some scripts
// that are loaded still are looking for
// a global even when an AMD loader is in use.
return factory($, _, Backbone, wrapper.Strophe, wrapper.$iq);
});
} else {
// Browser globals
factory(root.$, root._, root.Backbone, root.Strophe, root.$iq);
}
}(this,function ($, _, Backbone, Strophe, $iq) {
// Add the **PubSub** plugin to Strophe
Strophe.addConnectionPlugin('Disco', {
_connection: null,
_identities: [],
_features: [],
// **init** sets up the connection
init: function (connection) {
this._connection = connection;
},
// Add Disco handlers
statusChanged: function (status, condition) {
var self = this;
if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) {
self._connection.addHandler(self._onDiscoInfo.bind(self), Strophe.NS.DISCO_INFO, 'iq', 'get', null, null);
}
},
// **addIdentity** adds an identity
addIdentity: function (identity) {
this._identities.push(identity);
},
// **addFeature** adds a feature
addFeature: function (feature) {
this._features.push(feature);
},
// **info** queries an entity for its support and
// returns **identities** and **features
info: function (to, node) {
var d = $.Deferred(),
query = {xmlns: Strophe.NS.DISCO_INFO},
identities = [], features = [];
if (node) {
query.node = node;
}
var iq = $iq({to: to, type: 'get'})
.c('query', query);
this._connection.sendIQ(iq, function (response) {
_.each($('identity', response), function (node) {
identities.push(_.reduce(node.attributes, function (identity, attr) {
identity[attr.nodeName] = attr.nodeValue; return identity;
}, {}));
});
_.each($('feature', response), function (node) {
features.push(node.getAttribute('var'));
});
d.resolve({identities: identities, features: features});
}, d.reject);
return d.promise();
},
// Handle Disco info requests.
_onDiscoInfo: function (iq) {
var response = $iq({
to: iq.getAttribute('from'),
id: iq.getAttribute('id'),
type: 'result'})
.c('query', {xmlns: Strophe.NS.DISCO_INFO});
_.each(this._identities, function (identity) {
response.c('identity', identity).up();
});
_.each(this._features, function (feature) {
response.c('feature', {'var': feature}).up();
});
this._connection.send(response.tree());
return true;
}
});
}));