mod_presence: Send unavailable presence in response to probes to the sender's full...
authorWaqas Hussain <waqas20@gmail.com>
Thu, 3 Jun 2010 13:01:05 +0000 (18:01 +0500)
committerWaqas Hussain <waqas20@gmail.com>
Thu, 3 Jun 2010 13:01:05 +0000 (18:01 +0500)
plugins/mod_presence.lua

index 6da0f8639f65cc42418bc885e4538dd86aedca2e..4fb8c3e4ec8323cebab4ac0a5e1e2cb893a88bc3 100644 (file)
@@ -31,7 +31,7 @@ function core_route_stanza(origin, stanza)
                local node, host = jid_split(stanza.attr.to);
                host = hosts[host];
                if node and host and host.type == "local" then
-                       handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
+                       handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
                        return;
                end
        end
@@ -64,7 +64,7 @@ end
 
 local ignore_presence_priority = module:get_option("ignore_presence_priority");
 
-function handle_normal_presence(origin, stanza)
+function handle_normal_presence(origin, stanza, core_route_stanza)
        if ignore_presence_priority then
                local priority = stanza:child_with_name("priority");
                if priority and priority[1] ~= "0" then
@@ -97,7 +97,7 @@ function handle_normal_presence(origin, stanza)
                for jid, item in pairs(roster) do -- probe all contacts we are subscribed to
                        if item.subscription == "both" or item.subscription == "to" then
                                probe.attr.to = jid;
-                               core_post_stanza(origin, probe, true);
+                               core_route_stanza(origin, probe);
                        end
                end
                for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources
@@ -159,7 +159,7 @@ function handle_normal_presence(origin, stanza)
        stanza.attr.to = nil; -- reset it
 end
 
-function send_presence_of_available_resources(user, host, jid, recipient_session, stanza)
+function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza, stanza)
        local h = hosts[host];
        local count = 0;
        if h and h.type == "local" then
@@ -181,16 +181,13 @@ function send_presence_of_available_resources(user, host, jid, recipient_session
        return count;
 end
 
-function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
+function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
        local node, host = jid_split(from_bare);
        if to_bare == origin.username.."@"..origin.host 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);
-       if stanza.attr.type == "probe" then
-               stanza.attr.from, stanza.attr.to = st_from, st_to;
-               return;
-       elseif stanza.attr.type == "subscribe" then
+       if stanza.attr.type == "subscribe" then
                -- 1. route stanza
                -- 2. roster push (subscription = none, ask = subscribe)
                if rostermanager.set_contact_pending_out(node, host, to_bare) then
@@ -212,7 +209,7 @@ function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_
                        rostermanager.roster_push(node, host, to_bare);
                end
                core_route_stanza(origin, stanza);
-               send_presence_of_available_resources(node, host, to_bare, origin);
+               send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
        elseif stanza.attr.type == "unsubscribed" then
                -- 1. route stanza
                -- 2. roster push (subscription = none or to)
@@ -222,10 +219,9 @@ function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_
                core_route_stanza(origin, stanza);
        end
        stanza.attr.from, stanza.attr.to = st_from, st_to;
-       return true;
 end
 
-function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
+function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
        local node, host = jid_split(to_bare);
        local st_from, st_to = stanza.attr.from, stanza.attr.to;
        stanza.attr.from, stanza.attr.to = from_bare, to_bare;
@@ -234,7 +230,7 @@ function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_b
        if stanza.attr.type == "probe" then
                local result, err = rostermanager.is_contact_subscribed(node, host, from_bare);
                if result then
-                       if 0 == send_presence_of_available_resources(node, host, st_from, origin) then
+                       if 0 == send_presence_of_available_resources(node, host, st_from, origin, core_route_stanza) then
                                core_route_stanza(hosts[host], st.presence({from=to_bare, to=st_from, type="unavailable"})); -- TODO send last activity
                        end
                elseif not err then
@@ -244,7 +240,7 @@ function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_b
                if rostermanager.is_contact_subscribed(node, host, from_bare) then
                        core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
                        -- Sending presence is not clearly stated in the RFC, but it seems appropriate
-                       if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
+                       if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
                                core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
                        end
                else
@@ -272,7 +268,6 @@ function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_b
                end
        end -- discard any other type
        stanza.attr.from, stanza.attr.to = st_from, st_to;
-       return true;
 end
 
 local outbound_presence_handler = function(data)
@@ -283,7 +278,8 @@ local outbound_presence_handler = function(data)
        if to then
                local t = stanza.attr.type;
                if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes
-                       return handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
+                       handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
+                       return true;
                end
 
                local to_bare = jid_bare(to);
@@ -310,7 +306,8 @@ module:hook("presence/bare", function(data)
        local t = stanza.attr.type;
        if to then
                if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID
-                       return handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
+                       handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
+                       return true;
                end
        
                local user = bare_sessions[to];
@@ -322,7 +319,7 @@ module:hook("presence/bare", function(data)
                        end
                end -- no resources not online, discard
        elseif not t or t == "unavailable" then
-               handle_normal_presence(origin, stanza);
+               handle_normal_presence(origin, stanza, core_route_stanza);
        end
        return true;
 end);
@@ -332,7 +329,8 @@ module:hook("presence/full", function(data)
 
        local t = stanza.attr.type;
        if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
-               return handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
+               handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
+               return true;
        end
 
        local session = full_sessions[stanza.attr.to];