mod_presence: Reflect the user's own presence back to them.
[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 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 function handle_normal_presence(origin, stanza, core_route_stanza)
66         if full_sessions[origin.full_jid] then -- if user is still connected
67                 origin.send(stanza); -- reflect their presence back to them
68         end
69         local roster = origin.roster;
70         local node, host = origin.username, origin.host;
71         local user = bare_sessions[node.."@"..host];
72         for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
73                 if res ~= origin and res.presence then -- to resource
74                         stanza.attr.to = res.full_jid;
75                         core_route_stanza(origin, stanza);
76                 end
77         end
78         for jid, item in pairs(roster) do -- broadcast to all interested contacts
79                 if item.subscription == "both" or item.subscription == "from" then
80                         stanza.attr.to = jid;
81                         core_route_stanza(origin, stanza);
82                 end
83         end
84         if stanza.attr.type == nil and not origin.presence then -- initial presence
85                 origin.presence = stanza; -- FIXME repeated later
86                 local probe = st.presence({from = origin.full_jid, type = "probe"});
87                 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to
88                         if item.subscription == "both" or item.subscription == "to" then
89                                 probe.attr.to = jid;
90                                 core_route_stanza(origin, probe);
91                         end
92                 end
93                 for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources
94                         if res ~= origin and res.presence then
95                                 res.presence.attr.to = origin.full_jid;
96                                 core_route_stanza(res, res.presence);
97                                 res.presence.attr.to = nil;
98                         end
99                 end
100                 if roster.pending then -- resend incoming subscription requests
101                         for jid in pairs(roster.pending) do
102                                 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
103                         end
104                 end
105                 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
106                 for jid, item in pairs(roster) do -- resend outgoing subscription requests
107                         if item.ask then
108                                 request.attr.to = jid;
109                                 core_route_stanza(origin, request);
110                         end
111                 end
112                 local offline = offlinemanager.load(node, host);
113                 if offline then
114                         for _, msg in ipairs(offline) do
115                                 origin.send(msg); -- FIXME do we need to modify to/from in any way?
116                         end
117                         offlinemanager.deleteAll(node, host);
118                 end
119         end
120         if stanza.attr.type == "unavailable" then
121                 origin.presence = nil;
122                 if origin.priority then
123                         origin.priority = nil;
124                         recalc_resource_map(user);
125                 end
126                 if origin.directed then
127                         for jid in pairs(origin.directed) do
128                                 stanza.attr.to = jid;
129                                 core_route_stanza(origin, stanza);
130                         end
131                         origin.directed = nil;
132                 end
133         else
134                 origin.presence = stanza;
135                 local priority = stanza:child_with_name("priority");
136                 if priority and #priority > 0 then
137                         priority = t_concat(priority);
138                         if s_find(priority, "^[+-]?[0-9]+$") then
139                                 priority = tonumber(priority);
140                                 if priority < -128 then priority = -128 end
141                                 if priority > 127 then priority = 127 end
142                         else priority = 0; end
143                 else priority = 0; end
144                 if origin.priority ~= priority then
145                         origin.priority = priority;
146                         recalc_resource_map(user);
147                 end
148         end
149         stanza.attr.to = nil; -- reset it
150 end
151
152 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza, stanza)
153         local h = hosts[host];
154         local count = 0;
155         if h and h.type == "local" then
156                 local u = h.sessions[user];
157                 if u then
158                         for k, session in pairs(u.sessions) do
159                                 local pres = session.presence;
160                                 if pres then
161                                         if stanza then pres = stanza; pres.attr.from = session.full_jid; end
162                                         pres.attr.to = jid;
163                                         core_route_stanza(session, pres);
164                                         pres.attr.to = nil;
165                                         count = count + 1;
166                                 end
167                         end
168                 end
169         end
170         log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
171         return count;
172 end
173
174 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
175         local node, host = jid_split(from_bare);
176         if to_bare == origin.username.."@"..origin.host then return; end -- No self contacts
177         local st_from, st_to = stanza.attr.from, stanza.attr.to;
178         stanza.attr.from, stanza.attr.to = from_bare, to_bare;
179         log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
180         if 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_route_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_route_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_route_stanza(origin, stanza);
202                 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
203         elseif stanza.attr.type == "unsubscribed" then
204                 -- 1. route stanza
205                 -- 2. roster push (subscription = none or to)
206                 if rostermanager.unsubscribed(node, host, to_bare) then
207                         rostermanager.roster_push(node, host, to_bare);
208                 end
209                 core_route_stanza(origin, stanza);
210         end
211         stanza.attr.from, stanza.attr.to = st_from, st_to;
212 end
213
214 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
215         local node, host = jid_split(to_bare);
216         local st_from, st_to = stanza.attr.from, stanza.attr.to;
217         stanza.attr.from, stanza.attr.to = from_bare, to_bare;
218         log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
219         
220         if not node then
221                 log("debug", "dropping presence sent to host or invalid address '%s'", tostring(to_bare));
222         end
223         
224         if stanza.attr.type == "probe" then
225                 if rostermanager.is_contact_subscribed(node, host, from_bare) then
226                         if 0 == send_presence_of_available_resources(node, host, st_from, origin, core_route_stanza) then
227                                 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
228                         end
229                 else
230                         core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
231                 end
232         elseif stanza.attr.type == "subscribe" then
233                 if rostermanager.is_contact_subscribed(node, host, from_bare) then
234                         core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- 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, core_route_stanza) then
237                                 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
238                         end
239                 else
240                         core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- 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 end
265
266 local outbound_presence_handler = function(data)
267         -- outbound presence recieved
268         local origin, stanza = data.origin, data.stanza;
269
270         local to = stanza.attr.to;
271         if to then
272                 local t = stanza.attr.type;
273                 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes
274                         handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
275                         return true;
276                 end
277
278                 local to_bare = jid_bare(to);
279                 if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
280                         origin.directed = origin.directed or {};
281                         if t then -- removing from directed presence list on sending an error or unavailable
282                                 origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?
283                         else
284                                 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
285                         end
286                 end
287         end -- TODO maybe handle normal presence here, instead of letting it pass to incoming handlers?
288 end
289
290 module:hook("pre-presence/full", outbound_presence_handler);
291 module:hook("pre-presence/bare", outbound_presence_handler);
292 module:hook("pre-presence/host", outbound_presence_handler);
293
294 module:hook("presence/bare", function(data)
295         -- inbound presence to bare JID recieved
296         local origin, stanza = data.origin, data.stanza;
297
298         local to = stanza.attr.to;
299         local t = stanza.attr.type;
300         if to then
301                 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID
302                         handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
303                         return true;
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, core_route_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                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
326                 return true;
327         end
328
329         local session = full_sessions[stanza.attr.to];
330         if session then
331                 -- TODO fire post processing event
332                 session.send(stanza);
333         end -- resource not online, discard
334         return true;
335 end);
336 module:hook("presence/host", function(data)
337         -- inbound presence to the host
338         local origin, stanza = data.origin, data.stanza;
339         
340         local from_bare = jid_bare(stanza.attr.from);
341         local t = stanza.attr.type;
342         if t == "probe" then
343                 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
344         elseif t == "subscribe" then
345                 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id, type = "subscribed" }));
346                 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
347         end
348         return true;
349 end);
350
351 module:hook("resource-unbind", function(event)
352         local session, err = event.session, event.error;
353         -- Send unavailable presence
354         if session.presence then
355                 local pres = st.presence{ type = "unavailable" };
356                 if not(err) or err == "closed" then err = "connection closed"; end
357                 pres:tag("status"):text("Disconnected: "..err):up();
358                 session:dispatch_stanza(pres);
359         elseif session.directed then
360                 local pres = st.presence{ type = "unavailable", from = session.full_jid };
361                 if not(err) or err == "closed" then err = "connection closed"; end
362                 pres:tag("status"):text("Disconnected: "..err):up();
363                 for jid in pairs(session.directed) do
364                         pres.attr.to = jid;
365                         core_route_stanza(session, pres);
366                 end
367                 session.directed = nil;
368         end
369 end);