Changeset

3672:b24db47995ac

mod_pubsub, util.pubsub: Support node creation
author Florian Zeitz <florob@babelmonkeys.de>
date Wed, 01 Dec 2010 23:38:47 +0100
parents 3671:b7d5fe8eb829
children 3673:43b854062206
files plugins/mod_pubsub.lua util/pubsub.lua
diffstat 2 files changed, 30 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_pubsub.lua	Sun Nov 28 21:19:18 2010 +0000
+++ b/plugins/mod_pubsub.lua	Wed Dec 01 23:38:47 2010 +0100
@@ -55,6 +55,28 @@
 	return origin.send(reply);
 end
 
+function handlers.set_create(origin, stanza, create)
+	local node = create.attr.node;
+	local ok, ret, reply;
+	if node then
+		ok, ret = service:create(node, stanza.attr.from);
+		if ok then
+			reply = st.reply(stanza);
+		else
+			reply = st.error_reply(stanza, "cancel", ret);
+		end
+	else
+		repeat
+			node = uuid_generate();
+			ok, ret = service:create(node, stanza.attr.from);
+		until ok;
+		reply = st.reply(stanza)
+			:tag("pubsub", { xmlns = xmlns_pubsub })
+				:tag("create", { node = node });
+	end
+	origin.send(reply);
+end
+
 function handlers.set_subscribe(origin, stanza, subscribe)
 	local node, jid = subscribe.attr.node, subscribe.attr.jid;
 	if jid_bare(jid) ~= jid_bare(stanza.attr.from) then
--- a/util/pubsub.lua	Sun Nov 28 21:19:18 2010 +0000
+++ b/util/pubsub.lua	Wed Dec 01 23:38:47 2010 +0100
@@ -28,6 +28,14 @@
 	end
 end
 
+function service:create(node, actor)
+	if not self.nodes[node] then
+		self.nodes[node] = { name = node, subscribers = {}, config = {}, data = {} };
+		return true;
+	end
+	return false, "conflict";
+end
+
 function service:publish(node, actor, id, item)
 	local node_obj = self.nodes[node];
 	if not node_obj then