util.sasl_cyrus: Protect the call to cyrussasl.server_new properly.
[prosody.git] / plugins / mod_presence.lua
index 4aa8f497f52523737edfdd570e2e3ab2846555bd..648c78b3f046e1106e07c43ac42bda873ab81071 100644 (file)
@@ -38,30 +38,53 @@ function core_route_stanza(origin, stanza)
        _core_route_stanza(origin, stanza);
 end
 
-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 select_top_resources;
+local bare_message_delivery_policy = module:get_option("bare_message_delivery_policy") or "priority";
+if bare_message_delivery_policy == "broadcast" then
+       function select_top_resources(user)
+               local recipients = {};
+               for _, session in pairs(user.sessions) do -- find resources with non-negative priority
                        local p = session.priority;
-                       if p > priority then
-                               priority = p;
-                               recipients = {session};
-                       elseif p == priority then
+                       if p and p >= 0 then
                                t_insert(recipients, session);
                        end
                end
+               return recipients;
+       end
+else
+       if bare_message_delivery_policy ~= "priority" then
+               module:log("warn", "Invalid value for config option bare_message_delivery_policy");
+       end
+       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
-       return recipients;
 end
-local function recalc_resource_map(origin)
-       local user = hosts[origin.host].sessions[origin.username];
-       user.top_resources = select_top_resources(user);
-       if #user.top_resources == 0 then user.top_resources = nil; 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
 
 function handle_normal_presence(origin, stanza, core_route_stanza)
+       if full_sessions[origin.full_jid] then -- if user is still connected
+               origin.send(stanza); -- reflect their presence back to them
+       end
        local roster = origin.roster;
        local node, host = origin.username, origin.host;
        local user = bare_sessions[node.."@"..host];
@@ -117,7 +140,7 @@ function handle_normal_presence(origin, stanza, core_route_stanza)
                origin.presence = nil;
                if origin.priority then
                        origin.priority = nil;
-                       recalc_resource_map(origin);
+                       recalc_resource_map(user);
                end
                if origin.directed then
                        for jid in pairs(origin.directed) do
@@ -139,7 +162,7 @@ function handle_normal_presence(origin, stanza, core_route_stanza)
                else priority = 0; end
                if origin.priority ~= priority then
                        origin.priority = priority;
-                       recalc_resource_map(origin);
+                       recalc_resource_map(user);
                end
        end
        stanza.attr.to = nil; -- reset it