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 lfs = require "lfs";
18 local path_sep = package.config:sub(1,1);
20 module "configmanager"
24 local config_mt = { __index = function (t, k) return rawget(t, "*"); end};
25 local config = setmetatable({ ["*"] = { } }, config_mt);
27 -- When host not found, use global
28 local host_mt = { __index = function(_, k) return config["*"][k] end }
34 function get(host, key, _oldkey)
36 key = _oldkey; -- COMPAT with code that still uses "core"
38 return config[host][key];
40 function _M.rawget(host, key, _oldkey)
42 key = _oldkey; -- COMPAT with code that still uses "core"
44 local hostconfig = rawget(config, host);
46 return rawget(hostconfig, key);
50 local function set(config, host, key, value)
52 local hostconfig = rawget(config, host);
53 if not hostconfig then
54 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
56 hostconfig[key] = value;
62 function _M.set(host, key, value, _oldvalue)
64 key, value = value, _oldvalue; --COMPAT with code that still uses "core"
66 return set(config, host, key, value);
69 -- Helper function to resolve relative paths (needed by config)
71 function resolve_relative_path(parent_path, path)
74 parent_path = parent_path:gsub("%"..path_sep.."+$", "");
75 path = path:gsub("^%.%"..path_sep.."+", "");
78 if path_sep == "/" and path:sub(1,1) ~= "/" then
80 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" and path:sub(2,3) ~= ":/")) then
84 return parent_path..path_sep..path;
91 -- Helper function to convert a glob to a Lua pattern
92 local function glob_to_pattern(glob)
93 return "^"..glob:gsub("[%p*?]", function (c)
104 function load(filename, format)
105 format = format or filename:match("%w+$");
107 if parsers[format] and parsers[format].load then
108 local f, err = io.open(filename);
110 local new_config = setmetatable({ ["*"] = { } }, config_mt);
111 local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
115 fire_event("config-reloaded", {
121 return ok, "parser", err;
123 return f, "file", err;
127 return nil, "file", "no parser specified";
129 return nil, "file", "no parser for "..(format);
133 function save(filename, format)
136 function addparser(format, parser)
137 if format and parser then
138 parsers[format] = parser;
142 -- _M needed to avoid name clash with local 'parsers'
143 function _M.parsers()
145 for format in pairs(parsers) do
146 table.insert(p, format);
151 -- Built-in Lua parser
153 local pcall, setmetatable = _G.pcall, _G.setmetatable;
154 local rawget = _G.rawget;
156 function parsers.lua.load(data, config_file, config)
158 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
160 Host = true, host = true, VirtualHost = true,
161 Component = true, component = true,
162 Include = true, include = true, RunScript = true }, {
163 __index = function (t, k)
164 return rawget(_G, k);
166 __newindex = function (t, k, v)
167 set(config, env.__currenthost or "*", k, v);
171 rawset(env, "__currenthost", "*") -- Default is global
172 function env.VirtualHost(name)
173 if rawget(config, name) and rawget(config[name], "component_module") then
174 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
175 name, config[name].component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
177 rawset(env, "__currenthost", name);
178 -- Needs at least one setting to logically exist :)
179 set(config, name or "*", "defined", true);
180 return function (config_options)
181 rawset(env, "__currenthost", "*"); -- Return to global scope
182 for option_name, option_value in pairs(config_options) do
183 set(config, name or "*", option_name, option_value);
187 env.Host, env.host = env.VirtualHost, env.VirtualHost;
189 function env.Component(name)
190 if rawget(config, name) and rawget(config[name], "defined") and not rawget(config[name], "component_module") then
191 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
192 name, name, name), 0);
194 set(config, name, "component_module", "component");
195 -- Don't load the global modules by default
196 set(config, name, "load_global_modules", false);
197 rawset(env, "__currenthost", name);
198 local function handle_config_options(config_options)
199 rawset(env, "__currenthost", "*"); -- Return to global scope
200 for option_name, option_value in pairs(config_options) do
201 set(config, name or "*", option_name, option_value);
205 return function (module)
206 if type(module) == "string" then
207 set(config, name, "component_module", module);
208 return handle_config_options;
210 return handle_config_options(module);
213 env.component = env.Component;
215 function env.Include(file)
216 if file:match("[*?]") then
217 local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
218 local path = file:sub(1, math_max(path_pos-2,0));
219 local config_path = config_file:gsub("[^"..path_sep.."]+$", "");
221 path = resolve_relative_path(config_path, path);
225 local patt = glob_to_pattern(glob);
226 for f in lfs.dir(path) do
227 if f:sub(1,1) ~= "." and f:match(patt) then
228 env.Include(path..path_sep..f);
232 local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
233 local f, err = io.open(file);
235 local ret, err = parsers.lua.load(f:read("*a"), file, config);
236 if not ret then error(err:gsub("%[string.-%]", file), 0); end
238 if not f then error("Error loading included "..file..": "..err, 0); end
242 env.include = env.Include;
244 function env.RunScript(file)
245 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
248 local chunk, err = envload(data, "@"..config_file, env);
254 local ok, err = pcall(chunk);