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