comparison functions.js @ 13:161d4ea1c3f8

Migration of the client-side to XMPP.js instead of Strophe.js. Drop BOSH support and add WebSockets support. The server-side is untested, may be broken.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 03 Nov 2011 14:23:10 -0700
parents
children
comparison
equal deleted inserted replaced
12:d67380657687 13:161d4ea1c3f8
1 var Error = function(s) {
2 this.error = s;
3 this.toString = function() {
4 return this.error;
5 };
6 };
7
8 var Item = function(service, node, id, payload) {
9 this.service = service;
10 this.node = node;
11 this.id = id;
12 this.payload = payload;
13 this.ns = payload.namespaceURI;
14 };
15
16 var verify = function(elem, ns, name, attributes, children) {
17 if (ns && elem.namespaceURI !== ns)
18 throw new Error('not the right namespace.');
19 if (name && elem.localName !== name)
20 throw new Error('not the right name.');
21 if (attributes)
22 for (var attribute in attributes) {
23 var value = attributes[attribute]
24 if (elem.getAttributeNS(null, attribute) !== value)
25 throw new Error('attribute '+attribute+' invalid.');
26 }
27 if (typeof children === 'number' && children < 2 && elem.children.length != children)
28 throw new Error('not the right number of children');
29 };
30
31 XMPP.prototype = {
32 discoInfo: function(aTo, aNode, aCallback) {
33 this.send(stanzas.discoInfo(aTo, aNode), function(answer){
34 aCallback(answer);
35 });
36 },
37
38 pubsubItems: function(aTo, aNode, aCallback) {
39 this.send(stanzas.pubsubItems(aTo, aNode), function(answer){
40 var items = [];
41 //try {
42 verify(answer, ns.j, 'iq', {type: 'result', from: aTo}, 1);
43
44 var pubsub = answer.firstChild;
45 verify(pubsub, ns.ps, 'pubsub', undefined, 1);
46
47 var items = pubsub.firstChild;
48 verify(items, ns.ps, 'items', {node: aNode});
49
50 var items = items.children;
51
52 var list = [];
53 for (var i in items) {
54 var node = items[i];
55 var id = node.getAttributeNS(null, 'id');
56 if (!id)
57 throw new Error('WARNING: invalid item! (no id)');
58
59 if (node.children.length != 1)
60 throw new Error('WARNING: invalid item! (more than one payload)');
61
62 var payload = node.firstChild;
63
64 list[id] = new Item(aTo, aNode, id, payload);
65 }
66 /*} catch (e) {
67 aCallback(e);
68 }*/
69 if(aCallback)
70 aCallback({service: aTo, node: aNode, items: list});
71 });
72 }
73 };