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