# HG changeset patch # User Matthew Wild # Date 1291656755 0 # Node ID 23c6ce0e3f20bd678270154a2e34d8ac4bce8eb6 # Parent c8fcd63e95267d59808f22f10f83ee75c08f712b# Parent 4f22615c836163a96ccb025ec3852c1f5e5abdcf Merge with Zash diff -r 4f22615c8361 -r 23c6ce0e3f20 plugins/mod_pubsub.lua --- a/plugins/mod_pubsub.lua Fri Dec 03 16:40:42 2010 +0100 +++ b/plugins/mod_pubsub.lua Mon Dec 06 17:32:35 2010 +0000 @@ -25,8 +25,10 @@ end local pubsub_errors = { + ["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]; @@ -50,7 +52,7 @@ :tag("pubsub", { xmlns = xmlns_pubsub }) :add_child(data); else - reply = st.error_reply(stanza, "cancel", "item-not-found", "Item could not be found in this node"); + reply = pubsub_error_reply(stanza, "item-not-found"); end return origin.send(reply); end @@ -63,7 +65,7 @@ if ok then reply = st.reply(stanza); else - reply = st.error_reply(stanza, "cancel", ret); + reply = pubsub_error_reply(stanza, ret); end else repeat @@ -74,7 +76,7 @@ :tag("pubsub", { xmlns = xmlns_pubsub }) :tag("create", { node = node }); end - origin.send(reply); + return origin.send(reply); end function handlers.set_subscribe(origin, stanza, subscribe) @@ -98,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"); @@ -115,7 +132,27 @@ return origin.send(reply); end +function handlers.set_retract(origin, stanza, retract) + local node, notify = retract.attr.node, retract.attr.notify; + notify = (notify == "1") or (notify == "true"); + local item = retract:get_child("item"); + local id = item and item.attr.id + local reply, notifier; + if notify then + notifier = st.stanza("retract", { id = id }); + end + local ok, ret = service:retract(node, stanza.attr.from, id, notifier); + if ok then + reply = st.reply(stanza); + else + reply = pubsub_error_reply(stanza, ret); + end + return origin.send(reply); +end + function simple_broadcast(node, jids, item) + item = st.clone(item); + item.attr.xmlns = nil; -- Clear the pubsub namespace local message = st.message({ from = module.host, type = "headline" }) :tag("event", { xmlns = xmlns_pubsub_event }) :tag("items", { node = node }) diff -r 4f22615c8361 -r 23c6ce0e3f20 util/pubsub.lua --- a/util/pubsub.lua Fri Dec 03 16:40:42 2010 +0100 +++ b/util/pubsub.lua Mon Dec 06 17:32:35 2010 +0000 @@ -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 @@ -47,6 +54,18 @@ return true; end +function service:retract(node, actor, id, retract) + local node_obj = self.nodes[node]; + if (not node_obj) or (not node_obj.data[id]) then + return false, "item-not-found"; + end + node_obj.data[id] = nil; + if retract then + self.cb.broadcaster(node, node_obj.subscribers, retract); + end + return true +end + function service:get(node, actor, id) local node_obj = self.nodes[node]; if node_obj then