aboutsummaryrefslogtreecommitdiffstats
path: root/core/configmanager.lua
diff options
context:
space:
mode:
Diffstat (limited to 'core/configmanager.lua')
-rw-r--r--core/configmanager.lua113
1 files changed, 95 insertions, 18 deletions
diff --git a/core/configmanager.lua b/core/configmanager.lua
index 092b3946..bd12e169 100644
--- a/core/configmanager.lua
+++ b/core/configmanager.lua
@@ -11,14 +11,15 @@ local setmetatable, rawget, rawset, io, os, error, dofile, type, pairs, ipairs =
setmetatable, rawget, rawset, io, os, error, dofile, type, pairs, ipairs;
local format, math_max, t_insert = string.format, math.max, table.insert;
-local envload = require"util.envload".envload;
-local deps = require"util.dependencies";
-local resolve_relative_path = require"util.paths".resolve_relative_path;
-local glob_to_pattern = require"util.paths".glob_to_pattern;
+local envload = require"prosody.util.envload".envload;
+local deps = require"prosody.util.dependencies";
+local it = require"prosody.util.iterators";
+local resolve_relative_path = require"prosody.util.paths".resolve_relative_path;
+local glob_to_pattern = require"prosody.util.paths".glob_to_pattern;
local path_sep = package.config:sub(1,1);
-local get_traceback_table = require "util.debug".get_traceback_table;
+local get_traceback_table = require "prosody.util.debug".get_traceback_table;
-local encodings = deps.softreq"util.encodings";
+local encodings = deps.softreq"prosody.util.encodings";
local nameprep = encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
local _M = {};
@@ -40,16 +41,10 @@ function _M.getconfig()
return config;
end
-function _M.get(host, key, _oldkey)
- if key == "core" then
- key = _oldkey; -- COMPAT with code that still uses "core"
- end
+function _M.get(host, key)
return config[host][key];
end
-function _M.rawget(host, key, _oldkey)
- if key == "core" then
- key = _oldkey; -- COMPAT with code that still uses "core"
- end
+function _M.rawget(host, key)
local hostconfig = rawget(config, host);
if hostconfig then
return rawget(hostconfig, key);
@@ -68,10 +63,17 @@ local function set(config_table, host, key, value)
return false;
end
-function _M.set(host, key, value, _oldvalue)
- if key == "core" then
- key, value = value, _oldvalue; --COMPAT with code that still uses "core"
+local function rawget_option(config_table, host, key)
+ if host and key then
+ local hostconfig = rawget(config_table, host);
+ if not hostconfig then
+ return nil;
+ end
+ return rawget(hostconfig, key);
end
+end
+
+function _M.set(host, key, value)
return set(config, host, key, value);
end
@@ -114,6 +116,51 @@ do
end
end
end
+
+ local config_option_proxy_mt = {
+ __index = setmetatable({
+ append = function (self, value)
+ local original_option = self:value();
+ if original_option == nil then
+ original_option = {};
+ end
+ if type(value) ~= "table" then
+ error("'append' operation expects a list of values to append to the existing list", 2);
+ end
+ if value[1] ~= nil then
+ for _, v in ipairs(value) do
+ t_insert(original_option, v);
+ end
+ else
+ for k, v in pairs(value) do
+ original_option[k] = v;
+ end
+ end
+ set(self.config_table, self.host, self.option_name, original_option);
+ return self;
+ end;
+ value = function (self)
+ return rawget_option(self.config_table, self.host, self.option_name);
+ end;
+ values = function (self)
+ return it.values(self:value());
+ end;
+ }, {
+ __index = function (t, k) --luacheck: ignore 212/t
+ error("Unknown config option operation: '"..k.."'", 2);
+ end;
+ });
+
+ __call = function (self, v2)
+ local v = self:value() or {};
+ if type(v) == "table" and type(v2) == "table" then
+ return self:append(v2);
+ end
+
+ error("Invalid syntax - missing '=' perhaps?", 2);
+ end;
+ };
+
parser = {};
function parser.load(data, config_file, config_table)
local set_options = {}; -- set_options[host.."/"..option_name] = true (when the option has been set already in this file)
@@ -128,7 +175,37 @@ do
if k:match("^ENV_") then
return os.getenv(k:sub(5));
end
- return rawget(_G, k);
+ if k == "Lua" then
+ return _G;
+ end
+ local val = rawget_option(config_table, env.__currenthost or "*", k);
+
+ local g_val = rawget(_G, k);
+
+ if val ~= nil or g_val == nil then
+ if type(val) == "table" then
+ return setmetatable({
+ config_table = config_table;
+ host = env.__currenthost or "*";
+ option_name = k;
+ }, config_option_proxy_mt);
+ end
+ return val;
+ end
+
+ if g_val ~= nil then
+ t_insert(
+ warnings,
+ ("%s:%d: direct usage of the Lua API is deprecated - replace `%s` with `Lua.%s`"):format(
+ config_file,
+ get_line_number(config_file),
+ k,
+ k
+ )
+ );
+ end
+
+ return g_val;
end,
__newindex = function (_, k, v)
local host = env.__currenthost or "*";