mod_blocklist: Explicitly halt event propagation after returning a reply (send return...
[prosody.git] / plugins / mod_blocklist.lua
index 3d528eb143034b2f110fda77ed5b6488afcddfc0..8589c1e9f3d0d445c39653ce19ec86d7db919399 100644 (file)
@@ -13,11 +13,11 @@ local user_exists = require"core.usermanager".user_exists;
 local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed;
 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 = {};
@@ -43,7 +43,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 +51,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,7 +72,7 @@ end
 local function get_blocklist(username)
        local blocklist = cache[username];
        if not blocklist then
-               if not user_exists(username, host) then
+               if not user_exists(username, module.host) then
                        return null_blocklist;
                end
                blocklist = storage:get(username);
@@ -95,7 +95,8 @@ 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 some jid(s) from the blocklist
@@ -103,24 +104,25 @@ end);
 local function edit_blocklist(event)
        local origin, stanza = event.origin, event.stanza;
        local username = origin.username;
-       local act = stanza.tags[1];
+       local action = stanza.tags[1];
        local new = {};
 
-       local jid;
-       for item in act:childtags("item") do
-               jid = jid_prep(item.attr.jid);
+       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] = is_contact_subscribed(username, module.host, jid) or false;
        end
 
-       local mode = act.name == "block" or nil;
+       local mode = action.name == "block" or nil;
 
        if mode 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);
@@ -142,21 +144,24 @@ 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
                                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
+                       :add_child(action); -- I am lazy
 
                for _, session in pairs(sessions[username].sessions) do
                        if session.interested_blocklist then
@@ -174,15 +179,15 @@ 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
                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 +212,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 +249,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