net/xmppcomponent_listener.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 14 Mar 2010 03:04:04 +0000
changeset 2755 28f2416a3e4e
parent 2754 b73055c5d8a1
child 2756 d81af905ccce
permissions -rw-r--r--
net.xmppcomponent_listener: Don't handle stream errors from component sessions that have already been destroyed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1249
diff changeset
     1
-- Prosody IM
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
-- Copyright (C) 2008-2009 Matthew Wild
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
-- Copyright (C) 2008-2009 Waqas Hussain
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
-- 
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
-- COPYING file in the source package for more information.
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
--
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local hosts = _G.hosts;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local t_concat = table.concat;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
local lxp = require "lxp";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
local logger = require "util.logger";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
local config = require "core.configmanager";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
local connlisteners = require "net.connlisteners";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
local cm_register_component = require "core.componentmanager".register_component;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
local cm_deregister_component = require "core.componentmanager".deregister_component;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
local uuid_gen = require "util.uuid".generate;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
local sha1 = require "util.hashes".sha1;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
local st = require "util.stanza";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
local init_xmlhandlers = require "core.xmlhandlers";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
local sessions = {};
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
local log = logger.init("componentlistener");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
local component_listener = { default_port = 5347; default_mode = "*a"; default_interface = config.get("*", "core", "component_interface") or "127.0.0.1" };
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
local xmlns_component = 'jabber:component:accept';
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
--- Callbacks/data for xmlhandlers to handle streams for us ---
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
2466
0e44b6035210 net.xmpp{client,server,component}: Update for new xmlhandlers syntax
Matthew Wild <mwild1@gmail.com>
parents: 2163
diff changeset
    35
local stream_callbacks = { default_ns = xmlns_component };
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
2754
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    37
local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    38
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
function stream_callbacks.error(session, error, data, data2)
2755
28f2416a3e4e net.xmppcomponent_listener: Don't handle stream errors from component sessions that have already been destroyed
Matthew Wild <mwild1@gmail.com>
parents: 2754
diff changeset
    40
	if session.destroyed then return; end
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
	log("warn", "Error processing component stream: "..tostring(error));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
	if error == "no-stream" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
		session:close("invalid-namespace");
2754
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    44
	elseif error == "parse-error" then
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    45
		session.log("warn", "External component %s XML parse error: %s", tostring(session.host), tostring(data));
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
		session:close("xml-not-well-formed");
2754
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    47
	elseif error == "stream-error" then
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    48
		local condition, text = "undefined-condition";
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    49
		for child in data:children() do
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    50
			if child.attr.xmlns == xmlns_xmpp_streams then
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    51
				if child.name ~= "text" then
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    52
					condition = child.name;
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    53
				else
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    54
					text = child:get_text();
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    55
				end
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    56
				if condition ~= "undefined-condition" and text then
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    57
					break;
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    58
				end
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    59
			end
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    60
		end
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    61
		text = condition .. (text and (" ("..text..")") or "");
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    62
		session.log("info", "Session closed by remote with error: %s", text);
b73055c5d8a1 net.xmppcomponent_listener: Fix to correctly handle stream errors from components
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
    63
		session:close(nil, text);
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
	end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
