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;
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;
17 local st = require "util.stanza";
18 local jid_split = require "util.jid".split;
19 local jid_bare = require "util.jid".bare;
23 local rostermanager = require "core.rostermanager";
24 local sessionmanager = require "core.sessionmanager";
25 local offlinemanager = require "core.offlinemanager";
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);
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);
38 _core_route_stanza(origin, stanza);
41 local select_top_resources;
42 local bare_message_delivery_policy = module:get_option("bare_message_delivery_policy") or "priority";
43 if bare_message_delivery_policy == "broadcast" then
44 function select_top_resources(user)
45 local recipients = {};
46 for _, session in pairs(user.sessions) do -- find resources with non-negative priority
47 local p = session.priority;
49 t_insert(recipients, session);
55 if bare_message_delivery_policy ~= "priority" then
56 module:log("warn", "Invalid value for config option bare_message_delivery_policy");
58 function select_top_resources(user)
60 local recipients = {};
61 for _, session in pairs(user.sessions) do -- find resource with greatest priority
62 if session.presence then
63 -- TODO check active privacy list for session
64 local p = session.priority;
67 recipients = {session};
68 elseif p == priority then
69 t_insert(recipients, session);
77 local function recalc_resource_map(user)
79 user.top_resources = select_top_resources(user);
80 if #user.top_resources == 0 then user.top_resources = nil; end
84 function handle_normal_presence(origin, stanza, core_route_stanza)
85 if full_sessions[origin.full_jid] then -- if user is still connected
86 origin.send(stanza); -- reflect their presence back to them
88 local roster = origin.roster;
89 local node, host = origin.username, origin.host;
90 local user = bare_sessions[node.."@"..host];
91 for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
92 if res ~= origin and res.presence then -- to resource
93 stanza.attr.to = res.full_jid;
94 core_route_stanza(origin, stanza);
97 for jid, item in pairs(roster) do -- broadcast to all interested contacts
98 if item.subscription == "both" or item.subscription == "from" then
100 core_route_stanza(origin, stanza);
103 if stanza.attr.type == nil and not origin.presence then -- initial presence
104 origin.presence = stanza; -- FIXME repeated later
105 local probe = st.presence({from = origin.full_jid, type = "probe"});
106 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to
107 if item.subscription == "both" or item.subscription == "to" then
109 core_route_stanza(origin, probe);
112 for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources
113 if res ~= origin and res.presence then
114 res.presence.attr.to = origin.full_jid;
115 core_route_stanza(res, res.presence);
116 res.presence.attr.to = nil;
119 if roster.pending then -- resend incoming subscription requests
120 for jid in pairs(roster.pending) do
121 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
124 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
125 for jid, item in pairs(roster) do -- resend outgoing subscription requests
127 request.attr.to = jid;
128 core_route_stanza(origin, request);
131 local offline = offlinemanager.load(node, host);
133 for _, msg in ipairs(offline) do
134 origin.send(msg); -- FIXME do we need to modify to/from in any way?
136 offlinemanager.deleteAll(node, host);
139 if stanza.attr.type == "unavailable" then
140 origin.presence = nil;
141 if origin.priority then
142 origin.priority = nil;
143 recalc_resource_map(user);
145 if origin.directed then
146 for jid in pairs(origin.directed) do
147 stanza.attr.to = jid;
148 core_route_stanza(origin, stanza);
150 origin.directed = nil;
153 origin.presence = stanza;
154 local priority = stanza:child_with_name("priority");
155 if priority and #priority > 0 then
156 priority = t_concat(priority);
157 if s_find(priority, "^[+-]?[0-9]+$") then
158 priority = tonumber(priority);
159 if priority < -128 then priority = -128 end
160 if priority > 127 then priority = 127 end
161 else priority = 0; end
162 else priority = 0; end
163 if origin.priority ~= priority then
164 origin.priority = priority;
165 recalc_resource_map(user);
168 stanza.attr.to = nil; -- reset it
171 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza, stanza)
172 local h = hosts[host];
174 if h and h.type == "local" then
175 local u = h.sessions[user];
177 for k, session in pairs(u.sessions) do
178 local pres = session.presence;
180 if stanza then pres = stanza; pres.attr.from = session.full_jid; end
182 core_route_stanza(session, pres);
189 log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
193 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
194 local node, host = jid_split(from_bare);
195 if to_bare == origin.username.."@"..origin.host then return; end -- No self contacts
196 local st_from, st_to = stanza.attr.from, stanza.attr.to;
197 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
198 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
199 if stanza.attr.type == "subscribe" then
201 -- 2. roster push (subscription = none, ask = subscribe)
202 if rostermanager.set_contact_pending_out(node, host, to_bare) then
203 rostermanager.roster_push(node, host, to_bare);
204 end -- else file error
205 core_route_stanza(origin, stanza);
206 elseif stanza.attr.type == "unsubscribe" then
208 -- 2. roster push (subscription = none or from)
209 if rostermanager.unsubscribe(node, host, to_bare) then
210 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
211 end -- else file error
212 core_route_stanza(origin, stanza);
213 elseif stanza.attr.type == "subscribed" then
216 -- 3. send_presence_of_available_resources
217 if rostermanager.subscribed(node, host, to_bare) then
218 rostermanager.roster_push(node, host, to_bare);
220 core_route_stanza(origin, stanza);
221 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
222 elseif stanza.attr.type == "unsubscribed" then
224 -- 2. roster push (subscription = none or to)
225 if rostermanager.unsubscribed(node, host, to_bare) then
226 rostermanager.roster_push(node, host, to_bare);
228 core_route_stanza(origin, stanza);
230 stanza.attr.from, stanza.attr.to = st_from, st_to;
233 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
234 local node, host = jid_split(to_bare);
235 local st_from, st_to = stanza.attr.from, stanza.attr.to;
236 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
237 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
240 log("debug", "dropping presence sent to host or invalid address '%s'", tostring(to_bare));
243 if stanza.attr.type == "probe" then
244 if rostermanager.is_contact_subscribed(node, host, from_bare) then
245 if 0 == send_presence_of_available_resources(node, host, st_from, origin, core_route_stanza) then
246 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
249 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
251 elseif stanza.attr.type == "subscribe" then
252 if rostermanager.is_contact_subscribed(node, host, from_bare) then
253 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
254 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
255 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
256 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
259 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- acknowledging receipt
260 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
261 if rostermanager.set_contact_pending_in(node, host, from_bare) then
262 sessionmanager.send_to_available_resources(node, host, stanza);
263 end -- TODO else return error, unable to save
266 elseif stanza.attr.type == "unsubscribe" then
267 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
268 sessionmanager.send_to_interested_resources(node, host, stanza);
269 rostermanager.roster_push(node, host, from_bare);
271 elseif stanza.attr.type == "subscribed" then
272 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
273 sessionmanager.send_to_interested_resources(node, host, stanza);
274 rostermanager.roster_push(node, host, from_bare);
276 elseif stanza.attr.type == "unsubscribed" then
277 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
278 sessionmanager.send_to_interested_resources(node, host, stanza);
279 rostermanager.roster_push(node, host, from_bare);
281 end -- discard any other type
282 stanza.attr.from, stanza.attr.to = st_from, st_to;
285 local outbound_presence_handler = function(data)
286 -- outbound presence recieved
287 local origin, stanza = data.origin, data.stanza;
289 local to = stanza.attr.to;
291 local t = stanza.attr.type;
292 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes
293 handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
297 local to_bare = jid_bare(to);
298 if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
299 origin.directed = origin.directed or {};
300 if t then -- removing from directed presence list on sending an error or unavailable
301 origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?
303 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
306 end -- TODO maybe handle normal presence here, instead of letting it pass to incoming handlers?
309 module:hook("pre-presence/full", outbound_presence_handler);
310 module:hook("pre-presence/bare", outbound_presence_handler);
311 module:hook("pre-presence/host", outbound_presence_handler);
313 module:hook("presence/bare", function(data)
314 -- inbound presence to bare JID recieved
315 local origin, stanza = data.origin, data.stanza;
317 local to = stanza.attr.to;
318 local t = stanza.attr.type;
320 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID
321 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
325 local user = bare_sessions[to];
327 for _, session in pairs(user.sessions) do
328 if session.presence then -- only send to available resources
329 session.send(stanza);
332 end -- no resources not online, discard
333 elseif not t or t == "unavailable" then
334 handle_normal_presence(origin, stanza, core_route_stanza);
338 module:hook("presence/full", function(data)
339 -- inbound presence to full JID recieved
340 local origin, stanza = data.origin, data.stanza;
342 local t = stanza.attr.type;
343 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
344 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
348 local session = full_sessions[stanza.attr.to];
350 -- TODO fire post processing event
351 session.send(stanza);
352 end -- resource not online, discard
355 module:hook("presence/host", function(data)
356 -- inbound presence to the host
357 local origin, stanza = data.origin, data.stanza;
359 local from_bare = jid_bare(stanza.attr.from);
360 local t = stanza.attr.type;
362 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
363 elseif t == "subscribe" then
364 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id, type = "subscribed" }));
365 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
370 module:hook("resource-unbind", function(event)
371 local session, err = event.session, event.error;
372 -- Send unavailable presence
373 if session.presence then
374 local pres = st.presence{ type = "unavailable" };
375 if not(err) or err == "closed" then err = "connection closed"; end
376 pres:tag("status"):text("Disconnected: "..err):up();
377 session:dispatch_stanza(pres);
378 elseif session.directed then
379 local pres = st.presence{ type = "unavailable", from = session.full_jid };
380 if not(err) or err == "closed" then err = "connection closed"; end
381 pres:tag("status"):text("Disconnected: "..err):up();
382 for jid in pairs(session.directed) do
384 core_route_stanza(session, pres);
386 session.directed = nil;