prosody.cfg.lua.dist: Remove lie about requiring console_enabled
[prosody.git] / plugins / mod_presence.lua
index dbcf55d6d64a9159d363c9e48fe64d407485039a..abbc3a3da3473638f260182398162dc047a9cd2d 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,6 +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 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);
@@ -207,22 +209,28 @@ 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;
        log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
+       
+       if not node then
+               log("debug", "dropping presence sent to host or invalid address '%s'", tostring(to_bare));
+       end
+       
        if stanza.attr.type == "probe" then
                if rostermanager.is_contact_subscribed(node, host, from_bare) then
                        if 0 == send_presence_of_available_resources(node, host, st_from, origin, core_route_stanza) then
                                -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
                        end
                else
-                       core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
+                       core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
                end
        elseif stanza.attr.type == "subscribe" then
                if rostermanager.is_contact_subscribed(node, host, from_bare) then
-                       core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
+                       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, core_route_stanza) then
                                -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
                        end
                else
+                       core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- acknowledging receipt
                        if not rostermanager.is_contact_pending_in(node, host, from_bare) then
                                if rostermanager.set_contact_pending_in(node, host, from_bare) then
                                        sessionmanager.send_to_available_resources(node, host, stanza);
@@ -231,14 +239,17 @@ function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_b
                end
        elseif stanza.attr.type == "unsubscribe" then
                if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
+                       sessionmanager.send_to_interested_resources(node, host, stanza);
                        rostermanager.roster_push(node, host, from_bare);
                end
        elseif stanza.attr.type == "subscribed" then
                if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
+                       sessionmanager.send_to_interested_resources(node, host, stanza);
                        rostermanager.roster_push(node, host, from_bare);
                end
        elseif stanza.attr.type == "unsubscribed" then
                if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
+                       sessionmanager.send_to_interested_resources(node, host, stanza);
                        rostermanager.roster_push(node, host, from_bare);
                end
        end -- discard any other type
@@ -325,7 +336,7 @@ module:hook("resource-unbind", function(event)
                pres:tag("status"):text("Disconnected: "..err):up();
                session:dispatch_stanza(pres);
        elseif session.directed then
-               local pres = st.presence{ type = "unavailable" };
+               local pres = st.presence{ type = "unavailable", from = session.full_jid };
                if not(err) or err == "closed" then err = "connection closed"; end
                pres:tag("status"):text("Disconnected: "..err):up();
                for jid in pairs(session.directed) do