Comparison

core/presencemanager.lua @ 403:da92afa267cf

Merging with main branch.
author Tobias Markmann <tm@ayena.de>
date Sun, 23 Nov 2008 20:44:48 +0100
parent 326:99a8317d1235
child 468:ab49cb6d0e92
comparison
equal deleted inserted replaced
402:50f1c09541cd 403:da92afa267cf
1
2 local log = require "util.logger".init("presencemanager")
3
4 local require = require;
5 local pairs = pairs;
6
7 local st = require "util.stanza";
8 local jid_split = require "util.jid".split;
9 local hosts = hosts;
10
11 local rostermanager = require "core.rostermanager";
12 local sessionmanager = require "core.sessionmanager";
13
14 module "presencemanager"
15
16 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
17 local h = hosts[host];
18 local count = 0;
19 if h and h.type == "local" then
20 local u = h.sessions[user];
21 if u then
22 for k, session in pairs(u.sessions) do
23 local pres = session.presence;
24 if pres then
25 pres.attr.to = jid;
26 pres.attr.from = session.full_jid;
27 core_route_stanza(session, pres);
28 pres.attr.to = nil;
29 pres.attr.from = nil;
30 count = count + 1;
31 end
32 end
33 end
34 end
35 log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
36 return count;
37 end
38
39 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
40 local node, host = jid_split(from_bare);
41 local st_from, st_to = stanza.attr.from, stanza.attr.to;
42 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
43 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
44 if stanza.attr.type == "subscribe" then
45 -- 1. route stanza
46 -- 2. roster push (subscription = none, ask = subscribe)
47 if rostermanager.set_contact_pending_out(node, host, to_bare) then
48 rostermanager.roster_push(node, host, to_bare);
49 end -- else file error
50 core_route_stanza(origin, stanza);
51 elseif stanza.attr.type == "unsubscribe" then
52 -- 1. route stanza
53 -- 2. roster push (subscription = none or from)
54 if rostermanager.unsubscribe(node, host, to_bare) then
55 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
56 end -- else file error
57 core_route_stanza(origin, stanza);
58 elseif stanza.attr.type == "subscribed" then
59 -- 1. route stanza
60 -- 2. roster_push ()
61 -- 3. send_presence_of_available_resources
62 if rostermanager.subscribed(node, host, to_bare) then
63 rostermanager.roster_push(node, host, to_bare);
64 end
65 core_route_stanza(origin, stanza);
66 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
67 elseif stanza.attr.type == "unsubscribed" then
68 -- 1. route stanza
69 -- 2. roster push (subscription = none or to)
70 if rostermanager.unsubscribed(node, host, to_bare) then
71 rostermanager.roster_push(node, host, to_bare);
72 end
73 core_route_stanza(origin, stanza);
74 end
75 stanza.attr.from, stanza.attr.to = st_from, st_to;
76 end
77
78 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
79 local node, host = jid_split(to_bare);
80 local st_from, st_to = stanza.attr.from, stanza.attr.to;
81 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
82 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
83 if stanza.attr.type == "probe" then
84 if rostermanager.is_contact_subscribed(node, host, from_bare) then
85 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
86 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
87 end
88 else
89 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
90 end
91 elseif stanza.attr.type == "subscribe" then
92 if rostermanager.is_contact_subscribed(node, host, from_bare) then
93 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
94 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
95 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
96 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
97 end
98 else
99 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
100 if rostermanager.set_contact_pending_in(node, host, from_bare) then
101 sessionmanager.send_to_available_resources(node, host, stanza);
102 end -- TODO else return error, unable to save
103 end
104 end
105 elseif stanza.attr.type == "unsubscribe" then
106 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
107 rostermanager.roster_push(node, host, from_bare);
108 end
109 elseif stanza.attr.type == "subscribed" then
110 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
111 rostermanager.roster_push(node, host, from_bare);
112 end
113 elseif stanza.attr.type == "unsubscribed" then
114 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
115 rostermanager.roster_push(node, host, from_bare);
116 end
117 end -- discard any other type
118 stanza.attr.from, stanza.attr.to = st_from, st_to;
119 end
120
121 return _M;