mod_storage, plus a bit of SQL and XML.
[prosody.git] / plugins / mod_privacy.lua
index 18f7384e1a4be52c22616ed5825549e077306f93..d3043d6963d34387b9eb9fb54caceb99530ba81c 100644 (file)
@@ -17,16 +17,6 @@ local jid_split = util_Jid.split;
 local load_roster = require "core.rostermanager".load_roster;
 local to_number = tonumber;
 
-function findNamedList(privacy_lists, name)
-       if privacy_lists.lists then
-               for i=1,#privacy_lists.lists do
-                       if privacy_lists.lists[i].name == name then
-                               return i;
-                       end
-               end
-       end
-end
-
 function isListUsed(origin, name, privacy_lists)
        local user = bare_sessions[origin.username.."@"..origin.host];
        if user then
@@ -75,42 +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 wether to send unavailable presence stanza or not");
-               local item = listnameOrItem;
-               local serialize = require "util.serialization".serialize;
-
-
-               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
@@ -128,30 +82,17 @@ function declineList(privacy_lists, origin, stanza, which)
 end
 
 function activateList(privacy_lists, origin, stanza, which, name)
-       local idx = findNamedList(privacy_lists, 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 idx ~= nil then
+       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 idx ~= nil then
+       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
@@ -159,19 +100,19 @@ function activateList(privacy_lists, origin, stanza, which, name)
 end
 
 function deleteList(privacy_lists, origin, stanza, name)
-       local idx = findNamedList(privacy_lists, name);
+       local list = privacy_lists.lists[name];
 
-       if idx ~= nil then
+       if list then
                if isListUsed(origin, name, privacy_lists) then
                        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
-               table.remove(privacy_lists.lists, idx);
+               privacy_lists.lists[name] = nil;
                origin.send(st.reply(stanza));
                return true;
        end
@@ -179,19 +120,16 @@ function deleteList(privacy_lists, origin, stanza, name)
 end
 
 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
-       local idx = findNamedList(privacy_lists, name);
        local bare_jid = origin.username.."@"..origin.host;
        
        if privacy_lists.lists == nil then
                privacy_lists.lists = {};
        end
 
-       if idx == nil then
-               idx = #privacy_lists.lists + 1;
-       end
+       local list = {};
+       privacy_lists.lists[name] = list;
 
        local orderCheck = {};
-       local list = {};
        list.name = name;
        list.items = {};
 
@@ -202,7 +140,7 @@ function createOrReplaceList (privacy_lists, origin, stanza, name, entries, rost
                
                if item.attr.type ~= nil and item.attr.type ~= "jid" and item.attr.type ~= "subscription" and item.attr.type ~= "group" then
                        return {"modify", "bad-request", "Type attribute not valid."};
-               end 
+               end
                
                local tmp = {};
                orderCheck[item.attr.order] = true;
@@ -253,20 +191,11 @@ 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
        
        table.sort(list, function(a, b) return a.order < b.order; end);
 
-       privacy_lists.lists[idx] = list;
        origin.send(st.reply(stanza));
        if bare_sessions[bare_jid] ~= nil then
                local iq = st.iq ( { type = "set", id="push1" } );
@@ -288,17 +217,20 @@ 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
-               local idx = findNamedList(privacy_lists, name);
-               if idx ~= nil then
-                       local list = privacy_lists.lists[idx];
+               local list = privacy_lists.lists[name];
+               if list then
                        reply = reply:tag("list", {name=list.name});
                        for _,item in ipairs(list.items) do
                                reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
@@ -323,10 +255,19 @@ 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 
+                       if #query.tags == 1 then --  the <query/> element MUST NOT include more than one child element
                                for _,tag in ipairs(query.tags) do
                                        if tag.name == "active" or tag.name == "default" then
                                                if tag.attr.name == nil then -- Client declines the use of active / default list
@@ -360,13 +301,14 @@ module:hook("iq/bare/jabber:iq:privacy:query", function(data)
                end
 
                if valid ~= true then
-                       if valid[0] == nil then
-                               valid[0] = "cancel";
-                       end
+                       valid = valid or { "cancel", "bad-request", "Couldn't understand request" };
                        if valid[1] == nil then
-                               valid[1] = "bad-request";
+                               valid[1] = "cancel";
+                       end
+                       if valid[2] == nil then
+                               valid[2] = "bad-request";
                        end
-                       origin.send(st.error_reply(stanza, valid[0], valid[1], valid[2]));
+                       origin.send(st.error_reply(stanza, valid[1], valid[2], valid[3]));
                else
                        datamanager.store(origin.username, origin.host, "privacy", privacy_lists);
                end
@@ -378,101 +320,96 @@ function checkIfNeedToBeBlocked(e, session)
        local origin, stanza = e.origin, e.stanza;
        local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {};
        local bare_jid = session.username.."@"..session.host;
-
-       module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(stanza.attr.to), tostring(stanza.attr.from));
+       local to = stanza.attr.to;
+       local from = stanza.attr.from;
        
-       if stanza.attr.to ~= nil and stanza.attr.from ~= nil then
-               if privacy_lists.lists == nil or
-                       (session.activePrivacyList == nil or session.activePrivacyList == "") and
-                       (privacy_lists.default == nil     or privacy_lists.default == "")
-               then 
-                       return; -- Nothing to block, default is Allow all
-               end
-               if jid_bare(stanza.attr.from) == bare_jid and jid_bare(stanza.attr.to) == bare_jid then
-                       module:log("debug", "Never block communications from one of a user's resources to another.");
-                       return; -- from one of a user's resource to another => HANDS OFF!
-               end 
-
-               local idx;
-               local list;
-               local item;
-               local listname = session.activePrivacyList;
-               if listname == nil or listname == "" then
-                       listname = privacy_lists.default; -- no active list selected, use default list
-               end
-               idx = findNamedList(privacy_lists, listname);
-               if idx == nil then
-                       module:log("error", "given privacy listname not found. name: %s", listname);
-                       return;
-               end
-               list = privacy_lists.lists[idx];
-               if list == nil then
-                       module:log("info", "privacy list index wrong. index: %d", idx);
-                       return;
+       local is_to_user = bare_jid == jid_bare(to);
+       local is_from_user = bare_jid == jid_bare(from);
+       
+       module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
+       
+       if privacy_lists.lists == nil or
+               not (session.activePrivacyList or privacy_lists.default)
+       then
+               return; -- Nothing to block, default is Allow all
+       end
+       if is_from_user and is_to_user then
+               module:log("debug", "Not blocking communications between user's resources");
+               return; -- from one of a user's resource to another => HANDS OFF!
+       end
+       
+       local item;
+       local listname = session.activePrivacyList;
+       if listname == nil then
+               listname = privacy_lists.default; -- no active list selected, use default list
+       end
+       local list = privacy_lists.lists[listname];
+       if not list then
+               module:log("debug", "given privacy list not found. name: %s", listname);
+               return;
+       end
+       for _,item in ipairs(list.items) do
+               local apply = false;
+               local block = false;
+               if (
+                       (stanza.name == "message" and item.message) or
+                       (stanza.name == "iq" and item.iq) or
+                       (stanza.name == "presence" and is_to_user and item["presence-in"]) or
+                       (stanza.name == "presence" and is_from_user and item["presence-out"]) or
+                       (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-out"] == false)
+               ) then
+                       apply = true;
                end
-               for _,item in ipairs(list.items) do
-                       local apply = false;
-                       local block = false;
-                       if (
-                               (stanza.name == "message" and item.message) or
-                               (stanza.name == "iq" and item.iq) or
-                               (stanza.name == "presence" and jid_bare(stanza.attr.to) == bare_jid and item["presence-in"]) or
-                               (stanza.name == "presence" and jid_bare(stanza.attr.from) == bare_jid and item["presence-out"]) or
-                               (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-in"] == false)
-                       ) then
-                               apply = true;
+               if apply then
+                       local evilJid = {};
+                       apply = false;
+                       if is_to_user then
+                               module:log("debug", "evil jid is (from): %s", from);
+                               evilJid.node, evilJid.host, evilJid.resource = jid_split(from);
+                       else
+                               module:log("debug", "evil jid is (to): %s", to);
+                               evilJid.node, evilJid.host, evilJid.resource = jid_split(to);
                        end
-                       if apply then
-                               local evilJid = {};
-                               apply = false;
-                               if jid_bare(stanza.attr.to) == bare_jid then
-                                       module:log("debug", "evil jid is (from): %s", stanza.attr.from);
-                                       evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.from);
-                               else
-                                       module:log("debug", "evil jid is (to): %s", stanza.attr.to);
-                                       evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.to);
-                               end
-                               if      item.type == "jid" and 
-                                       (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
-                                       (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
-                                       (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
-                                       (evilJid.host and item.value == evilJid.host) then
-                                       apply = true;
-                                       block = (item.action == "deny");
-                               elseif item.type == "group" then
-                                       local roster = load_roster(session.username, session.host);
-                                       local groups = roster[evilJid.node .. "@" .. evilJid.host].groups;
-                                       for group in pairs(groups) do
-                                               if group == item.value then
-                                                       apply = true;
-                                                       block = (item.action == "deny");
-                                                       break;
-                                               end
-                                       end
-                               elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid
-                                       local roster = load_roster(session.username, session.host);
-                                       if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then
+                       if      item.type == "jid" and
+                               (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
+                               (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
+                               (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
+                               (evilJid.host and item.value == evilJid.host) then
+                               apply = true;
+                               block = (item.action == "deny");
+                       elseif item.type == "group" then
+                               local roster = load_roster(session.username, session.host);
+                               local groups = roster[evilJid.node .. "@" .. evilJid.host].groups;
+                               for group in pairs(groups) do
+                                       if group == item.value then
                                                apply = true;
                                                block = (item.action == "deny");
+                                               break;
                                        end
-                               elseif item.type == nil then
+                               end
+                       elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid
+                               local roster = load_roster(session.username, session.host);
+                               if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then
                                        apply = true;
                                        block = (item.action == "deny");
                                end
+                       elseif item.type == nil then
+                               apply = true;
+                               block = (item.action == "deny");
                        end
-                       if apply then
-                               if block then
-                                       module:log("info", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(stanza.attr.to), tostring(stanza.attr.from));
-                                       if stanza.name == "message" then
-                                               origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
-                                       elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
-                                               origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
-                                       end
-                                       return true; -- stanza blocked !
-                               else
-                                       module:log("info", "stanza explicit allowed!")
-                                       return;
+               end
+               if apply then
+                       if block then
+                               module:log("debug", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
+                               if stanza.name == "message" then
+                                       origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
+                               elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
+                                       origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
                                end
+                               return true; -- stanza blocked !
+                       else
+                               module:log("debug", "stanza explicitly allowed!")
+                               return;
                        end
                end
        end