core/hostmanager.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 27 Mar 2024 15:35:15 +0000
branch0.12
changeset 13469 54a936345aaa
parent 11509 61d38c4b749d
child 12976 ead41e25ebc0
permissions -rw-r--r--
prosodyctl check: Warn about invalid domain names in the config file This ensures that domain names of virtual hosts and components are valid in XMPP, and that they are encoded correctly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1467
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2877
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2877
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5377
diff changeset
     4
--
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1467
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1467
diff changeset
     6
-- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1467
diff changeset
     7
--
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1467
diff changeset
     8
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
local configmanager = require "core.configmanager";
1975
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
    10
local modulemanager = require "core.modulemanager";
1188
fa48e69c4786 core: hosts[*].events
Waqas Hussain <waqas20@gmail.com>
parents: 1095
diff changeset
    11
local events_new = require "util.events".new;
3599
adc0b4df6fdd hostmanager: Added function get_children(host) which copies componentmanager.get_children(host).
Waqas Hussain <waqas20@gmail.com>
parents: 3597
diff changeset
    12
local disco_items = require "util.multitable".new();
adc0b4df6fdd hostmanager: Added function get_children(host) which copies componentmanager.get_children(host).
Waqas Hussain <waqas20@gmail.com>
parents: 3597
diff changeset
    13
local NULL = {};
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
3434
428f13c098d5 hostmanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 2984
diff changeset
    15
local log = require "util.logger".init("hostmanager");
428f13c098d5 hostmanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 2984
diff changeset
    16
5360
e44cfbf0ae8d hostmanager: Use prosody.hosts instead of 'hosts' global
Matthew Wild <mwild1@gmail.com>
parents: 5357
diff changeset
    17
local hosts = prosody.hosts;
3434
428f13c098d5 hostmanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 2984
diff changeset
    18
local prosody_events = prosody.events;
1975
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
    19
if not _G.prosody.incoming_s2s then
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
    20
	require "core.s2smanager";
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
    21
end
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
    22
local incoming_s2s = _G.prosody.incoming_s2s;
5347
32e25358c3c2 hostmanager: Import core_post_stanza from the global prosody table
Kim Alvefur <zash@zash.se>
parents: 5123
diff changeset
    23
local core_route_stanza = _G.prosody.core_route_stanza;
1975
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
    24
7146
fa6cd0e9105d hostmanager: Remove unused imports of 'jid_split' and 'select'
Matthew Wild <mwild1@gmail.com>
parents: 6794
diff changeset
    25
local pairs, rawget = pairs, rawget;
3717
135128fdf565 hostmanager: Improved error handling.
Waqas Hussain <waqas20@gmail.com>
parents: 3716
diff changeset
    26
local tostring, type = tostring, type;
6709
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    27
local setmetatable = setmetatable;
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
6782
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6709
diff changeset
    29
local _ENV = nil;
8558
4f0f5b49bb03 vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8256
diff changeset
    30
-- luacheck: std none
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
6709
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    32
local host_mt = { }
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    33
function host_mt:__tostring()
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    34
	if self.type == "component" then
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    35
		local typ = configmanager.get(self.host, "component_module");
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    36
		if typ == "component" then
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    37
			return ("Component %q"):format(self.host);
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    38
		end
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    39
		return ("Component %q %q"):format(self.host, typ);
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    40
	elseif self.type == "local" then
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    41
		return ("VirtualHost %q"):format(self.host);
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    42
	end
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    43
end
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    44
1095
cad4205f4925 hostmanager: Reduce log output at startup to 'debug'
Matthew Wild <mwild1@gmail.com>
parents: 749
diff changeset
    45
local hosts_loaded_once;
cad4205f4925 hostmanager: Reduce log output at startup to 'debug'
Matthew Wild <mwild1@gmail.com>
parents: 749
diff changeset
    46
6782
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6709
diff changeset
    47
local activate, deactivate;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6709
diff changeset
    48
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
local function load_enabled_hosts(config)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	local defined_hosts = config or configmanager.getconfig();
2855
66a1aa0bedc0 hostmanager: Log an error if no hosts are defined
Matthew Wild <mwild1@gmail.com>
parents: 1980
diff changeset
    51
	local activated_any_host;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5377
diff changeset
    52
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
	for host, host_config in pairs(defined_hosts) do
