mod_admin_adhoc, mod_admin_telnet, mod_bosh, mod_c2s, mod_component, mod_pep, mod_pre...
[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 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 hosts = hosts;
22 local NULL = {};
23
24 local rostermanager = require "core.rostermanager";
25 local sessionmanager = require "core.sessionmanager";
26
27 local function select_top_resources(user)
28         local priority = 0;
29         local recipients = {};
30         for _, session in pairs(user.sessions) do -- find resource with greatest priority
31                 if session.presence then
32                         -- TODO check active privacy list for session
33                         local p = session.priority;
34                         if p > priority then
35                                 priority = p;
36                                 recipients = {session};
37                         elseif p == priority then
38                                 t_insert(recipients, session);
39                         end
40                 end
41         end
42         return recipients;
43 end
44 local function recalc_resource_map(user)
45         if user then
46                 user.top_resources = select_top_resources(user);
47                 if #user.top_resources == 0 then user.top_resources = nil; end
48         end
49 end
50
51 local ignore_presence_priority = module:get_option("ignore_presence_priority");
52
53 function handle_normal_presence(origin, stanza)
54         if ignore_presence_priority then
55                 local priority = stanza:child_with_name("priority");
56                 if priority and priority[1] ~= "0" then
57                         for i=#priority.tags,1,-1 do priority.tags[i] = nil; end
58                         for i=#priority,1,-1 do priority[i] = nil; end
59                         priority[1] = "0";
60                 end
61         end
62         local priority = stanza:child_with_name("priority");
63         if priority and #priority > 0 then
64                 priority = t_concat(priority);
65                 if s_find(priority, "^[+-]?[0-9]+$") then
66                         priority = tonumber(priority);
67                         if priority < -128 then priority = -128 end
68                         if priority > 127 then priority = 127 end
69                 else priority = 0; end
70         else priority = 0; end
71         if full_sessions[origin.full_jid] then -- if user is still connected
72                 origin.send(stanza); -- reflect their presence back to them
73         end
74         local roster = origin.roster;
75         local node, host = origin.username, origin.host;
76         local user = bare_sessions[node.."@"..host];
77         for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
78                 if res ~= origin and res.presence then -- to resource
79                         stanza.attr.to = res.full_jid;
80                         core_post_stanza(origin, stanza, true);
81                 end
82         end
83         for jid, item in pairs(roster) do -- broadcast to all interested contacts
84                 if item.subscription == "both" or item.subscription == "from" then
85                         stanza.attr.to = jid;
86                         core_post_stanza(origin, stanza, true);
87                 end
88         end
89         if stanza.attr.type == nil and not origin.presence then -- initial presence
90                 origin.presence = stanza; -- FIXME repeated later
91                 local probe = st.presence({from = origin.full_jid, type = "probe"});
92                 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to
93                         if item.subscription == "both" or item.subscription == "to" then
94                                 probe.attr.to = jid;
95                                 core_post_stanza(origin, probe, true);
96                         end
97                 end
98                 for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources
99                         if res ~= origin and res.presence then
100                                 res.presence.attr.to = origin.full_jid;
101                                 core_post_stanza(res, res.presence, true);
102                                 res.presence.attr.to = nil;
103                         end
104                 end
105                 if roster.pending then -- resend incoming subscription requests
106                         for jid in pairs(roster.pending) do
107                                 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
108                         end
109                 end
110                 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
111                 for jid, item in pairs(roster) do -- resend outgoing subscription requests
112                         if item.ask then
113                                 request.attr.to = jid;
114                                 core_post_stanza(origin, request, true);
115                         end
116                 end
117
118                 if priority >= 0 then
119                         local event = { origin = origin }
120                         module:fire_event('message/offline/broadcast', event);
121                 end
122         end
123         if stanza.attr.type == "unavailable" then
124                 origin.presence = nil;
125                 if origin.priority then
126                         origin.priority = nil;
127                         recalc_resource_map(user);
128                 end
129                 if origin.directed then
130                         for jid in pairs(origin.directed) do
131                                 stanza.attr.to = jid;
132                                 core_post_stanza(origin, stanza, true);
133                         end
134                         origin.directed = nil;
135                 end
136         else
137                 origin.presence = stanza;
138                 if origin.priority ~= priority then
139                         origin.priority = priority;
140                         recalc_resource_map(user);
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, 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_post_stanza(session, pres, true);
158                                         pres.attr.to = nil;
159                                         count = count + 1;
160                                 end
161                         end
162                 end
163         end
164         log("debug", "broadcasted presence of %d resources from %s@%s to %s", count, user, host, jid);
165         return count;
166 end
167
168 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
169         local node, host = jid_split(from_bare);
170         if to_bare == from_bare 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 %s from %s for %s", stanza.attr.type, from_bare, to_bare);
174         if stanza.attr.type == "probe" then
175                 stanza.attr.from, stanza.attr.to = st_from, st_to;
176                 return;
177         elseif stanza.attr.type == "subscribe" then
178                 -- 1. route stanza
179                 -- 2. roster push (subscription = none, ask = subscribe)
180                 if rostermanager.set_contact_pending_out(node, host, to_bare) then
181                         rostermanager.roster_push(node, host, to_bare);
182                 end -- else file error
183                 core_post_stanza(origin, stanza);
184         elseif stanza.attr.type == "unsubscribe" then
185                 -- 1. route stanza
186                 -- 2. roster push (subscription = none or from)
187                 if rostermanager.unsubscribe(node, host, to_bare) then
188                         rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
189                 end -- else file error
190                 core_post_stanza(origin, stanza);
191         elseif stanza.attr.type == "subscribed" then
192                 -- 1. route stanza
193                 -- 2. roster_push ()
194                 -- 3. send_presence_of_available_resources
195                 if rostermanager.subscribed(node, host, to_bare) then
196                         rostermanager.roster_push(node, host, to_bare);
197                 end
198                 core_post_stanza(origin, stanza);
199                 send_presence_of_available_resources(node, host, to_bare, origin);
200         elseif stanza.attr.type == "unsubscribed" then
201                 -- 1. route stanza
202                 -- 2. roster push (subscription = none or to)
203                 if rostermanager.unsubscribed(node, host, to_bare) then
204                         rostermanager.roster_push(node, host, to_bare);
205                 end
206                 core_post_stanza(origin, stanza);
207         else
208                 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid presence type"));
209         end
210         stanza.attr.from, stanza.attr.to = st_from, st_to;
211         return true;
212 end
213
214 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
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 %s from %s for %s", stanza.attr.type, from_bare, to_bare);
219         
220         if stanza.attr.type == "probe" then
221                 local result, err = rostermanager.is_contact_subscribed(node, host, from_bare);
222                 if result then
223                         if 0 == send_presence_of_available_resources(node, host, st_from, origin) then
224                                 core_post_stanza(hosts[host], st.presence({from=to_bare, to=st_from, type="unavailable"}), true); -- TODO send last activity
225                         end
226                 elseif not err then
227                         core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"}), true);
228                 end
229         elseif stanza.attr.type == "subscribe" then
230                 if rostermanager.is_contact_subscribed(node, host, from_bare) then
231                         core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"}), true); -- already subscribed
232                         -- Sending presence is not clearly stated in the RFC, but it seems appropriate
233                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
234                                 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"}), true); -- TODO send last activity
235                         end
236                 else
237                         core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"}), true); -- acknowledging receipt
238                         if not rostermanager.is_contact_pending_in(node, host, from_bare) then
239                                 if rostermanager.set_contact_pending_in(node, host, from_bare) then
240                                         sessionmanager.send_to_available_resources(node, host, stanza);
241                                 end -- TODO else return error, unable to save
242                         end
243                 end
244         elseif stanza.attr.type == "unsubscribe" then
245                 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
246                         sessionmanager.send_to_interested_resources(node, host, stanza);
247                         rostermanager.roster_push(node, host, from_bare);
248                 end
249         elseif stanza.attr.type == "subscribed" then
250                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
251                         sessionmanager.send_to_interested_resources(node, host, stanza);
252                         rostermanager.roster_push(node, host, from_bare);
253                 end
254         elseif stanza.attr.type == "unsubscribed" then
255                 if rostermanager.process_inbound_subscription_cancellation(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         else
260                 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid presence type"));
261         end
262         stanza.attr.from, stanza.attr.to = st_from, st_to;
263         return true;
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                         return handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
275                 end
276
277                 local to_bare = jid_bare(to);
278                 local roster = origin.roster;
279                 if roster and not(roster[to_bare] and (roster[to_bare].subscription == "both" or 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                         return handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
303                 end
304         
305                 local user = bare_sessions[to];
306                 if user then
307                         for _, session in pairs(user.sessions) do
308                                 if session.presence then -- only send to available resources
309                                         session.send(stanza);
310                                 end
311                         end
312                 end -- no resources not online, discard
313         elseif not t or t == "unavailable" then
314                 handle_normal_presence(origin, stanza);
315         else
316                 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid presence type"));
317         end
318         return true;
319 end);
320 module:hook("presence/full", function(data)
321         -- inbound presence to full JID recieved
322         local origin, stanza = data.origin, data.stanza;
323
324         local t = stanza.attr.type;
325         if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
326                 return handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
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_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
344         elseif t == "subscribe" then
345                 core_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id, type = "subscribed" }));
346                 core_post_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 err then
357                         pres:tag("status"):text("Disconnected: "..err):up();
358                 end
359                 session:dispatch_stanza(pres);
360         elseif session.directed then
361                 local pres = st.presence{ type = "unavailable", from = session.full_jid };
362                 if err then
363                         pres:tag("status"):text("Disconnected: "..err):up();
364                 end
365                 for jid in pairs(session.directed) do
366                         pres.attr.to = jid;
367                         core_post_stanza(session, pres, true);
368                 end
369                 session.directed = nil;
370         end
371 end);