MUC: Split out handling of normal (un)available presence into its own method
[prosody.git] / core / configmanager.lua
index db23009e6370b5fcb45145d35549d214df36db82..16d4b8e2f4a0ecb2841600bc01973c6c83d00b60 100644 (file)
@@ -19,10 +19,11 @@ local resolve_relative_path = require"util.paths".resolve_relative_path;
 local glob_to_pattern = require"util.paths".glob_to_pattern;
 local path_sep = package.config:sub(1,1);
 
-local have_encodings, encodings = pcall(require, "util.encodings");
-local nameprep = have_encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
+local encodings = deps.softreq"util.encodings";
+local nameprep = encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
 
-module "configmanager"
+local _M = {};
+local _ENV = nil;
 
 _M.resolve_relative_path = resolve_relative_path; -- COMPAT
 
@@ -34,11 +35,11 @@ local config = setmetatable({ ["*"] = { } }, config_mt);
 -- When host not found, use global
 local host_mt = { __index = function(_, k) return config["*"][k] end }
 
-function getconfig()
+function _M.getconfig()
        return config;
 end
 
-function get(host, key, _oldkey)
+function _M.get(host, key, _oldkey)
        if key == "core" then
                key = _oldkey; -- COMPAT with code that still uses "core"
        end
@@ -73,7 +74,7 @@ function _M.set(host, key, value, _oldvalue)
        return set(config, host, key, value);
 end
 
-function load(filename, config_format)
+function _M.load(filename, config_format)
        config_format = config_format or filename:match("%w+$");
 
        if parsers[config_format] and parsers[config_format].load then
@@ -102,7 +103,7 @@ function load(filename, config_format)
        end
 end
 
-function addparser(config_format, parser)
+function _M.addparser(config_format, parser)
        if config_format and parser then
                parsers[config_format] = parser;
        end
@@ -128,11 +129,11 @@ do
                        Host = true, host = true, VirtualHost = true,
                        Component = true, component = true,
                        Include = true, include = true, RunScript = true }, {
-                               __index = function (t, k)
+                               __index = function (_, k)
                                        return rawget(_G, k);
                                end,
-                               __newindex = function (t, k, v)
-                                       set(config, env.__currenthost or "*", k, v);
+                               __newindex = function (_, k, v)
+                                       set(config_table, env.__currenthost or "*", k, v);
                                end
                });
 
@@ -183,6 +184,7 @@ do
                env.component = env.Component;
 
                function env.Include(file)
+                       -- Check whether this is a wildcard Include
                        if file:match("[*?]") then
                                local lfs = deps.softreq "lfs";
                                if not lfs then
@@ -202,16 +204,17 @@ do
                                                env.Include(path..path_sep..f);
                                        end
                                end
-                       else
-                               local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
-                               local f, err = io.open(file);
-                               if f then
-                                       local ret, err = parsers.lua.load(f:read("*a"), file, config);
-                                       if not ret then error(err:gsub("%[string.-%]", file), 0); end
-                               end
-                               if not f then error("Error loading included "..file..": "..err, 0); end
-                               return f, err;
+                               return;
+                       end
+                       -- Not a wildcard, so resolve (potentially) relative path and run through config parser
+                       file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
+                       local f, err = io.open(file);
+                       if f then
+                               local ret, err = parsers.lua.load(f:read("*a"), file, config_table);
+                               if not ret then error(err:gsub("%[string.-%]", file), 0); end
                        end
+                       if not f then error("Error loading included "..file..": "..err, 0); end
+                       return f, err;
                end
                env.include = env.Include;