Annotate

core/presencemanager.lua @ 391:79bd7a3e906c

Typo prevented modulemanager.load() from returning the error if load failed.
author Matthew Wild <mwild1@gmail.com>
date Sun, 23 Nov 2008 05:14:37 +0000
parent 326:99a8317d1235
child 468:ab49cb6d0e92
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
322
d370fea67264 Fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents: 321
diff changeset
16 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
321
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;
323
7c9d2cabd245 Second fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents: 322
diff changeset
27 core_route_stanza(session, pres);
321
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
325
5de1048b3fbf Adding logging for outbound presence broadcasts (changed it a bit)
Waqas Hussain <waqas20@gmail.com>
parents: 324
diff changeset
35 log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 return count;
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 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
40 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
41 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
42 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
43 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
44 if stanza.attr.type == "subscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 -- 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
47 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
48 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
49 end -- else file error
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 elseif stanza.attr.type == "unsubscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 -- 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
54 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
55 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
56 end -- else file error
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 core_route_stanza(origin, stanza);
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 elseif stanza.attr.type == "subscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 -- 2. roster_push ()
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 -- 3. send_presence_of_available_resources
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 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
63 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
64 end
326
99a8317d1235 Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 325
diff changeset
65 core_route_stanza(origin, stanza);
99a8317d1235 Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 325
diff changeset
66 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 elseif stanza.attr.type == "unsubscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 -- 1. route stanza
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 -- 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
70 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
71 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
72 end
326
99a8317d1235 Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 325
diff changeset
73 core_route_stanza(origin, stanza);
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 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
76 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 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
79 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
80 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
81 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
82 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
83 if stanza.attr.type == "probe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 if rostermanager.is_contact_subscribed(node, host, from_bare) then
322
d370fea67264 Fix for broadcasting presence of available resources to newly approved contact
Waqas Hussain <waqas20@gmail.com>
parents: 321
diff changeset
85 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 -- 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
87 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 else
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 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
90 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 elseif stanza.attr.type == "subscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 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
93 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
326
99a8317d1235 Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 325
diff changeset
94 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
99a8317d1235 Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 325
diff changeset
95 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
99a8317d1235 Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 325
diff changeset
96 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
99a8317d1235 Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 325
diff changeset
97 end
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 else
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 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
100 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
101 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
102 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
103 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 elseif stanza.attr.type == "unsubscribe" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 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
107 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
108 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 elseif stanza.attr.type == "subscribed" then
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 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
111 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
112 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 elseif stanza.attr.type == "unsubscribed" then
326
99a8317d1235 Send unsubscribe and unsubscribed on roster item delete, and broadcast available resources' presence in reply to pre-approved subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 325
diff changeset
114 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
321
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 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
116 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 end -- discard any other type
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 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
119 end
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120
31fe15ce6fac Moved presence subscription code from stanza_router to presencemanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 return _M;