core/componentmanager.lua
author Waqas Hussain <waqas20@gmail.com>
Wed, 10 Nov 2010 19:57:35 +0500
changeset 3597 8090880f0e18
parent 3587 d94aacb2771a
child 3602 a977d8be6277
permissions -rw-r--r--
hostmanager, componentmanager: hostmanager now handles component initialization at server start, not componentmanager.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1442
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2323
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2323
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 270
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 751
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 751
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 270
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 270
diff changeset
     8
1970
edab0c61fc02 componentmanager: Handle missing prosody object (helps us pass some tests)
Matthew Wild <mwild1@gmail.com>
parents: 1942
diff changeset
     9
local prosody = _G.prosody;
610
d98106902f74 Enable dialback for components
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
    10
local log = require "util.logger".init("componentmanager");
2629
fe207a2c5cef componentmanager: Use certmanager for creating contexts
Matthew Wild <mwild1@gmail.com>
parents: 2447
diff changeset
    11
local certmanager = require "core.certmanager";
751
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
    12
local configmanager = require "core.configmanager";
847
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    13
local modulemanager = require "core.modulemanager";
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    14
local jid_split = require "util.jid".split;
3435
6cb971a31397 componentmanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    15
local fire_event = prosody.events.fire_event;
1188
fa48e69c4786 core: hosts[*].events
Waqas Hussain <waqas20@gmail.com>
parents: 1177
diff changeset
    16
local events_new = require "util.events".new;
982
dbbeb73952e6 componentmanager: Improve default component stanza handler
Matthew Wild <mwild1@gmail.com>
parents: 970
diff changeset
    17
local st = require "util.stanza";
2323
b7f683c55a44 componentmanager: Set ssl_ctx[_in] for components correctly, and use global SSL context if available
Matthew Wild <mwild1@gmail.com>
parents: 2075
diff changeset
    18
