net.dns: Support for resolving AAAA records
[prosody.git] / plugins / mod_presence.lua
index 1380c5c68a73167192cf394664d1d20aa50ad6c4..6d039d83f9862bb1e9a7dc8861c9e778d64a1d5c 100644 (file)
@@ -22,7 +22,6 @@ local NULL = {};
 
 local rostermanager = require "core.rostermanager";
 local sessionmanager = require "core.sessionmanager";
-local offlinemanager = require "core.offlinemanager";
 
 local function select_top_resources(user)
        local priority = 0;
@@ -59,6 +58,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 +113,10 @@ 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?
-                       end
-                       offlinemanager.deleteAll(node, host);
+
+               if priority >= 0 then
+                        local event = { origin = origin }
+                        module:fire_event('message/offline/broadcast', event);
                end
        end
        if stanza.attr.type == "unavailable" then
@@ -128,15 +134,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 +166,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);
@@ -206,6 +203,8 @@ function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_
                        rostermanager.roster_push(node, host, to_bare);
                end
                core_post_stanza(origin, stanza);
+       else
+               origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid presence type"));
        end
        stanza.attr.from, stanza.attr.to = st_from, st_to;
        return true;
@@ -256,7 +255,9 @@ function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_b
                        sessionmanager.send_to_interested_resources(node, host, stanza);
                        rostermanager.roster_push(node, host, from_bare);
                end
-       end -- discard any other type
+       else
+               origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid presence type"));
+       end
        stanza.attr.from, stanza.attr.to = st_from, st_to;
        return true;
 end
@@ -273,7 +274,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?
@@ -309,6 +311,8 @@ module:hook("presence/bare", function(data)
                end -- no resources not online, discard
        elseif not t or t == "unavailable" then
                handle_normal_presence(origin, stanza);
+       else
+               origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid presence type"));
        end
        return true;
 end);