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];
51 local function set(config, host, section, key, value)
52 if host and section and key then
53 local hostconfig = rawget(config, host);
54 if not hostconfig then
55 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
57 if not rawget(hostconfig, section) then
58 hostconfig[section] = setmetatable({}, section_mt(section));
60 hostconfig[section][key] = value;
66 function _M.set(host, section, key, value)
67 return set(config, host, section, key, value);
70 -- Helper function to resolve relative paths (needed by config)
72 local rel_path_start = ".."..path_sep;
73 function resolve_relative_path(parent_path, path)
76 parent_path = parent_path:gsub("%"..path_sep.."+$", "");
77 path = path:gsub("^%.%"..path_sep.."+", "");
80 if path_sep == "/" and path:sub(1,1) ~= "/" then
82 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\") then
86 return parent_path..path_sep..path;
93 -- Helper function to convert a glob to a Lua pattern
94 local function glob_to_pattern(glob)
95 return "^"..glob:gsub("[%p*?]", function (c)
106 function load(filename, format)
107 format = format or filename:match("%w+$");
109 if parsers[format] and parsers[format].load then
110 local f, err = io.open(filename);
112 local new_config = setmetatable({ ["*"] = { core = {} } }, config_mt);
113 local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
117 fire_event("config-reloaded", {
123 return ok, "parser", err;
125 return f, "file", err;
129 return nil, "file", "no parser specified";
131 return nil, "file", "no parser for "..(format);
135 function save(filename, format)
138 function addparser(format, parser)
139 if format and parser then
140 parsers[format] = parser;
144 -- _M needed to avoid name clash with local 'parsers'
145 function _M.parsers()
147 for format in pairs(parsers) do
148 table.insert(p, format);
153 -- Built-in Lua parser
155 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
156 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
158 function parsers.lua.load(data, config_file, config)
160 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
162 Host = true, host = true, VirtualHost = true,
163 Component = true, component = true,
164 Include = true, include = true, RunScript = true }, {
165 __index = function (t, k)
166 return rawget(_G, k) or
167 function (settings_table)
168 config[__currenthost or "*"][k] = settings_table;
171 __newindex = function (t, k, v)
172 set(config, env.__currenthost or "*", "core", k, v);
176 rawset(env, "__currenthost", "*") -- Default is global
177 function env.VirtualHost(name)
178 if rawget(config, name) and rawget(config[name].core, "component_module") then
179 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
180 name, config[name].core.component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
182 rawset(env, "__currenthost", name);
183 -- Needs at least one setting to logically exist :)
184 set(config, name or "*", "core", "defined", true);
185 return function (config_options)
186 rawset(env, "__currenthost", "*"); -- Return to global scope
187 for option_name, option_value in pairs(config_options) do
188 set(config, name or "*", "core", option_name, option_value);
192 env.Host, env.host = env.VirtualHost, env.VirtualHost;
194 function env.Component(name)
195 if rawget(config, name) and rawget(config[name].core, "defined") and not rawget(config[name].core, "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, "core", "component_module", "component");
200 -- Don't load the global modules by default
201 set(config, name, "core", "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 "*", "core", option_name, option_value);
210 return function (module)
211 if type(module) == "string" then
212 set(config, name, "core", "component_module", module);
213 return handle_config_options;
215 return handle_config_options(module);
218 env.component = env.Component;
220 function env.Include(file, wildcard)
221 if file:match("[*?]") then
222 local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
223 local path = file:sub(1, math_max(path_pos-2,0));
225 path = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), path);
229 local patt = glob_to_pattern(glob);
230 for f in lfs.dir(path) do
231 if f:sub(1,1) ~= "." and f:match(patt) then
232 env.Include(path..path_sep..f);
236 local f, err = io.open(file);
238 local data = f:read("*a");
239 local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
240 local ret, err = parsers.lua.load(data, file, config);
241 if not ret then error(err:gsub("%[string.-%]", file), 0); end
243 if not f then error("Error loading included "..file..": "..err, 0); end
247 env.include = env.Include;
249 function env.RunScript(file)
250 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
253 local chunk, err = loadstring(data, "@"..config_file);
261 local ok, err = pcall(chunk);