core/configmanager.lua
author Kim Alvefur <zash@zash.se>
Thu, 04 Nov 2021 01:00:06 +0100
branch0.11
changeset 12093 76b4e3f12b53
parent 9244 2d82a926826f
child 9878 c9f5ccdcdf80
permissions -rw-r--r--
mod_pep: Wipe pubsub service on user deletion Data is already wiped from storage, but this ensures everything is properly unsubscribed, possibly with notifications etc. Clears recipient cache as well, since it is no longer relevant.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1504
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2862
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2862
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
     4
--
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 750
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 750
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 466
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 466
diff changeset
     8
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
local _G = _G;
9244
2d82a926826f configmanager: Allow referencing environment variables in the config as as ENV_<name>
Matthew Wild <mwild1@gmail.com>
parents: 8695
diff changeset
    10
local setmetatable, rawget, rawset, io, os, error, dofile, type, pairs =
2d82a926826f configmanager: Allow referencing environment variables in the config as as ENV_<name>
Matthew Wild <mwild1@gmail.com>
parents: 8695
diff changeset
    11
      setmetatable, rawget, rawset, io, os, error, dofile, type, pairs;
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    12
local format, math_max = string.format, math.max;
2861
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
    13
5021
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 4530
diff changeset
    14
local envload = require"util.envload".envload;
6166
46cb87d531a7 configmanager: Delay importing LuaFileSystem until needed by an Include line
Kim Alvefur <zash@zash.se>
parents: 5811
diff changeset
    15
local deps = require"util.dependencies";
6164
ef4024f6bc40 core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents: 5814
diff changeset
    16
local resolve_relative_path = require"util.paths".resolve_relative_path;
ef4024f6bc40 core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents: 5814
diff changeset
    17
local glob_to_pattern = require"util.paths".glob_to_pattern;
3609
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    18
local path_sep = package.config:sub(1,1);
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    19
6782
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6721
diff changeset
    20
local encodings = deps.softreq"util.encodings";
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6721
diff changeset
    21
local nameprep = encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
6323
5926f01e5cd2 configmanager: nameprep VirtualHost and Component names
Matthew Wild <mwild1@gmail.com>
parents: 6166
diff changeset
    22
6782
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6721
diff changeset
    23
local _M = {};
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6721
diff changeset
    24
local _ENV = nil;
8558
4f0f5b49bb03 vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8161
diff changeset
    25
-- luacheck: std none
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
6164
ef4024f6bc40 core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents: 5814
diff changeset
    27
_M.resolve_relative_path = resolve_relative_path; -- COMPAT
ef4024f6bc40 core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents: 5814
diff changeset
    28
8156
c22d5680ca68 configmanager: Remove support for multiple parsers, fixes #852.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7950
diff changeset
    29
