mod_presence: remove unused one-letter loop variable [luacheck]
[prosody.git] / plugins / mod_presence.lua
index 9e8f37dbe94a8dfe3aab528372c025203a2545dd..e4a317bc1f843ec04aa8c0f3057fd1c72b182f86 100644 (file)
@@ -10,7 +10,7 @@ local log = module._log;
 
 local require = require;
 local pairs = pairs;
-local t_concat, t_insert = table.concat, table.insert;
+local t_concat = table.concat;
 local s_find = string.find;
 local tonumber = tonumber;
 
@@ -27,31 +27,9 @@ local NULL = {};
 local rostermanager = require "core.rostermanager";
 local sessionmanager = require "core.sessionmanager";
 
-local function select_top_resources(user)
-       local priority = 0;
-       local recipients = {};
-       for _, session in pairs(user.sessions) do -- find resource with greatest priority
-               if session.presence then
-                       -- TODO check active privacy list for session
-                       local p = session.priority;
-                       if p > priority then
-                               priority = p;
-                               recipients = {session};
-                       elseif p == priority then
-                               t_insert(recipients, session);
-                       end
-               end
-       end
-       return recipients;
-end
-local function recalc_resource_map(user)
-       if user then
-               user.top_resources = select_top_resources(user);
-               if #user.top_resources == 0 then user.top_resources = nil; end
-       end
-end
+local recalc_resource_map = require "util.presence".recalc_resource_map;
 
-local ignore_presence_priority = module:get_option("ignore_presence_priority");
+local ignore_presence_priority = module:get_option_boolean("ignore_presence_priority", false);
 
 function handle_normal_presence(origin, stanza)
        if ignore_presence_priority then
@@ -106,10 +84,8 @@ function handle_normal_presence(origin, stanza)
                                res.presence.attr.to = nil;
                        end
                end
-               if roster.pending then -- resend incoming subscription requests
-                       for jid in pairs(roster.pending) do
-                               origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
-                       end
+               for jid in pairs(roster[false].pending) do -- resend incoming subscription requests
+                       origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
                end
                local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
                for jid, item in pairs(roster) do -- resend outgoing subscription requests
@@ -154,7 +130,7 @@ function send_presence_of_available_resources(user, host, jid, recipient_session
        if h and h.type == "local" then
                local u = h.sessions[user];
                if u then
-                       for k, session in pairs(u.sessions) do
+                       for _, session in pairs(u.sessions) do
                                local pres = session.presence;
                                if pres then
                                        if stanza then pres = stanza; pres.attr.from = session.full_jid; end
@@ -202,6 +178,7 @@ function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_
                end
                core_post_stanza(origin, stanza);
                send_presence_of_available_resources(node, host, to_bare, origin);
+               core_post_stanza(origin, st.presence({ type = "probe", from = from_bare, to = to_bare }));
        elseif stanza.attr.type == "unsubscribed" then
                -- 1. send unavailable
                -- 2. route stanza
@@ -381,3 +358,27 @@ module:hook("resource-unbind", function(event)
                session.directed = nil;
        end
 end);
+
+module:hook("roster-item-removed", function (event)
+       local username = event.username;
+       local session = event.origin;
+       local roster = event.roster or session and session.roster;
+       local jid = event.jid;
+       local item = event.item;
+       local from_jid = session.full_jid or (username .. "@" .. module.host);
+
+       local subscription = item and item.subscription or "none";
+       local ask = item and item.ask;
+       local pending = roster and roster[false].pending[jid];
+
+       if subscription == "both" or subscription == "from" or pending then
+               core_post_stanza(session, st.presence({type="unsubscribed", from=from_jid, to=jid}));
+       end
+
+       if subscription == "both" or subscription == "to" or ask then
+               send_presence_of_available_resources(username, module.host, jid, session, st.presence({type="unavailable"}));
+               core_post_stanza(session, st.presence({type="unsubscribe", from=from_jid, to=jid}));
+       end
+
+end, -1);
+