prosodyctl: remove unused one-letter loop variable [luacheck]
[prosody.git] / plugins / mod_blocklist.lua
index a870d278416f6a63c45300672c1a21cb695a16e3..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 = require"util.jid".prep;
@@ -117,8 +121,20 @@ local function edit_blocklist(event)
        local origin, stanza = event.origin, event.stanza;
        local username = origin.username;
        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
@@ -126,11 +142,16 @@ local function edit_blocklist(event)
                        return true;
                end
                item.attr.jid = jid; -- echo back prepped
-               new[jid] = is_contact_subscribed(username, module.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 is_blocking = action.name == "block" or nil; -- nil if unblocking
-
        if is_blocking and not next(new) then
                -- <block/> element does not contain at least one <item/> child element
                origin.send(st_error_reply(stanza, "modify", "bad-request"));
@@ -161,8 +182,8 @@ local function edit_blocklist(event)
        end
 
        if is_blocking then
-               for jid, in_roster in pairs(new) do
-                       if not blocklist[jid] and in_roster and sessions[username] then
+               for jid in pairs(send_unavailable) do
+                       if not blocklist[jid] then
                                for _, session in pairs(sessions[username].sessions) do
                                        if session.presence then
                                                module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
@@ -170,16 +191,24 @@ local function edit_blocklist(event)
                                end
                        end
                end
-       end
-       if sessions[username] then
-               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);
+
+               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
 
@@ -192,7 +221,7 @@ module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
 -- Cache invalidation, solved!
 module:hook_global("user-deleted", function (event)
        if event.host == module.host then
-               cache:set(event.username, nil);
+               cache2:set(event.username, nil);
                cache[event.username] = nil;
        end
 end);