5357
ac530c44772e configmanager, hostmanager, prosody: Almost complete removal of section-related code, and the infamous 'core' section. Still backwards-compatible with API users.
Matthew Wild <mwild1@gmail.com>
parents: 5347
diff changeset
    54
		if host ~= "*" and host_config.enabled ~= false then
ac530c44772e configmanager, hostmanager, prosody: Almost complete removal of section-related code, and the infamous 'core' section. Still backwards-compatible with API users.
Matthew Wild <mwild1@gmail.com>
parents: 5347
diff changeset
    55
			if not host_config.component_module then
3597
8090880f0e18 hostmanager, componentmanager: hostmanager now handles component initialization at server start, not componentmanager.
Waqas Hussain <waqas20@gmail.com>
parents: 3594
diff changeset
    56
				activated_any_host = true;
8090880f0e18 hostmanager, componentmanager: hostmanager now handles component initialization at server start, not componentmanager.
Waqas Hussain <waqas20@gmail.com>
parents: 3594
diff changeset
    57
			end
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
			activate(host, host_config);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
		end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5377
diff changeset
    61
2855
66a1aa0bedc0 hostmanager: Log an error if no hosts are defined
Matthew Wild <mwild1@gmail.com>
parents: 1980
diff changeset
    62
	if not activated_any_host then
2981
15a7afea7f59 hostmanager: Re-word log messages in line with config changes
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
    63
		log("error", "No active VirtualHost entries in the config file. This may cause unexpected behaviour as no modules will be loaded.");
2855
66a1aa0bedc0 hostmanager: Log an error if no hosts are defined
Matthew Wild <mwild1@gmail.com>
parents: 1980
diff changeset
    64
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5377
diff changeset
    65
3434
428f13c098d5 hostmanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 2984
diff changeset
    66
	prosody_events.fire_event("hosts-activated", defined_hosts);
1095
cad4205f4925 hostmanager: Reduce log output at startup to 'debug'
Matthew Wild <mwild1@gmail.com>
parents: 749
diff changeset
    67
	hosts_loaded_once = true;
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
3434
428f13c098d5 hostmanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 2984
diff changeset
    70
prosody_events.add_handler("server-starting", load_enabled_hosts);
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
4460
a813a130cede hostmanager: Add send() method to hosts
Matthew Wild <mwild1@gmail.com>
parents: 3984
diff changeset
    72
local function host_send(stanza)
a813a130cede hostmanager: Add send() method to hosts
Matthew Wild <mwild1@gmail.com>
parents: 3984
diff changeset
    73
	core_route_stanza(nil, stanza);
a813a130cede hostmanager: Add send() method to hosts
Matthew Wild <mwild1@gmail.com>
parents: 3984
diff changeset
    74
end
a813a130cede hostmanager: Add send() method to hosts
Matthew Wild <mwild1@gmail.com>
parents: 3984
diff changeset
    75
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
function activate(host, host_config)
5361
38e7a5fafb28 hostmanager: Use rawget to check for activated hosts to prevent recursion in dynamic host loaders
Matthew Wild <mwild1@gmail.com>
parents: 5360
diff changeset
    77
	if rawget(hosts, host) then return nil, "The host "..host.." is already activated"; end
3715
d7574530572c hostmanager: activate() now gets the host config from configmanager when a config isn't given.
Waqas Hussain <waqas20@gmail.com>
parents: 3601
diff changeset
    78
	host_config = host_config or configmanager.getconfig()[host];
3717
135128fdf565 hostmanager: Improved error handling.
Waqas Hussain <waqas20@gmail.com>
parents: 3716
diff changeset
    79
	if not host_config then return nil, "Couldn't find the host "..tostring(host).." defined in the current config"; end
3594
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    80
	local host_session = {
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    81
		host = host;
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    82
		s2sout = {};
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    83
		events = events_new();
4460
a813a130cede hostmanager: Add send() method to hosts
Matthew Wild <mwild1@gmail.com>
parents: 3984
diff changeset
    84
		send = host_send;
5123
7c5c86fa552e hostmanager, modulemanager: Ensure hosts[*].modules always exists.
Waqas Hussain <waqas20@gmail.com>
parents: 5081
diff changeset
    85
		modules = {};
3594
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    86
	};
