view mod_bidi/mod_bidi.lua @ 932:4e235e565693

mod_bidi, mod_dwd, mod_s2s_idle_timeout: Update for recent 0.9 changes (612467e263af)
author Matthew Wild <>
date Fri, 22 Mar 2013 14:24:05 +0000
parents 4cd018025a26
children c714ed7de4ee
line wrap: on
line source

-- Bidirectional Server-to-Server Connections
-- Copyright (C) 2013 Kim Alvefur
-- This file is MIT/X11 licensed.
local s2smanager = require"core.s2smanager";
local add_filter = require "util.filters".add_filter;
local st = require "util.stanza";
local jid_split = require"util.jid".prepped_split;

local xmlns_bidi_feature = "urn:xmpp:features:bidi"
local xmlns_bidi = "urn:xmpp:bidi";
local noop = function () end
local core_process_stanza = prosody.core_process_stanza or core_process_stanza;
local traceback = debug.traceback;

local function handleerr(err) log("error", "Traceback[s2s]: %s: %s", tostring(err), traceback()); end
local function handlestanza(session, stanza)
	if stanza.attr.xmlns == "jabber:client" then --COMPAT: Prosody pre-0.6.2 may send jabber:client
		stanza.attr.xmlns = nil;
	-- stanza = session.filter("stanzas/in", stanza);
	if stanza then
		return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);

local function new_bidi(origin)
	local bidi_session, remote_host;
	origin.log("debug", "Creating bidirectional session wrapper");
	if origin.direction == "incoming" then -- then we create an "outgoing" bidirectional session
		local conflicting_session = hosts[origin.to_host].s2sout[origin.from_host]
		if conflicting_session then
			conflicting_session.log("info", "We already have an outgoing connection to %s, closing it...", origin.from_host);
			conflicting_session:close{ condition = "conflict", text = "Replaced by bidirectional stream" }
		remote_host = origin.from_host;
		bidi_session = s2smanager.new_outgoing(origin.to_host, origin.from_host)
	else -- outgoing -- then we create an "incoming" bidirectional session
		remote_host = origin.to_host;
		bidi_session = s2smanager.new_incoming(origin.conn)
		bidi_session.to_host = origin.from_host;
		bidi_session.from_host = origin.to_host;
		add_filter(origin, "stanzas/in", function(stanza)
			if stanza.attr.xmlns ~= nil then return stanza end
			local _, host = jid_split(stanza.attr.from);
			if host ~= remote_host then return stanza end
			handlestanza(bidi_session, stanza);
		end, 1);
	origin.bidi_session = bidi_session;
	bidi_session.sends2s = origin.sends2s;
	bidi_session.bounce_sendq = noop;
	bidi_session.notopen = nil;
	bidi_session.is_bidi = true;
	bidi_session.bidi_session = false;
	bidi_session.orig_session = origin; =;
	bidi_session.cert_identity_status = origin.cert_identity_status;
	bidi_session.cert_chain_status = origin.cert_chain_status;
	bidi_session.close = function(...)
		return origin.close(...);

	bidi_session.log("info", "Bidirectional session established");
	module:fire_event("s2s-authenticated", { session = bidi_session, host = remote_host });
	return bidi_session;

-- Incoming s2s
module:hook("s2s-stream-features", function(event)
	local origin, features = event.origin, event.features;
	if not origin.is_bidi and not hosts[].s2sout[origin.from_host] then
		module:log("debug", "Announcing support for bidirectional streams");
		features:tag("bidi", { xmlns = xmlns_bidi_feature }):up();

module:hook("stanza/urn:xmpp:bidi:bidi", function(event)
	local origin = event.session or event.origin;
	if not origin.is_bidi and not origin.bidi_session then
		module:log("debug", "%s requested bidirectional stream", origin.from_host);
		origin.do_bidi = true;
		return true;

-- Outgoing s2s
module:hook("stanza/", function(event)
	local origin = event.session or event.origin;
	if not ( origin.bidi_session or origin.is_bidi or origin.do_bidi)
	and event.stanza:get_child("bidi", xmlns_bidi_feature) then
		module:log("debug", "%s supports bidirectional streams", origin.to_host);
		origin.sends2s(st.stanza("bidi", { xmlns = xmlns_bidi }));
		origin.do_bidi = true;
end, 160);

function enable_bidi(event)
	local session = event.session;
	if session.do_bidi and not ( session.is_bidi or session.bidi_session ) then
		session.do_bidi = nil;

module:hook("s2sin-established", enable_bidi);
module:hook("s2sout-established", enable_bidi);

function disable_bidi(event)
	local session = event.session;
	if session.bidi_session then
		local bidi_session = session.bidi_session;
		session.bidi_session, bidi_session.orig_session = nil, nil;
		session.log("debug", "Tearing down bidirectional stream");
		s2smanager.destroy_session(bidi_session, event.reason);
	elseif session.orig_session then
		local orig_session = session.orig_session;
		orig_session.bidi_session, session.orig_session = nil, nil;
		orig_session.log("debug", "Tearing down bidirectional stream");
		s2smanager.destroy_session(orig_session, event.reason);

module:hook("s2sin-destroyed", disable_bidi);
module:hook("s2sout-destroyed", disable_bidi);