plugins/mod_component.lua
author Matthew Wild <mwild1@gmail.com>
Tue, 11 Oct 2022 11:37:55 +0100
changeset 12767 d26eefe98d09
parent 12690 5f182bccf33f
child 12981 74b9e05af71e
permissions -rw-r--r--
util.dbuffer: Add efficient shortcuts for discard() in certain cases If the buffer is already empty, nothing to do. If we're throwing away the whole buffer, we can just empty it and avoid read_chunk() (which in turn may collapse()). These shortcuts are much more efficient.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1405
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2490
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2490
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5728
diff changeset
     4
--
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
-- COPYING file in the source package for more information.
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
--
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
     9
module:set_global();
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local t_concat = table.concat;
9565
acf74ad0b795 Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents: 8891
diff changeset
    12
local tostring, type = tostring, type;
acf74ad0b795 Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents: 8891
diff changeset
    13
local xpcall = require "util.xpcall".xpcall;
5724
a49f32e3d73b mod_component: xpcall() stanza processing, as per other listeners, preventing potentially harmful 'top-level errors'
Matthew Wild <mwild1@gmail.com>
parents: 5370
diff changeset
    14
local traceback = debug.traceback;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    16
local logger = require "util.logger";
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
local sha1 = require "util.hashes".sha1;
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
    18
local st = require "util.stanza";
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
12690
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
    20
local jid_host = require "util.jid".host;
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    21
local new_xmpp_stream = require "util.xmppstream".new;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    22
local uuid_gen = require "util.uuid".generate;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    23
5013
ab693eea0869 mod_admin_adhoc, mod_admin_telnet, mod_bosh, mod_c2s, mod_component, mod_pep, mod_presence, mod_roster, mod_s2s: Import core_post_stanza from the global prosody table.
Kim Alvefur <zash@zash.se>
parents: 4993
diff changeset
    24
local core_process_stanza = prosody.core_process_stanza;
5370
7838acadb0fa mod_announce, mod_auth_anonymous, mod_c2s, mod_c2s, mod_component, mod_iq, mod_message, mod_presence, mod_tls: Access prosody.{hosts,bare_sessions,full_sessions} instead of the old globals
Kim Alvefur <zash@zash.se>
parents: 5316
diff changeset
    25
local hosts = prosody.hosts;
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    26
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
local log = module._log;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
5953
1c08d6cca552 mod_component: Enable TCP keepalives on component streams
Kim Alvefur <zash@zash.se>
parents: 5952
diff changeset
    29
