mod_pubsub, util.pubsub: Implement the purge action
authorKim Alvefur <zash@zash.se>
Wed, 23 Jan 2013 23:58:03 +0000 (00:58 +0100)
committerKim Alvefur <zash@zash.se>
Wed, 23 Jan 2013 23:58:03 +0000 (00:58 +0100)
plugins/mod_pubsub.lua
util/pubsub.lua

index f0aa4e4ff8d3f671ecf9484985f93641980cf9ca..687e38f1c3823fc5577eb5ef38a4b50194319039 100644 (file)
@@ -178,6 +178,10 @@ function handlers.set_retract(origin, stanza, retract)
        notify = (notify == "1") or (notify == "true");
        local item = retract:get_child("item");
        local id = item and item.attr.id
+       if not (node and id) then
+               origin.send(st.error_reply(stanza, "modify", "bad-request"));
+               return true;
+       end
        local reply, notifier;
        if notify then
                notifier = st.stanza("retract", { id = id });
@@ -191,6 +195,26 @@ function handlers.set_retract(origin, stanza, retract)
        return origin.send(reply);
 end
 
+function handlers.set_purge(origin, stanza, purge)
+       local node, notify = purge.attr.node, purge.attr.notify;
+       notify = (notify == "1") or (notify == "true");
+       local reply, notifier;
+       if not node then
+               origin.send(st.error_reply(stanza, "modify", "bad-request"));
+               return true;
+       end
+       if notify then
+               notifier = st.stanza("purge");
+       end
+       local ok, ret = service:purge(node, stanza.attr.from, 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
@@ -212,6 +236,7 @@ local disco_info;
 local feature_map = {
        create = { "create-nodes", "instant-nodes", "item-ids" };
        retract = { "delete-items", "retract-items" };
+       purge = { "purge-nodes" };
        publish = { "publish", autocreate_on_publish and "auto-create" };
        get_items = { "retrieve-items" };
        add_subscription = { "subscribe" };
index 8ff458e711f45f9700aaf49c8e332523bd2bc6ea..17795cf1b566841be728045f339a0b18d2e19519 100644 (file)
@@ -259,13 +259,21 @@ function service:retract(node, actor, id, retract)
        if (not node_obj) or (not node_obj.data[id]) then
                return false, "item-not-found";
        end
-       node_obj.data[id] = nil;
+       if id then
+               node_obj.data[id] = nil;
+       else
+               node_obj.data = {}; -- Purge
+       end
        if retract then
                self.config.broadcaster(node, node_obj.subscribers, retract);
        end
        return true
 end
 
+function service:purge(node, actor, purge)
+       return self:retract(node, actor, nil, purge);
+end
+
 function service:get_items(node, actor, id)
        -- Access checking
        if not self:may(node, actor, "get_items") then