2 -- Copyright (C) 2008-2009 Matthew Wild
\r
3 -- Copyright (C) 2008-2009 Waqas Hussain
\r
5 -- This project is MIT/X11 licensed. Please see the
\r
6 -- COPYING file in the source package for more information.
\r
9 local log = module._log;
\r
11 local require = require;
\r
12 local pairs, ipairs = pairs, ipairs;
\r
13 local t_concat = table.concat;
\r
14 local s_find = string.find;
\r
15 local tonumber = tonumber;
\r
17 local st = require "util.stanza";
\r
18 local jid_split = require "util.jid".split;
\r
19 local jid_bare = require "util.jid".bare;
\r
20 local hosts = hosts;
\r
22 local rostermanager = require "core.rostermanager";
\r
23 local sessionmanager = require "core.sessionmanager";
\r
24 local offlinemanager = require "core.offlinemanager";
\r
26 local _core_route_stanza = core_route_stanza;
\r
27 local core_route_stanza;
\r
28 function core_route_stanza(origin, stanza)
\r
29 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
\r
30 local node, host = jid_split(stanza.attr.to);
\r
32 if host and host.type == "local" then
\r
33 handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
\r
37 _core_route_stanza(origin, stanza);
\r
40 function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound)
\r
41 local type = stanza.attr.type;
\r
42 if type and type ~= "unavailable" and type ~= "error" then
\r
44 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
\r
46 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
\r
48 elseif not inbound and not stanza.attr.to then
\r
49 handle_normal_presence(origin, stanza, core_route_stanza);
\r
51 core_route_stanza(origin, stanza);
\r
55 function handle_normal_presence(origin, stanza, core_route_stanza)
\r
56 if origin.roster then
\r
57 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
\r
58 local subscription = origin.roster[jid].subscription;
\r
59 if subscription == "both" or subscription == "from" then
\r
60 stanza.attr.to = jid;
\r
61 core_route_stanza(origin, stanza);
\r
64 local node, host = jid_split(stanza.attr.from);
\r
65 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
\r
66 if res ~= origin and res.presence then -- to resource
\r
67 stanza.attr.to = res.full_jid;
\r
68 core_route_stanza(origin, stanza);
\r
71 if stanza.attr.type == nil and not origin.presence then -- initial presence
\r
72 local probe = st.presence({from = origin.full_jid, type = "probe"});
\r
73 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
\r
74 local subscription = origin.roster[jid].subscription;
\r
75 if subscription == "both" or subscription == "to" then
\r
76 probe.attr.to = jid;
\r
77 core_route_stanza(origin, probe);
\r
80 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
\r
81 if res ~= origin and res.presence then
\r
82 res.presence.attr.to = origin.full_jid;
\r
83 core_route_stanza(res, res.presence);
\r
84 res.presence.attr.to = nil;
\r
87 if origin.roster.pending then -- resend incoming subscription requests
\r
88 for jid in pairs(origin.roster.pending) do
\r
89 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
\r
92 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
\r
93 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
\r
95 request.attr.to = jid;
\r
96 core_route_stanza(origin, request);
\r
99 local offline = offlinemanager.load(node, host);
\r
101 for _, msg in ipairs(offline) do
\r
102 origin.send(msg); -- FIXME do we need to modify to/from in any way?
\r
104 offlinemanager.deleteAll(node, host);
\r
107 origin.priority = 0;
\r
108 if stanza.attr.type == "unavailable" then
\r
109 origin.presence = nil;
\r
110 if origin.directed then
\r
111 local old_from = stanza.attr.from;
\r
112 stanza.attr.from = origin.full_jid;
\r
113 for jid in pairs(origin.directed) do
\r
114 stanza.attr.to = jid;
\r
115 core_route_stanza(origin, stanza);
\r
117 stanza.attr.from = old_from;
\r
118 origin.directed = nil;
\r
121 origin.presence = stanza;
\r
122 local priority = stanza:child_with_name("priority");
\r
123 if priority and #priority > 0 then
\r
124 priority = t_concat(priority);
\r
125 if s_find(priority, "^[+-]?[0-9]+$") then
\r
126 priority = tonumber(priority);
\r
127 if priority < -128 then priority = -128 end
\r
128 if priority > 127 then priority = 127 end
\r
129 origin.priority = priority;
\r
133 stanza.attr.to = nil; -- reset it
\r
135 log("warn", "presence recieved from client with no roster");
\r
139 function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
\r
140 local h = hosts[host];
\r
142 if h and h.type == "local" then
\r
143 local u = h.sessions[user];
\r
145 for k, session in pairs(u.sessions) do
\r
146 local pres = session.presence;
\r
148 pres.attr.to = jid;
\r
149 core_route_stanza(session, pres);
\r
150 pres.attr.to = nil;
\r
156 log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
\r
160 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
\r
161 local node, host = jid_split(from_bare);
\r
162 local st_from, st_to = stanza.attr.from, stanza.attr.to;
\r
163 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
\r
164 log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
\r
165 if stanza.attr.type == "subscribe" then
\r
167 -- 2. roster push (subscription = none, ask = subscribe)
\r
168 if rostermanager.set_contact_pending_out(node, host, to_bare) then
\r
169 rostermanager.roster_push(node, host, to_bare);
\r
170 end -- else file error
\r
171 core_route_stanza(origin, stanza);
\r
172 elseif stanza.attr.type == "unsubscribe" then
\r
174 -- 2. roster push (subscription = none or from)
\r
175 if rostermanager.unsubscribe(node, host, to_bare) then
\r
176 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
\r
177 end -- else file error
\r
178 core_route_stanza(origin, stanza);
\r
179 elseif stanza.attr.type == "subscribed" then
\r
181 -- 2. roster_push ()
\r
182 -- 3. send_presence_of_available_resources
\r
183 if rostermanager.subscribed(node, host, to_bare) then
\r
184 rostermanager.roster_push(node, host, to_bare);
\r
186 core_route_stanza(origin, stanza);
\r
187 send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
\r
188 elseif stanza.attr.type == "unsubscribed" then
\r
190 -- 2. roster push (subscription = none or to)
\r
191 if rostermanager.unsubscribed(node, host, to_bare) then
\r
192 rostermanager.roster_push(node, host, to_bare);
\r
194 core_route_stanza(origin, stanza);
\r
196 stanza.attr.from, stanza.attr.to = st_from, st_to;
\r
199 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
\r
200 local node, host = jid_split(to_bare);
\r
201 local st_from, st_to = stanza.attr.from, stanza.attr.to;
\r
202 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
\r
203 log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
\r
204 if stanza.attr.type == "probe" then
\r
205 if rostermanager.is_contact_subscribed(node, host, from_bare) then
\r
206 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
\r
207 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
\r
210 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
\r
212 elseif stanza.attr.type == "subscribe" then
\r
213 if rostermanager.is_contact_subscribed(node, host, from_bare) then
\r
214 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
\r
215 -- Sending presence is not clearly stated in the RFC, but it seems appropriate
\r
216 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
\r
217 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
\r
220 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
\r
221 if rostermanager.set_contact_pending_in(node, host, from_bare) then
\r
222 sessionmanager.send_to_available_resources(node, host, stanza);
\r
223 end -- TODO else return error, unable to save
\r
226 elseif stanza.attr.type == "unsubscribe" then
\r
227 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
\r
228 rostermanager.roster_push(node, host, from_bare);
\r
230 elseif stanza.attr.type == "subscribed" then
\r
231 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
\r
232 rostermanager.roster_push(node, host, from_bare);
\r
234 elseif stanza.attr.type == "unsubscribed" then
\r
235 if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
\r
236 rostermanager.roster_push(node, host, from_bare);
\r
238 end -- discard any other type
\r
239 stanza.attr.from, stanza.attr.to = st_from, st_to;
\r
242 local function presence_handler(data)
\r
243 local origin, stanza = data.origin, data.stanza;
\r
244 local to = stanza.attr.to;
\r
245 local node, host = jid_split(to);
\r
246 local to_bare = jid_bare(to);
\r
247 local from_bare = jid_bare(stanza.attr.from);
\r
248 if origin.type == "c2s" then
\r
249 if to ~= nil and not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
\r
250 origin.directed = origin.directed or {};
\r
251 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
\r
253 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
\r
254 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
\r
256 handle_normal_presence(origin, stanza, core_route_stanza);
\r
258 core_route_stanza(origin, stanza);
\r
260 elseif (origin.type == "s2sin" or origin.type == "component") and hosts[host] then
\r
261 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
\r
262 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
\r
264 core_route_stanza(origin, stanza);
\r
270 prosody.events.add_handler(module:get_host().."/presence", presence_handler);
\r
271 module.unload = function()
\r
272 prosody.events.remove_handler(module:get_host().."/presence", presence_handler);
\r
275 module:hook("pre-presence/full", function(data)
\r
276 -- outbound presence to full JID recieved
\r
277 local origin, stanza = data.origin, data.stanza;
\r
279 local t = stanza.attr.type;
\r
280 if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to full JID
\r
281 handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);
\r
285 local to = stanza.attr.to;
\r
286 local to_bare = jid_bare(to);
\r
287 if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence
\r
288 origin.directed = origin.directed or {};
\r
289 if t then -- removing from directed presence list on sending an error or unavailable
\r
290 origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?
\r
292 origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?
\r