local prosody, hosts = prosody, prosody.hosts;
2447
9eb539222f22 componentmanager: Add dialback_secret to components to allow s2s to work (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2323
diff changeset
    19
local uuid_gen = require "util.uuid".generate;
751
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
    20
2323
b7f683c55a44 componentmanager: Set ssl_ctx[_in] for components correctly, and use global SSL context if available
Matthew Wild <mwild1@gmail.com>
parents: 2075
diff changeset
    21
local pairs, setmetatable, type, tostring = pairs, setmetatable, type, tostring;
847
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    22
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    23
local components = {};
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    24
939
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    25
local disco_items = require "util.multitable".new();
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    26
local NULL = {};
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    27
847
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    28
module "componentmanager"
751
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
    29
961
b48ed2149d0a componentmanager: Reply with service-unavailable for unconnected components
Matthew Wild <mwild1@gmail.com>
parents: 945
diff changeset
    30
local function default_component_handler(origin, stanza)
2075
b4bfa6cb3d83 componentmanager: Improved logging for stanzas being bounced for unavailable components.
Waqas Hussain <waqas20@gmail.com>
parents: 1992
diff changeset
    31
	log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
1773
f47aa1d336b5 ComponentManager: Fixed: Default handler sent error replies on result stanzas.
Waqas Hussain <waqas20@gmail.com>
parents: 1772
diff changeset
    32
	if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
1774
53bd9c9f3103 ComponentManager: Removed dependency on core_route_stanza. The default component handler now replies using origin.send().
Waqas Hussain <waqas20@gmail.com>
parents: 1773
diff changeset
    33
		origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
982
dbbeb73952e6 componentmanager: Improve default component stanza handler
Matthew Wild <mwild1@gmail.com>
parents: 970
diff changeset
    34
	end
961
b48ed2149d0a componentmanager: Reply with service-unavailable for unconnected components
Matthew Wild <mwild1@gmail.com>
parents: 945
diff changeset
    35
end
b48ed2149d0a componentmanager: Reply with service-unavailable for unconnected components
Matthew Wild <mwild1@gmail.com>
parents: 945
diff changeset
    36
1851
0a4d4ba01db8 componentmanager: Preserve existing events table (if any) when registering a component
Matthew Wild <mwild1@gmail.com>
parents: 1850
diff changeset
    37
function create_component(host, component, events)
970
5516f9e66482 sessionmanager, componentmanager: Fix some wacky indentation (thanks greyback!)
Matthew Wild <mwild1@gmail.com>
parents: 961
diff changeset
    38
	-- TODO check for host well-formedness
3587
d94aacb2771a componentmanager, hostmanager, modulemanager, mod_component: Got rid of the useless hosts[*].connected property.
Waqas Hussain <waqas20@gmail.com>
parents: 3586
diff changeset
    39
	return { type = "component", host = host, s2sout = {},
3584
5d880c365dd4 componentmanager: Removed SSL context creation (mod_tls handles that now).
Waqas Hussain <waqas20@gmail.com>
parents: 3561
diff changeset
    40
			events = events or events_new(),
3469
011566d72331 componentmanager: Relocate disallow_s2s to its correct position (thanks darkhoptipomatus)
Matthew Wild <mwild1@gmail.com>
parents: 3462
diff changeset
    41
			dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen(),
011566d72331 componentmanager: Relocate disallow_s2s to its correct position (thanks darkhoptipomatus)
Matthew Wild <mwild1@gmail.com>
parents: 3462
diff changeset
    42
			disallow_s2s = configmanager.get(host, "core", "disallow_s2s"); };
847
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    43
end
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    44
3547
3242e70f1058 componentmanager: Removed an unused third parameter from the register_component function.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
    45
function register_component(host, component)
3587
d94aacb2771a componentmanager, hostmanager, modulemanager, mod_component: Got rid of the useless hosts[*].connected property.
Waqas Hussain <waqas20@gmail.com>
parents: 3586
diff changeset
    46
	if not hosts[host] or hosts[host].type == 'component' then
1264
498293bce4bf componentmanager: Create events object for configured hosts, and carry it over to a new component if one is registered with no events object
Matthew Wild <mwild1@gmail.com>
parents: 1257
diff changeset
    47
		local old_events = hosts[host] and hosts[host].events;
498293bce4bf componentmanager: Create events object for configured hosts, and carry it over to a new component if one is registered with no events object
Matthew Wild <mwild1@gmail.com>
parents: 1257
diff changeset
    48
847
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    49
		components[host] = component;
3547
3242e70f1058 componentmanager: Removed an unused third parameter from the register_component function.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
    50
		hosts[host] = create_component(host, component, old_events);
2447
9eb539222f22 componentmanager: Add dialback_secret to components to allow s2s to work (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2323
diff changeset
    51
1257
8c9f20d3a17f componentmanager: Add events object to registered components if they don't already have one
Matthew Wild <mwild1@gmail.com>
parents: 1188
diff changeset
    52
		-- Add events object if not already one
8c9f20d3a17f componentmanager: Add events object to registered components if they don't already have one
Matthew Wild <mwild1@gmail.com>
parents: 1188
diff changeset
    53
		if not hosts[host].events then
1264
498293bce4bf componentmanager: Create events object for configured hosts, and carry it over to a new component if one is registered with no events object
Matthew Wild <mwild1@gmail.com>
parents: 1257
diff changeset
    54
			hosts[host].events = old_events or events_new();
1257
8c9f20d3a17f componentmanager: Add events object to registered components if they don't already have one
Matthew Wild <mwild1@gmail.com>
parents: 1188
diff changeset
    55
		end
2447
9eb539222f22 componentmanager: Add dialback_secret to components to allow s2s to work (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2323
diff changeset
    56
9eb539222f22 componentmanager: Add dialback_secret to components to allow s2s to work (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2323
diff changeset
    57
		if not hosts[host].dialback_secret then
9eb539222f22 componentmanager: Add dialback_secret to components to allow s2s to work (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2323
diff changeset
    58
			hosts[host].dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen();
9eb539222f22 componentmanager: Add dialback_secret to components to allow s2s to work (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2323
diff changeset
    59
		end
9eb539222f22 componentmanager: Add dialback_secret to components to allow s2s to work (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2323
diff changeset
    60
939
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    61
		-- add to disco_items
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    62
		if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    63
			disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    64
		end
847
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    65
		modulemanager.load(host, "dialback");
1942
f74c267f7e1b componentmanager: Auto-load mod_tls for components #hack
Matthew Wild <mwild1@gmail.com>
parents: 1941
diff changeset
    66
		modulemanager.load(host, "tls");
847
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    67
		log("debug", "component added: "..host);
3547
3242e70f1058 componentmanager: Removed an unused third parameter from the register_component function.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
    68
		return hosts[host];
847
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    69
	else
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    70
		log("error", "Attempt to set component for existing host: "..host);
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    71
	end
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    72
end
673
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
    73
703
f9909efed20c componentmanager: Removed unneeded parameter from componentmanager.deregister_component
Waqas Hussain <waqas20@gmail.com>
parents: 673
diff changeset
    74
function deregister_component(host)
673
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
    75
	if components[host] then
1991
ed4cd15bf4dd componentmanager: Unload mod_tls for components before removing them.
Waqas Hussain <waqas20@gmail.com>
parents: 1990
diff changeset
    76
		modulemanager.unload(host, "tls");
751
7c22619fdb19 componentmanager: Add support for loading components defined in the config
Matthew Wild <mwild1@gmail.com>
parents: 703
diff changeset
    77
		modulemanager.unload(host, "dialback");
984
9acc1c2ceb2c componentmanager: Restore default component when unregistering
Matthew Wild <mwild1@gmail.com>
parents: 983
diff changeset
    78
		local host_config = configmanager.getconfig()[host];
9acc1c2ceb2c componentmanager: Restore default component when unregistering
Matthew Wild <mwild1@gmail.com>
parents: 983
diff changeset
    79
		if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
961
b48ed2149d0a componentmanager: Reply with service-unavailable for unconnected components
Matthew Wild <mwild1@gmail.com>
parents: 945
diff changeset
    80
			-- Set default handler
984
9acc1c2ceb2c componentmanager: Restore default component when unregistering
Matthew Wild <mwild1@gmail.com>
parents: 983
diff changeset
    81
			components[host] = default_component_handler;
961
b48ed2149d0a componentmanager: Reply with service-unavailable for unconnected components
Matthew Wild <mwild1@gmail.com>
parents: 945
diff changeset
    82
		else
b48ed2149d0a componentmanager: Reply with service-unavailable for unconnected components
Matthew Wild <mwild1@gmail.com>
parents: 945
diff changeset
    83
			-- Component not in config, or disabled, remove
1992
bbaba17537a9 componentmanager: Added a FIXME comment.
Waqas Hussain <waqas20@gmail.com>
parents: 1991
diff changeset
    84
			hosts[host] = nil; -- FIXME do proper unload of all modules and other cleanup before removing
984
9acc1c2ceb2c componentmanager: Restore default component when unregistering
Matthew Wild <mwild1@gmail.com>
parents: 983
diff changeset
    85
			components[host] = nil;
961
b48ed2149d0a componentmanager: Reply with service-unavailable for unconnected components
Matthew Wild <mwild1@gmail.com>
parents: 945
diff changeset
    86
		end
939
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    87
		-- remove from disco_items
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    88
		if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    89
			disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
b832f786af62 Added: componentmanager: Automatically add component.host.name to the disco items list of host.name
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    90
		end
673
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
    91
		log("debug", "component removed: "..host);
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
    92
		return true;
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
    93
	else
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
    94
		log("error", "Attempt to remove component for non-existing host: "..host);
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
    95
	end
c9bc58e84e96 componentmanager: Added support for component deregistering
Waqas Hussain <waqas20@gmail.com>
parents: 638
diff changeset
    96
end
847
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
    97
1693
56e23b0b3893 componentmanager: Added function to get a list of children for a given hostname
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    98
function get_children(host)
56e23b0b3893 componentmanager: Added function to get a list of children for a given hostname
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    99
	return disco_items:get(host) or NULL;
56e23b0b3893 componentmanager: Added function to get a list of children for a given hostname
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
   100
end
56e23b0b3893 componentmanager: Added function to get a list of children for a given hostname
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
   101
847
2d424936723c core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
Matthew Wild <mwild1@gmail.com>
parents: 777
diff changeset
   102
return _M;