net.server_select: Return handler from addclient
[prosody.git] / util / pubsub.lua
index 621cf1a61a4d9a105beb068143470eafd357dec7..0dfd196b2db22b5ef08545febaeca4b51b4b7aa0 100644 (file)
@@ -29,13 +29,13 @@ end
 
 function service:may(node, actor, action)
        if actor == true then return true; end
-       
+
        local node_obj = self.nodes[node];
        local node_aff = node_obj and node_obj.affiliations[actor];
        local service_aff = self.affiliations[actor]
                         or self.config.get_affiliation(actor, node, action)
                         or "none";
-       
+
        -- Check if node allows/forbids it
        local node_capabilities = node_obj and node_obj.capabilities;
        if node_capabilities then
@@ -47,7 +47,7 @@ function service:may(node, actor, action)
                        end
                end
        end
-       
+
        -- Check service-wide capabilities instead
        local service_capabilities = self.config.capabilities;
        local caps = service_capabilities[node_aff or service_aff];
@@ -57,7 +57,7 @@ function service:may(node, actor, action)
                        return can;
                end
        end
-       
+
        return false;
 end
 
@@ -171,6 +171,18 @@ function service:remove_subscription(node, actor, jid)
        return true;
 end
 
+function service:remove_all_subscriptions(actor, jid)
+       local normal_jid = self.config.normalize_jid(jid);
+       local subs = self.subscriptions[normal_jid]
+       subs = subs and subs[jid];
+       if subs then
+               for node in pairs(subs) do
+                       self:remove_subscription(node, true, jid);
+               end
+       end
+       return true;
+end
+
 function service:get_subscription(node, actor, jid)
        -- Access checking
        local cap;
@@ -199,7 +211,7 @@ function service:create(node, actor)
        if self.nodes[node] then
                return false, "conflict";
        end
-       
+
        self.nodes[node] = {
                name = node;
                subscribers = {};
@@ -214,6 +226,21 @@ function service:create(node, actor)
        return ok, err;
 end
 
+function service:delete(node, actor)
+       -- Access checking
+       if not self:may(node, actor, "delete") then
+               return false, "forbidden";
+       end
+       --
+       local node_obj = self.nodes[node];
+       if not node_obj then
+               return false, "item-not-found";
+       end
+       self.nodes[node] = nil;
+       self.config.broadcaster("delete", node, node_obj.subscribers);
+       return true;
+end
+
 function service:publish(node, actor, id, item)
        -- Access checking
        if not self:may(node, actor, "publish") then
@@ -232,7 +259,8 @@ function service:publish(node, actor, id, item)
                node_obj = self.nodes[node];
        end
        node_obj.data[id] = item;
-       self.config.broadcaster(node, node_obj.subscribers, item);
+       self.events.fire_event("item-published", { node = node, actor = actor, id = id, item = item });
+       self.config.broadcaster("items", node, node_obj.subscribers, item);
        return true;
 end
 
@@ -248,7 +276,24 @@ function service:retract(node, actor, id, retract)
        end
        node_obj.data[id] = nil;
        if retract then
-               self.config.broadcaster(node, node_obj.subscribers, retract);
+               self.config.broadcaster("items", node, node_obj.subscribers, retract);
+       end
+       return true
+end
+
+function service:purge(node, actor, notify)
+       -- Access checking
+       if not self:may(node, actor, "retract") then
+               return false, "forbidden";
+       end
+       --
+       local node_obj = self.nodes[node];
+       if not node_obj then
+               return false, "item-not-found";
+       end
+       node_obj.data = {}; -- Purge
+       if notify then
+               self.config.broadcaster("purge", node, node_obj.subscribers);
        end
        return true
 end
@@ -308,7 +353,7 @@ function service:get_subscriptions(node, actor, jid)
                        if node then -- Return only subscriptions to this node
                                if subscribed_nodes[node] then
                                        ret[#ret+1] = {
-                                               node = subscribed_node;
+                                               node = node;
                                                jid = jid;
                                                subscription = node_obj.subscribers[jid];
                                        };