Annotate

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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local log = require "util.logger".init("presencemanager")
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local require = require;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local pairs = pairs;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 local st = require "util.stanza";
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 local jid_split = require "util.jid".split;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local hosts = hosts;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local rostermanager = require "core.rostermanager";
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local sessionmanager = require "core.sessionmanager";
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 module "presencemanager"
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 function send_presence_of_available_resources(user, host, jid, recipient_session)
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local h = hosts[host];
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local count = 0;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 if h and h.type == "local" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local u = h.sessions[user];
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 if u then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 for k, session in pairs(u.sessions) do
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local pres = session.presence;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 if pres then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 pres.attr.to = jid;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 pres.attr.from = session.full_jid;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 recipient_session.send(pres);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 pres.attr.to = nil;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 pres.attr.from = nil;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 count = count + 1;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 return count;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 local node, host = jid_split(from_bare);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 local st_from, st_to = stanza.attr.from, stanza.attr.to;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 if stanza.attr.type == "subscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 -- 2. roster push (subscription = none, ask = subscribe)
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 if rostermanager.set_contact_pending_out(node, host, to_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 rostermanager.roster_push(node, host, to_bare);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 end -- else file error
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 elseif stanza.attr.type == "unsubscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 -- 2. roster push (subscription = none or from)
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 if rostermanager.unsubscribe(node, host, to_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 end -- else file error
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 elseif stanza.attr.type == "subscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 -- 2. roster_push ()
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 -- 3. send_presence_of_available_resources
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 if rostermanager.subscribed(node, host, to_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 rostermanager.roster_push(node, host, to_bare);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 send_presence_of_available_resources(node, host, to_bare, origin);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 elseif stanza.attr.type == "unsubscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 -- 2. roster push (subscription = none or to)
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 if rostermanager.unsubscribed(node, host, to_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 rostermanager.roster_push(node, host, to_bare);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 stanza.attr.from, stanza.attr.to = st_from, st_to;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 local node, host = jid_split(to_bare);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 local st_from, st_to = stanza.attr.from, stanza.attr.to;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 if stanza.attr.type == "probe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 if rostermanager.is_contact_subscribed(node, host, from_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 else
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 elseif stanza.attr.type == "subscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 if rostermanager.is_contact_subscribed(node, host, from_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 else
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 if rostermanager.set_contact_pending_in(node, host, from_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 sessionmanager.send_to_available_resources(node, host, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 end -- TODO else return error, unable to save
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 elseif stanza.attr.type == "unsubscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 rostermanager.roster_push(node, host, from_bare);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 elseif stanza.attr.type == "subscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 rostermanager.roster_push(node, host, from_bare);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 elseif stanza.attr.type == "unsubscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 rostermanager.roster_push(node, host, from_bare);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 end -- discard any other type
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 stanza.attr.from, stanza.attr.to = st_from, st_to;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 return _M;