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