mod_proxy65: Give the 'iq/host' stanza handler a negative priority, to allow mod_iq...
[prosody.git] / plugins / mod_presence.lua
index 1380c5c68a73167192cf394664d1d20aa50ad6c4..65abe665e29725b3dcda5665e867908fd4255239 100644 (file)
@@ -59,6 +59,15 @@ function handle_normal_presence(origin, stanza)
                        priority[1] = "0";
                end
        end
+       local priority = stanza:child_with_name("priority");
+       if priority and #priority > 0 then
+               priority = t_concat(priority);
+               if s_find(priority, "^[+-]?[0-9]+$") then
+                       priority = tonumber(priority);
+                       if priority < -128 then priority = -128 end
+                       if priority > 127 then priority = 127 end
+               else priority = 0; end
+       else priority = 0; end
        if full_sessions[origin.full_jid] then -- if user is still connected
                origin.send(stanza); -- reflect their presence back to them
        end
@@ -105,12 +114,15 @@ function handle_normal_presence(origin, stanza)
                                core_post_stanza(origin, request, true);
                        end
                end
-               local offline = offlinemanager.load(node, host);
-               if offline then
-                       for _, msg in ipairs(offline) do
-                               origin.send(msg); -- FIXME do we need to modify to/from in any way?
+
+               if priority >= 0 then
+                       local offline = offlinemanager.load(node, host);
+                       if offline then
+                               for _, msg in ipairs(offline) do
+                                       origin.send(msg); -- FIXME do we need to modify to/from in any way?
+                               end
+                               offlinemanager.deleteAll(node, host);
                        end
-                       offlinemanager.deleteAll(node, host);
                end
        end
        if stanza.attr.type == "unavailable" then
@@ -128,15 +140,6 @@ function handle_normal_presence(origin, stanza)
                end
        else
                origin.presence = stanza;
-               local priority = stanza:child_with_name("priority");
-               if priority and #priority > 0 then
-                       priority = t_concat(priority);
-                       if s_find(priority, "^[+-]?[0-9]+$") then
-                               priority = tonumber(priority);
-                               if priority < -128 then priority = -128 end
-                               if priority > 127 then priority = 127 end
-                       else priority = 0; end
-               else priority = 0; end
                if origin.priority ~= priority then
                        origin.priority = priority;
                        recalc_resource_map(user);
@@ -169,7 +172,7 @@ end
 
 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
        local node, host = jid_split(from_bare);
-       if to_bare == origin.username.."@"..origin.host then return; end -- No self contacts
+       if to_bare == from_bare then return; end -- No self contacts
        local st_from, st_to = stanza.attr.from, stanza.attr.to;
        stanza.attr.from, stanza.attr.to = from_bare, to_bare;
        log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
@@ -273,7 +276,8 @@ local outbound_presence_handler = function(data)
                end
 
                local to_bare = jid_bare(to);
-               if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
+               local roster = origin.roster;
+               if roster and not(roster[to_bare] and (roster[to_bare].subscription == "both" or roster[to_bare].subscription == "from")) then -- directed presence
                        origin.directed = origin.directed or {};
                        if t then -- removing from directed presence list on sending an error or unavailable
                                origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?