local opt_keepalives = module:get_option_boolean("component_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true));
11544
1937b3c3efb5 mod_c2s, mod_s2s, mod_component, mod_bosh, mod_websockets: Set default stanza size limits
Matthew Wild <mwild1@gmail.com>
parents: 9565
diff changeset
    30
local stanza_size_limit = module:get_option_number("component_stanza_size_limit", module:get_option_number("s2s_stanza_size_limit", 1024*512));
5953
1c08d6cca552 mod_component: Enable TCP keepalives on component streams
Kim Alvefur <zash@zash.se>
parents: 5952
diff changeset
    31
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    32
local sessions = module:shared("sessions");
3581
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
    33
7889
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
    34
local function keepalive(event)
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
    35
	local session = event.session;
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
    36
	if not session.notopen then
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
    37
		return event.session.send(' ');
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
    38
	end
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
    39
end
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
    40
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    41
function module.add_host(module)
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    42
	if module:get_host_type() ~= "component" then
7362
a5a080c12c96 Update every link to the documentation to use HTTPS
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7304
diff changeset
    43
		error("Don't load mod_component manually, it should be for a component, please see https://prosody.im/doc/components", 0);
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    44
	end
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
    45
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    46
	local env = module.environment;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    47
	env.connected = false;
6916
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    48
	env.session = false;
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    49
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    50
	local send;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    51
7304
995777582044 mod_component: Some cleanup [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 7303
diff changeset
    52
	local function on_destroy(session, err) --luacheck: ignore 212/err
9874
8f4880576835 mod_component: Set module status to indicate whether component is connected
Matthew Wild <mwild1@gmail.com>
parents: 9788
diff changeset
    53
		module:set_status("warn", err and ("Disconnected: "..err) or "Disconnected");
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    54
		env.connected = false;
6916
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    55
		env.session = false;
3581
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
    56
		send = nil;
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
    57
		session.on_destroy = nil;
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
    58
	end
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
    59
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    60
	-- Handle authentication attempts by component
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    61
	local function handle_component_auth(event)
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    62
		local session, stanza = event.origin, event.stanza;
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
    63
4806
27a99c289b90 mod_component: Allow unauthenticated components to authenticate (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents: 4805
diff changeset
    64
		if session.type ~= "component_unauthed" then return; end
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
    65
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    66
		if (not session.host) or #stanza.tags > 0 then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    67
			(session.log or log)("warn", "Invalid component handshake for host: %s", session.host);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    68
			session:close("not-authorized");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    69
			return true;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    70
		end
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
    71
8137
b87e281a7d59 mod_component: Use typed config API
Kim Alvefur <zash@zash.se>
parents: 7889
diff changeset
    72
		local secret = module:get_option_string("component_secret");
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    73
		if not secret then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    74
			(session.log or log)("warn", "Component attempted to identify as %s, but component_secret is not set", session.host);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    75
			session:close("not-authorized");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    76
			return true;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    77
		end
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
    78
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    79
		local supplied_token = t_concat(stanza);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    80
		local calculated_token = sha1(session.streamid..secret, true);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    81
		if supplied_token:lower() ~= calculated_token:lower() then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    82
			module:log("info", "Component authentication failed for %s", session.host);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    83
			session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    84
			return true;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    85
		end
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
    86
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    87
		if env.connected then
6916
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    88
			local policy = module:get_option_string("component_conflict_resolve", "kick_new");
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    89
			if policy == "kick_old" then
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    90
				env.session:close{ condition = "conflict", text = "Replaced by a new connection" };
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    91
			else -- kick_new
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    92
				module:log("error", "Second component attempted to connect, denying connection");
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    93
				session:close{ condition = "conflict", text = "Component already connected" };
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    94
				return true;
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    95
			end
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    96
		end
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
    97
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
    98
		env.connected = true;
6916
c7a0d5299933 mod_component: Add config option for deciding what happens if a component connects while already connected (fixes #525)
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
    99
		env.session = session;
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   100
		send = session.send;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   101
		session.on_destroy = on_destroy;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   102
		session.component_validate_from = module:get_option_boolean("validate_from_addresses", true);
4805
1aeece2cc814 mod_component: Components start out as component_unauthed until successful authentication (thanks xnyhps)
Matthew Wild <mwild1@gmail.com>
parents: 4803
diff changeset
   103
		session.type = "component";
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   104
		module:log("info", "External component successfully authenticated");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   105
		session.send(st.stanza("handshake"));
6776
ef22c17cc24d mod_component: Fire an event on successful component authentication (For Goffi)
Kim Alvefur <zash@zash.se>
parents: 6380
diff changeset
   106
		module:fire_event("component-authenticated", { session = session });
9874
8f4880576835 mod_component: Set module status to indicate whether component is connected
Matthew Wild <mwild1@gmail.com>
parents: 9788
diff changeset
   107
		module:set_status("info", "Connected");
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
   108
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   109
		return true;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   110
	end
5952
31b800964dfb mod_component: Decrease priority of component auth hook
Kim Alvefur <zash@zash.se>
parents: 5728
diff changeset
   111
	module:hook("stanza/jabber:component:accept:handshake", handle_component_auth, -1);
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   112
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   113
	-- Handle stanzas addressed to this component
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   114
	local function handle_stanza(event)
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   115
		local stanza = event.stanza;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   116
		if send then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   117
			stanza.attr.xmlns = nil;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   118
			send(stanza);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   119
		else
5059
246ba539a5cd mod_component: For disconnected external components, if a name is specified in config, return it in disco#info replies.
Waqas Hussain <waqas20@gmail.com>
parents: 5013
diff changeset
   120
			if stanza.name == "iq" and stanza.attr.type == "get" and stanza.attr.to == module.host then
246ba539a5cd mod_component: For disconnected external components, if a name is specified in config, return it in disco#info replies.
Waqas Hussain <waqas20@gmail.com>
parents: 5013
diff changeset
   121
				local query = stanza.tags[1];
246ba539a5cd mod_component: For disconnected external components, if a name is specified in config, return it in disco#info replies.
Waqas Hussain <waqas20@gmail.com>
parents: 5013
diff changeset
   122
				local node = query.attr.node;
246ba539a5cd mod_component: For disconnected external components, if a name is specified in config, return it in disco#info replies.
Waqas Hussain <waqas20@gmail.com>
parents: 5013
diff changeset
   123
				if query.name == "query" and query.attr.xmlns == "http://jabber.org/protocol/disco#info" and (not node or node == "") then
246ba539a5cd mod_component: For disconnected external components, if a name is specified in config, return it in disco#info replies.
Waqas Hussain <waqas20@gmail.com>
parents: 5013
diff changeset
   124
					local name = module:get_option_string("name");
246ba539a5cd mod_component: For disconnected external components, if a name is specified in config, return it in disco#info replies.
Waqas Hussain <waqas20@gmail.com>
parents: 5013
diff changeset
   125
					if name then
8889
9aa35cb939ac mod_component: Fix a wrongly-placed closing parenthesis, fixes #1164.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8511
diff changeset
   126
						local reply = st.reply(stanza):tag("query", { xmlns = "http://jabber.org/protocol/disco#info" })
9aa35cb939ac mod_component: Fix a wrongly-placed closing parenthesis, fixes #1164.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8511
diff changeset
   127
							:tag("identity", { category = "component", type = "generic", name = module:get_option_string("name", "Prosody") }):up()
9aa35cb939ac mod_component: Fix a wrongly-placed closing parenthesis, fixes #1164.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8511
diff changeset
   128
							:tag("feature", { var = "http://jabber.org/protocol/disco#info" }):up();
9aa35cb939ac mod_component: Fix a wrongly-placed closing parenthesis, fixes #1164.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8511
diff changeset
   129
						event.origin.send(reply);
5059
246ba539a5cd mod_component: For disconnected external components, if a name is specified in config, return it in disco#info replies.
Waqas Hussain <waqas20@gmail.com>
parents: 5013
diff changeset
   130
						return true;
246ba539a5cd mod_component: For disconnected external components, if a name is specified in config, return it in disco#info replies.
Waqas Hussain <waqas20@gmail.com>
parents: 5013
diff changeset
   131
					end
246ba539a5cd mod_component: For disconnected external components, if a name is specified in config, return it in disco#info replies.
Waqas Hussain <waqas20@gmail.com>
parents: 5013
diff changeset
   132
				end
246ba539a5cd mod_component: For disconnected external components, if a name is specified in config, return it in disco#info replies.
Waqas Hussain <waqas20@gmail.com>
parents: 5013
diff changeset
   133
			end
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   134
			module:log("warn", "Component not connected, bouncing error for: %s", stanza:top_tag());
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   135
			if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
11039
ba1143ddae9b mod_component: Return extended error condition when not connected
Kim Alvefur <zash@zash.se>
parents: 11038
diff changeset
   136
				event.origin.send(st.error_reply(stanza, "wait", "remote-server-timeout", "Component unavailable", module.host)
ba1143ddae9b mod_component: Return extended error condition when not connected
Kim Alvefur <zash@zash.se>
parents: 11038
diff changeset
   137
					:tag("not-connected", { xmlns = "xmpp:prosody.im/protocol/component" }));
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   138
			end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   139
		end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   140
		return true;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   141
	end
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
   142
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   143
	module:hook("iq/bare", handle_stanza, -1);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   144
	module:hook("message/bare", handle_stanza, -1);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   145
	module:hook("presence/bare", handle_stanza, -1);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   146
	module:hook("iq/full", handle_stanza, -1);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   147
	module:hook("message/full", handle_stanza, -1);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   148
	module:hook("presence/full", handle_stanza, -1);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   149
	module:hook("iq/host", handle_stanza, -1);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   150
	module:hook("message/host", handle_stanza, -1);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   151
	module:hook("presence/host", handle_stanza, -1);
7889
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   152
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   153
	module:hook("component-read-timeout", keepalive, -1);
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   154
end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   155
7889
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   156
module:hook("component-read-timeout", keepalive, -1);
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   157
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   158
--- Network and stream part ---
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   159
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   160
local xmlns_component = 'jabber:component:accept';
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   161
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   162
local listener = {};
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   163
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   164
--- Callbacks/data for xmppstream to handle streams for us ---
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   165
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   166
local stream_callbacks = { default_ns = xmlns_component };
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   167
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   168
local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   169
7304
995777582044 mod_component: Some cleanup [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 7303
diff changeset
   170
function stream_callbacks.error(session, error, data)
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   171
	if session.destroyed then return; end
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9874
diff changeset
   172
	module:log("warn", "Error processing component stream: %s", error);
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   173
	if error == "no-stream" then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   174
		session:close("invalid-namespace");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   175
	elseif error == "parse-error" then
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9874
diff changeset
   176
		session.log("warn", "External component %s XML parse error: %s", session.host, data);
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   177
		session:close("not-well-formed");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   178
	elseif error == "stream-error" then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   179
		local condition, text = "undefined-condition";
8236
4e7269c53659 mod_component, mod_s2s: Iterate over child tags instead of child nodes (can include text) in stream error (same as 176b7f4e4ac9)
Kim Alvefur <zash@zash.se>
parents: 6776
diff changeset
   180
		for child in data:childtags(nil, xmlns_xmpp_streams) do
4e7269c53659 mod_component, mod_s2s: Iterate over child tags instead of child nodes (can include text) in stream error (same as 176b7f4e4ac9)
Kim Alvefur <zash@zash.se>
parents: 6776
diff changeset
   181
			if child.name ~= "text" then
4e7269c53659 mod_component, mod_s2s: Iterate over child tags instead of child nodes (can include text) in stream error (same as 176b7f4e4ac9)
Kim Alvefur <zash@zash.se>
parents: 6776
diff changeset
   182
				condition = child.name;
4e7269c53659 mod_component, mod_s2s: Iterate over child tags instead of child nodes (can include text) in stream error (same as 176b7f4e4ac9)
Kim Alvefur <zash@zash.se>
parents: 6776
diff changeset
   183
			else
4e7269c53659 mod_component, mod_s2s: Iterate over child tags instead of child nodes (can include text) in stream error (same as 176b7f4e4ac9)
Kim Alvefur <zash@zash.se>
parents: 6776
diff changeset
   184
				text = child:get_text();
4e7269c53659 mod_component, mod_s2s: Iterate over child tags instead of child nodes (can include text) in stream error (same as 176b7f4e4ac9)
Kim Alvefur <zash@zash.se>
parents: 6776
diff changeset
   185
			end
4e7269c53659 mod_component, mod_s2s: Iterate over child tags instead of child nodes (can include text) in stream error (same as 176b7f4e4ac9)
Kim Alvefur <zash@zash.se>
parents: 6776
diff changeset
   186
			if condition ~= "undefined-condition" and text then
4e7269c53659 mod_component, mod_s2s: Iterate over child tags instead of child nodes (can include text) in stream error (same as 176b7f4e4ac9)
Kim Alvefur <zash@zash.se>
parents: 6776
diff changeset
   187
				break;
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   188
			end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   189
		end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   190
		text = condition .. (text and (" ("..text..")") or "");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   191
		session.log("info", "Session closed by remote with error: %s", text);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   192
		session:close(nil, text);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   193
	end
3581
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
   194
end
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
   195
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   196
function stream_callbacks.streamopened(session, attr)
11244
0f7ecc9a4560 mod_component: Distinguish missing stream @to from unknown local component (thanks Daniel_W)
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
   197
	if not attr.to then
0f7ecc9a4560 mod_component: Distinguish missing stream @to from unknown local component (thanks Daniel_W)
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
   198
		session:close{ condition = "improper-addressing", text = "A 'to' attribute is required on stream headers" };
0f7ecc9a4560 mod_component: Distinguish missing stream @to from unknown local component (thanks Daniel_W)
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
   199
		return;
0f7ecc9a4560 mod_component: Distinguish missing stream @to from unknown local component (thanks Daniel_W)
Kim Alvefur <zash@zash.se>
parents: 11039
diff changeset
   200
	end
4655
9159546cb2f3 mod_component: Handle component connecting to non-existent host
Matthew Wild <mwild1@gmail.com>
parents: 4650
diff changeset
   201
	if not hosts[attr.to] or not hosts[attr.to].modules.component then
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   202
		session:close{ condition = "host-unknown", text = tostring(attr.to).." does not match any configured external components" };
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   203
		return;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   204
	end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   205
	session.host = attr.to;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   206
	session.streamid = uuid_gen();
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   207
	session.notopen = nil;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   208
	-- Return stream header
6063
e626ee2fe106 mod_c2s, mod_s2s, mod_component, util.xmppstream: Move all session:open_stream() functions to util.xmppstream
Kim Alvefur <zash@zash.se>
parents: 5954
diff changeset
   209
	session:open_stream();
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   210
end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   211
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   212
function stream_callbacks.streamclosed(session)
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   213
	session.log("debug", "Received </stream:stream>");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   214
	session:close();
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   215
end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   216
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9874
diff changeset
   217
local function handleerr(err) log("error", "Traceback[component]: %s", traceback(err, 2)); end
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   218
function stream_callbacks.handlestanza(session, stanza)
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   219
	-- Namespaces are icky.
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   220
	if not stanza.attr.xmlns and stanza.name == "handshake" then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   221
		stanza.attr.xmlns = xmlns_component;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   222
	end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   223
	if not stanza.attr.xmlns or stanza.attr.xmlns == "jabber:client" then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   224
		local from = stanza.attr.from;
12690
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   225
		if session.component_validate_from then
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   226
			if not from or (jid_host(from) ~= session.host) then
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   227
				-- Return error
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   228
				session.log("warn", "Component sent stanza with missing or invalid 'from' address");
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   229
				session:close{
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   230
					condition = "invalid-from";
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   231
					text = "Component tried to send from address <"..(from or "< [missing 'from' attribute] >")
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   232
						   .."> which is not in domain <"..tostring(session.host)..">";
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   233
				};
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   234
				return;
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   235
			end
12690
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   236
		elseif not from then
5f182bccf33f mod_component: Require 'from' attribute on stanzas by default
Matthew Wild <mwild1@gmail.com>
parents: 11872
diff changeset
   237
			stanza.attr.from = session.host;
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   238
		end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   239
		if not stanza.attr.to then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   240
			session.log("warn", "Rejecting stanza with no 'to' address");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   241
			session.send(st.error_reply(stanza, "modify", "bad-request", "Components MUST specify a 'to' address on stanzas"));
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   242
			return;
3581
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
   243
		end
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
   244
	end
5724
a49f32e3d73b mod_component: xpcall() stanza processing, as per other listeners, preventing potentially harmful 'top-level errors'
Matthew Wild <mwild1@gmail.com>
parents: 5370
diff changeset
   245
a49f32e3d73b mod_component: xpcall() stanza processing, as per other listeners, preventing potentially harmful 'top-level errors'
Matthew Wild <mwild1@gmail.com>
parents: 5370
diff changeset
   246
	if stanza then
9565
acf74ad0b795 Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
Matthew Wild <mwild1@gmail.com>
parents: 8891
diff changeset
   247
		return xpcall(core_process_stanza, handleerr, session, stanza);
5724
a49f32e3d73b mod_component: xpcall() stanza processing, as per other listeners, preventing potentially harmful 'top-level errors'
Matthew Wild <mwild1@gmail.com>
parents: 5370
diff changeset
   248
	end
3581
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
   249
end
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
   250
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   251
--- Closing a component connection
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   252
local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   253
local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   254
local function session_close(session, reason)
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   255
	if session.destroyed then return; end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   256
	if session.conn then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   257
		if session.notopen then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   258
			session.send("<?xml version='1.0'?>");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   259
			session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   260
		end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   261
		if reason then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   262
			if type(reason) == "string" then -- assume stream error
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   263
				module:log("info", "Disconnecting component, <stream:error> is: %s", reason);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   264
				session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
11872
ae093c259da2 mod_c2s,etc: Identify stanza object with appropriate function
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   265
			elseif st.is_stanza(reason) then
ae093c259da2 mod_c2s,etc: Identify stanza object with appropriate function
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   266
				module:log("info", "Disconnecting component, <stream:error> is: %s", reason);
ae093c259da2 mod_c2s,etc: Identify stanza object with appropriate function
Kim Alvefur <zash@zash.se>
parents: 11564
diff changeset
   267
				session.send(reason);
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   268
			elseif type(reason) == "table" then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   269
				if reason.condition then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   270
					local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   271
					if reason.text then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   272
						stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   273
					end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   274
					if reason.extra then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   275
						stanza:add_child(reason.extra);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   276
					end
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9874
diff changeset
   277
					module:log("info", "Disconnecting component, <stream:error> is: %s", stanza);
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   278
					session.send(stanza);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   279
				end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   280
			end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   281
		end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   282
		session.send("</stream:stream>");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   283
		session.conn:close();
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   284
		listener.ondisconnect(session.conn, "stream error");
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   285
	end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   286
end
3581
3f3f8227ba76 mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Waqas Hussain <waqas20@gmail.com>
parents: 3579
diff changeset
   287
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   288
--- Component connlistener
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   289
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   290
function listener.onconnect(conn)
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   291
	local _send = conn.write;
4805
1aeece2cc814 mod_component: Components start out as component_unauthed until successful authentication (thanks xnyhps)
Matthew Wild <mwild1@gmail.com>
parents: 4803
diff changeset
   292
	local session = { type = "component_unauthed", conn = conn, send = function (data) return _send(conn, tostring(data)); end };
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   293
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   294
	-- Logging functions --
5306
10bc0e2aa55e s2smanager: Generate session names used for logging the same way everywhere
Kim Alvefur <zash@zash.se>
parents: 5298
diff changeset
   295
	local conn_name = "jcp"..tostring(session):match("[a-f0-9]+$");
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   296
	session.log = logger.init(conn_name);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   297
	session.close = session_close;
5953
1c08d6cca552 mod_component: Enable TCP keepalives on component streams
Kim Alvefur <zash@zash.se>
parents: 5952
diff changeset
   298
1c08d6cca552 mod_component: Enable TCP keepalives on component streams
Kim Alvefur <zash@zash.se>
parents: 5952
diff changeset
   299
	if opt_keepalives then
1c08d6cca552 mod_component: Enable TCP keepalives on component streams
Kim Alvefur <zash@zash.se>
parents: 5952
diff changeset
   300
		conn:setoption("keepalive", opt_keepalives);
1c08d6cca552 mod_component: Enable TCP keepalives on component streams
Kim Alvefur <zash@zash.se>
parents: 5952
diff changeset
   301
	end
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
   302
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   303
	session.log("info", "Incoming Jabber component connection");
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
   304
11544
1937b3c3efb5 mod_c2s, mod_s2s, mod_component, mod_bosh, mod_websockets: Set default stanza size limits
Matthew Wild <mwild1@gmail.com>
parents: 9565
diff changeset
   305
	local stream = new_xmpp_stream(session, stream_callbacks, stanza_size_limit);
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   306
	session.stream = stream;
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
   307
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   308
	session.notopen = true;
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
   309
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   310
	function session.reset_stream()
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   311
		session.notopen = true;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   312
		session.stream:reset();
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   313
	end
3531
f41e1cfe92f4 mod_component: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3503
diff changeset
   314
7304
995777582044 mod_component: Some cleanup [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 7303
diff changeset
   315
	function session.data(_, data)
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   316
		local ok, err = stream:feed(data);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   317
		if ok then return; end
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9874
diff changeset
   318
		log("debug", "Received invalid XML (%s) %d bytes: %q", err, #data, data:sub(1, 300));
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   319
		session:close("not-well-formed");
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   320
	end
7876
8d1ebb9a9b44 mod_component: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7668
diff changeset
   321
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   322
	session.dispatch_stanza = stream_callbacks.handlestanza;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   323
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   324
	sessions[conn] = session;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   325
end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   326
function listener.onincoming(conn, data)
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   327
	local session = sessions[conn];
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   328
	session.data(conn, data);
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   329
end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   330
function listener.ondisconnect(conn, err)
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   331
	local session = sessions[conn];
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   332
	if session then
10115
0f335815244f plugins: Remove tostring call from logging
Kim Alvefur <zash@zash.se>
parents: 9874
diff changeset
   333
		(session.log or log)("info", "component disconnected: %s (%s)", session.host, err);
7668
2e553f80aedd mod_component: Fire 'component-disconnected' event on host, to maintain consistency - sessions with no host never authenticated. Fixes #737
Matthew Wild <mwild1@gmail.com>
parents: 7304
diff changeset
   334
		if session.host then
2e553f80aedd mod_component: Fire 'component-disconnected' event on host, to maintain consistency - sessions with no host never authenticated. Fixes #737
Matthew Wild <mwild1@gmail.com>
parents: 7304
diff changeset
   335
			module:context(session.host):fire_event("component-disconnected", { session = session, reason = err });
2e553f80aedd mod_component: Fire 'component-disconnected' event on host, to maintain consistency - sessions with no host never authenticated. Fixes #737
Matthew Wild <mwild1@gmail.com>
parents: 7304
diff changeset
   336
		end
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   337
		if session.on_destroy then session:on_destroy(err); end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   338
		sessions[conn] = nil;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   339
		for k in pairs(session) do
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   340
			if k ~= "log" and k ~= "close" then
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   341
				session[k] = nil;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   342
			end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   343
		end
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   344
		session.destroyed = true;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   345
	end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   346
end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   347
6380
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 5953
diff changeset
   348
function listener.ondetach(conn)
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 5953
diff changeset
   349
	sessions[conn] = nil;
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 5953
diff changeset
   350
end
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 5953
diff changeset
   351
7889
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   352
function listener.onreadtimeout(conn)
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   353
	local session = sessions[conn];
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   354
	if session then
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   355
		return (hosts[session.host] or prosody).events.fire_event("component-read-timeout", { session = session });
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   356
	end
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   357
end
679746cdf3cc mod_component: Add read timeout handler (same behaviour as c2s and s2s)
Kim Alvefur <zash@zash.se>
parents: 7876
diff changeset
   358
5120
bcabea740c00 mod_{admin_telnet,c2s,component,http,net_multiplex,s2s}: Use module:provides() instead of module:add_item().
Waqas Hussain <waqas20@gmail.com>
parents: 5059
diff changeset
   359
module:provides("net", {
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   360
	name = "component";
5298
6d34ed9fce69 mod_component: Make listener private (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents: 5120
diff changeset
   361
	private = true;
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   362
	listener = listener;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   363
	default_port = 5347;
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   364
	multiplex = {
5316
310c7e5bb906 mod_component: Look for the correct xmlns (thanks NebuK)
Kim Alvefur <zash@zash.se>
parents: 5306
diff changeset
   365
		pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:component:accept%1.*>";
4650
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   366
	};
464ca74ddf6a mod_component: Make a shared module, and move the xmppcomponent_listener into it ('port'ing over to portmanager). Ha ha.
Matthew Wild <mwild1@gmail.com>
parents: 4464
diff changeset
   367
});