A little whitespace fix
[prosody.git] / core / stanza_router.lua
index 41e89f31995b1862d506bbe840f6d0666a77f3ae..4f6918d2baed1feeac427274ef9c7716210ad2ab 100644 (file)
@@ -22,6 +22,7 @@ local modules_handle_stanza = require "core.modulemanager".handle_stanza;
 local format = string.format;
 local tostring = tostring;
 local t_concat = table.concat;
+local t_insert = table.insert;
 local tonumber = tonumber;
 local s_find = string.find;
 
@@ -50,7 +51,7 @@ function core_process_stanza(origin, stanza)
        if origin.type == "c2s" then
                stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
        end
-       
+
        if not to then
                core_handle_stanza(origin, stanza);
        elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
@@ -77,7 +78,7 @@ function core_handle_stanza(origin, stanza)
        if modules_handle_stanza(origin, stanza) then return; end
        if origin.type == "c2s" or origin.type == "c2s_unauthed" then
                local session = origin;
-               
+
                if stanza.name == "presence" and origin.roster then
                        if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
                                for jid in pairs(origin.roster) do -- broadcast to all interested contacts
@@ -218,6 +219,7 @@ function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_b
        local st_from, st_to = stanza.attr.from, stanza.attr.to;
        stanza.attr.from, stanza.attr.to = from_bare, to_bare;
        if stanza.attr.type == "probe" then
+               log("debug", "inbound probe from "..from_bare.." for "..to_bare);
                if rostermanager.is_contact_subscribed(node, host, from_bare) then
                        if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
                                -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
@@ -258,7 +260,7 @@ end
 function core_route_stanza(origin, stanza)
        -- Hooks
        --- ...later
-       
+
        -- Deliver
        local to = stanza.attr.to;
        local node, host, resource = jid_split(to);
@@ -281,22 +283,28 @@ function core_route_stanza(origin, stanza)
                                        if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
                                                handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
                                        else -- sender is available or unavailable
-                                               for k in pairs(user.sessions) do -- presence broadcast to all user resources. FIXME should this be just for available resources? Do we need to check subscription?
-                                                       if user.sessions[k].full_jid then
-                                                               stanza.attr.to = user.sessions[k].full_jid; -- reset at the end of function
-                                                               user.sessions[k].send(stanza);
+                                               for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
+                                                       if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
+                                                               stanza.attr.to = session.full_jid; -- reset at the end of function
+                                                               session.send(stanza);
                                                        end
                                                end
                                        end
                                elseif stanza.name == "message" then -- select a resource to recieve message
-                                       for k in pairs(user.sessions) do
-                                               if user.sessions[k].full_jid then
-                                                       res = user.sessions[k];
-                                                       break;
+                                       local priority = 0;
+                                       local recipients = {};
+                                       for _, session in pairs(user.sessions) do -- find resource with greatest priority
+                                               local p = session.priority;
+                                               if p > priority then
+                                                       priority = p;
+                                                       recipients = {session};
+                                               elseif p == priority then
+                                                       t_insert(recipients, session);
                                                end
                                        end
-                                       -- TODO find resource with greatest priority
-                                       res.send(stanza);
+                                       for _, session in pairs(recipient) do
+                                               session.send(stanza);
+                                       end
                                else
                                        -- TODO send IQ error
                                end