mod_storage, plus a bit of SQL and XML.
[prosody.git] / plugins / mod_privacy.lua
index 4e95e6e4d8c3727b238d6401d53edccd7f5e1bbf..d3043d6963d34387b9eb9fb54caceb99530ba81c 100644 (file)
@@ -65,40 +65,6 @@ function sendUnavailable(origin, to, from)
        end
 end
 
-function sendNeededUnavailablePersences(origin, listnameOrItem) -- TODO implement it correctly!
-       if type(listnameOrItem) == "string" then
-               local listname = listnameOrItem;
-               for _,list in ipairs(privacy_lists.lists) do
-                       if list.name == listname then
-                               for _,item in ipairs(list.items) do
-                                       sendNeededUnavailablePersences(origin, item);
-                               end
-                       end
-               end
-       elseif type(listnameOrItem) == "table" then
-               module:log("debug", "got an item, check whether to send unavailable presence stanza or not");
-               local item = listnameOrItem;
-
-               if item["presence-out"] == true then
-                       if item.type == "jid" then
-                               sendUnavailable(origin, item.value, origin.full_jid);
-                       elseif item.type == "group" then
-                       elseif item.type == "subscription" then
-                       elseif item.type == nil then
-                       end
-               elseif item["presence-in"] == true then
-                       if item.type == "jid" then
-                               sendUnavailable(origin, origin.full_jid, item.value);
-                       elseif item.type == "group" then
-                       elseif item.type == "subscription" then
-                       elseif item.type == nil then
-                       end
-               end
-       else
-               module:log("debug", "got unknown type: %s", type(listnameOrItem));
-       end
-end
-
 function declineList(privacy_lists, origin, stanza, which)
        if which == "default" then
                if isAnotherSessionUsingDefaultList(origin) then
@@ -118,28 +84,15 @@ end
 function activateList(privacy_lists, origin, stanza, which, name)
        local list = privacy_lists.lists[name];
 
-       if privacy_lists.default == nil then
-               privacy_lists.default = "";
-       end
-       if origin.activePrivacyList == nil then
-               origin.activePrivacyList = "";
-       end
-       
        if which == "default" and list then
                if isAnotherSessionUsingDefaultList(origin) then
                        return {"cancel", "conflict", "Another session is online and using the default list."};
                end
                privacy_lists.default = name;
                origin.send(st.reply(stanza));
---[[
-               if origin.activePrivacyList == nil then
-                       sendNeededUnavailablePersences(origin, name);
-               end
-]]--
        elseif which == "active" and list then
                origin.activePrivacyList = name;
                origin.send(st.reply(stanza));
-               -- sendNeededUnavailablePersences(origin, name);
        else
                return {"modify", "bad-request", "Either not active or default given or unknown list name specified."};
        end
@@ -154,10 +107,10 @@ function deleteList(privacy_lists, origin, stanza, name)
                        return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
                end
                if privacy_lists.default == name then
-                       privacy_lists.default = "";
+                       privacy_lists.default = nil;
                end
                if origin.activePrivacyList == name then
-                       origin.activePrivacyList = "";
+                       origin.activePrivacyList = nil;
                end
                privacy_lists.lists[name] = nil;
                origin.send(st.reply(stanza));
@@ -238,14 +191,6 @@ function createOrReplaceList (privacy_lists, origin, stanza, name, entries, rost
                if tmp.action ~= "deny" and tmp.action ~= "allow" then
                        return {"cancel", "bad-request", "Action must be either deny or allow."};
                end
-               
---[[
-               if (privacy_lists.default == name and origin.activePrivacyList == nil) or origin.activePrivacyList == name then
-                       module:log("debug", "calling sendNeededUnavailablePresences!");
-                       -- item is valid and list is active, so send needed unavailable stanzas
-                       sendNeededUnavailablePersences(origin, tmp);
-               end
-]]--
                list.items[#list.items + 1] = tmp;
        end
        
@@ -272,11 +217,15 @@ function getList(privacy_lists, origin, stanza, name)
        reply:tag("query", {xmlns="jabber:iq:privacy"});
 
        if name == nil then
-               reply:tag("active", {name=origin.activePrivacyList or ""}):up();
-               reply:tag("default", {name=privacy_lists.default or ""}):up();
                if privacy_lists.lists then
-                       for _,list in ipairs(privacy_lists.lists) do
-                               reply:tag("list", {name=list.name}):up();
+                       if origin.ActivePrivacyList then
+                               reply:tag("active", {name=origin.activePrivacyList}):up();
+                       end
+                       if privacy_lists.default then
+                               reply:tag("default", {name=privacy_lists.default}):up();
+                       end
+                       for name,list in pairs(privacy_lists.lists) do
+                               reply:tag("list", {name=name}):up();
                        end
                end
        else
@@ -306,7 +255,16 @@ module:hook("iq/bare/jabber:iq:privacy:query", function(data)
        if stanza.attr.to == nil then -- only service requests to own bare JID
                local query = stanza.tags[1]; -- the query element
                local valid = false;
-               local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or {};
+               local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or { lists = {} };
+
+               if privacy_lists.lists[1] then -- Code to migrate from old privacy lists format, remove in 0.8
+                       module:log("info", "Upgrading format of stored privacy lists for %s@%s", origin.username, origin.host);
+                       local lists = privacy_lists.lists;
+                       for idx, list in ipairs(lists) do
+                               lists[list.name] = list;
+                               lists[idx] = nil;
+                       end
+               end
 
                if stanza.attr.type == "set" then
                        if #query.tags == 1 then --  the <query/> element MUST NOT include more than one child element
@@ -371,8 +329,7 @@ function checkIfNeedToBeBlocked(e, session)
        module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
        
        if privacy_lists.lists == nil or
-               (session.activePrivacyList == nil or session.activePrivacyList == "") and
-               (privacy_lists.default == nil     or privacy_lists.default == "")
+               not (session.activePrivacyList or privacy_lists.default)
        then
                return; -- Nothing to block, default is Allow all
        end
@@ -383,7 +340,7 @@ function checkIfNeedToBeBlocked(e, session)
        
        local item;
        local listname = session.activePrivacyList;
-       if listname == nil or listname == "" then
+       if listname == nil then
                listname = privacy_lists.default; -- no active list selected, use default list
        end
        local list = privacy_lists.lists[listname];