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)
44 local sec = config[host][section];
50 function _M.rawget(host, section, key)
51 local hostconfig = rawget(config, host);
53 local sectionconfig = rawget(hostconfig, section);
55 return rawget(sectionconfig, key);
60 local function set(config, host, section, key, value)
61 if host and section and key then
62 local hostconfig = rawget(config, host);
63 if not hostconfig then
64 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
66 if not rawget(hostconfig, section) then
67 hostconfig[section] = setmetatable({}, section_mt(section));
69 hostconfig[section][key] = value;
75 function _M.set(host, section, key, value)
76 return set(config, host, section, key, value);
79 -- Helper function to resolve relative paths (needed by config)
81 local rel_path_start = ".."..path_sep;
82 function resolve_relative_path(parent_path, path)
85 parent_path = parent_path:gsub("%"..path_sep.."+$", "");
86 path = path:gsub("^%.%"..path_sep.."+", "");
89 if path_sep == "/" and path:sub(1,1) ~= "/" then
91 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\") then
95 return parent_path..path_sep..path;
102 -- Helper function to convert a glob to a Lua pattern
103 local function glob_to_pattern(glob)
104 return "^"..glob:gsub("[%p*?]", function (c)
115 function load(filename, format)
116 format = format or filename:match("%w+$");
118 if parsers[format] and parsers[format].load then
119 local f, err = io.open(filename);
121 local new_config = setmetatable({ ["*"] = { core = {} } }, config_mt);
122 local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
126 fire_event("config-reloaded", {
132 return ok, "parser", err;
134 return f, "file", err;
138 return nil, "file", "no parser specified";
140 return nil, "file", "no parser for "..(format);
144 function save(filename, format)
147 function addparser(format, parser)
148 if format and parser then
149 parsers[format] = parser;
153 -- _M needed to avoid name clash with local 'parsers'
154 function _M.parsers()
156 for format in pairs(parsers) do
157 table.insert(p, format);
162 -- Built-in Lua parser
164 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
165 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
167 function parsers.lua.load(data, config_file, config)
169 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
171 Host = true, host = true, VirtualHost = true,
172 Component = true, component = true,
173 Include = true, include = true, RunScript = true }, {
174 __index = function (t, k)
175 return rawget(_G, k) or
176 function (settings_table)
177 config[__currenthost or "*"][k] = settings_table;
180 __newindex = function (t, k, v)
181 set(config, env.__currenthost or "*", "core", k, v);
185 rawset(env, "__currenthost", "*") -- Default is global
186 function env.VirtualHost(name)
187 if rawget(config, name) and rawget(config[name].core, "component_module") then
188 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
189 name, config[name].core.component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
191 rawset(env, "__currenthost", name);
192 -- Needs at least one setting to logically exist :)
193 set(config, name or "*", "core", "defined", true);
194 return function (config_options)
195 rawset(env, "__currenthost", "*"); -- Return to global scope
196 for option_name, option_value in pairs(config_options) do
197 set(config, name or "*", "core", option_name, option_value);
201 env.Host, env.host = env.VirtualHost, env.VirtualHost;
203 function env.Component(name)
204 if rawget(config, name) and rawget(config[name].core, "defined") and not rawget(config[name].core, "component_module") then
205 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
206 name, name, name), 0);
208 set(config, name, "core", "component_module", "component");
209 -- Don't load the global modules by default
210 set(config, name, "core", "load_global_modules", false);
211 rawset(env, "__currenthost", name);
212 local function handle_config_options(config_options)
213 rawset(env, "__currenthost", "*"); -- Return to global scope
214 for option_name, option_value in pairs(config_options) do
215 set(config, name or "*", "core", option_name, option_value);
219 return function (module)
220 if type(module) == "string" then
221 set(config, name, "core", "component_module", module);
222 return handle_config_options;
224 return handle_config_options(module);
227 env.component = env.Component;
229 function env.Include(file, wildcard)
230 if file:match("[*?]") then
231 local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
232 local path = file:sub(1, math_max(path_pos-2,0));
233 local config_path = config_file:gsub("[^"..path_sep.."]+$", "");
235 path = resolve_relative_path(config_path, path);
239 local patt = glob_to_pattern(glob);
240 for f in lfs.dir(path) do
241 if f:sub(1,1) ~= "." and f:match(patt) then
242 env.Include(path..path_sep..f);
246 local f, err = io.open(file);
248 local data = f:read("*a");
249 local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
250 local ret, err = parsers.lua.load(data, file, config);
251 if not ret then error(err:gsub("%[string.-%]", file), 0); end
253 if not f then error("Error loading included "..file..": "..err, 0); end
257 env.include = env.Include;
259 function env.RunScript(file)
260 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
263 local chunk, err = loadstring(data, "@"..config_file);
271 local ok, err = pcall(chunk);