Comparison

plugins/mod_s2s/mod_s2s.lua @ 5351:901ed253bbf7

mod_s2s, mod_saslauth, mod_compression: Refactor to have common code for opening streams
author Kim Alvefur <zash@zash.se>
date Sat, 16 Mar 2013 17:46:43 +0100
parent 5345:9b85976c873f
child 5362:612467e263af
comparison
equal deleted inserted replaced
5350:fba042a2c228 5351:901ed253bbf7
246 end 246 end
247 end 247 end
248 248
249 if session.secure and not session.cert_chain_status then check_cert_status(session); end 249 if session.secure and not session.cert_chain_status then check_cert_status(session); end
250 250
251 send("<?xml version='1.0'?>"); 251 session:open_stream()
252 send(st.stanza("stream:stream", { xmlns='jabber:server',
253 ["xmlns:db"]= hosts[to].modules.dialback and 'jabber:server:dialback' or nil,
254 ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=to, to=from, version=(session.version > 0 and "1.0" or nil) }):top_tag());
255 if session.version >= 1.0 then 252 if session.version >= 1.0 then
256 local features = st.stanza("stream:features"); 253 local features = st.stanza("stream:features");
257 254
258 if to then 255 if to then
259 hosts[to].events.fire_event("s2s-stream-features", { origin = session, features = features }); 256 hosts[to].events.fire_event("s2s-stream-features", { origin = session, features = features });
346 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" }; 343 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
347 local function session_close(session, reason, remote_reason) 344 local function session_close(session, reason, remote_reason)
348 local log = session.log or log; 345 local log = session.log or log;
349 if session.conn then 346 if session.conn then
350 if session.notopen then 347 if session.notopen then
351 session.sends2s("<?xml version='1.0'?>"); 348 session:open_stream()
352 session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
353 end 349 end
354 if reason then -- nil == no err, initiated by us, false == initiated by remote 350 if reason then -- nil == no err, initiated by us, false == initiated by remote
355 if type(reason) == "string" then -- assume stream error 351 if type(reason) == "string" then -- assume stream error
356 log("debug", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, reason); 352 log("debug", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, reason);
357 session.sends2s(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' })); 353 session.sends2s(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
394 conn:close(); -- Close immediately, as this is an outgoing connection or is not authed 390 conn:close(); -- Close immediately, as this is an outgoing connection or is not authed
395 end 391 end
396 end 392 end
397 end 393 end
398 394
395 function session_open_stream(session, from, to)
396 local from = from or session.from_host;
397 local to = to or session.to_host;
398 local attr = {
399 ["xmlns:stream"] = 'http://etherx.jabber.org/streams',
400 xmlns = 'jabber:server',
401 version = session.version and (session.version > 0 and "1.0" or nil),
402 ["xml:lang"] = 'en',
403 id = session.streamid,
404 from = from, to = to,
405 }
406 local local_host = session.direction == "outgoing" and from or to;
407 if not local_host or hosts[local_host].modules.dialback then
408 attr["xmlns:db"] = 'jabber:server:dialback';
409 end
410
411 session.sends2s("<?xml version='1.0'?>");
412 session.sends2s(st.stanza("stream:stream", attr):top_tag());
413 return true;
414 end
415
399 -- Session initialization logic shared by incoming and outgoing 416 -- Session initialization logic shared by incoming and outgoing
400 local function initialize_session(session) 417 local function initialize_session(session)
401 local stream = new_xmpp_stream(session, stream_callbacks); 418 local stream = new_xmpp_stream(session, stream_callbacks);
402 session.stream = stream; 419 session.stream = stream;
403 420
405 422
406 function session.reset_stream() 423 function session.reset_stream()
407 session.notopen = true; 424 session.notopen = true;
408 session.stream:reset(); 425 session.stream:reset();
409 end 426 end
427
428 session.open_stream = session_open_stream;
410 429
411 local filter = session.filter; 430 local filter = session.filter;
412 function session.data(data) 431 function session.data(data)
413 data = filter("bytes/in", data); 432 data = filter("bytes/in", data);
414 if data then 433 if data then