Merge 0.10->trunk
[prosody.git] / plugins / mod_blocklist.lua
index 41a66f35a13765c80160eaaa1b0293a08aaae24c..8efbfd965295ab0ad250692831178e6112b1a9e2 100644 (file)
@@ -1,7 +1,7 @@
 -- Prosody IM
 -- Copyright (C) 2009-2010 Matthew Wild
 -- Copyright (C) 2009-2010 Waqas Hussain
--- Copyright (C) 2014 Kim Alvefur
+-- Copyright (C) 2014-2015 Kim Alvefur
 --
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
 --
 
 local user_exists = require"core.usermanager".user_exists;
-local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed;
+local rostermanager = require"core.rostermanager";
+local is_contact_subscribed = rostermanager.is_contact_subscribed;
+local is_contact_pending_in = rostermanager.is_contact_pending_in;
+local load_roster = rostermanager.load_roster;
+local save_roster = rostermanager.save_roster;
 local st = require"util.stanza";
 local st_error_reply = st.error_reply;
-local jid_prep, jid_split = import("jid", "prep", "split");
+local jid_prep = require"util.jid".prep;
+local jid_split = require"util.jid".split;
 
-local host = module.host;
 local storage = module:open_store();
-local sessions = prosody.hosts[host].sessions;
+local sessions = prosody.hosts[module.host].sessions;
 
--- Cache of blocklists used since module was loaded
-local cache = {};
-if module:get_option_boolean("blocklist_weak_cache") then
-       -- Lower memory usage, more IO and latency
-       setmetatable(cache, { __mode = "v" });
-end
+-- First level cache of blocklists by username.
+-- Weak table so may randomly expire at any time.
+local cache = setmetatable({}, { __mode = "v" });
+
+-- Second level of caching, keeps a fixed number of items, also anchors
+-- items in the above cache.
+--
+-- The size of this affects how often we will need to load a blocklist from
+-- disk, which we want to avoid during routing. On the other hand, we don't
+-- want to use too much memory either, so this can be tuned by advanced
+-- users. TODO use science to figure out a better default, 64 is just a guess.
+local cache_size = module:get_option_number("blocklist_cache_size", 64);
+local cache2 = require"util.cache".new(cache_size);
 
 local null_blocklist = {};
 
@@ -36,6 +47,7 @@ local function set_blocklist(username, blocklist)
                return ok, err;
        end
        -- Successful save, update the cache
+       cache2:set(username, blocklist);
        cache[username] = blocklist;
        return true;
 end
@@ -43,7 +55,6 @@ end
 -- Migrates from the old mod_privacy storage
 local function migrate_privacy_list(username)
        local migrated_data = { [false] = "not empty" };
-       module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username);
        local legacy_data = module:open_store("privacy"):get(username);
        if legacy_data and legacy_data.lists and legacy_data.default then
                legacy_data = legacy_data.lists[legacy_data.default];
@@ -52,6 +63,7 @@ local function migrate_privacy_list(username)
                return migrated_data;
        end
        if legacy_data then
+               module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username);
                local item, jid;
                for i = 1, #legacy_data do
                        item = legacy_data[i];
@@ -72,15 +84,19 @@ end
 local function get_blocklist(username)
        local blocklist = cache[username];
        if not blocklist then
-               if not user_exists(username, host) then
+               blocklist = cache2:get(username);
+       end
+       if not blocklist then
+               if not user_exists(username, module.host) then
                        return null_blocklist;
                end
                blocklist = storage:get(username);
                if not blocklist then
                        blocklist = migrate_privacy_list(username);
                end
-               cache[username] = blocklist;
+               cache2:set(username, blocklist);
        end
+       cache[username] = blocklist;
        return blocklist;
 end
 
@@ -95,44 +111,63 @@ module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event)
                end
        end
        origin.interested_blocklist = true; -- Gets notified about changes
-       return origin.send(reply);
+       origin.send(reply);
+       return true;
 end);
 
--- Add or remove a bare jid from the blocklist
+-- Add or remove some jid(s) from the blocklist
 -- We want this to be atomic and not do a partial update
 local function edit_blocklist(event)
        local origin, stanza = event.origin, event.stanza;
        local username = origin.username;
