changeset 34:dcf1f09f8cee

Add message to publish module and various fixes.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 05 Nov 2010 15:31:13 +0100
parents 62cbb1c49bc5
children 6697f394301f
files backends/directory.js modules/mod_publish.js modules/mod_publish_message.js psgxs.js util.js
diffstat 5 files changed, 139 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/backends/directory.js
+++ b/backends/directory.js
@@ -115,6 +115,9 @@ backend.load = function(dir) {
 
 			for (var i in files) {
 				var file = files[i];
+				if (/^\./.test(file))
+					continue;
+
 				var nodeID = file.replace(/\.json$/, '');
 
 				var data = fs.readFileSync(dir+'/'+file).toString();
--- a/modules/mod_publish.js
+++ b/modules/mod_publish.js
@@ -1,7 +1,9 @@
 var config = require('../configuration');
 var storage = require('../storage');
 var notifs = require('../notifs');
-var toBareJID = require('../util').toBareJID;
+var errors = require('../errors');
+var utils = require('../util');
+var toBareJID = utils.toBareJID;
 var NS = require('../namespaces');
 
 // SECTION 7.1: Publish an Item to a Node
new file mode 100644
--- /dev/null
+++ b/modules/mod_publish_message.js
@@ -0,0 +1,87 @@
+var config = require('../configuration');
+var storage = require('../storage');
+var notifs = require('../notifs');
+var errors = require('../errors');
+var utils = require('../util');
+var toBareJID = utils.toBareJID;
+var NS = require('../namespaces');
+var xmpp = require('xmpp');
+
+exports.publish = {
+	stanza: 'message',
+	child: 'body',
+	func: function(response, stanza, request, to, from) {
+		if (!config.enabled('publish'))
+			return makeError(response, errors.pub.publish.item_publication_not_supported.n);
+
+		var nodeID = utils.toResource(from);
+		console.log(require('sys').inspect(nodeID));
+		if (!nodeID || nodeID == '')
+			return makeError(response, errors.nodeid_required.n);
+
+		var autocreate = false;
+		if (!storage.existsNode(nodeID)) {
+			if (config.enabled('auto-create'))
+				autocreate = true;
+			else
+				return makeError(response, errors.node_does_not_exist.n);
+		}
+
+		var affil = storage.getAffiliation(toBareJID(to), nodeID);
+		if (typeof affil == 'number' && affil != errors.node_does_not_exist.n)
+			return makeError(response, affil);
+		if (!autocreate && affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'publish-only')
+			return makeError(response, errors.forbidden.n);
+
+		if (!config.enabled('item-ids'))
+			return makeError(response, errors.itemid_required.n);
+		var itemID = utils.makeRandomId();
+
+		var now = new Date();
+
+		var item = xmpp.stanza('item', {xmlns: NS.PUBSUB, id: itemID});
+		item.c('entry', {xmlns: NS.ATOM});
+		if (stanza.getChild('subject'))
+			item.c('title').t(stanza.getChild('subject')).up();
+		item.c('summary').t(request.getText()).up();
+		item.c('id').t(itemID).up();
+		item.c('published').t(now.toString()).up();
+		item.c('updated').t(now.toString()).up();
+		console.log(require('sys').inspect(item, false, null));
+
+		if (!config.enabled('persistent-items')) {
+			var notifications = storage.purgeNode(nodeID);
+			if (typeof notifications == 'number')
+				return makeError(response, r);
+		}
+
+		if (autocreate) {
+			if (!form)
+				form = {};
+			form['pubsub#creator'] = toBareJID(to);
+
+			var r = storage.createNode(nodeID, form);
+			if (typeof r == 'number')
+				return makeError(response, r);
+		}
+
+		var content = item.getChild();
+
+		subscribers = storage.setItem(nodeID, itemID, content);
+		if (typeof subscribers == 'number')
+			return makeError(response, subscribers);
+
+		var attrs = {};
+		if (content)
+			attrs[itemID] = {content: content};
+		else
+			attrs[itemID] = {};
+		notifs.send(subscribers, 'items', nodeID, attrs);
+
+		response.c('pubsub', {xmlns: NS.PUBSUB})
+			.c('publish', {node: nodeID})
+			.c('item', {id: itemID});
+
+		return response;
+	}
+}
--- a/psgxs.js
+++ b/psgxs.js
@@ -88,6 +88,9 @@ function onIq(stanza) {
 				for (var k in modules) {
 					var module = modules[k];
 
+					if (module.stanza && (module.stanza != 'iq'))
+						continue;
+
 					if (module.type && (module.type != type))
 						continue;
 
@@ -100,7 +103,7 @@ function onIq(stanza) {
 					if (module.number && (module.number != j))
 						continue;
 
-					var toSend = module.func(response, stanza, child2, to);
+					var toSend = module.func(response, stanza, child2, to, from);
 					if (toSend) {
 						response = toSend;
 						send = true;
@@ -112,6 +115,9 @@ function onIq(stanza) {
 			for (var k in modules) {
 				var module = modules[k];
 
+				if (module.stanza && (module.stanza != 'iq'))
+					continue;
+
 				if (module.type && (module.type != type))
 					continue;
 
@@ -124,7 +130,7 @@ function onIq(stanza) {
 				if (module.number && (module.number != k))
 					continue;
 
-				var toSend = module.func(response, stanza, child1, to);
+				var toSend = module.func(response, stanza, child1, to, from);
 				if (toSend) {
 					response = toSend;
 					send = true;
@@ -148,6 +154,39 @@ function onMessage(stanza) {
 	else
 		response = xmpp.message({to: to, from: from});
 
+	var send = false;
+
+	for (var i in stanza.tags) {
+		var child = stanza.tags[i];
+		for (var k in modules) {
+			var module = modules[k];
+
+			if (module.stanza !== 'message')
+				continue;
+
+			if (module.type && (module.type != type))
+				continue;
+
+			if (module.child && (module.child != child.name))
+				continue;
+
+			if (module.ns && (module.ns != child.attr.xmlns))
+				continue;
+
+			if (module.number && (module.number != k))
+				continue;
+
+			var toSend = module.func(response, stanza, child, to, from);
+			if (toSend) {
+				response = toSend;
+				send = true;
+				delete toSend;
+			}
+		}
+	}
+
+	conn.send(send? response: makeError(response, errors.feature_not_implemented.n));
+
 	var x = stanza.getChild('x', 'jabber:x:data');
 	if (x) {
 		var form = forms.parse(x);
--- a/util.js
+++ b/util.js
@@ -50,3 +50,8 @@ util.toBareJID = function(jid) {
 	var j = new JID(jid);
 	return j.bare;
 };
+
+util.toResource = function(jid) {
+	var j = new JID(jid);
+	return j.resource;
+};