function stream_callbacks.streamopened(session, attr)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
	if config.get(attr.to, "core", "component_module") ~= "component" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
		-- Trying to act as a component domain which 
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
		-- hasn't been configured
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
		session:close{ condition = "host-unknown", text = tostring(attr.to).." does not match any configured external components" };
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
		return;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
	end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
	
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
	-- Store the original host (this is used for config, etc.)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
	session.user = attr.to;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
	-- Set the host for future reference
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
	session.host = config.get(attr.to, "core", "component_address") or attr.to;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
	-- Note that we don't create the internal component 
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	-- until after the external component auths successfully
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
	session.streamid = uuid_gen();
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	session.notopen = nil;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	session.send(st.stanza("stream:stream", { xmlns=xmlns_component,
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
			["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag());
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
function stream_callbacks.streamclosed(session)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
	session.send("</stream:stream>");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
	session.notopen = true;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
local core_process_stanza = core_process_stanza;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
function stream_callbacks.handlestanza(session, stanza)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
	-- Namespaces are icky.
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
	if not stanza.attr.xmlns and stanza.name == "handshake" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
		stanza.attr.xmlns = xmlns_component;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
	end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
	return core_process_stanza(session, stanza);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
--- Closing a component connection
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
2466
0e44b6035210 net.xmpp{client,server,component}: Update for new xmlhandlers syntax
Matthew Wild <mwild1@gmail.com>
parents: 2163
diff changeset
   107
local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
local function session_close(session, reason)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
	local log = session.log or log;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
	if session.conn then
1617
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
   111
		if session.notopen then
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
   112
			session.send("<?xml version='1.0'?>");
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
   113
			session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
   114
		end
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
		if reason then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
			if type(reason) == "string" then -- assume stream error
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
				log("info", "Disconnecting component, <stream:error> is: %s", reason);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
				session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
			elseif type(reason) == "table" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
				if reason.condition then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
					local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
					if reason.text then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
						stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
					end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
					if reason.extra then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
						stanza:add_child(reason.extra);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
					end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
					log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
					session.send(stanza);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
				elseif reason.name then -- a stanza
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
					log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
					session.send(reason);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
				end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
			end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
		end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
		session.send("</stream:stream>");
2489
d8296ab96f55 xmppcomponent_listener: Update for new connection API
Matthew Wild <mwild1@gmail.com>
parents: 2466
diff changeset
   137
		session.conn:close();
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 2077
diff changeset
   138
		component_listener.ondisconnect(session.conn, "stream error");
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
	end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
--- Component connlistener
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 2077
diff changeset
   143
function component_listener.onincoming(conn, data)
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
	local session = sessions[conn];
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
	if not session then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
		local _send = conn.write;
2135
2119a5d603d8 xmppcomponent_listener: Use new API for writing to components, fixes traceback (thanks Tobias)
Matthew Wild <mwild1@gmail.com>
parents: 2129
diff changeset
   147
		session = { type = "component", conn = conn, send = function (data) return _send(conn, tostring(data)); end };
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
		sessions[conn] = session;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
		-- Logging functions --
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
		
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
		local conn_name = "jcp"..tostring(conn):match("[a-f0-9]+$");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
		session.log = logger.init(conn_name);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
		session.close = session_close;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
		
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
		session.log("info", "Incoming Jabber component connection");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
		
2077
e33658f6052c Changed separator between attribute names and prefixes from '|' to '\1' (optimization and cleanup).
Waqas Hussain <waqas20@gmail.com>
parents: 1635
diff changeset
   158
		local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1");
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
		session.parser = parser;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
		
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
		session.notopen = true;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
		
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
		function session.data(conn, data)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
			local ok, err = parser:parse(data);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
			if ok then return; end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
			session:close("xml-not-well-formed");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
		end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
		
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
		session.dispatch_stanza = stream_callbacks.handlestanza;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
		
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
	end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
	if data then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
		session.data(conn, data);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
	end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
	
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 2077
diff changeset
   177
function component_listener.ondisconnect(conn, err)
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
	local session = sessions[conn];
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
	if session then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   180
		(session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
		if session.host then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
			log("debug", "Deregistering component");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   183
			cm_deregister_component(session.host);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
			hosts[session.host].connected = nil;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
		end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
		sessions[conn]  = nil;
1635
33d087466767 xmppcomponent_listener: Fix to collect component sessions on disconnect
Matthew Wild <mwild1@gmail.com>
parents: 1617
diff changeset
   187
		for k in pairs(session) do session[k] = nil; end
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
		session = nil;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   189
	end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   190
end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   191
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   192
connlisteners.register('xmppcomponent', component_listener);