core/configmanager.lua
author Waqas Hussain <waqas20@gmail.com>
Mon, 22 Apr 2013 11:54:15 -0400
changeset 5502 c773aa4467f1
parent 5413 0bf5e90be086
child 5776 bd0ff8ae98a8
child 5811 27ae988f5b70
permissions -rw-r--r--
storagemanager: Fix traceback in logging when store type is nil, and store is unsupported.
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
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 466
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;
5413
0bf5e90be086 configmanager: Some cleanup, remove unused variables and imports
Matthew Wild <mwild1@gmail.com>
parents: 5380
diff changeset
    10
local setmetatable, rawget, rawset, io, error, dofile, type, pairs, table =
0bf5e90be086 configmanager: Some cleanup, remove unused variables and imports
Matthew Wild <mwild1@gmail.com>
parents: 5380
diff changeset
    11
      setmetatable, rawget, rawset, io, error, dofile, type, pairs, table;
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
3384
b7600dd7cd42 configmanager: Remove dependency on eventmanager, and global 'prosody' object
Matthew Wild <mwild1@gmail.com>
parents: 3012
diff changeset
    14
local fire_event = prosody and prosody.events.fire_event or function () end;
1000
a73715a9267f core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents: 913
diff changeset
    15
5021
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 4530
diff changeset
    16
local envload = require"util.envload".envload;
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    17
local lfs = require "lfs";
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
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
    20
module "configmanager"
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
    21
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
    22