-       local act = stanza.tags[1];
-       local new = {};
-
-       local jid;
-       for item in act:childtags("item") do
-               jid = jid_prep(item.attr.jid);
+       local action = stanza.tags[1]; -- "block" or "unblock"
+       local is_blocking = action.name == "block" or nil; -- nil if unblocking
+       local new = {}; -- JIDs to block depending or unblock on action
+
+       -- XEP-0191 sayeth:
+       -- > When the user blocks communications with the contact, the user's
+       -- > server MUST send unavailable presence information to the contact (but
+       -- > only if the contact is allowed to receive presence notifications [...]
+       -- So contacts we need to do that for are added to the set below.
+       local send_unavailable = is_blocking and {};
+
+       -- Because blocking someone currently also blocks the ability to reject
+       -- subscription requests, we'll preemptively reject such
+       local remove_pending = is_blocking and {};
+
+       for item in action:childtags("item") do
+               local jid = jid_prep(item.attr.jid);
                if not jid then
-                       return origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
+                       origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
+                       return true;
                end
                item.attr.jid = jid; -- echo back prepped
-               new[jid] = is_contact_subscribed(username, host, jid) or false;
+               new[jid] = true;
+               if is_blocking then
+                       if is_contact_subscribed(username, module.host, jid) then
+                               send_unavailable[jid] = true;
+                       elseif is_contact_pending_in(username, module.host, jid) then
+                               remove_pending[jid] = true;
+                       end
+               end
        end
 
-       local mode = act.name == "block" or nil;
-
-       if mode and not next(new) then
+       if is_blocking and not next(new) then
                -- <block/> element does not contain at least one <item/> child element
-               return origin.send(st_error_reply(stanza, "modify", "bad-request"));
+               origin.send(st_error_reply(stanza, "modify", "bad-request"));
+               return true;
        end
 
        local blocklist = get_blocklist(username);
 
        local new_blocklist = {};
 
-       if mode and next(new) then
+       if is_blocking or next(new) then
                for jid in pairs(blocklist) do
                        new_blocklist[jid] = true;
                end
                for jid in pairs(new) do
-                       new_blocklist[jid] = mode;
+                       new_blocklist[jid] = is_blocking;
                end
                -- else empty the blocklist
        end
@@ -142,27 +177,38 @@ local function edit_blocklist(event)
        if ok then
                origin.send(st.reply(stanza));
        else
-               return origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
+               origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
+               return true;
        end
 
-       if mode then
-               for jid, in_roster in pairs(new) do
-                       if not blocklist[jid] and in_roster and sessions[username] then
+       if is_blocking then
+               for jid in pairs(send_unavailable) do
+                       if not blocklist[jid] then
                                for _, session in pairs(sessions[username].sessions) do
-                                       module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
+                                       if session.presence then
+                                               module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
+                                       end
                                end
                        end
                end
-       end
-       if sessions[username] then
-               local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
-                       :add_child(act); -- I am lazy
-
-               for _, session in pairs(sessions[username].sessions) do
-                       if session.interested_blocklist then
-                               blocklist_push.attr.to = session.full_jid;
-                               session.send(blocklist_push);
+
+               if next(remove_pending) then
+                       local roster = load_roster(username, module.host);
+                       for jid in pairs(remove_pending) do
+                               roster[false].pending[jid] = nil;
                        end
+                       save_roster(username, module.host, roster);
+                       -- Not much we can do about save failing here
+               end
+       end
+
+       local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
+               :add_child(action); -- I am lazy
+
+       for _, session in pairs(sessions[username].sessions) do
+               if session.interested_blocklist then
+                       blocklist_push.attr.to = session.full_jid;
+                       session.send(blocklist_push);
                end
        end
 
@@ -174,15 +220,16 @@ module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
 
 -- Cache invalidation, solved!
 module:hook_global("user-deleted", function (event)
-       if event.host == host then
+       if event.host == module.host then
+               cache2:set(event.username, nil);
                cache[event.username] = nil;
        end
 end);
 
 -- Buggy clients
 module:hook("iq-error/self/blocklist-push", function (event)
-       local type, condition, text = event.stanza:get_error();
-       (event.origin.log or module._log)("warn", "client returned an error in response to notification from mod_%s: %s%s%s", module.name, condition, text and ": " or "", text or "");
+       local _, condition, text = event.stanza:get_error();
+       (event.origin.log or module._log)("warn", "Client returned an error in response to notification from mod_%s: %s%s%s", module.name, condition, text and ": " or "", text or "");
        return true;
 end);
 
@@ -207,7 +254,8 @@ end
 local function bounce_stanza(event)
        local origin, stanza = event.origin, event.stanza;
        if drop_stanza(event) then
-               return origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
+               origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
+               return true;
        end
 end
 
@@ -243,8 +291,9 @@ local function bounce_outgoing(event)
                return drop_outgoing(event);
        end
        if drop_outgoing(event) then
-               return origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
+               origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
                        :tag("blocked", { xmlns = "urn:xmpp:blocking:errors" }));
+               return true;
        end
 end
 
@@ -263,6 +312,9 @@ module:hook("pre-message/bare", bounce_outgoing, prio_out);
 module:hook("pre-message/full", bounce_outgoing, prio_out);
 module:hook("pre-message/host", bounce_outgoing, prio_out);
 
+-- Note: MUST bounce these, but we don't because this would produce
+-- lots of error replies due to server-generated presence.
+-- FIXME some day, likely needing changes to mod_presence
 module:hook("pre-presence/bare", drop_outgoing, prio_out);
 module:hook("pre-presence/full", drop_outgoing, prio_out);
 module:hook("pre-presence/host", drop_outgoing, prio_out);