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";
26 local function select_top_resources(user)
28 local recipients = {};
29 for _, session in pairs(user.sessions) do -- find resource with greatest priority
30 if session.presence then
31 -- TODO check active privacy list for session
32 local p = session.priority;
35 recipients = {session};
36 elseif p == priority then
37 t_insert(recipients, session);
43 local function recalc_resource_map(user)
45 user.top_resources = select_top_resources(user);
46 if #user.top_resources == 0 then user.top_resources = nil; end
50 local ignore_presence_priority = module:get_option("ignore_presence_priority");
52 function handle_normal_presence(origin, stanza)
53 if ignore_presence_priority then
54 local priority = stanza:child_with_name("priority");
55 if priority and priority[1] ~= "0" then
56 for i=#priority.tags,1,-1 do priority.tags[i] = nil; end
57 for i=#priority,1,-1 do priority[i] = nil; end
61 local priority = stanza:child_with_name("priority");
62 if priority and #priority > 0 then
63 priority = t_concat(priority);
64 if s_find(priority, "^[+-]?[0-9]+$") then
65 priority = tonumber(priority);
66 if priority < -128 then priority = -128 end
67 if priority > 127 then priority = 127 end
68 else priority = 0; end
69 else priority = 0; end
70 if full_sessions[origin.full_jid] then -- if user is still connected
71 origin.send(stanza); -- reflect their presence back to them
73 local roster = origin.roster;
74 local node, host = origin.username, origin.host;
75 local user = bare_sessions[node.."@"..host];
76 for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
77 if res ~= origin and res.presence then -- to resource
78 stanza.attr.to = res.full_jid;
79 core_post_stanza(origin, stanza, true);
82 for jid, item in pairs(roster) do -- broadcast to all interested contacts
83 if item.subscription == "both" or item.subscription == "from" then
85 core_post_stanza(origin, stanza, true);
88 if stanza.attr.type == nil and not origin.presence then -- initial presence
89 origin.presence = stanza; -- FIXME repeated later
90 local probe = st.presence({from = origin.full_jid, type = "probe"});
91 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to
92 if item.subscription == "both" or item.subscription == "to" then
94 core_post_stanza(origin, probe, true);
97 for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources
98 if res ~= origin and res.presence then
99 res.presence.attr.to = origin.full_jid;
100 core_post_stanza(res, res.presence, true);
101 res.presence.attr.to = nil;
104 if roster.pending then -- resend incoming subscription requests
105 for jid in pairs(roster.pending) do
106 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
109 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
110 for jid, item in pairs(roster) do -- resend outgoing subscription requests
112 request.attr.to = jid;
113 core_post_stanza(origin, request, true);
117 if priority >= 0 then
118 local event = { origin = origin }
119 module:fire_event('message/offline/broadcast', event);
120 module:fire_event('message/offline/delete', event);
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);
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);
134 origin.directed = nil;
137 origin.presence = stanza;
138 if origin.priority ~= priority then
139 origin.priority = priority;
140 recalc_resource_map(user);
143 stanza.attr.to = nil; -- reset it
146 function send_presence_of_available_resources(user, host, jid, recipient_session, stanza)
147 local h = hosts[host];
149 if h and h.type == "local" then
150 local u = h.sessions[user];
152 for k, session in pairs(u.sessions) do
153 local pres = session.presence;
155 if stanza then pres = stanza; pres.attr.from = session.full_jid; end
157 core_post_stanza(session, pres, true);
164 log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
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 "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
174 if stanza.attr.type == "probe" then
175 stanza.attr.from, stanza.attr.to = st_from, st_to;
177 elseif 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_post_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_post_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_post_stanza(origin, stanza);
199 send_presence_of_available_resources(node, host, to_bare, origin);
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_post_stanza(origin, stanza);
208 stanza.attr.from, stanza.attr.to = st_from, st_to;
212 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
213 local node, host = jid_split(to_bare);
214 local st_from, st_to = stanza.attr.from, stanza.attr.to;
215 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
216 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
218 if stanza.attr.type == "probe" then
219 local result, err = rostermanager.is_contact_subscribed(node, host, from_bare);
221 if 0 == send_presence_of_available_resources(node, host, st_from, origin) then
222 core_post_stanza(hosts[host], st.presence({from=to_bare, to=st_from, type="unavailable"}), true); -- TODO send last activity
225 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"}), true);
227 elseif stanza.attr.type == "subscribe" then
228 if rostermanager.is_contact_subscribed(node, host, from_bare) then
229 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"}), true); -- already subscribed
230 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
231 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
232 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"}), true); -- TODO send last activity
235 core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"}), true); -- acknowledging receipt
236 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
237 if rostermanager.set_contact_pending_in(node, host, from_bare) then
238 sessionmanager.send_to_available_resources(node, host, stanza);
239 end -- TODO else return error, unable to save
242 elseif stanza.attr.type == "unsubscribe" then
243 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
244 sessionmanager.send_to_interested_resources(node, host, stanza);
245 rostermanager.roster_push(node, host, from_bare);
247 elseif stanza.attr.type == "subscribed" then
248 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
249 sessionmanager.send_to_interested_resources(node, host, stanza);
250 rostermanager.roster_push(node, host, from_bare);
252 elseif stanza.attr.type == "unsubscribed" then
253 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
254 sessionmanager.send_to_interested_resources(node, host, stanza);
255 rostermanager.roster_push(node, host, from_bare);
257 end -- discard any other type
258 stanza.attr.from, stanza.attr.to = st_from, st_to;
262 local outbound_presence_handler = function(data)
263 -- outbound presence recieved
264 local origin, stanza = data.origin, data.stanza;
266 local to = stanza.attr.to;
268 local t = stanza.attr.type;
269 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes
270 return handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
273 local to_bare = jid_bare(to);
274 local roster = origin.roster;
275 if roster and not(roster[to_bare] and (roster[to_bare].subscription == "both" or roster[to_bare].subscription == "from")) then -- directed presence
276 origin.directed = origin.directed or {};
277 if t then -- removing from directed presence list on sending an error or unavailable
278 origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?
280 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
283 end -- TODO maybe handle normal presence here, instead of letting it pass to incoming handlers?
286 module:hook("pre-presence/full", outbound_presence_handler);
287 module:hook("pre-presence/bare", outbound_presence_handler);
288 module:hook("pre-presence/host", outbound_presence_handler);
290 module:hook("presence/bare", function(data)
291 -- inbound presence to bare JID recieved
292 local origin, stanza = data.origin, data.stanza;
294 local to = stanza.attr.to;
295 local t = stanza.attr.type;
297 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID
298 return handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
301 local user = bare_sessions[to];
303 for _, session in pairs(user.sessions) do
304 if session.presence then -- only send to available resources
305 session.send(stanza);
308 end -- no resources not online, discard
309 elseif not t or t == "unavailable" then
310 handle_normal_presence(origin, stanza);
314 module:hook("presence/full", function(data)
315 -- inbound presence to full JID recieved
316 local origin, stanza = data.origin, data.stanza;
318 local t = stanza.attr.type;
319 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
320 return handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to));
323 local session = full_sessions[stanza.attr.to];
325 -- TODO fire post processing event
326 session.send(stanza);
327 end -- resource not online, discard
330 module:hook("presence/host", function(data)
331 -- inbound presence to the host
332 local origin, stanza = data.origin, data.stanza;
334 local from_bare = jid_bare(stanza.attr.from);
335 local t = stanza.attr.type;
337 core_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
338 elseif t == "subscribe" then
339 core_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id, type = "subscribed" }));
340 core_post_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id }));
345 module:hook("resource-unbind", function(event)
346 local session, err = event.session, event.error;
347 -- Send unavailable presence
348 if session.presence then
349 local pres = st.presence{ type = "unavailable" };
350 if not(err) or err == "closed" then err = "connection closed"; end
351 pres:tag("status"):text("Disconnected: "..err):up();
352 session:dispatch_stanza(pres);
353 elseif session.directed then
354 local pres = st.presence{ type = "unavailable", from = session.full_jid };
355 if not(err) or err == "closed" then err = "connection closed"; end
356 pres:tag("status"):text("Disconnected: "..err):up();
357 for jid in pairs(session.directed) do
359 core_post_stanza(session, pres, true);
361 session.directed = nil;