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, format =
11 setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table, string.format;
14 local fire_event = prosody and prosody.events.fire_event or function () end;
16 local path_sep = package.config:sub(1,1);
18 module "configmanager"
22 local config_mt = { __index = function (t, k) return rawget(t, "*"); end};
23 local config = setmetatable({ ["*"] = { core = {} } }, config_mt);
25 -- When host not found, use global
28 -- When key not found in section, check key in global's section
29 function section_mt(section_name)
30 return { __index = function (t, k)
31 local section = rawget(config["*"], section_name);
32 if not section then return nil; end
42 function get(host, section, key)
43 local sec = config[host][section];
50 local function set(config, host, section, key, value)
51 if host and section and key then
52 local hostconfig = rawget(config, host);
53 if not hostconfig then
54 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
56 if not rawget(hostconfig, section) then
57 hostconfig[section] = setmetatable({}, section_mt(section));
59 hostconfig[section][key] = value;
65 function _M.set(host, section, key, value)
66 return set(config, host, section, key, value);
69 -- Helper function to resolve relative paths (needed by config)
71 local rel_path_start = ".."..path_sep;
72 function resolve_relative_path(parent_path, path)
75 if path_sep == "/" and path:sub(1,1) ~= "/" then
77 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\") then
81 return parent_path..path_sep..path;
88 function load(filename, format)
89 format = format or filename:match("%w+$");
91 if parsers[format] and parsers[format].load then
92 local f, err = io.open(filename);
94 local new_config = setmetatable({ ["*"] = { core = {} } }, config_mt);
95 local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
99 fire_event("config-reloaded", {
105 return ok, "parser", err;
107 return f, "file", err;
111 return nil, "file", "no parser specified";
113 return nil, "file", "no parser for "..(format);
117 function save(filename, format)
120 function addparser(format, parser)
121 if format and parser then
122 parsers[format] = parser;
126 -- _M needed to avoid name clash with local 'parsers'
127 function _M.parsers()
129 for format in pairs(parsers) do
130 table.insert(p, format);
135 -- Built-in Lua parser
137 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
138 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
140 function parsers.lua.load(data, filename, config)
142 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
144 Host = true, host = true, VirtualHost = true,
145 Component = true, component = true,
146 Include = true, include = true, RunScript = true }, {
147 __index = function (t, k)
148 return rawget(_G, k) or
149 function (settings_table)
150 config[__currenthost or "*"][k] = settings_table;
153 __newindex = function (t, k, v)
154 set(config, env.__currenthost or "*", "core", k, v);
158 rawset(env, "__currenthost", "*") -- Default is global
159 function env.VirtualHost(name)
160 if rawget(config, name) and rawget(config[name].core, "component_module") then
161 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
162 name, config[name].core.component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
164 rawset(env, "__currenthost", name);
165 -- Needs at least one setting to logically exist :)
166 set(config, name or "*", "core", "defined", true);
167 return function (config_options)
168 rawset(env, "__currenthost", "*"); -- Return to global scope
169 for option_name, option_value in pairs(config_options) do
170 set(config, name or "*", "core", option_name, option_value);
174 env.Host, env.host = env.VirtualHost, env.VirtualHost;
176 function env.Component(name)
177 if rawget(config, name) and rawget(config[name].core, "defined") and not rawget(config[name].core, "component_module") then
178 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
179 name, name, name), 0);
181 set(config, name, "core", "component_module", "component");
182 -- Don't load the global modules by default
183 set(config, name, "core", "load_global_modules", false);
184 rawset(env, "__currenthost", name);
185 local function handle_config_options(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 return function (module)
193 if type(module) == "string" then
194 set(config, name, "core", "component_module", module);
195 return handle_config_options;
197 return handle_config_options(module);
200 env.component = env.Component;
202 function env.Include(file)
203 local f, err = io.open(file);
205 local data = f:read("*a");
206 local file = resolve_relative_path(filename:gsub("[^"..path_sep.."]+$", ""), file);
207 local ret, err = parsers.lua.load(data, file, config);
208 if not ret then error(err:gsub("%[string.-%]", file), 0); end
210 if not f then error("Error loading included "..file..": "..err, 0); end
213 env.include = env.Include;
215 function env.RunScript(file)
216 return dofile(resolve_relative_path(filename:gsub("[^"..path_sep.."]+$", ""), file));
219 local chunk, err = loadstring(data, "@"..filename);
227 local ok, err = pcall(chunk);