diff psgxs.js @ 8:efe8dbd34780

Rename cx method to cnode and define it if xmpp.js didn’t.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 26 Aug 2010 23:50:04 +0200
parents 781ac4f1e304
children a6429f48e403
line wrap: on
line diff
--- a/psgxs.js
+++ b/psgxs.js
@@ -46,6 +46,16 @@ function _(m, c) {
 	sys.puts('');
 };
 
+if (typeof xmpp.StanzaBuilder.cnode != 'function' || typeof xmpp.StanzaBuilder.prototype.cnode != 'function') {
+	xmpp.StanzaBuilder.prototype.cnode = function (stanza)
+	{
+		var parent = this;
+		parent.tags.push(stanza);
+		parent.children.push(stanza);
+		return this;
+	};
+}
+
 function onIq(stanza) {
 	var type = stanza.getAttribute('type');
 	var from = stanza.getAttribute('to');
@@ -66,7 +76,7 @@ function onIq(stanza) {
 				.s('name').t('PSĜS')
 				.s('version').t(config.version)
 				.s('os').t(config.os);
-			response.cx(query);
+			response.cnode(query);
 
 		// SECTION 5.1
 		} else if (stanza.getChild('query', 'http://jabber.org/protocol/disco#info')) {
@@ -94,9 +104,9 @@ function onIq(stanza) {
 				if (config.enabled('meta-data')) {
 					var x = forms.build('result', 'node_metadata', storage.getMetadata(nodeID), true);
 					if (x)
-						q.cx(x);
+						q.cnode(x);
 				}
-				response.cx(q);
+				response.cnode(q);
 
 			// SECTION 5.1
 			} else {
@@ -110,7 +120,7 @@ function onIq(stanza) {
 					for (var i in config.activated)
 						if (typeof i == 'string')
 							q.s('feature', {'var': 'http://jabber.org/protocol/pubsub#' + config.activated[i]});
-				response.cx(q);
+				response.cnode(q);
 			}
 
 		// SECTION 5.2
@@ -152,7 +162,7 @@ function onIq(stanza) {
 
 				q.s('item', attr);
 			}
-			response.cx(q);
+			response.cnode(q);
 		} else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub')) {
 			var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub');
 
@@ -177,8 +187,8 @@ function onIq(stanza) {
 					s.s('subscription', {node: i, jid: to, subscription: subs[i].type, subid: subs[i].subid});
 
 				var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'});
-				p.cx(s);
-				response.cx(p);
+				p.cnode(s);
+				response.cnode(p);
 
 			// SECTION 5.7
 			} else if (pubsub.getChild('affiliations')) {
@@ -201,8 +211,8 @@ function onIq(stanza) {
 					s.s('affiliation', {node: i, affiliation: affils[i]});
 
 				var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'});
-				p.cx(s);
-				response.cx(p);
+				p.cnode(s);
+				response.cnode(p);
 
 			// SECTION 6.3.2
 			} else if (pubsub.getChild('options')) {
@@ -230,9 +240,9 @@ function onIq(stanza) {
 				var s = xmpp.stanza('options', {node: nodeID, jid: jid});
 				var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'});
 				var form = forms.build('form', 'subscribe_options', subs.options, true);
-				s.cx(form);
-				p.cx(s);
-				response.cx(p);
+				s.cnode(form);
+				p.cnode(s);
+				response.cnode(p);
 
 			// SECTION 6.4
 			} else if (pubsub.getChild('default')) {
@@ -253,9 +263,9 @@ function onIq(stanza) {
 					s = xmpp.stanza('default');
 
 				var form = forms.build('form', 'subscribe_options', 'default', false);
-				s.cx(form);
-				p.cx(s);
-				response.cx(p);
+				s.cnode(form);
+				p.cnode(s);
+				response.cnode(p);
 
 			// SECTION 6.5
 			} else if (pubsub.getChild('items')) {
@@ -297,8 +307,8 @@ function onIq(stanza) {
 							return makeError(response, j);
 
 						var k = xmpp.stanza('item', {id: item[i]})
-						k.cx(j);
-						s.cx(k);
+						k.cnode(j);
+						s.cnode(k);
 					}
 				} else {
 					var s = xmpp.stanza('items', {node: nodeID});
@@ -314,12 +324,12 @@ function onIq(stanza) {
 					var k = 0;
 					for (var i in j) {
 						var contentItem = xmpp.stanza('item', {id: i}).t(j[i].content);
-						s.cx(contentItem);
+						s.cnode(contentItem);
 					}
 				}
 				var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'});
-				p.cx(s);
-				response.cx(p);
+				p.cnode(s);
+				response.cnode(p);
 			} else
 				return makeError(response, errors.feature_not_implemented.n);
 		} else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner')) {
@@ -343,9 +353,9 @@ function onIq(stanza) {
 				var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'});
 				var s = xmpp.stanza('configure', {node: nodeID});
 				var form = forms.build('form', 'node_config', 'default', true);
-				s.cx(form);
-				p.cx(s);
-				response.cx(p);
+				s.cnode(form);
+				p.cnode(s);
+				response.cnode(p);
 
 			// SECTION 8.3
 			} else if (pubsub.getChild('default')) {
@@ -355,9 +365,9 @@ function onIq(stanza) {
 				var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'});
 				var s = xmpp.stanza('default');
 				var form = forms.build('node_config', service_configuration.node_config, null, true);
-				s.cx(form);
-				p.cx(s);
-				response.cx(p);
+				s.cnode(form);
+				p.cnode(s);
+				response.cnode(p);
 
 			// SECTION 8.8
 			} else if (pubsub.getChild('subscriptions')) {
@@ -383,8 +393,8 @@ function onIq(stanza) {
 				for (var jid in subs)
 					s.s('subscription', {jid: jid, subscription: subs[jid].type, subid: subs[jid].subid})
 
-				p.cx(s);
-				response.cx(p);
+				p.cnode(s);
+				response.cnode(p);
 
 			// SECTION 8.9
 			} else if (pubsub.getChild('affiliations')) {
@@ -410,8 +420,8 @@ function onIq(stanza) {
 				for (var jid in affils)
 					s.s('affiliation', {jid: jid, affiliation: affils[jid]})
 
-				p.cx(s);
-				response.cx(p);
+				p.cnode(s);
+				response.cnode(p);
 			} else
 				return makeError(response, errors.feature_not_implemented.n);
 		} else
@@ -485,14 +495,14 @@ function onIq(stanza) {
 					.c('subscription', {node: nodeID, jid: jid, subid: subID.subid, subscription: subID.type});
 
 				if (conf)
-					response.cx(options);
+					response.cnode(options);
 
 				if (config.enabled('get-pending')) {
 					var affiliates = storage.getAffiliationsFromNodeID(nodeID);
 					var form = forms.build('form', 'subscribe_authorization', {allow: false, node: nodeID, subscriber_jid: jid}, true); //168
 					for (var i in affiliates) {
 						if (affiliates[i] == 'super-owner' || affiliates[i] == 'owner') {
-							var message = xmpp.message({to: i}).cx(form);
+							var message = xmpp.message({to: i}).cnode(form);
 							conn.send(message);
 						}
 					}
@@ -945,7 +955,7 @@ function onPresence(stanza) {
 function makeError(response, errorNumber, payload) {
 	response.attr.type = 'error';
 	if (payload)
-		response.cx(payload);
+		response.cnode(payload);
 
 	var e = errors.reverse[errorNumber];
 	var error = xmpp.stanza('error', {type: e.type});
@@ -959,7 +969,7 @@ function makeError(response, errorNumber
 			error.s(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors'});
 	}
 
-	response.cx(error);
+	response.cnode(error);
 	conn.send(response);
 }
 
@@ -979,14 +989,14 @@ function sendNotifs(notifs, type, nodeID
 		}
 		var affiliations = xmpp.stanza('affiliations', {node: nodeID})
 			.c('affiliation', args);
-		ev.cx(affiliations);
+		ev.cnode(affiliations);
 	} else if (type == 'collection') {
 		var collection = xmpp.stanza('collection', {node: nodeID});
 		if (a1 == 'associate')
-			collection.cx('associate', {node: nodeID});
+			collection.cnode('associate', {node: nodeID});
 		else
-			collection.cx('disassociate', {node: nodeID});
-		ev.cx(collection);
+			collection.cnode('disassociate', {node: nodeID});
+		ev.cnode(collection);
 	} else if (type == 'configuration') {
 		if (!config.enabled('config-node')) {
 			_('Error #4', 41)
@@ -997,14 +1007,14 @@ function sendNotifs(notifs, type, nodeID
 		if (a1) {
 			var x = forms.build('node_config', service_configuration.node_config, storage.getConfiguration(nodeID));
 			if (x)
-				configuration.cx(x); //TODO: voir exemple 150
+				configuration.cnode(x); //TODO: voir exemple 150
 		}
-		ev.cx(configuration);
+		ev.cnode(configuration);
 	} else if (type == 'delete') {
 		var del = xmpp.stanza('delete', {node: nodeID});
 		if (a1)
 			del.c('redirect', {uri: a1});
-		ev.cx(del);
+		ev.cnode(del);
 	} else if (type == 'items') {
 		var items = xmpp.stanza(type, {node: nodeID});
 		if (a2 == 'retract')
@@ -1022,11 +1032,11 @@ function sendNotifs(notifs, type, nodeID
 					args.publisher = item.publisher;
 				var it = xmpp.stanza('item', args);
 				if (item.content)
-					it.cx(item.content);
-				items.cx(it);
+					it.cnode(item.content);
+				items.cnode(it);
 			}
 		}
-		ev.cx(items);
+		ev.cnode(items);
 	} else if (type == 'purge') {
 		ev.c('purge', {node: nodeID});
 	} else if (type == 'subscription') {
@@ -1053,7 +1063,7 @@ function sendNotifs(notifs, type, nodeID
 			return;
 		}
 		var sub = xmpp.stanza('subscription', args);
-		ev.cx(sub);
+		ev.cnode(sub);
 	} else {
 		_('Error #1', 41)
 		return;
@@ -1097,7 +1107,7 @@ function sendNotifs(notifs, type, nodeID
 		}
 
 		var message = xmpp.message({to: i, from: componentJID, id: conn.getUniqueId()});
-		message.cx(ev);
+		message.cnode(ev);
 		conn.send(message);
 	}
 }
@@ -1109,7 +1119,7 @@ function sendDigest(jid, nodeID) {
 
 	var message = xmpp.message({to: jid, from: componentJID, id: conn.getUniqueId()});
 	for (var i in sub.digest)
-		message.cx(sub.digest[i]);
+		message.cnode(sub.digest[i]);
 	conn.send(message);
 }