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, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table =
11 setmetatable, loadfile, pcall, 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 lfs = require "lfs";
17 local path_sep = package.config:sub(1,1);
19 module "configmanager"
23 local config_mt = { __index = function (t, k) return rawget(t, "*"); end};
24 local config = setmetatable({ ["*"] = { core = {} } }, config_mt);
26 -- When host not found, use global
29 -- When key not found in section, check key in global's section
30 function section_mt(section_name)
31 return { __index = function (t, k)
32 local section = rawget(config["*"], section_name);
33 if not section then return nil; end
43 function get(host, section, key)
45 section, key = "core", section;
47 local sec = config[host][section];
53 function _M.rawget(host, section, key)
54 local hostconfig = rawget(config, host);
56 local sectionconfig = rawget(hostconfig, section);
58 return rawget(sectionconfig, key);
63 local function set(config, host, section, key, value)
64 if host and section and key then
65 local hostconfig = rawget(config, host);
66 if not hostconfig then
67 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
69 if not rawget(hostconfig, section) then
70 hostconfig[section] = setmetatable({}, section_mt(section));
72 hostconfig[section][key] = value;
78 function _M.set(host, section, key, value)
79 return set(config, host, section, key, value);
82 -- Helper function to resolve relative paths (needed by config)
84 local rel_path_start = ".."..path_sep;
85 function resolve_relative_path(parent_path, path)
88 parent_path = parent_path:gsub("%"..path_sep.."+$", "");
89 path = path:gsub("^%.%"..path_sep.."+", "");
92 if path_sep == "/" and path:sub(1,1) ~= "/" then
94 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" or path:sub(2,3) ~= ":/")) then
98 return parent_path..path_sep..path;
105 -- Helper function to convert a glob to a Lua pattern
106 local function glob_to_pattern(glob)
107 return "^"..glob:gsub("[%p*?]", function (c)
118 function load(filename, format)
119 format = format or filename:match("%w+$");
121 if parsers[format] and parsers[format].load then
122 local f, err = io.open(filename);
124 local new_config = setmetatable({ ["*"] = { core = {} } }, config_mt);
125 local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
129 fire_event("config-reloaded", {
135 return ok, "parser", err;
137 return f, "file", err;
141 return nil, "file", "no parser specified";
143 return nil, "file", "no parser for "..(format);
147 function save(filename, format)
150 function addparser(format, parser)
151 if format and parser then
152 parsers[format] = parser;
156 -- _M needed to avoid name clash with local 'parsers'
157 function _M.parsers()
159 for format in pairs(parsers) do
160 table.insert(p, format);
165 -- Built-in Lua parser
167 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
168 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
170 function parsers.lua.load(data, config_file, config)
172 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
174 Host = true, host = true, VirtualHost = true,
175 Component = true, component = true,
176 Include = true, include = true, RunScript = true }, {
177 __index = function (t, k)
178 return rawget(_G, k) or
179 function (settings_table)
180 config[__currenthost or "*"][k] = settings_table;
183 __newindex = function (t, k, v)
184 set(config, env.__currenthost or "*", "core", k, v);
188 rawset(env, "__currenthost", "*") -- Default is global
189 function env.VirtualHost(name)
190 if rawget(config, name) and rawget(config[name].core, "component_module") then
191 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
192 name, config[name].core.component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
194 rawset(env, "__currenthost", name);
195 -- Needs at least one setting to logically exist :)
196 set(config, name or "*", "core", "defined", true);
197 return function (config_options)
198 rawset(env, "__currenthost", "*"); -- Return to global scope
199 for option_name, option_value in pairs(config_options) do
200 set(config, name or "*", "core", option_name, option_value);
204 env.Host, env.host = env.VirtualHost, env.VirtualHost;
206 function env.Component(name)
207 if rawget(config, name) and rawget(config[name].core, "defined") and not rawget(config[name].core, "component_module") then
208 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
209 name, name, name), 0);
211 set(config, name, "core", "component_module", "component");
212 -- Don't load the global modules by default
213 set(config, name, "core", "load_global_modules", false);
214 rawset(env, "__currenthost", name);
215 local function handle_config_options(config_options)
216 rawset(env, "__currenthost", "*"); -- Return to global scope
217 for option_name, option_value in pairs(config_options) do
218 set(config, name or "*", "core", option_name, option_value);
222 return function (module)
223 if type(module) == "string" then
224 set(config, name, "core", "component_module", module);
225 return handle_config_options;
227 return handle_config_options(module);
230 env.component = env.Component;
232 function env.Include(file, wildcard)
233 if file:match("[*?]") then
234 local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
235 local path = file:sub(1, math_max(path_pos-2,0));
236 local config_path = config_file:gsub("[^"..path_sep.."]+$", "");
238 path = resolve_relative_path(config_path, path);
242 local patt = glob_to_pattern(glob);
243 for f in lfs.dir(path) do
244 if f:sub(1,1) ~= "." and f:match(patt) then
245 env.Include(path..path_sep..f);
249 local f, err = io.open(file);
251 local data = f:read("*a");
252 local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
253 local ret, err = parsers.lua.load(data, file, config);
254 if not ret then error(err:gsub("%[string.-%]", file), 0); end
256 if not f then error("Error loading included "..file..": "..err, 0); end
260 env.include = env.Include;
262 function env.RunScript(file)
263 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
266 local chunk, err = loadstring(data, "@"..config_file);
274 local ok, err = pcall(chunk);