Merge with trunk
[prosody.git] / plugins / mod_presence.lua
index 02ec6f7949459e6f720b674c6d8d377d70d6927f..f83e017b3b54c631bc92b622a58bc4989f590a89 100644 (file)
@@ -29,7 +29,7 @@ function core_route_stanza(origin, stanza)
        if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
                local node, host = jid_split(stanza.attr.to);
                host = hosts[host];
-               if host and host.type == "local" then
+               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), core_route_stanza);
                        return;
                end
@@ -142,7 +142,7 @@ function handle_normal_presence(origin, stanza, core_route_stanza)
        stanza.attr.to = nil; -- reset it
 end
 
-function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_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
@@ -151,6 +151,7 @@ function send_presence_of_available_resources(user, host, jid, recipient_session
                        for k, session in pairs(u.sessions) do
                                local pres = session.presence;
                                if pres then
+                                       if stanza then pres = stanza; pres.attr.from = session.full_jid; end
                                        pres.attr.to = jid;
                                        core_route_stanza(session, pres);
                                        pres.attr.to = nil;
@@ -165,7 +166,7 @@ end
 
 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
        local node, host = jid_split(from_bare);
-       if node == origin.username and host == origin.host then return; end -- No self contacts
+       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);
@@ -199,6 +200,9 @@ function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_
                        rostermanager.roster_push(node, host, to_bare);
                end
                core_route_stanza(origin, stanza);
+               -- COMPAT: Some legacy clients keep displaying unsubscribed contacts as online unless an unavailable presence is sent:
+               send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza,
+                       st.presence({ type="unavailable", from=from_bare, to=to_bare, id=stanza.attr.id }));
        end
        stanza.attr.from, stanza.attr.to = st_from, st_to;
 end