-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroster.js
308 lines (293 loc) · 9.79 KB
/
roster.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
/** Package: roster
* Parsing and eventing for the XMPP roster data.
*
* Returns:
* <RosterContacts>
*
* Event: roster.change
* One or more roster items have been updated.
*
* Payload:
* (RosterContacts) changedContacts - Contains <RosterContacts> that have been modified.
*
* Event: roster.ready
* The roster has been loaded.
*
* Event: roster.unavailable
* A contact goes offline or becomes otherwise unavailable. All its resources are disconnected.
*
* Payload:
* (Element) presence - The <presence/> stanza.
* (String) jid - The bare address of the contact.
* (String) resource - The resource of the contact.
* (Object) status - Contains the type and the resource's last message.
*
* Event: roster.available
* A contact's resource comes online or changes their availability.
*
* Payload:
* (Element) presence - The <presence/> stanza.
* (String) jid - The bare address of the contact.
* (String) resource - The resource of the contact.
* (RosterResource) rosterResource - The parsed <RosterResource> object.
*
* Event: roster.subscribe
*
* Payload:
* (Element) presence - The <presence/> stanza.
* (String) jid - The bare address of the contact.
* (String) resource - The resource of the contact.
*
* Event: roster.subscribed
* See: <roster.subscribe>
*
* Event: roster.unsubscribe
* See: <roster.subscribe>
*
* Event: roster.unsubscribed
* See: <roster.subscribe>
*/
define(
["core/events", "core/xmpp", "core/xpath", "modules/rosterCache", "modules/jidParser"],
function (events, xmpp, xpath, rosterCache, jidParser) {
var $xml = function (snippet) {
return (new DOMParser).parseFromString(snippet, "text/xml").documentElement;
};
var xmlns = {
stream: "http://etherx.jabber.org/streams",
client: "jabber:client",
roster: "jabber:iq:roster",
rosterver: "urn:xmpp:features:rosterver"
};
var versioningSupported = false;
var roster = {
version: "",
/** Interface: RosterContacts
* Map containing all roster items as <RosterItems>.
*
* Example:
* (code)
* "user@server": {
* subscription: "both",
* ask: "subscribe",
* name: "Some User",
* groups: [
* "somegroup", // ... more group names
* ],
* resources: {
* "web_client_s1d51fsf231": {
* show: "away",
* status: "I'm not here right now",
* priority: 5
* }, // ... more resources
* }
* }, // ... more contacts
* (end)
*/
/** Interface: RosterItem
* Contains the information of a single contact.
*
* Properties:
* (String) jid - The bare address of the contact.
* (String) subscription - Subscription state of the contact. Values: *both*, *from*, *none*, *remove*, *to*
* (String) ask - Whether the contact is pending approval. Values: *subscribe* or an empty string
* (String) name - Name of the contact in the roster.
* (Array) groups - Names of groups which the contact is part of.
* (Object) resources - All the available <RosterResources> of the contact. Accessed by resource.
*/
/** Interface: RosterResource
* Stores information about the available resource.
*
* Properties:
* (String) status - A simple text message of the resource.
* (String) show - The type of availability of the resource. Values: *ffc* (Free For Chat), *dnd* (Do Not Disturb or Busy), *away*, *xaway* (Extended Away or Idle) or empty string (Available).
* (Number) priority - An integer between -127 and 128 incidicating the importance of the contact for message routing.
*/
contacts: {}
};
var onConnected = function () {
versioningSupported = !!(xmpp.features && xpath(xmpp.features, "/stream:features/rosterver:ver", Object, xmlns));
var iq = $xml("<iq type='get'><query xmlns='jabber:iq:roster'/></iq>");
if (versioningSupported) {
iq.firstChild.setAttribute("ver", rosterCache.version(xmpp.connection.jid.bare));
}
xmpp.sendIQ(iq, function (iq) {
var oldContacts = roster.contacts;
var newRoster;
if (versioningSupported && !xpath(iq, "/client:iq/roster:query", Object, xmlns)) {
newRoster = rosterCache.load(xmpp.connection.jid.bare);
} else {
newRoster = parseRosterIQ(iq).roster;
rosterCache.save(xmpp.connection.jid.bare, newRoster);
}
var changedContacts = compareContacts(oldContacts, newRoster.contacts);
mergeRoster(roster, newRoster);
if (Object.keys(changedContacts).length > 0) {
events.publish("roster.change", changedContacts);
}
xmpp.subscribe(rosterIQHandler);
xmpp.subscribe(presenceHandler);
events.publish("roster.ready");
xmpp.send($xml("<presence/>"));
});
events.subscribe("xmpp.disconnected", onDisconnected);
};
var onDisconnected = function () {
Object.keys(roster.contacts).forEach(function (jid) {
var contact = roster.contacts[jid];
Object.keys(contact.resources).forEach(function (resource) {
delete contact.resources[resource];
events.publish("roster.unavailable", null, jid, resource, "");
});
});
xmpp.unsubscribe(rosterIQHandler);
xmpp.unsubscribe(presenceHandler);
events.subscribe("xmpp.connected", onConnected);
};
var mergeRoster = function (oldRoster, newRoster) {
oldRoster.version = newRoster.version;
Object.keys(oldRoster.contacts).forEach(function (jid) {
delete oldRoster.contacts[jid];
});
Object.keys(newRoster.contacts).forEach(function (jid) {
oldRoster.contacts[jid] = newRoster.contacts[jid];
});
};
var isIdenticalRosterItem = function (item1, item2) {
return (
["name", "ask", "subscription"].reduce(function (identical, property) {
return identical && (item1[property] === item2[property]);
}, item1.groups.reduce(function (identical, group) {
return identical && (item2.groups.indexOf(group) !== -1)
}, (item1.groups.length === item2.groups.length)))
);
};
var compareContacts = function (oldContacts, newContacts) {
var changedContacts = {};
// New or changed contact
Object.keys(newContacts).forEach(function (jid) {
if (!Object.hasOwnProperty.call(oldContacts, jid) ||
!isIdenticalRosterItem(newContacts[jid], oldContacts[jid])
) {
changedContacts[jid] = newContacts[jid];
}
});
// Removed contact
Object.keys(oldContacts).forEach(function (jid) {
if (!Object.hasOwnProperty.call(newContacts, jid)) {
changedContacts[jid] = {jid: jid, subscription: "remove"};
}
});
return changedContacts;
};
var presenceHandler = {
xpath: "/client:presence[@from]", xmlns: xmlns,
callback: function (presence) {
var jid = jidParser(presence.getAttribute("from"));
if (jid && Object.hasOwnProperty.call(roster.contacts, jid.bare)) {
var contact = roster.contacts[jid.bare],
resource = Object.hasOwnProperty.call(contact.resources, jid.resource)
? contact.resources[jid.resource]
: (contact.resources[jid.resource] = {}),
type = presence.hasAttribute("type")
? presence.getAttribute("type")
: "";
switch (type) {
case "": // Available is indicated by lack of "type" attribute
["show", "status", "priority"].forEach(function (tagName) {
var tag = xpath(presence, "/client:presence/client:" + tagName, Object, xmlns);
resource[tagName] = tag ? tag.textContent : "";
});
resource.priority = parseInt(resource.priority, 10);
if (isNaN(resource.priority)
|| resource.priority < -128
|| resource.priority > 127
) {
resource.priority = 0;
}
events.publish(
"roster.available",
presence,
jid.bare,
jid.resource,
resource
);
break;
case "unavailable":
delete contact.resources[jid.resource];
var status = xpath(presence, "/client:presence/status", Object, xmlns);
events.publish(
"roster.unavailable",
presence,
jid.bare,
jid.resource,
status ? status.textContent : ""
);
break;
case "subscribe":
case "subscribed":
case "unsubscribe":
case "unsubscribed":
events.publish("roster." + type, presence, jid.bare, jid.resource);
break;
}
}
return true;
}
};
var rosterIQHandler = {
xpath: "/client:iq[@type='set']/roster:query", xmlns: xmlns,
callback: function (iq, query) {
var changedContacts = parseRosterIQ(iq, roster).changedContacts;
if (Object.keys(changedContacts).length > 0) {
rosterCache.save(xmpp.connection.jid.bare, roster);
events.publish("roster.change", changedContacts);
}
return true;
}
};
var parseRosterIQ = function (iq, roster) {
var changedContacts = {};
roster = roster || {
version: "",
contacts: {}
};
xpath(iq, "/client:iq/roster:query/roster:item", Array, xmlns).forEach(function (item) {
var jid = item.getAttribute("jid")
if (jid) {
var contact = {jid: jid};
["name", "ask", "subscription"].forEach(function (property) {
contact[property] = item.getAttribute(property) || "";
});
contact.groups = [];
contact.resources = Object.hasOwnProperty.call(roster.contacts, jid)
? roster.contacts[jid].resources
: {};
xpath(item, "roster:group", Array, xmlns).forEach(function (group) {
if (contact.groups.indexOf(group.textContent) === -1) {
contact.groups.push(group.textContent);
}
});
if (!Object.hasOwnProperty.call(roster.contacts, jid) ||
!isIdenticalRosterItem(contact, roster.contacts[jid])
) {
changedContacts[jid] = contact;
}
if (contact.subscription === "remove") {
delete roster.contacts[jid];
} else {
roster.contacts[jid] = contact;
}
}
});
var query = xpath(iq, "/client:iq/roster:query", Object, xmlns);
roster.version = query.hasAttribute("ver") ? query.getAttribute("ver") : "";
return {roster: roster, changedContacts: changedContacts};
};
if (xmpp.connection.status === "connected") {
onConnected();
} else {
events.subscribe("xmpp.connected", onConnected);
}
return roster.contacts;
});