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