2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local setmetatable, rawget, rawset, io, error, dofile, type, pairs, table =
11 setmetatable, rawget, rawset, io, error, dofile, type, pairs, table;
12 local format, math_max = string.format, math.max;
14 local fire_event = prosody and prosody.events.fire_event or function () end;
16 local envload = require"util.envload".envload;
17 local deps = require"util.dependencies";
18 local path_sep = package.config:sub(1,1);
20 local have_encodings, encodings = pcall(require, "util.encodings");
21 local nameprep = have_encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
23 module "configmanager"
27 local config_mt = { __index = function (t, k) return rawget(t, "*"); end};
28 local config = setmetatable({ ["*"] = { } }, config_mt);
30 -- When host not found, use global
31 local host_mt = { __index = function(_, k) return config["*"][k] end }
37 function get(host, key, _oldkey)
39 key = _oldkey; -- COMPAT with code that still uses "core"
41 return config[host][key];
43 function _M.rawget(host, key, _oldkey)
45 key = _oldkey; -- COMPAT with code that still uses "core"
47 local hostconfig = rawget(config, host);
49 return rawget(hostconfig, key);
53 local function set(config, host, key, value)
55 local hostconfig = rawget(config, host);
56 if not hostconfig then
57 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
59 hostconfig[key] = value;
65 function _M.set(host, key, value, _oldvalue)
67 key, value = value, _oldvalue; --COMPAT with code that still uses "core"
69 return set(config, host, key, value);
72 -- Helper function to resolve relative paths (needed by config)
74 function resolve_relative_path(parent_path, path)
77 parent_path = parent_path:gsub("%"..path_sep.."+$", "");
78 path = path:gsub("^%.%"..path_sep.."+", "");
81 if path_sep == "/" and path:sub(1,1) ~= "/" then
83 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" and path:sub(2,3) ~= ":/")) then
87 return parent_path..path_sep..path;
94 -- Helper function to convert a glob to a Lua pattern
95 local function glob_to_pattern(glob)
96 return "^"..glob:gsub("[%p*?]", function (c)
107 function load(filename, format)
108 format = format or filename:match("%w+$");
110 if parsers[format] and parsers[format].load then
111 local f, err = io.open(filename);
113 local new_config = setmetatable({ ["*"] = { } }, config_mt);
114 local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
118 fire_event("config-reloaded", {
124 return ok, "parser", err;
126 return f, "file", err;
130 return nil, "file", "no parser specified";
132 return nil, "file", "no parser for "..(format);
136 function save(filename, format)
139 function addparser(format, parser)
140 if format and parser then
141 parsers[format] = parser;
145 -- _M needed to avoid name clash with local 'parsers'
146 function _M.parsers()
148 for format in pairs(parsers) do
149 table.insert(p, format);
154 -- Built-in Lua parser
156 local pcall, setmetatable = _G.pcall, _G.setmetatable;
157 local rawget = _G.rawget;
159 function parsers.lua.load(data, config_file, config)
161 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
163 Host = true, host = true, VirtualHost = true,
164 Component = true, component = true,
165 Include = true, include = true, RunScript = true }, {
166 __index = function (t, k)
167 return rawget(_G, k);
169 __newindex = function (t, k, v)
170 set(config, env.__currenthost or "*", k, v);
174 rawset(env, "__currenthost", "*") -- Default is global
175 function env.VirtualHost(name)
176 name = nameprep(name);
177 if rawget(config, name) and rawget(config[name], "component_module") then
178 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
179 name, config[name].component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
181 rawset(env, "__currenthost", name);
182 -- Needs at least one setting to logically exist :)
183 set(config, name or "*", "defined", true);
184 return function (config_options)
185 rawset(env, "__currenthost", "*"); -- Return to global scope
186 for option_name, option_value in pairs(config_options) do
187 set(config, name or "*", option_name, option_value);
191 env.Host, env.host = env.VirtualHost, env.VirtualHost;
193 function env.Component(name)
194 name = nameprep(name);
195 if rawget(config, name) and rawget(config[name], "defined") and not rawget(config[name], "component_module") then
196 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
197 name, name, name), 0);
199 set(config, name, "component_module", "component");
200 -- Don't load the global modules by default
201 set(config, name, "load_global_modules", false);
202 rawset(env, "__currenthost", name);
203 local function handle_config_options(config_options)
204 rawset(env, "__currenthost", "*"); -- Return to global scope
205 for option_name, option_value in pairs(config_options) do
206 set(config, name or "*", option_name, option_value);
210 return function (module)
211 if type(module) == "string" then
212 set(config, name, "component_module", module);
213 return handle_config_options;
215 return handle_config_options(module);
218 env.component = env.Component;
220 function env.Include(file)
221 if file:match("[*?]") then
222 local lfs = deps.softreq "lfs";
224 error(format("Error expanding wildcard pattern in Include %q - LuaFileSystem not available", file));
226 local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
227 local path = file:sub(1, math_max(path_pos-2,0));
228 local config_path = config_file:gsub("[^"..path_sep.."]+$", "");
230 path = resolve_relative_path(config_path, path);
234 local patt = glob_to_pattern(glob);
235 for f in lfs.dir(path) do
236 if f:sub(1,1) ~= "." and f:match(patt) then
237 env.Include(path..path_sep..f);
241 local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
242 local f, err = io.open(file);
244 local ret, err = parsers.lua.load(f:read("*a"), file, config);
245 if not ret then error(err:gsub("%[string.-%]", file), 0); end
247 if not f then error("Error loading included "..file..": "..err, 0); end
251 env.include = env.Include;
253 function env.RunScript(file)
254 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
257 local chunk, err = envload(data, "@"..config_file, env);
263 local ok, err = pcall(chunk);