Merge 0.9->0.10
[prosody.git] / plugins / mod_pubsub / pubsub.lib.lua
index 2b015e34983c7ff3bd6ee9602d145f2e9bcb537d..d85c71be6db35a554965b49b4ae7622eefd4ad90 100644 (file)
@@ -1,8 +1,10 @@
 local st = require "util.stanza";
 local uuid_generate = require "util.uuid".generate;
+local dataform = require"util.dataforms".new;
 
 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
+local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
 
 local _M = {};
 
@@ -16,7 +18,8 @@ local pubsub_errors = {
        ["nodeid-required"] = { "modify", "bad-request", nil, "nodeid-required" };
        ["item-not-found"] = { "cancel", "item-not-found" };
        ["not-subscribed"] = { "modify", "unexpected-request", nil, "not-subscribed" };
-       ["forbidden"] = { "cancel", "forbidden" };
+       ["forbidden"] = { "auth", "forbidden" };
+       ["not-allowed"] = { "cancel", "not-allowed" };
 };
 local function pubsub_error_reply(stanza, error)
        local e = pubsub_errors[error];
@@ -28,6 +31,19 @@ local function pubsub_error_reply(stanza, error)
 end
 _M.pubsub_error_reply = pubsub_error_reply;
 
+local node_config_form = require"util.dataforms".new {
+       {
+               type = "hidden";
+               name = "FORM_TYPE";
+               value = "http://jabber.org/protocol/pubsub#node_config";
+       };
+       {
+               type = "text-single";
+               name = "pubsub#max_items";
+               label = "Max # of items to persist";
+       };
+};
+
 function handlers.get_items(origin, stanza, items, service)
        local node = items.attr.node;
        local item = items:get_child("item");
@@ -42,8 +58,8 @@ function handlers.get_items(origin, stanza, items, service)
        end
 
        local data = st.stanza("items", { node = node });
-       for _, entry in pairs(results) do
-               data:add_child(entry);
+       for _, id in ipairs(results) do
+               data:add_child(results[id]);
        end
        local reply;
        if data then
@@ -222,4 +238,53 @@ function handlers.set_purge(origin, stanza, purge, service)
        return origin.send(reply);
 end
 
+function handlers.get_configure(origin, stanza, config, service)
+       local node = config.attr.node;
+       if not node then
+               return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
+       end
+
+       if not service:may(node, stanza.attr.from, "configure") then
+               return origin.send(pubsub_error_reply(stanza, "forbidden"));
+       end
+
+       local node_obj = service.nodes[node];
+       if not node_obj then
+               return origin.send(pubsub_error_reply(stanza, "item-not-found"));
+       end
+
+       local reply = st.reply(stanza)
+               :tag("pubsub", { xmlns = xmlns_pubsub_owner })
+                       :tag("configure", { node = node })
+                               :add_child(node_config_form:form(node_obj.config));
+       return origin.send(reply);
+end
+
+function handlers.set_configure(origin, stanza, config, service)
+       local node = config.attr.node;
+       if not node then
+               return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
+       end
+       if not service:may(node, stanza.attr.from, "configure") then
+               return origin.send(pubsub_error_reply(stanza, "forbidden"));
+       end
+       local new_config, err = node_config_form:data(config.tags[1]);
+       if not new_config then
+               return origin.send(st.error_reply(stanza, "modify", "bad-request", err));
+       end
+       local ok, err = service:set_node_config(node, stanza.attr.from, new_config);
+       if not ok then
+               return origin.send(pubsub_error_reply(stanza, err));
+       end
+       return origin.send(st.reply(stanza));
+end
+
+function handlers.get_default(origin, stanza, default, service)
+       local reply = st.reply(stanza)
+               :tag("pubsub", { xmlns = xmlns_pubsub_owner })
+                       :tag("default")
+                               :add_child(node_config_form:form(service.node_defaults));
+       return origin.send(reply);
+end
+
 return _M;