Changeset

3821:cef2d5dc65e3

mod_pubsub, util.pubsub: Support for unsubscribing
author Florian Zeitz <florob@babelmonkeys.de>
date Sun, 05 Dec 2010 02:46:08 +0100
parents 3820:6f25c09916a5
children 3822:d6028f4eb610
files plugins/mod_pubsub.lua util/pubsub.lua
diffstat 2 files changed, 24 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_pubsub.lua	Fri Dec 03 16:36:13 2010 +0100
+++ b/plugins/mod_pubsub.lua	Sun Dec 05 02:46:08 2010 +0100
@@ -28,6 +28,7 @@
 	["conflict"] = { "cancel", "conflict" };
 	["invalid-jid"] = { "modify", "bad-request", nil, "invalid-jid" };
 	["item-not-found"] = { "cancel", "item-not-found" };
+	["not-subscribed"] = { "modify", "unexpected-request", nil, "not-subscribed" };
 };
 function pubsub_error_reply(stanza, error)
 	local e = pubsub_errors[error];
@@ -99,6 +100,21 @@
 	return origin.send(reply);
 end
 
+function handlers.set_unsubscribe(origin, stanza, unsubscribe)
+	local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid;
+	if jid_bare(jid) ~= jid_bare(stanza.attr.from) then
+		return origin.send(pubsub_error_reply(stanza, "invalid-jid"));
+	end
+	local ok, ret = service:remove_subscription(node, stanza.attr.from, jid);
+	local reply;
+	if ok then
+		reply = st.reply(stanza);
+	else
+		reply = pubsub_error_reply(stanza, ret);
+	end
+	return origin.send(reply);
+end
+
 function handlers.set_publish(origin, stanza, publish)
 	local node = publish.attr.node;
 	local item = publish:get_child("item");
--- a/util/pubsub.lua	Fri Dec 03 16:36:13 2010 +0100
+++ b/util/pubsub.lua	Sun Dec 05 02:46:08 2010 +0100
@@ -17,7 +17,14 @@
 end
 
 function service:remove_subscription(node, actor, jid)
-	self.nodes[node].subscribers[jid] = nil;
+	local node_obj = self.nodes[node];
+	if not node_obj then
+		return false, "item-not-found";
+	end
+	if not node_obj.subscribers[jid] then
+		return false, "not-subscribed";
+	end
+	node_obj.subscribers[jid] = nil;
 	return true;
 end