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