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