local parsers = {};
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
    23
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    24
local config_mt = { __index = function (t, k) 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
    25
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
    26
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
    27
-- 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
    28
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
    29
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    30
function getconfig()
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    31
	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
    32
end
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    33
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
    34
function get(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
    35
	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
    36
		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
    37
	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
    38
	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
    39
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
    40
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
    41
	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
    42
		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
    43
	end
4001
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    44
	local hostconfig = rawget(config, host);
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    45
	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
    46
		return rawget(hostconfig, key);
4001
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    47
	end
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    48
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
    49
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
    50
local function set(config, host, key, value)
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
    51
	if host and key then
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
    52
		local hostconfig = rawget(config, host);
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
    53
		if not hostconfig 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
    54
			hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
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
    55
		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
    56
		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
    57
		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
    58
	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
    59
	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
    60
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
    61
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
    62
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
    63
	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
    64
		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
    65
	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
    66
	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
    67
end
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    68
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
    69
-- Helper function to resolve relative paths (needed by config)
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
    70
do
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
    71
	function resolve_relative_path(parent_path, path)
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
    72
		if path then
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    73
			-- Some normalization
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    74
			parent_path = parent_path:gsub("%"..path_sep.."+$", "");
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    75
			path = path:gsub("^%.%"..path_sep.."+", "");
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    76
			
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
    77
			local is_relative;
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
    78
			if path_sep == "/" and path:sub(1,1) ~= "/" then
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
    79
				is_relative = true;
4358
86be454168fb configmanager: resolve_relative_path: Improved detection of absolute paths on Windows.
Waqas Hussain <waqas20@gmail.com>
parents: 4001
diff changeset
    80
			elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" or path:sub(2,3) ~= ":/")) then
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
    81
				is_relative = true;
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
    82
			end
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
    83
			if is_relative then
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
    84
				return parent_path..path_sep..path;
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
    85
			end
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
    86
		end
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
    87
		return path;
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
    88
	end	
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
    89
end
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
    90
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    91
-- Helper function to convert a glob to a Lua pattern
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    92
local function glob_to_pattern(glob)
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    93
	return "^"..glob:gsub("[%p*?]", function (c)
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    94
		if c == "*" then
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    95
			return ".*";
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    96
		elseif c == "?" then
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    97
			return ".";
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    98
		else
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    99
			return "%"..c;
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   100
		end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   101
	end).."$";
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   102
end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   103
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
   104
function load(filename, 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
   105
	format = 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
   106
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
   107
	if parsers[format] and parsers[format].load then
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
   108
		local f, err = io.open(filename);
2552
8dda55217e83 configmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2427
diff changeset
   109
		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
   110
			local new_config = setmetatable({ ["*"] = { } }, config_mt);
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
   111
			local ok, err = parsers[format].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
   112
			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
   113
			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
   114
				config = new_config;
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   115
				fire_event("config-reloaded", {
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   116
					filename = filename,
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   117
					format = format,
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   118
					config = config
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   119
				});
1000
a73715a9267f core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents: 913
diff changeset
   120
			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
   121
			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
   122
		end
793
55add3b87c01 Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
   123
		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
   124
	end
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
   125
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   126
	if not format then
793
55add3b87c01 Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
   127
		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
   128
	else
793
55add3b87c01 Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
   129
		return nil, "file", "no parser for "..(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
   130
	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
   131
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
   132
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
   133
function save(filename, format)
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
   134
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
   135
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
   136
function addparser(format, parser)
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
   137
	if format and parser 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
   138
		parsers[format] = parser;
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
   139
	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
   140
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
   141
2427
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   142
-- _M needed to avoid name clash with local 'parsers'
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   143
function _M.parsers()
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   144
	local p = {};
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   145
	for format in pairs(parsers) do
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   146
		table.insert(p, format);
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   147
	end
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   148
	return p;
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   149
end
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   150
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   151
-- 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
   152
do
5021
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 4530
diff changeset
   153
	local pcall, setmetatable = _G.pcall, _G.setmetatable;
5413
0bf5e90be086 configmanager: Some cleanup, remove unused variables and imports
Matthew Wild <mwild1@gmail.com>
parents: 5380
diff changeset
   154
	local rawget = _G.rawget;
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
   155
	parsers.lua = {};
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   156
	function parsers.lua.load(data, config_file, config)
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   157
		local env;
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   158
		-- 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
   159
		env = setmetatable({
3012
6d86e26f0923 Merge configmanager->trunk
Matthew Wild <mwild1@gmail.com>
parents: 2985 3011
diff changeset
   160
			Host = true, host = true, VirtualHost = true,
6d86e26f0923 Merge configmanager->trunk
Matthew Wild <mwild1@gmail.com>
parents: 2985 3011
diff changeset
   161
			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
   162
			Include = true, include = true, RunScript = true }, {
2975
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   163
				__index = function (t, k)
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
   164
					return rawget(_G, k);
2975
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   165
				end,
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   166
				__newindex = function (t, k, v)
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
   167
					set(config, env.__currenthost or "*", k, v);
2975
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   168
				end
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   169
		});
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
   170
		
1615
0e3eacf135f2 configmanager: Default options appearing before Host "*" to global (fixes potential traceback)
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
   171
		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
   172
		function env.VirtualHost(name)
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
   173
			if rawget(config, name) and rawget(config[name], "component_module") then
2861
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   174
				error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
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
   175
					name, config[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
   176
			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
   177
			rawset(env, "__currenthost", name);
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   178
			-- Needs at least one setting to logically exist :)
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
   179
			set(config, 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
   180
			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
   181
				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
   182
				for option_name, option_value in pairs(config_options) 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: 5124
diff changeset
   183
					set(config, 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
   184
				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
   185
			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
   186
		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
   187
		env.Host, env.host = env.VirtualHost, env.VirtualHost;
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
   188
		
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   189
		function env.Component(name)
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
   190
			if rawget(config, name) and rawget(config[name], "defined") and not rawget(config[name], "component_module") then
2861
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   191
				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
   192
					name, name, name), 0);
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   193
			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
   194
			set(config, 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
   195
			-- Don't load the global modules by default
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
   196
			set(config, 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
   197
			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
   198
			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
   199
				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
   200
				for option_name, option_value in pairs(config_options) 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: 5124
diff changeset
   201
					set(config, 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
   202
				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
   203
			end
913
3e2dac84017d core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
   204
	
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   205
			return function (module)
857
49298263f241 core.configmanager: Small fix to check validity of Component definitions
Matthew Wild <mwild1@gmail.com>
parents: 795
diff changeset
   206
					if type(module) == "string" 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
   207
						set(config, 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
   208
						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
   209
					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
   210
					return handle_config_options(module);
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   211
				end
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   212
		end
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   213
		env.component = env.Component;
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   214
		
5413
0bf5e90be086 configmanager: Some cleanup, remove unused variables and imports
Matthew Wild <mwild1@gmail.com>
parents: 5380
diff changeset
   215
		function env.Include(file)
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   216
			if file:match("[*?]") then
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   217
				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
   218
				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
   219
				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
   220
				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
   221
					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
   222
				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
   223
					path = config_path;
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   224
				end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   225
				local patt = glob_to_pattern(glob);
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   226
				for f in lfs.dir(path) do
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   227
					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
   228
						env.Include(path..path_sep..f);
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   229
					end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   230
				end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   231
			else
5124
a4a74a0e9b9c configmanager: Fix include of relative files via Include directive in config
Matthew Wild <mwild1@gmail.com>
parents: 5021
diff changeset
   232
				local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   233
				local f, err = io.open(file);
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   234
				if f then
5124
a4a74a0e9b9c configmanager: Fix include of relative files via Include directive in config
Matthew Wild <mwild1@gmail.com>
parents: 5021
diff changeset
   235
					local ret, err = parsers.lua.load(f:read("*a"), file, config);
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   236
					if not ret then error(err:gsub("%[string.-%]", file), 0); end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   237
				end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   238
				if not f then error("Error loading included "..file..": "..err, 0); end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   239
				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
   240
			end
912dc389935a Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
   241
		end
912dc389935a Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
   242
		env.include = env.Include;
912dc389935a Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
   243
		
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
   244
		function env.RunScript(file)
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   245
			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
   246
		end
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
   247
		
5021
85b2689dbcfe Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents: 4530
diff changeset
   248
		local chunk, err = envload(data, "@"..config_file, env);
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
   249
		
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
   250
		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
   251
			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
   252
		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
   253
		
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
   254
		local ok, err = pcall(chunk);
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
   255
		
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
   256
		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
   257
			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
   258
		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
   259
		
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
   260
		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
   261
	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
   262
	
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
   263
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
   264
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
   265
return _M;