mod_tls: Fixed an extra :up() in s2s stream feature generation.
[prosody.git] / plugins / mod_presence.lua
index a077d7bd20471227b0771abc2d025b171b4492c7..f6ea9e6b7b36cd91298b651e42ae07bfb43fff9b 100644 (file)
@@ -18,6 +18,7 @@ local st = require "util.stanza";
 local jid_split = require "util.jid".split;
 local jid_bare = require "util.jid".bare;
 local hosts = hosts;
+local NULL = {};
 
 local rostermanager = require "core.rostermanager";
 local sessionmanager = require "core.sessionmanager";
@@ -29,7 +30,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
@@ -54,16 +55,18 @@ local function select_top_resources(user)
        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)
        local roster = origin.roster;
        local node, host = origin.username, origin.host;
-       for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
+       local user = bare_sessions[node.."@"..host];
+       for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
                if res ~= origin and res.presence then -- to resource
                        stanza.attr.to = res.full_jid;
                        core_route_stanza(origin, stanza);
@@ -76,6 +79,7 @@ function handle_normal_presence(origin, stanza, core_route_stanza)
                end
        end
        if stanza.attr.type == nil and not origin.presence then -- initial presence
+               origin.presence = stanza; -- FIXME repeated later
                local probe = st.presence({from = origin.full_jid, type = "probe"});
                for jid, item in pairs(roster) do -- probe all contacts we are subscribed to
                        if item.subscription == "both" or item.subscription == "to" then
@@ -83,7 +87,7 @@ function handle_normal_presence(origin, stanza, core_route_stanza)
                                core_route_stanza(origin, probe);
                        end
                end
-               for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
+               for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources
                        if res ~= origin and res.presence then
                                res.presence.attr.to = origin.full_jid;
                                core_route_stanza(res, res.presence);
@@ -114,7 +118,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
@@ -136,13 +140,13 @@ 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
 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 +155,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 +170,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);
@@ -215,19 +221,20 @@ function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_b
        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)
+                               core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
                        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)
+                               core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
                        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);
@@ -236,14 +243,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
@@ -320,6 +330,20 @@ module:hook("presence/full", function(data)
        end -- resource not online, discard
        return true;
 end);
+module:hook("presence/host", function(data)
+       -- inbound presence to the host
+       local origin, stanza = data.origin, data.stanza;
+       
+       local from_bare = jid_bare(stanza.attr.from);
+       local t = stanza.attr.type;
+       if t == "probe" then
+               core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
+       elseif t == "subscribe" then
+               core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id, type = "subscribed" }));
+               core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
+       end
+       return true;
+end);
 
 module:hook("resource-unbind", function(event)
        local session, err = event.session, event.error;