2 local log = require "util.logger".init("presencemanager")
\r
4 local require = require;
\r
7 local st = require "util.stanza";
\r
8 local jid_split = require "util.jid".split;
\r
11 local rostermanager = require "core.rostermanager";
\r
12 local sessionmanager = require "core.sessionmanager";
\r
14 module "presencemanager"
\r
16 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
\r
17 local h = hosts[host];
\r
19 if h and h.type == "local" then
\r
20 local u = h.sessions[user];
\r
22 for k, session in pairs(u.sessions) do
\r
23 local pres = session.presence;
\r
26 pres.attr.from = session.full_jid;
\r
27 core_route_stanza(session, pres);
\r
29 pres.attr.from = nil;
\r
35 log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
\r
39 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
\r
40 local node, host = jid_split(from_bare);
\r
41 local st_from, st_to = stanza.attr.from, stanza.attr.to;
\r
42 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
\r
43 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
\r
44 if stanza.attr.type == "subscribe" then
\r
46 -- 2. roster push (subscription = none, ask = subscribe)
\r
47 if rostermanager.set_contact_pending_out(node, host, to_bare) then
\r
48 rostermanager.roster_push(node, host, to_bare);
\r
49 end -- else file error
\r
50 core_route_stanza(origin, stanza);
\r
51 elseif stanza.attr.type == "unsubscribe" then
\r
53 -- 2. roster push (subscription = none or from)
\r
54 if rostermanager.unsubscribe(node, host, to_bare) then
\r
55 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
\r
56 end -- else file error
\r
57 core_route_stanza(origin, stanza);
\r
58 elseif stanza.attr.type == "subscribed" then
\r
60 -- 2. roster_push ()
\r
61 -- 3. send_presence_of_available_resources
\r
62 if rostermanager.subscribed(node, host, to_bare) then
\r
63 rostermanager.roster_push(node, host, to_bare);
\r
65 core_route_stanza(origin, stanza);
\r
66 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
\r
67 elseif stanza.attr.type == "unsubscribed" then
\r
69 -- 2. roster push (subscription = none or to)
\r
70 if rostermanager.unsubscribed(node, host, to_bare) then
\r
71 rostermanager.roster_push(node, host, to_bare);
\r
73 core_route_stanza(origin, stanza);
\r
75 stanza.attr.from, stanza.attr.to = st_from, st_to;
\r
78 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
\r
79 local node, host = jid_split(to_bare);
\r
80 local st_from, st_to = stanza.attr.from, stanza.attr.to;
\r
81 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
\r
82 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
\r
83 if stanza.attr.type == "probe" then
\r
84 if rostermanager.is_contact_subscribed(node, host, from_bare) then
\r
85 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
\r
86 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
\r
89 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
\r
91 elseif stanza.attr.type == "subscribe" then
\r
92 if rostermanager.is_contact_subscribed(node, host, from_bare) then
\r
93 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
\r
94 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
\r
95 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
\r
96 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
\r
99 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
\r
100 if rostermanager.set_contact_pending_in(node, host, from_bare) then
\r
101 sessionmanager.send_to_available_resources(node, host, stanza);
\r
102 end -- TODO else return error, unable to save
\r
105 elseif stanza.attr.type == "unsubscribe" then
\r
106 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
\r
107 rostermanager.roster_push(node, host, from_bare);
\r
109 elseif stanza.attr.type == "subscribed" then
\r
110 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
\r
111 rostermanager.roster_push(node, host, from_bare);
\r
113 elseif stanza.attr.type == "unsubscribed" then
\r
114 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
\r
115 rostermanager.roster_push(node, host, from_bare);
\r
117 end -- discard any other type
\r
118 stanza.attr.from, stanza.attr.to = st_from, st_to;
\r