Comparison

core/stanza_router.lua @ 403:da92afa267cf

Merging with main branch.
author Tobias Markmann <tm@ayena.de>
date Sun, 23 Nov 2008 20:44:48 +0100
parent 372:e7c1e30d06d5
child 438:193f9dd64f17
comparison
equal deleted inserted replaced
402:50f1c09541cd 403:da92afa267cf
18 local s2s_verify_dialback = require "core.s2smanager".verify_dialback; 18 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
19 local s2s_make_authenticated = require "core.s2smanager".make_authenticated; 19 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
20 20
21 local modules_handle_stanza = require "core.modulemanager".handle_stanza; 21 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
22 local component_handle_stanza = require "core.componentmanager".handle_stanza; 22 local component_handle_stanza = require "core.componentmanager".handle_stanza;
23
24 local handle_outbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
25 local handle_inbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_inbound_presence_subscriptions_and_probes;
23 26
24 local format = string.format; 27 local format = string.format;
25 local tostring = tostring; 28 local tostring = tostring;
26 local t_concat = table.concat; 29 local t_concat = table.concat;
27 local t_insert = table.insert; 30 local t_insert = table.insert;
30 33
31 local jid_split = require "util.jid".split; 34 local jid_split = require "util.jid".split;
32 local print = print; 35 local print = print;
33 36
34 function core_process_stanza(origin, stanza) 37 function core_process_stanza(origin, stanza)
35 log("debug", "Received[%s]: %s", origin.type, stanza:pretty_top_tag()) 38 (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:pretty_print()) --top_tag())
36 39
37 if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling. 40 if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling.
38 -- TODO verify validity of stanza (as well as JID validity) 41 -- TODO verify validity of stanza (as well as JID validity)
39 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then 42 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
40 if stanza.attr.type == "set" or stanza.attr.type == "get" then 43 if stanza.attr.type == "set" or stanza.attr.type == "get" then
85 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource 88 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
86 component_handle_stanza(origin, stanza); 89 component_handle_stanza(origin, stanza);
87 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component 90 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
88 component_handle_stanza(origin, stanza); 91 component_handle_stanza(origin, stanza);
89 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then 92 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
90 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); 93 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
91 elseif origin.type ~= "c2s" and stanza.name == "iq" and not resource then -- directed at bare JID 94 elseif origin.type ~= "c2s" and stanza.name == "iq" and not resource then -- directed at bare JID
92 core_handle_stanza(origin, stanza); 95 core_handle_stanza(origin, stanza);
93 else 96 else
94 core_route_stanza(origin, stanza); 97 core_route_stanza(origin, stanza);
95 end 98 end
172 end 175 end
173 end 176 end
174 stanza.attr.to = nil; -- reset it 177 stanza.attr.to = nil; -- reset it
175 else 178 else
176 log("warn", "Unhandled c2s presence: %s", tostring(stanza)); 179 log("warn", "Unhandled c2s presence: %s", tostring(stanza));
177 if stanza.attr.type ~= "error" then 180 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" then
178 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? 181 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
179 end 182 end
180 end 183 end
181 else 184 else
182 log("warn", "Unhandled c2s stanza: %s", tostring(stanza)); 185 log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
183 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then 186 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
184 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? 187 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
185 end 188 end
186 end -- TODO handle other stanzas 189 end -- TODO handle other stanzas
187 else 190 else
188 log("warn", "Unhandled origin: %s", origin.type); 191 log("warn", "Unhandled origin: %s", origin.type);
189 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then 192 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
190 -- s2s stanzas can get here 193 -- s2s stanzas can get here
191 (origin.sends2s or origin.send)(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error? 194 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
192 end 195 end
193 end 196 end
194 end
195
196 function send_presence_of_available_resources(user, host, jid, recipient_session)
197 local h = hosts[host];
198 local count = 0;
199 if h and h.type == "local" then
200 local u = h.sessions[user];
201 if u then
202 for k, session in pairs(u.sessions) do
203 local pres = session.presence;
204 if pres then
205 pres.attr.to = jid;
206 pres.attr.from = session.full_jid;
207 recipient_session.send(pres);
208 pres.attr.to = nil;
209 pres.attr.from = nil;
210 count = count + 1;
211 end
212 end
213 end
214 end
215 return count;
216 end
217
218 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
219 local node, host = jid_split(from_bare);
220 local st_from, st_to = stanza.attr.from, stanza.attr.to;
221 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
222 if stanza.attr.type == "subscribe" then
223 log("debug", "outbound subscribe from "..from_bare.." for "..to_bare);
224 -- 1. route stanza
225 -- 2. roster push (subscription = none, ask = subscribe)
226 if rostermanager.set_contact_pending_out(node, host, to_bare) then
227 rostermanager.roster_push(node, host, to_bare);
228 end -- else file error
229 core_route_stanza(origin, stanza);
230 elseif stanza.attr.type == "unsubscribe" then
231 log("debug", "outbound unsubscribe from "..from_bare.." for "..to_bare);
232 -- 1. route stanza
233 -- 2. roster push (subscription = none or from)
234 if rostermanager.unsubscribe(node, host, to_bare) then
235 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
236 end -- else file error
237 core_route_stanza(origin, stanza);
238 elseif stanza.attr.type == "subscribed" then
239 log("debug", "outbound subscribed from "..from_bare.." for "..to_bare);
240 -- 1. route stanza
241 -- 2. roster_push ()
242 -- 3. send_presence_of_available_resources
243 if rostermanager.subscribed(node, host, to_bare) then
244 rostermanager.roster_push(node, host, to_bare);
245 core_route_stanza(origin, stanza);
246 send_presence_of_available_resources(node, host, to_bare, origin);
247 end
248 elseif stanza.attr.type == "unsubscribed" then
249 log("debug", "outbound unsubscribed from "..from_bare.." for "..to_bare);
250 -- 1. route stanza
251 -- 2. roster push (subscription = none or to)
252 if rostermanager.unsubscribed(node, host, to_bare) then
253 rostermanager.roster_push(node, host, to_bare);
254 core_route_stanza(origin, stanza);
255 end
256 end
257 stanza.attr.from, stanza.attr.to = st_from, st_to;
258 end
259
260 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
261 local node, host = jid_split(to_bare);
262 local st_from, st_to = stanza.attr.from, stanza.attr.to;
263 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
264 if stanza.attr.type == "probe" then
265 log("debug", "inbound probe from "..from_bare.." for "..to_bare);
266 if rostermanager.is_contact_subscribed(node, host, from_bare) then
267 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
268 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
269 end
270 else
271 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
272 end
273 elseif stanza.attr.type == "subscribe" then
274 log("debug", "inbound subscribe from "..from_bare.." for "..to_bare);
275 if rostermanager.is_contact_subscribed(node, host, from_bare) then
276 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
277 else
278 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
279 if rostermanager.set_contact_pending_in(node, host, from_bare) then
280 sessionmanager.send_to_available_resources(node, host, stanza);
281 end -- TODO else return error, unable to save
282 end
283 end
284 elseif stanza.attr.type == "unsubscribe" then
285 log("debug", "inbound unsubscribe from "..from_bare.." for "..to_bare);
286 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
287 rostermanager.roster_push(node, host, from_bare);
288 end
289 elseif stanza.attr.type == "subscribed" then
290 log("debug", "inbound subscribed from "..from_bare.." for "..to_bare);
291 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
292 rostermanager.roster_push(node, host, from_bare);
293 end
294 elseif stanza.attr.type == "unsubscribed" then
295 log("debug", "inbound unsubscribed from "..from_bare.." for "..to_bare);
296 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
297 rostermanager.roster_push(node, host, from_bare);
298 end
299 end -- discard any other type
300 stanza.attr.from, stanza.attr.to = st_from, st_to;
301 end 197 end
302 198
303 function core_route_stanza(origin, stanza) 199 function core_route_stanza(origin, stanza)
304 -- Hooks 200 -- Hooks
305 --- ...later 201 --- ...later
310 local to_bare = node and (node.."@"..host) or host; -- bare JID 206 local to_bare = node and (node.."@"..host) or host; -- bare JID
311 local from = stanza.attr.from; 207 local from = stanza.attr.from;
312 local from_node, from_host, from_resource = jid_split(from); 208 local from_node, from_host, from_resource = jid_split(from);
313 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID 209 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
314 210
211 -- Auto-detect origin if not specified
212 origin = origin or hosts[from_host];
213 if not origin then return false; end
214
315 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end 215 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
316 216
317 local host_session = hosts[host] 217 local host_session = hosts[host]
318 if host_session and host_session.type == "local" then 218 if host_session and host_session.type == "local" then
319 -- Local host 219 -- Local host
322 local res = user.sessions[resource]; 222 local res = user.sessions[resource];
323 if not res then 223 if not res then
324 -- if we get here, resource was not specified or was unavailable 224 -- if we get here, resource was not specified or was unavailable
325 if stanza.name == "presence" then 225 if stanza.name == "presence" then
326 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then 226 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
327 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); 227 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
328 else -- sender is available or unavailable 228 else -- sender is available or unavailable
329 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources. 229 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
330 if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription? 230 if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
331 stanza.attr.to = session.full_jid; -- reset at the end of function 231 stanza.attr.to = session.full_jid; -- reset at the end of function
332 session.send(stanza); 232 session.send(stanza);
365 else 265 else
366 -- user not online 266 -- user not online
367 if user_exists(node, host) then 267 if user_exists(node, host) then
368 if stanza.name == "presence" then 268 if stanza.name == "presence" then
369 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then 269 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
370 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); 270 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
371 else 271 else
372 -- TODO send unavailable presence or unsubscribed 272 -- TODO send unavailable presence or unsubscribed
373 end 273 end
374 elseif stanza.name == "message" then 274 elseif stanza.name == "message" then
375 if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then 275 if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
402 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors 302 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
403 stanza.attr.xmlns = xmlns; -- reset 303 stanza.attr.xmlns = xmlns; -- reset
404 elseif origin.type == "component" or origin.type == "local" then 304 elseif origin.type == "component" or origin.type == "local" then
405 -- Route via s2s for components and modules 305 -- Route via s2s for components and modules
406 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host); 306 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
407 for k,v in pairs(origin) do print("origin:", tostring(k), tostring(v)); end
408 print(tostring(host), tostring(from_host))
409 send_s2s(origin.host, host, stanza); 307 send_s2s(origin.host, host, stanza);
410 else 308 else
411 log("warn", "received stanza from unhandled connection type: %s", origin.type); 309 log("warn", "received stanza from unhandled connection type: %s", origin.type);
412 end 310 end
413 stanza.attr.to = to; -- reset 311 stanza.attr.to = to; -- reset