view 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
line wrap: on
line source

var Error = function(s) {
	this.error = s;
	this.toString = function() {
		return this.error;
	};
};

var Item = function(service, node, id, payload) {
	this.service = service;
	this.node = node;
	this.id = id;
	this.payload = payload;
	this.ns = payload.namespaceURI;
};

var verify = function(elem, ns, name, attributes, children) {
	if (ns && elem.namespaceURI !== ns)
		throw new Error('not the right namespace.');
	if (name && elem.localName !== name)
		throw new Error('not the right name.');
	if (attributes)
		for (var attribute in attributes) {
			var value = attributes[attribute]
			if (elem.getAttributeNS(null, attribute) !== value)
				throw new Error('attribute '+attribute+' invalid.');
		}
	if (typeof children === 'number' && children < 2 && elem.children.length != children)
		throw new Error('not the right number of children');
};

XMPP.prototype = {
	discoInfo: function(aTo, aNode, aCallback) {
		this.send(stanzas.discoInfo(aTo, aNode), function(answer){
			aCallback(answer);
		});
	},

	pubsubItems: function(aTo, aNode, aCallback) {
		this.send(stanzas.pubsubItems(aTo, aNode), function(answer){
			var items = [];
			//try {
				verify(answer, ns.j, 'iq', {type: 'result', from: aTo}, 1);

				var pubsub = answer.firstChild;
				verify(pubsub, ns.ps, 'pubsub', undefined, 1);

				var items = pubsub.firstChild;
				verify(items, ns.ps, 'items', {node: aNode});

				var items = items.children;

				var list = [];
				for (var i in items) {
					var node = items[i];
					var id = node.getAttributeNS(null, 'id');
					if (!id)
						throw new Error('WARNING: invalid item! (no id)');

					if (node.children.length != 1)
						throw new Error('WARNING: invalid item! (more than one payload)');

					var payload = node.firstChild;

					list[id] = new Item(aTo, aNode, id, payload);
				}
			/*} catch (e) {
				aCallback(e);
			}*/
			if(aCallback)
				aCallback({service: aTo, node: aNode, items: list});
		});
	}
};