Merge 0.6.2/waqas with 0.6.2/MattJ
[prosody.git] / plugins / mod_presence.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local log = module._log;
10
11 local require = require;
12 local pairs, ipairs = pairs, ipairs;
13 local t_concat, t_insert = table.concat, table.insert;
14 local s_find = string.find;
15 local tonumber = tonumber;
16
17 local st = require "util.stanza";
18 local jid_split = require "util.jid".split;
19 local jid_bare = require "util.jid".bare;
20 local hosts = hosts;
21 local NULL = {};
22
23 local rostermanager = require "core.rostermanager";
24 local sessionmanager = require "core.sessionmanager";
25 local offlinemanager = require "core.offlinemanager";
26
27 local _core_route_stanza = core_route_stanza;
28 local core_route_stanza;
29 function core_route_stanza(origin, stanza)
30         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
31                 local node, host = jid_split(stanza.attr.to);
32                 host = hosts[host];
33                 if node and host and host.type == "local" then
34                         handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
35                         return;
36                 end
37         end
38         _core_route_stanza(origin, stanza);
39 end
40
41 local select_top_resources;
42 local bare_message_delivery_policy = module:get_option("bare_message_delivery_policy") or "priority";
43 if bare_message_delivery_policy == "broadcast" then
44         function select_top_resources(user)
45                 local recipients = {};
46                 for _, session in pairs(user.sessions) do -- find resources with non-negative priority
47                         local p = session.priority;
48                         if p and p >= 0 then
49                                 t_insert(recipients, session);
50                         end
51                 end
52                 return recipients;
53         end
54 else
55         if bare_message_delivery_policy ~= "priority" then
56                 module:log("warn", "Invalid value for config option bare_message_delivery_policy");
57         end
58         function select_top_resources(user)
59                 local priority = 0;
60                 local recipients = {};
61                 for _, session in pairs(user.sessions) do -- find resource with greatest priority
62                         if session.presence then
63                                 -- TODO check active privacy list for session
64                                 local p = session.priority;
65                                 if p > priority then
66                                         priority = p;
67                                         recipients = {session};
68                                 elseif p == priority then
69                                         t_insert(recipients, session);
70                                 end
71                         end
72                 end
73                 return recipients;
74         end
75 end
76
77 local function recalc_resource_map(user)
78         if user then
79                 user.top_resources = select_top_resources(user);
80                 if #user.top_resources == 0 then user.top_resources = nil; end
81         end
82 end
83
84 function handle_normal_presence(origin, stanza, core_route_stanza)
85         if full_sessions[origin.full_jid] then -- if user is still connected
86                 origin.send(stanza); -- reflect their presence back to them
87         end
88         local roster = origin.roster;
89         local node, host = origin.username, origin.host;
90         local user = bare_sessions[node.."@"..host];
91         for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
92                 if res ~= origin and res.presence then -- to resource
93                         stanza.attr.to = res.full_jid;
94                         core_route_stanza(origin, stanza);
95                 end
96         end
97         for jid, item in pairs(roster) do -- broadcast to all interested contacts
98                 if item.subscription == "both" or item.subscription == "from" then
99                         stanza.attr.to = jid;
100                         core_route_stanza(origin, stanza);
101                 end
102         end
103         if stanza.attr.type == nil and not origin.presence then -- initial presence
104                 origin.presence = stanza; -- FIXME repeated later
105                 local probe = st.presence({from = origin.full_jid, type = "probe"});
106                 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to
107                         if item.subscription == "both" or item.subscription == "to" then
108                                 probe.attr.to = jid;
109                                 core_route_stanza(origin, probe);
110                         end
111                 end
112                 for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources
113                         if res ~= origin and res.presence then
114                                 res.presence.attr.to = origin.full_jid;
115                                 core_route_stanza(res, res.presence);
116                                 res.presence.attr.to = nil;
117                         end
118                 end
119                 if roster.pending then -- resend incoming subscription requests
120                         for jid in pairs(roster.pending) do
121                                 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
122                         end
123                 end
124                 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
125                 for jid, item in pairs(roster) do -- resend outgoing subscription requests
126                         if item.ask then
127                                 request.attr.to = jid;
128                                 core_route_stanza(origin, request);
129                         end
130                 end
131                 local offline = offlinemanager.load(node, host);
132                 if offline then
133                         for _, msg in ipairs(offline) do
134                                 origin.send(msg); -- FIXME do we need to modify to/from in any way?
135                         end
136                         offlinemanager.deleteAll(node, host);
137                 end
138         end
139         if stanza.attr.type == "unavailable" then
140                 origin.presence = nil;
141                 if origin.priority then
142                         origin.priority = nil;
143                         recalc_resource_map(user);
144                 end
145                 if origin.directed then
146                         for jid in pairs(origin.directed) do
147                                 stanza.attr.to = jid;
148                                 core_route_stanza(origin, stanza);
149                         end
150                         origin.directed = nil;
151                 end
152         else
153                 origin.presence = stanza;
154                 local priority = stanza:child_with_name("priority");
155                 if priority and #priority > 0 then
156                         priority = t_concat(priority);
157                         if s_find(priority, "^[+-]?[0-9]+$") then
158                                 priority = tonumber(priority);
159                                 if priority < -128 then priority = -128 end
160                                 if priority > 127 then priority = 127 end
161                         else priority = 0; end
162                 else priority = 0; end
163                 if origin.priority ~= priority then
164                         origin.priority = priority;
165                         recalc_resource_map(user);
166                 end
167         end
168         stanza.attr.to = nil; -- reset it
169 end
170
171 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza, stanza)
172         local h = hosts[host];
173         local count = 0;
174         if h and h.type == "local" then
175                 local u = h.sessions[user];
176                 if u then
177                         for k, session in pairs(u.sessions) do
178                                 local pres = session.presence;
179                                 if pres then
180                                         if stanza then pres = stanza; pres.attr.from = session.full_jid; end
181                                         pres.attr.to = jid;
182                                         core_route_stanza(session, pres);
183                                         pres.attr.to = nil;
184                                         count = count + 1;
185                                 end
186                         end
187                 end
188         end
189         log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
190         return count;
191 end
192
193 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
194         local node, host = jid_split(from_bare);
195         if to_bare == origin.username.."@"..origin.host then return; end -- No self contacts
196         local st_from, st_to = stanza.attr.from, stanza.attr.to;
197         stanza.attr.from, stanza.attr.to = from_bare, to_bare;
198         log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
199         if stanza.attr.type == "subscribe" then
200                 -- 1. route stanza
201                 -- 2. roster push (subscription = none, ask = subscribe)
202                 if rostermanager.set_contact_pending_out(node, host, to_bare) then
203                         rostermanager.roster_push(node, host, to_bare);
204                 end -- else file error
205                 core_route_stanza(origin, stanza);
206         elseif stanza.attr.type == "unsubscribe" then
207                 -- 1. route stanza
208                 -- 2. roster push (subscription = none or from)
209                 if rostermanager.unsubscribe(node, host, to_bare) then
210                         rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
211                 end -- else file error
212                 core_route_stanza(origin, stanza);
213         elseif stanza.attr.type == "subscribed" then
214                 -- 1. route stanza
215                 -- 2. roster_push ()
216                 -- 3. send_presence_of_available_resources
217                 if rostermanager.subscribed(node, host, to_bare) then
218                         rostermanager.roster_push(node, host, to_bare);
219                 end
220                 core_route_stanza(origin, stanza);
221                 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
222         elseif stanza.attr.type == "unsubscribed" then
223                 -- 1. route stanza
224                 -- 2. roster push (subscription = none or to)
225                 if rostermanager.unsubscribed(node, host, to_bare) then
226                         rostermanager.roster_push(node, host, to_bare);
227                 end
228                 core_route_stanza(origin, stanza);
229         end
230         stanza.attr.from, stanza.attr.to = st_from, st_to;
231 end
232
233 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
234         local node, host = jid_split(to_bare);
235         local st_from, st_to = stanza.attr.from, stanza.attr.to;
236         stanza.attr.from, stanza.attr.to = from_bare, to_bare;
237         log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
238         
239         if not node then
240                 log("debug", "dropping presence sent to host or invalid address '%s'", tostring(to_bare));
241         end
242         
243         if stanza.attr.type == "probe" then
244                 if rostermanager.is_contact_subscribed(node, host, from_bare) then
245                         if 0 == send_presence_of_available_resources(node, host, st_from, origin, core_route_stanza) then
246                                 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
247                         end
248                 else
249                         core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
250                 end
251         elseif stanza.attr.type == "subscribe" then
252                 if rostermanager.is_contact_subscribed(node, host, from_bare) then
253                         core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
254                         -- Sending presence is not clearly stated in the RFC, but it seems appropriate
255                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
256                                 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
257                         end
258                 else
259                         core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- acknowledging receipt
260                         if not rostermanager.is_contact_pending_in(node, host, from_bare) then
261                                 if rostermanager.set_contact_pending_in(node, host, from_bare) then
262                                         sessionmanager.send_to_available_resources(node, host, stanza);
263                                 end -- TODO else return error, unable to save
264                         end
265                 end
266         elseif stanza.attr.type == "unsubscribe" then
267                 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
268                         sessionmanager.send_to_interested_resources(node, host, stanza);
269                         rostermanager.roster_push(node, host, from_bare);
270                 end
271         elseif stanza.attr.type == "subscribed" then
272                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
273                         sessionmanager.send_to_interested_resources(node, host, stanza);
274                         rostermanager.roster_push(node, host, from_bare);
275                 end
276         elseif stanza.attr.type == "unsubscribed" then
277                 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
278                         sessionmanager.send_to_interested_resources(node, host, stanza);
279                         rostermanager.roster_push(node, host, from_bare);
280                 end
281         end -- discard any other type
282         stanza.attr.from, stanza.attr.to = st_from, st_to;
283 end
284
285 local outbound_presence_handler = function(data)
286         -- outbound presence recieved
287         local origin, stanza = data.origin, data.stanza;
288
289         local to = stanza.attr.to;
290         if to then
291                 local t = stanza.attr.type;
292                 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes
293                         handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
294                         return true;
295                 end
296
297                 local to_bare = jid_bare(to);
298                 if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
299                         origin.directed = origin.directed or {};
300                         if t then -- removing from directed presence list on sending an error or unavailable
301                                 origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?
302                         else
303                                 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
304                         end
305                 end
306         end -- TODO maybe handle normal presence here, instead of letting it pass to incoming handlers?
307 end
308
309 module:hook("pre-presence/full", outbound_presence_handler);
310 module:hook("pre-presence/bare", outbound_presence_handler);
311 module:hook("pre-presence/host", outbound_presence_handler);
312
313 module:hook("presence/bare", function(data)
314         -- inbound presence to bare JID recieved
315         local origin, stanza = data.origin, data.stanza;
316
317         local to = stanza.attr.to;
318         local t = stanza.attr.type;
319         if to then
320                 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID
321                         handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
322                         return true;
323                 end
324         
325                 local user = bare_sessions[to];
326                 if user then
327                         for _, session in pairs(user.sessions) do
328                                 if session.presence then -- only send to available resources
329                                         session.send(stanza);
330                                 end
331                         end
332                 end -- no resources not online, discard
333         elseif not t or t == "unavailable" then
334                 handle_normal_presence(origin, stanza, core_route_stanza);
335         end
336         return true;
337 end);
338 module:hook("presence/full", function(data)
339         -- inbound presence to full JID recieved
340         local origin, stanza = data.origin, data.stanza;
341
342         local t = stanza.attr.type;
343         if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
344                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
345                 return true;
346         end
347
348         local session = full_sessions[stanza.attr.to];
349         if session then
350                 -- TODO fire post processing event
351                 session.send(stanza);
352         end -- resource not online, discard
353         return true;
354 end);
355 module:hook("presence/host", function(data)
356         -- inbound presence to the host
357         local origin, stanza = data.origin, data.stanza;
358         
359         local from_bare = jid_bare(stanza.attr.from);
360         local t = stanza.attr.type;
361         if t == "probe" then
362                 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
363         elseif t == "subscribe" then
364                 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id, type = "subscribed" }));
365                 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
366         end
367         return true;
368 end);
369
370 module:hook("resource-unbind", function(event)
371         local session, err = event.session, event.error;
372         -- Send unavailable presence
373         if session.presence then
374                 local pres = st.presence{ type = "unavailable" };
375                 if not(err) or err == "closed" then err = "connection closed"; end
376                 pres:tag("status"):text("Disconnected: "..err):up();
377                 session:dispatch_stanza(pres);
378         elseif session.directed then
379                 local pres = st.presence{ type = "unavailable", from = session.full_jid };
380                 if not(err) or err == "closed" then err = "connection closed"; end
381                 pres:tag("status"):text("Disconnected: "..err):up();
382                 for jid in pairs(session.directed) do
383                         pres.attr.to = jid;
384                         core_route_stanza(session, pres);
385                 end
386                 session.directed = nil;
387         end
388 end);