Comparison

core/presencemanager.lua @ 321:31fe15ce6fac

Moved presence subscription code from stanza_router to presencemanager
author Waqas Hussain <waqas20@gmail.com>
date Mon, 17 Nov 2008 10:15:04 +0500
child 322:d370fea67264
comparison
equal deleted inserted replaced
320:b7a24b0ce767 321:31fe15ce6fac
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)
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 recipient_session.send(pres);
28 pres.attr.to = nil;
29 pres.attr.from = nil;
30 count = count + 1;
31 end
32 end
33 end
34 end
35 return count;
36 end
37
38 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
39 local node, host = jid_split(from_bare);
40 local st_from, st_to = stanza.attr.from, stanza.attr.to;
41 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
42 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
43 if stanza.attr.type == "subscribe" then
44 -- 1. route stanza
45 -- 2. roster push (subscription = none, ask = subscribe)
46 if rostermanager.set_contact_pending_out(node, host, to_bare) then
47 rostermanager.roster_push(node, host, to_bare);
48 end -- else file error
49 core_route_stanza(origin, stanza);
50 elseif stanza.attr.type == "unsubscribe" then
51 -- 1. route stanza
52 -- 2. roster push (subscription = none or from)
53 if rostermanager.unsubscribe(node, host, to_bare) then
54 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
55 end -- else file error
56 core_route_stanza(origin, stanza);
57 elseif stanza.attr.type == "subscribed" then
58 -- 1. route stanza
59 -- 2. roster_push ()
60 -- 3. send_presence_of_available_resources
61 if rostermanager.subscribed(node, host, to_bare) then
62 rostermanager.roster_push(node, host, to_bare);
63 core_route_stanza(origin, stanza);
64 send_presence_of_available_resources(node, host, to_bare, origin);
65 end
66 elseif stanza.attr.type == "unsubscribed" then
67 -- 1. route stanza
68 -- 2. roster push (subscription = none or to)
69 if rostermanager.unsubscribed(node, host, to_bare) then
70 rostermanager.roster_push(node, host, to_bare);
71 core_route_stanza(origin, stanza);
72 end
73 end
74 stanza.attr.from, stanza.attr.to = st_from, st_to;
75 end
76
77 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
78 local node, host = jid_split(to_bare);
79 local st_from, st_to = stanza.attr.from, stanza.attr.to;
80 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
81 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
82 if stanza.attr.type == "probe" then
83 if rostermanager.is_contact_subscribed(node, host, from_bare) then
84 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
85 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
86 end
87 else
88 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
89 end
90 elseif stanza.attr.type == "subscribe" then
91 if rostermanager.is_contact_subscribed(node, host, from_bare) then
92 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
93 else
94 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
95 if rostermanager.set_contact_pending_in(node, host, from_bare) then
96 sessionmanager.send_to_available_resources(node, host, stanza);
97 end -- TODO else return error, unable to save
98 end
99 end
100 elseif stanza.attr.type == "unsubscribe" then
101 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
102 rostermanager.roster_push(node, host, from_bare);
103 end
104 elseif stanza.attr.type == "subscribed" then
105 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
106 rostermanager.roster_push(node, host, from_bare);
107 end
108 elseif stanza.attr.type == "unsubscribed" then
109 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
110 rostermanager.roster_push(node, host, from_bare);
111 end
112 end -- discard any other type
113 stanza.attr.from, stanza.attr.to = st_from, st_to;
114 end
115
116 return _M;