8562
d5d746e4c453 hostmanager: Add a logging close method in case something tries to close the origin of local-originated stanzas (see #1084)
Kim Alvefur <zash@zash.se>
parents: 8254
diff changeset
    87
	function host_session:close(reason)
d5d746e4c453 hostmanager: Add a logging close method in case something tries to close the origin of local-originated stanzas (see #1084)
Kim Alvefur <zash@zash.se>
parents: 8254
diff changeset
    88
		log("debug", "Attempt to close host session %s with reason: %s", self.host, reason);
d5d746e4c453 hostmanager: Add a logging close method in case something tries to close the origin of local-originated stanzas (see #1084)
Kim Alvefur <zash@zash.se>
parents: 8254
diff changeset
    89
	end
6709
60c692828cf6 hostmanager: Metatable with __tostring on hosts
Kim Alvefur <zash@zash.se>
parents: 6621
diff changeset
    90
	setmetatable(host_session, host_mt);
5357
ac530c44772e configmanager, hostmanager, prosody: Almost complete removal of section-related code, and the infamous 'core' section. Still backwards-compatible with API users.
Matthew Wild <mwild1@gmail.com>
parents: 5347
diff changeset
    91
	if not host_config.component_module then -- host
3594
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    92
		host_session.type = "local";
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    93
		host_session.sessions = {};
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    94
	else -- component
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    95
		host_session.type = "component";
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    96
	end
d3b57562cd87 hostmanager: Added support for components to hostmanager.activate().
Waqas Hussain <waqas20@gmail.com>
parents: 3587
diff changeset
    97
	hosts[host] = host_session;
8254
cb2d86f4b511 hostmanager: Add support for 'disco_hidden' option to hide hosts from disco#items (thanks Ge0rG)
Matthew Wild <mwild1@gmail.com>
parents: 6782
diff changeset
    98
	if not host_config.disco_hidden and not host:match("[@/]") then
5357
ac530c44772e configmanager, hostmanager, prosody: Almost complete removal of section-related code, and the infamous 'core' section. Still backwards-compatible with API users.
Matthew Wild <mwild1@gmail.com>
parents: 5347
diff changeset
    99
		disco_items:set(host:match("%.(.*)") or "*", host, host_config.name or true);
3601
829e23b374cc hostmanager: Don't include hosts with '@' or '/' in the name in the get_children(host) result.
Waqas Hussain <waqas20@gmail.com>
parents: 3599
diff changeset
   100
	end
5357
ac530c44772e configmanager, hostmanager, prosody: Almost complete removal of section-related code, and the infamous 'core' section. Still backwards-compatible with API users.
Matthew Wild <mwild1@gmail.com>
parents: 5347
diff changeset
   101
	for option_name in pairs(host_config) do
2981
15a7afea7f59 hostmanager: Re-word log messages in line with config changes
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
   102
		if option_name:match("_ports$") or option_name:match("_interface$") then
15a7afea7f59 hostmanager: Re-word log messages in line with config changes
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
   103
			log("warn", "%s: Option '%s' has no effect for virtual hosts - put it in the server-wide section instead", host, option_name);
1614
951ed38ad64f hostmanager: Warn when user puts port configuration under vhost section
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   104
		end
951ed38ad64f hostmanager: Warn when user puts port configuration under vhost section
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   105
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5377
diff changeset
   106
1095
cad4205f4925 hostmanager: Reduce log output at startup to 'debug'
Matthew Wild <mwild1@gmail.com>
parents: 749
diff changeset
   107
	log((hosts_loaded_once and "info") or "debug", "Activated host: %s", host);
4732
29ff25c8bf56 hostmanager: Remove unused host_config parameter from host-activated event
Matthew Wild <mwild1@gmail.com>
parents: 4731
diff changeset
   108
	prosody_events.fire_event("host-activated", host);
3597
8090880f0e18 hostmanager, componentmanager: hostmanager now handles component initialization at server start, not componentmanager.
Waqas Hussain <waqas20@gmail.com>
parents: 3594
diff changeset
   109
	return true;
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
1975
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   112
function deactivate(host, reason)
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   113
	local host_session = hosts[host];
3717
135128fdf565 hostmanager: Improved error handling.
Waqas Hussain <waqas20@gmail.com>
parents: 3716
diff changeset
   114
	if not host_session then return nil, "The host "..tostring(host).." is not activated"; end
575
428c951d0a33 Log in hostmanager when a vhost is activated/deactivated
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
   115
	log("info", "Deactivating host: %s", host);
4730
2587e249927f hostmanager: Convert host-deactivating event parameters to a table
Matthew Wild <mwild1@gmail.com>
parents: 4508
diff changeset
   116
	prosody_events.fire_event("host-deactivating", { host = host, host_session = host_session, reason = reason });
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5377
diff changeset
   117
3717
135128fdf565 hostmanager: Improved error handling.
Waqas Hussain <waqas20@gmail.com>
parents: 3716
diff changeset
   118
	if type(reason) ~= "table" then
135128fdf565 hostmanager: Improved error handling.
Waqas Hussain <waqas20@gmail.com>
parents: 3716
diff changeset
   119
		reason = { condition = "host-gone", text = tostring(reason or "This server has stopped serving "..host) };
135128fdf565 hostmanager: Improved error handling.
Waqas Hussain <waqas20@gmail.com>
parents: 3716
diff changeset
   120
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5377
diff changeset
   121
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
	-- Disconnect local users, s2s connections
4731
84596fc32b31 hostmanager: Add some TODO comments
Matthew Wild <mwild1@gmail.com>
parents: 4730
diff changeset
   123
	-- TODO: These should move to mod_c2s and mod_s2s (how do they know they're being unloaded and not reloaded?)
1975
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   124
	if host_session.sessions then
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   125
		for username, user in pairs(host_session.sessions) do
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   126
			for resource, session in pairs(user.sessions) do
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   127
				log("debug", "Closing connection for %s@%s/%s", username, host, resource);
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   128
				session:close(reason);
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   129
			end
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
		end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
	end
1975
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   132
	if host_session.s2sout then
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   133
		for remotehost, session in pairs(host_session.s2sout) do
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   134
			if session.close then
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   135
				log("debug", "Closing outgoing connection to %s", remotehost);
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   136
				session:close(reason);
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   137
			end
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   138
		end
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   139
	end
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   140
	for remote_session in pairs(incoming_s2s) do
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   141
		if remote_session.to_host == host then
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   142
			log("debug", "Closing incoming connection from %s", remote_session.from_host or "<unknown>");
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   143
			remote_session:close(reason);
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   144
		end
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   145
	end
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   146
4731
84596fc32b31 hostmanager: Add some TODO comments
Matthew Wild <mwild1@gmail.com>
parents: 4730
diff changeset
   147
	-- TODO: This should be done in modulemanager
1975
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   148
	if host_session.modules then
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   149
		for module in pairs(host_session.modules) do
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   150
			modulemanager.unload(host, module);
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   151
		end
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   152
	end
a9998fac292c hostmanager: deactivate() now accepts a reason, closes s2s connections as well as disconnecting users, and unloads modules for the host
Matthew Wild <mwild1@gmail.com>
parents: 1974
diff changeset
   153
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
	hosts[host] = nil;
3601
829e23b374cc hostmanager: Don't include hosts with '@' or '/' in the name in the get_children(host) result.
Waqas Hussain <waqas20@gmail.com>
parents: 3599
diff changeset
   155
	if not host:match("[@/]") then
829e23b374cc hostmanager: Don't include hosts with '@' or '/' in the name in the get_children(host) result.
Waqas Hussain <waqas20@gmail.com>
parents: 3599
diff changeset
   156
		disco_items:remove(host:match("%.(.*)") or "*", host);
829e23b374cc hostmanager: Don't include hosts with '@' or '/' in the name in the get_children(host) result.
Waqas Hussain <waqas20@gmail.com>
parents: 3599
diff changeset
   157
	end
3434
428f13c098d5 hostmanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 2984
diff changeset
   158
	prosody_events.fire_event("host-deactivated", host);
575
428c951d0a33 Log in hostmanager when a vhost is activated/deactivated
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
   159
	log("info", "Deactivated host: %s", host);
3716
ebb2c6209e24 hostmanager: deactivate() now returns true on success.
Waqas Hussain <waqas20@gmail.com>
parents: 3715
diff changeset
   160
	return true;
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
6782
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6709
diff changeset
   163
local function get_children(host)
3599
adc0b4df6fdd hostmanager: Added function get_children(host) which copies componentmanager.get_children(host).
Waqas Hussain <waqas20@gmail.com>
parents: 3597
diff changeset
   164
	return disco_items:get(host) or NULL;
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
6782
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6709
diff changeset
   167
return {
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6709
diff changeset
   168
	activate = activate;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6709
diff changeset
   169
	deactivate = deactivate;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6709
diff changeset
   170
	get_children = get_children;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6709
diff changeset
   171
}