2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 function select_top_resources(user)
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;
50 recipients = {session};
51 elseif p == priority then
52 t_insert(recipients, session);
58 local function recalc_resource_map(user)
60 user.top_resources = select_top_resources(user);
61 if #user.top_resources == 0 then user.top_resources = nil; end
65 function handle_normal_presence(origin, stanza, core_route_stanza)
66 local roster = origin.roster;
67 local node, host = origin.username, origin.host;
68 local user = bare_sessions[node.."@"..host];
69 for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
70 if res ~= origin and res.presence then -- to resource
71 stanza.attr.to = res.full_jid;
72 core_route_stanza(origin, stanza);
75 for jid, item in pairs(roster) do -- broadcast to all interested contacts
76 if item.subscription == "both" or item.subscription == "from" then
78 core_route_stanza(origin, stanza);
81 if stanza.attr.type == nil and not origin.presence then -- initial presence
82 origin.presence = stanza; -- FIXME repeated later
83 local probe = st.presence({from = origin.full_jid, type = "probe"});
84 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to
85 if item.subscription == "both" or item.subscription == "to" then
87 core_route_stanza(origin, probe);
90 for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources
91 if res ~= origin and res.presence then
92 res.presence.attr.to = origin.full_jid;
93 core_route_stanza(res, res.presence);
94 res.presence.attr.to = nil;
97 if roster.pending then -- resend incoming subscription requests
98 for jid in pairs(roster.pending) do
99 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
102 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
103 for jid, item in pairs(roster) do -- resend outgoing subscription requests
105 request.attr.to = jid;
106 core_route_stanza(origin, request);
109 local offline = offlinemanager.load(node, host);
111 for _, msg in ipairs(offline) do
112 origin.send(msg); -- FIXME do we need to modify to/from in any way?
114 offlinemanager.deleteAll(node, host);
117 if stanza.attr.type == "unavailable" then
118 origin.presence = nil;
119 if origin.priority then
120 origin.priority = nil;
121 recalc_resource_map(user);
123 if origin.directed then
124 for jid in pairs(origin.directed) do
125 stanza.attr.to = jid;
126 core_route_stanza(origin, stanza);
128 origin.directed = nil;
131 origin.presence = stanza;
132 local priority = stanza:child_with_name("priority");
133 if priority and #priority > 0 then
134 priority = t_concat(priority);
135 if s_find(priority, "^[+-]?[0-9]+$") then
136 priority = tonumber(priority);
137 if priority < -128 then priority = -128 end
138 if priority > 127 then priority = 127 end
139 else priority = 0; end
140 else priority = 0; end
141 if origin.priority ~= priority then
142 origin.priority = priority;
143 recalc_resource_map(user);
146 stanza.attr.to = nil; -- reset it
149 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza, stanza)
150 local h = hosts[host];
152 if h and h.type == "local" then
153 local u = h.sessions[user];
155 for k, session in pairs(u.sessions) do
156 local pres = session.presence;
158 if stanza then pres = stanza; pres.attr.from = session.full_jid; end
160 core_route_stanza(session, pres);
167 log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
171 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
172 local node, host = jid_split(from_bare);
173 if to_bare == origin.username.."@"..origin.host then return; end -- No self contacts
174 local st_from, st_to = stanza.attr.from, stanza.attr.to;
175 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
176 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
177 if stanza.attr.type == "subscribe" then
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_route_stanza(origin, stanza);
184 elseif stanza.attr.type == "unsubscribe" then
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_route_stanza(origin, stanza);
191 elseif stanza.attr.type == "subscribed" then
194 -- 3. send_presence_of_available_resources
195 if rostermanager.subscribed(node, host, to_bare) then
196 rostermanager.roster_push(node, host, to_bare);
198 core_route_stanza(origin, stanza);
199 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
200 elseif stanza.attr.type == "unsubscribed" then
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);
206 core_route_stanza(origin, stanza);
208 stanza.attr.from, stanza.attr.to = st_from, st_to;
211 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
212 local node, host = jid_split(to_bare);
213 local st_from, st_to = stanza.attr.from, stanza.attr.to;
214 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
215 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
218 log("debug", "dropping presence sent to host or invalid address '%s'", tostring(to_bare));
221 if stanza.attr.type == "probe" then
222 if rostermanager.is_contact_subscribed(node, host, from_bare) then
223 if 0 == send_presence_of_available_resources(node, host, st_from, origin, core_route_stanza) then
224 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
227 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
229 elseif stanza.attr.type == "subscribe" then
230 if rostermanager.is_contact_subscribed(node, host, from_bare) then
231 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- 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, core_route_stanza) then
234 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity
237 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- 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
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);
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);
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);
259 end -- discard any other type
260 stanza.attr.from, stanza.attr.to = st_from, st_to;
263 local outbound_presence_handler = function(data)
264 -- outbound presence recieved
265 local origin, stanza = data.origin, data.stanza;
267 local to = stanza.attr.to;
269 local t = stanza.attr.type;
270 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes
271 handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
275 local to_bare = jid_bare(to);
276 if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
277 origin.directed = origin.directed or {};
278 if t then -- removing from directed presence list on sending an error or unavailable
279 origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?
281 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
284 end -- TODO maybe handle normal presence here, instead of letting it pass to incoming handlers?
287 module:hook("pre-presence/full", outbound_presence_handler);
288 module:hook("pre-presence/bare", outbound_presence_handler);
289 module:hook("pre-presence/host", outbound_presence_handler);
291 module:hook("presence/bare", function(data)
292 -- inbound presence to bare JID recieved
293 local origin, stanza = data.origin, data.stanza;
295 local to = stanza.attr.to;
296 local t = stanza.attr.type;
298 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID
299 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
303 local user = bare_sessions[to];
305 for _, session in pairs(user.sessions) do
306 if session.presence then -- only send to available resources
307 session.send(stanza);
310 end -- no resources not online, discard
311 elseif not t or t == "unavailable" then
312 handle_normal_presence(origin, stanza, core_route_stanza);
316 module:hook("presence/full", function(data)
317 -- inbound presence to full JID recieved
318 local origin, stanza = data.origin, data.stanza;
320 local t = stanza.attr.type;
321 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
322 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
326 local session = full_sessions[stanza.attr.to];
328 -- TODO fire post processing event
329 session.send(stanza);
330 end -- resource not online, discard
333 module:hook("presence/host", function(data)
334 -- inbound presence to the host
335 local origin, stanza = data.origin, data.stanza;
337 local from_bare = jid_bare(stanza.attr.from);
338 local t = stanza.attr.type;
340 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
341 elseif t == "subscribe" then
342 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id, type = "subscribed" }));
343 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
348 module:hook("resource-unbind", function(event)
349 local session, err = event.session, event.error;
350 -- Send unavailable presence
351 if session.presence then
352 local pres = st.presence{ type = "unavailable" };
353 if not(err) or err == "closed" then err = "connection closed"; end
354 pres:tag("status"):text("Disconnected: "..err):up();
355 session:dispatch_stanza(pres);
356 elseif session.directed then
357 local pres = st.presence{ type = "unavailable", from = session.full_jid };
358 if not(err) or err == "closed" then err = "connection closed"; end
359 pres:tag("status"):text("Disconnected: "..err):up();
360 for jid in pairs(session.directed) do
362 core_route_stanza(session, pres);
364 session.directed = nil;