2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local log = module._log;
11 local require = require;
13 local t_concat, t_insert = table.concat, table.insert;
14 local s_find = string.find;
15 local tonumber = tonumber;
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 datetime = require "util.datetime";
22 local hosts = prosody.hosts;
23 local bare_sessions = prosody.bare_sessions;
24 local full_sessions = prosody.full_sessions;
27 local rostermanager = require "core.rostermanager";
28 local sessionmanager = require "core.sessionmanager";
30 local function select_top_resources(user)
32 local recipients = {};
33 for _, session in pairs(user.sessions) do -- find resource with greatest priority
34 if session.presence then
35 -- TODO check active privacy list for session
36 local p = session.priority;
39 recipients = {session};
40 elseif p == priority then
41 t_insert(recipients, session);
47 local function recalc_resource_map(user)
49 user.top_resources = select_top_resources(user);
50 if #user.top_resources == 0 then user.top_resources = nil; end
54 local ignore_presence_priority = module:get_option("ignore_presence_priority");
56 function handle_normal_presence(origin, stanza)
57 if ignore_presence_priority then
58 local priority = stanza:child_with_name("priority");
59 if priority and priority[1] ~= "0" then
60 for i=#priority.tags,1,-1 do priority.tags[i] = nil; end
61 for i=#priority,1,-1 do priority[i] = nil; end
65 local priority = stanza:child_with_name("priority");
66 if priority and #priority > 0 then
67 priority = t_concat(priority);
68 if s_find(priority, "^[+-]?[0-9]+$") then
69 priority = tonumber(priority);
70 if priority < -128 then priority = -128 end
71 if priority > 127 then priority = 127 end
72 else priority = 0; end
73 else priority = 0; end
74 if full_sessions[origin.full_jid] then -- if user is still connected
75 origin.send(stanza); -- reflect their presence back to them
77 local roster = origin.roster;
78 local node, host = origin.username, origin.host;
79 local user = bare_sessions[node.."@"..host];
80 for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
81 if res ~= origin and res.presence then -- to resource
82 stanza.attr.to = res.full_jid;
83 core_post_stanza(origin, stanza, true);
86 for jid, item in pairs(roster) do -- broadcast to all interested contacts
87 if item.subscription == "both" or item.subscription == "from" then
89 core_post_stanza(origin, stanza, true);
92 if stanza.attr.type == nil and not origin.presence then -- initial presence
93 origin.presence = stanza; -- FIXME repeated later
94 local probe = st.presence({from = origin.full_jid, type = "probe"});
95 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to
96 if item.subscription == "both" or item.subscription == "to" then
98 core_post_stanza(origin, probe, true);
101 for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources
102 if res ~= origin and res.presence then
103 res.presence.attr.to = origin.full_jid;
104 core_post_stanza(res, res.presence, true);
105 res.presence.attr.to = nil;
108 if roster.pending then -- resend incoming subscription requests
109 for jid in pairs(roster.pending) do
110 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
113 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
114 for jid, item in pairs(roster) do -- resend outgoing subscription requests
116 request.attr.to = jid;
117 core_post_stanza(origin, request, true);
121 if priority >= 0 then
122 local event = { origin = origin }
123 module:fire_event('message/offline/broadcast', event);
126 if stanza.attr.type == "unavailable" then
127 origin.presence = nil;
128 if origin.priority then
129 origin.priority = nil;
130 recalc_resource_map(user);
132 if origin.directed then
133 for jid in pairs(origin.directed) do
134 stanza.attr.to = jid;
135 core_post_stanza(origin, stanza, true);
137 origin.directed = nil;
140 if not origin.presence then
141 module:fire_event("presence/initial", { origin = origin, stanza = stanza } );
143 origin.presence = stanza;
144 stanza:tag("delay", { xmlns = "urn:xmpp:delay", from = host, stamp = datetime.datetime() }):up();
145 if origin.priority ~= priority then
146 origin.priority = priority;
147 recalc_resource_map(user);
150 stanza.attr.to = nil; -- reset it
153 function send_presence_of_available_resources(user, host, jid, recipient_session, stanza)
154 local h = hosts[host];
156 if h and h.type == "local" then
157 local u = h.sessions[user];
159 for k, session in pairs(u.sessions) do
160 local pres = session.presence;
162 if stanza then pres = stanza; pres.attr.from = session.full_jid; end
164 core_post_stanza(session, pres, true);
171 log("debug", "broadcasted presence of %d resources from %s@%s to %s", count, user, host, jid);
175 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
176 local node, host = jid_split(from_bare);
177 if to_bare == from_bare then return; end -- No self contacts
178 local st_from, st_to = stanza.attr.from, stanza.attr.to;
179 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
180 log("debug", "outbound presence %s from %s for %s", stanza.attr.type, from_bare, to_bare);
181 if stanza.attr.type == "probe" then
182 stanza.attr.from, stanza.attr.to = st_from, st_to;
184 elseif stanza.attr.type == "subscribe" then
186 -- 2. roster push (subscription = none, ask = subscribe)
187 if rostermanager.set_contact_pending_out(node, host, to_bare) then
188 rostermanager.roster_push(node, host, to_bare);
189 end -- else file error
190 core_post_stanza(origin, stanza);
191 elseif stanza.attr.type == "unsubscribe" then
193 -- 2. roster push (subscription = none or from)
194 if rostermanager.unsubscribe(node, host, to_bare) then
195 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
196 end -- else file error
197 core_post_stanza(origin, stanza);
198 elseif stanza.attr.type == "subscribed" then
201 -- 3. send_presence_of_available_resources
202 if rostermanager.subscribed(node, host, to_bare) then
203 rostermanager.roster_push(node, host, to_bare);
205 core_post_stanza(origin, stanza);
206 send_presence_of_available_resources(node, host, to_bare, origin);
207 elseif stanza.attr.type == "unsubscribed" then
208 -- 1. send unavailable
210 -- 3. roster push (subscription = from or both)
211 local success, pending_in, subscribed = rostermanager.unsubscribed(node, host, to_bare);
214 rostermanager.roster_push(node, host, to_bare);
216 core_post_stanza(origin, stanza);
218 send_presence_of_available_resources(node, host, to_bare, origin, st.presence({ type = "unavailable" }));
222 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid presence type"));
224 stanza.attr.from, stanza.attr.to = st_from, st_to;
228 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
229 local node, host = jid_split(to_bare);
230 local st_from, st_to = stanza.attr.from, stanza.attr.to;
231 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
232 log("debug", "inbound presence %s from %s for %s", stanza.attr.type, from_bare, to_bare);
234 if stanza.attr.type == "probe" then
235 local result, err = rostermanager.is_contact_subscribed(node, host, from_bare);
237 if 0 == send_presence_of_available_resources(node, host, st_from, origin) then
238 core_post_stanza(hosts[host], st.presence({from=to_bare, to=st_from, type="unavailable"}), true); -- TODO send last activity
241 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"}), true);
243 elseif stanza.attr.type == "subscribe" then
244 if rostermanager.is_contact_subscribed(node, host, from_bare) then
245 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"}), true); -- already subscribed
246 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
247 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
248 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"}), true); -- TODO send last activity
251 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"}), true); -- acknowledging receipt
252 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
253 if rostermanager.set_contact_pending_in(node, host, from_bare) then
254 sessionmanager.send_to_available_resources(node, host, stanza);
255 end -- TODO else return error, unable to save
258 elseif stanza.attr.type == "unsubscribe" then
259 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
260 sessionmanager.send_to_interested_resources(node, host, stanza);
261 rostermanager.roster_push(node, host, from_bare);
263 elseif stanza.attr.type == "subscribed" then
264 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
265 sessionmanager.send_to_interested_resources(node, host, stanza);
266 rostermanager.roster_push(node, host, from_bare);
268 elseif stanza.attr.type == "unsubscribed" then
269 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
270 sessionmanager.send_to_interested_resources(node, host, stanza);
271 rostermanager.roster_push(node, host, from_bare);
274 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid presence type"));
276 stanza.attr.from, stanza.attr.to = st_from, st_to;
280 local outbound_presence_handler = function(data)
281 -- outbound presence recieved
282 local origin, stanza = data.origin, data.stanza;
284 local to = stanza.attr.to;
286 local t = stanza.attr.type;
287 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes
288 return handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
291 local to_bare = jid_bare(to);
292 local roster = origin.roster;
293 if roster and not(roster[to_bare] and (roster[to_bare].subscription == "both" or roster[to_bare].subscription == "from")) then -- directed presence
294 origin.directed = origin.directed or {};
295 if t then -- removing from directed presence list on sending an error or unavailable
296 origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?
298 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
301 end -- TODO maybe handle normal presence here, instead of letting it pass to incoming handlers?
304 module:hook("pre-presence/full", outbound_presence_handler);
305 module:hook("pre-presence/bare", outbound_presence_handler);
306 module:hook("pre-presence/host", outbound_presence_handler);
308 module:hook("presence/bare", function(data)
309 -- inbound presence to bare JID recieved
310 local origin, stanza = data.origin, data.stanza;
312 local to = stanza.attr.to;
313 local t = stanza.attr.type;
315 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID
316 return handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
319 local user = bare_sessions[to];
321 for _, session in pairs(user.sessions) do
322 if session.presence then -- only send to available resources
323 session.send(stanza);
326 end -- no resources not online, discard
327 elseif not t or t == "unavailable" then
328 handle_normal_presence(origin, stanza);
330 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid presence type"));
334 module:hook("presence/full", function(data)
335 -- inbound presence to full JID recieved
336 local origin, stanza = data.origin, data.stanza;
338 local t = stanza.attr.type;
339 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
340 return handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
343 local session = full_sessions[stanza.attr.to];
345 -- TODO fire post processing event
346 session.send(stanza);
347 end -- resource not online, discard
350 module:hook("presence/host", function(data)
351 -- inbound presence to the host
352 local stanza = data.stanza;
354 local from_bare = jid_bare(stanza.attr.from);
355 local t = stanza.attr.type;
357 core_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
358 elseif t == "subscribe" then
359 core_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id, type = "subscribed" }));
360 core_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
365 module:hook("resource-unbind", function(event)
366 local session, err = event.session, event.error;
367 -- Send unavailable presence
368 if session.presence then
369 local pres = st.presence{ type = "unavailable" };
371 pres:tag("status"):text("Disconnected: "..err):up();
373 session:dispatch_stanza(pres);
374 elseif session.directed then
375 local pres = st.presence{ type = "unavailable", from = session.full_jid };
377 pres:tag("status"):text("Disconnected: "..err):up();
379 for jid in pairs(session.directed) do
381 core_post_stanza(session, pres, true);
383 session.directed = nil;