local parser = nil;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
6718
03a283aa679e configmanager: Rename unused function argument [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6717
diff changeset
    31
local config_mt = { __index = function (t, _) return rawget(t, "*"); 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: 5124
diff changeset
    32
local config = setmetatable({ ["*"] = { } }, config_mt);
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
-- When host not found, use global
5380
e119e378b1d9 configmanager: Fix so unset variables are searched for in the global section
Kim Alvefur <zash@zash.se>
parents: 5357
diff changeset
    35
local host_mt = { __index = function(_, k) return config["*"][k] end }
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
6782
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6721
diff changeset
    37
function _M.getconfig()
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    38
	return config;
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    39
end
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    40
6782
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6721
diff changeset
    41
function _M.get(host, key, _oldkey)
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: 5124
diff changeset
    42
	if key == "core" 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: 5124
diff changeset
    43
		key = _oldkey; -- COMPAT with code that still uses "core"
4530
40905e7bf680 configmanager: get(): Make section (core) optional (hurrah)
Matthew Wild <mwild1@gmail.com>
parents: 4358
diff changeset
    44
	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: 5124
diff changeset
    45
	return config[host][key];
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: 5124
diff changeset
    46
end
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: 5124
diff changeset
    47
function _M.rawget(host, key, _oldkey)
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: 5124
diff changeset
    48
	if key == "core" 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: 5124
diff changeset
    49
		key = _oldkey; -- COMPAT with code that still uses "core"
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	end
4001
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    51
	local hostconfig = rawget(config, host);
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    52
	if hostconfig 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: 5124
diff changeset
    53
		return rawget(hostconfig, key);
4001
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    54
	end
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    55
end
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
6715
29d5875ae38d configmanager: Rename variable to avoid name conflict [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6326
diff changeset
    57
local function set(config_table, host, key, value)
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: 5124
diff changeset
    58
	if host and key then
6715
29d5875ae38d configmanager: Rename variable to avoid name conflict [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6326
diff changeset
    59
		local hostconfig = rawget(config_table, host);
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
		if not hostconfig then
6715
29d5875ae38d configmanager: Rename variable to avoid name conflict [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6326
diff changeset
    61
			hostconfig = rawset(config_table, host, setmetatable({}, host_mt))[host];
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
		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: 5124
diff changeset
    63
		hostconfig[key] = value;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
		return true;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
	end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
	return false;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
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: 5124
diff changeset
    69
function _M.set(host, key, value, _oldvalue)
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: 5124
diff changeset
    70
	if key == "core" 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: 5124
diff changeset
    71
		key, value = value, _oldvalue; --COMPAT with code that still uses "core"
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: 5124
diff changeset
    72
	end
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: 5124
diff changeset
    73
	return set(config, host, key, value);
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    74
end
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    75
6782
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6721
diff changeset
    76
function _M.load(filename, config_format)
6716
b628870b1bd6 configmanager: Rename variable to avoid name conflict [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6715
diff changeset
    77
	config_format = config_format or filename:match("%w+$");
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
    78
8156
c22d5680ca68 configmanager: Remove support for multiple parsers, fixes #852.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7950
diff changeset
    79
	if config_format == "lua" then
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
    80
		local f, err = io.open(filename);
2552
8dda55217e83 configmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2427
diff changeset
    81
		if f 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: 5124
diff changeset
    82
			local new_config = setmetatable({ ["*"] = { } }, config_mt);
8156
c22d5680ca68 configmanager: Remove support for multiple parsers, fixes #852.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7950
diff changeset
    83
			local ok, err = parser.load(f:read("*a"), filename, new_config);
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
			f:close();
3613
f617718d2221 configmanager: Change parser API again to pass a config table to insert settings to. Fixes Include(). (Thanks Zash/answerman)
Matthew Wild <mwild1@gmail.com>
parents: 3610
diff changeset
    85
			if ok then
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    86
				config = new_config;
1000
a73715a9267f core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents: 913
diff changeset
    87
			end
3780
791aede977da configmanager: Switch back to returning 'ok' to signal config load success - fixes config errors not being displayed
Matthew Wild <mwild1@gmail.com>
parents: 3613
diff changeset
    88
			return ok, "parser", err;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
		end
793
55add3b87c01 Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
    90
		return f, "file", err;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
	end
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
    92
6716
b628870b1bd6 configmanager: Rename variable to avoid name conflict [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6715
diff changeset
    93
	if not config_format then
793
55add3b87c01 Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
    94
		return nil, "file", "no parser specified";
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    95
	else
6716
b628870b1bd6 configmanager: Rename variable to avoid name conflict [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6715
diff changeset
    96
		return nil, "file", "no parser for "..(config_format);
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    97
	end
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   100
-- Built-in Lua parser
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
do
6717
429068c24ea0 configmanager: Remove unnecessary function localizations [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6716
diff changeset
   102
	local pcall = _G.pcall;
8156
c22d5680ca68 configmanager: Remove support for multiple parsers, fixes #852.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7950
diff changeset
   103
	parser = {};
c22d5680ca68 configmanager: Remove support for multiple parsers, fixes #852.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7950
diff changeset
   104
	function parser.load(data, config_file, config_table)
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   105
		local env;
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   106
		-- The ' = true' are needed so as not to set off __newindex when we assign the functions below
2975
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   107
		env = setmetatable({
3012
6d86e26f0923 Merge configmanager->trunk
Matthew Wild <mwild1@gmail.com>
parents: 2985 3011
diff changeset
   108
			Host = true, host = true, VirtualHost = true,
6d86e26f0923 Merge configmanager->trunk
Matthew Wild <mwild1@gmail.com>
parents: 2985 3011
diff changeset
   109
			Component = true, component = true,
3610
2084959d4096 configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents: 3609
diff changeset
   110
			Include = true, include = true, RunScript = true }, {
6720
4fecfc81dac1 configmanager: Rename unused function arguments [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6719
diff changeset
   111
				__index = function (_, k)
9244
2d82a926826f configmanager: Allow referencing environment variables in the config as as ENV_<name>
Matthew Wild <mwild1@gmail.com>
parents: 8695
diff changeset
   112
					if k:match("^ENV_") then
2d82a926826f configmanager: Allow referencing environment variables in the config as as ENV_<name>
Matthew Wild <mwild1@gmail.com>
parents: 8695
diff changeset
   113
						return os.getenv(k:sub(5));
2d82a926826f configmanager: Allow referencing environment variables in the config as as ENV_<name>
Matthew Wild <mwild1@gmail.com>
parents: 8695
diff changeset
   114
					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: 5124
diff changeset
   115
					return rawget(_G, k);
2975
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   116
				end,
6720
4fecfc81dac1 configmanager: Rename unused function arguments [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6719
diff changeset
   117
				__newindex = function (_, k, v)
4fecfc81dac1 configmanager: Rename unused function arguments [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6719
diff changeset
   118
					set(config_table, env.__currenthost or "*", k, v);
2975
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   119
				end
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   120
		});
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   121
1615
0e3eacf135f2 configmanager: Default options appearing before Host "*" to global (fixes potential traceback)
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
   122
		rawset(env, "__currenthost", "*") -- Default is global
3011
1189a29cd846 configmanager: Add VirtualHost as an alias for Host (re-applied in trunk due to previous bad merge with 0.7)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   123
		function env.VirtualHost(name)
6323
5926f01e5cd2 configmanager: nameprep VirtualHost and Component names
Matthew Wild <mwild1@gmail.com>
parents: 6166
diff changeset
   124
			name = nameprep(name);
6719
2b78754b0f1c configmanager: Rename variable to avoid name conflicts [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6718
diff changeset
   125
			if rawget(config_table, name) and rawget(config_table[name], "component_module") then
2861
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   126
				error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
6719
2b78754b0f1c configmanager: Rename variable to avoid name conflicts [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6718
diff changeset
   127
					name, config_table[name].component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
2861
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   128
			end
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   129
			rawset(env, "__currenthost", name);
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   130
			-- Needs at least one setting to logically exist :)
6719
2b78754b0f1c configmanager: Rename variable to avoid name conflicts [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6718
diff changeset
   131
			set(config_table, name or "*", "defined", true);
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   132
			return function (config_options)
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   133
				rawset(env, "__currenthost", "*"); -- Return to global scope
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   134
				for option_name, option_value in pairs(config_options) do
6719
2b78754b0f1c configmanager: Rename variable to avoid name conflicts [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6718
diff changeset
   135
					set(config_table, name or "*", option_name, option_value);
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   136
				end
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   137
			end;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
		end
3011
1189a29cd846 configmanager: Add VirtualHost as an alias for Host (re-applied in trunk due to previous bad merge with 0.7)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   139
		env.Host, env.host = env.VirtualHost, env.VirtualHost;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   140
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   141
		function env.Component(name)
6323
5926f01e5cd2 configmanager: nameprep VirtualHost and Component names
Matthew Wild <mwild1@gmail.com>
parents: 6166
diff changeset
   142
			name = nameprep(name);
7950
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6782
diff changeset
   143
			if rawget(config_table, name) and rawget(config_table[name], "defined")
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6782
diff changeset
   144
				and not rawget(config_table[name], "component_module") then
2861
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   145
				error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   146
					name, name, name), 0);
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   147
			end
6719
2b78754b0f1c configmanager: Rename variable to avoid name conflicts [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6718
diff changeset
   148
			set(config_table, name, "component_module", "component");
913
3e2dac84017d core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
   149
			-- Don't load the global modules by default
6719
2b78754b0f1c configmanager: Rename variable to avoid name conflicts [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6718
diff changeset
   150
			set(config_table, name, "load_global_modules", false);
913
3e2dac84017d core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
   151
			rawset(env, "__currenthost", name);
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   152
			local function handle_config_options(config_options)
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   153
				rawset(env, "__currenthost", "*"); -- Return to global scope
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   154
				for option_name, option_value in pairs(config_options) do
6719
2b78754b0f1c configmanager: Rename variable to avoid name conflicts [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6718
diff changeset
   155
					set(config_table, name or "*", option_name, option_value);
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   156
				end
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   157
			end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   158
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   159
			return function (module)
857
49298263f241 core.configmanager: Small fix to check validity of Component definitions
Matthew Wild <mwild1@gmail.com>
parents: 795
diff changeset
   160
					if type(module) == "string" then
6719
2b78754b0f1c configmanager: Rename variable to avoid name conflicts [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6718
diff changeset
   161
						set(config_table, name, "component_module", module);
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   162
						return handle_config_options;
857
49298263f241 core.configmanager: Small fix to check validity of Component definitions
Matthew Wild <mwild1@gmail.com>
parents: 795
diff changeset
   163
					end
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   164
					return handle_config_options(module);
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   165
				end
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   166
		end
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   167
		env.component = env.Component;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   168
5413
0bf5e90be086 configmanager: Some cleanup, remove unused variables and imports
Matthew Wild <mwild1@gmail.com>
parents: 5380
diff changeset
   169
		function env.Include(file)
6721
be98ebe87eef configmanager: Refactor function to avoid re-declaring local variable [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6720
diff changeset
   170
			-- Check whether this is a wildcard Include
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   171
			if file:match("[*?]") then
6166
46cb87d531a7 configmanager: Delay importing LuaFileSystem until needed by an Include line
Kim Alvefur <zash@zash.se>
parents: 5811
diff changeset
   172
				local lfs = deps.softreq "lfs";
46cb87d531a7 configmanager: Delay importing LuaFileSystem until needed by an Include line
Kim Alvefur <zash@zash.se>
parents: 5811
diff changeset
   173
				if not lfs then
46cb87d531a7 configmanager: Delay importing LuaFileSystem until needed by an Include line
Kim Alvefur <zash@zash.se>
parents: 5811
diff changeset
   174
					error(format("Error expanding wildcard pattern in Include %q - LuaFileSystem not available", file));
46cb87d531a7 configmanager: Delay importing LuaFileSystem until needed by an Include line
Kim Alvefur <zash@zash.se>
parents: 5811
diff changeset
   175
				end
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   176
				local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   177
				local path = file:sub(1, math_max(path_pos-2,0));
3929
7cb03d67101b configmanager: Filenames without a path are also relative to the config file path, not the current working directory
Matthew Wild <mwild1@gmail.com>
parents: 3905
diff changeset
   178
				local config_path = config_file:gsub("[^"..path_sep.."]+$", "");
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   179
				if #path > 0 then
3929
7cb03d67101b configmanager: Filenames without a path are also relative to the config file path, not the current working directory
Matthew Wild <mwild1@gmail.com>
parents: 3905
diff changeset
   180
					path = resolve_relative_path(config_path, path);
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   181
				else
3929
7cb03d67101b configmanager: Filenames without a path are also relative to the config file path, not the current working directory
Matthew Wild <mwild1@gmail.com>
parents: 3905
diff changeset
   182
					path = config_path;
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   183
				end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   184
				local patt = glob_to_pattern(glob);
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   185
				for f in lfs.dir(path) do
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   186
					if f:sub(1,1) ~= "." and f:match(patt) then
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   187
						env.Include(path..path_sep..f);
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   188
					end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   189
				end
6721
be98ebe87eef configmanager: Refactor function to avoid re-declaring local variable [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6720
diff changeset
   190
				return;
794
912dc389935a Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
   191
			end
6721
be98ebe87eef configmanager: Refactor function to avoid re-declaring local variable [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6720
diff changeset
   192
			-- Not a wildcard, so resolve (potentially) relative path and run through config parser
be98ebe87eef configmanager: Refactor function to avoid re-declaring local variable [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6720
diff changeset
   193
			file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
be98ebe87eef configmanager: Refactor function to avoid re-declaring local variable [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6720
diff changeset
   194
			local f, err = io.open(file);
be98ebe87eef configmanager: Refactor function to avoid re-declaring local variable [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6720
diff changeset
   195
			if f then
8156
c22d5680ca68 configmanager: Remove support for multiple parsers, fixes #852.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7950
diff changeset
   196
				local ret, err = parser.load(f:read("*a"), file, config_table);
6721
be98ebe87eef configmanager: Refactor function to avoid re-declaring local variable [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6720
diff changeset
   197
				if not ret then error(err:gsub("%[string.-%]", file), 0); end
be98ebe87eef configmanager: Refactor function to avoid re-declaring local variable [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6720
diff changeset
   198
			end
be98ebe87eef configmanager: Refactor function to avoid re-declaring local variable [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6720
diff changeset
   199
			if not f then error("Error loading included "..file..": "..err, 0); end
be98ebe87eef configmanager: Refactor function to avoid re-declaring local variable [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 6720
diff changeset
   200
			return f, err;
794
912dc389935a Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
   201
		end
912dc389935a Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
   202
		env.include = env.Include;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   203
3610
2084959d4096 configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents: 3609
diff changeset
   204
		function env.RunScript(file)
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   205
			return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
3610
2084959d4096 configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents: 3609
diff changeset
   206
		end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   207
5021
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 4530
diff changeset
   208
		local chunk, err = envload(data, "@"..config_file, env);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   209
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   210
		if not chunk then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   211
			return nil, err;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   212
		end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   213
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   214
		local ok, err = pcall(chunk);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   215
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
		if not ok then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   217
			return nil, err;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   218
		end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   219
3613
f617718d2221 configmanager: Change parser API again to pass a config table to insert settings to. Fixes Include(). (Thanks Zash/answerman)
Matthew Wild <mwild1@gmail.com>
parents: 3610
diff changeset
   220
		return true;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   221
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5413
diff changeset
   222
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   223
end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
   225
return _M;