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 deps = require"util.dependencies";
18 local resolve_relative_path = require"util.paths".resolve_relative_path;
19 local glob_to_pattern = require"util.paths".glob_to_pattern;
20 local path_sep = package.config:sub(1,1);
22 module "configmanager"
24 _M.resolve_relative_path = resolve_relative_path; -- COMPAT
28 local config_mt = { __index = function (t, k) return rawget(t, "*"); end};
29 local config = setmetatable({ ["*"] = { } }, config_mt);
31 -- When host not found, use global
32 local host_mt = { __index = function(_, k) return config["*"][k] end }
38 function get(host, key, _oldkey)
40 key = _oldkey; -- COMPAT with code that still uses "core"
42 return config[host][key];
44 function _M.rawget(host, key, _oldkey)
46 key = _oldkey; -- COMPAT with code that still uses "core"
48 local hostconfig = rawget(config, host);
50 return rawget(hostconfig, key);
54 local function set(config, host, key, value)
56 local hostconfig = rawget(config, host);
57 if not hostconfig then
58 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
60 hostconfig[key] = value;
66 function _M.set(host, key, value, _oldvalue)
68 key, value = value, _oldvalue; --COMPAT with code that still uses "core"
70 return set(config, host, key, value);
73 function load(filename, format)
74 format = format or filename:match("%w+$");
76 if parsers[format] and parsers[format].load then
77 local f, err = io.open(filename);
79 local new_config = setmetatable({ ["*"] = { } }, config_mt);
80 local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
84 fire_event("config-reloaded", {
90 return ok, "parser", err;
92 return f, "file", err;
96 return nil, "file", "no parser specified";
98 return nil, "file", "no parser for "..(format);
102 function save(filename, format)
105 function addparser(format, parser)
106 if format and parser then
107 parsers[format] = parser;
111 -- _M needed to avoid name clash with local 'parsers'
112 function _M.parsers()
114 for format in pairs(parsers) do
115 table.insert(p, format);
120 -- Built-in Lua parser
122 local pcall, setmetatable = _G.pcall, _G.setmetatable;
123 local rawget = _G.rawget;
125 function parsers.lua.load(data, config_file, config)
127 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
129 Host = true, host = true, VirtualHost = true,
130 Component = true, component = true,
131 Include = true, include = true, RunScript = true }, {
132 __index = function (t, k)
133 return rawget(_G, k);
135 __newindex = function (t, k, v)
136 set(config, env.__currenthost or "*", k, v);
140 rawset(env, "__currenthost", "*") -- Default is global
141 function env.VirtualHost(name)
142 if rawget(config, name) and rawget(config[name], "component_module") then
143 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
144 name, config[name].component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
146 rawset(env, "__currenthost", name);
147 -- Needs at least one setting to logically exist :)
148 set(config, name or "*", "defined", true);
149 return function (config_options)
150 rawset(env, "__currenthost", "*"); -- Return to global scope
151 for option_name, option_value in pairs(config_options) do
152 set(config, name or "*", option_name, option_value);
156 env.Host, env.host = env.VirtualHost, env.VirtualHost;
158 function env.Component(name)
159 if rawget(config, name) and rawget(config[name], "defined") and not rawget(config[name], "component_module") then
160 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
161 name, name, name), 0);
163 set(config, name, "component_module", "component");
164 -- Don't load the global modules by default
165 set(config, name, "load_global_modules", false);
166 rawset(env, "__currenthost", name);
167 local function handle_config_options(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 "*", option_name, option_value);
174 return function (module)
175 if type(module) == "string" then
176 set(config, name, "component_module", module);
177 return handle_config_options;
179 return handle_config_options(module);
182 env.component = env.Component;
184 function env.Include(file)
185 if file:match("[*?]") then
186 local lfs = deps.softreq "lfs";
188 error(format("Error expanding wildcard pattern in Include %q - LuaFileSystem not available", file));
190 local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
191 local path = file:sub(1, math_max(path_pos-2,0));
192 local config_path = config_file:gsub("[^"..path_sep.."]+$", "");
194 path = resolve_relative_path(config_path, path);
198 local patt = glob_to_pattern(glob);
199 for f in lfs.dir(path) do
200 if f:sub(1,1) ~= "." and f:match(patt) then
201 env.Include(path..path_sep..f);
205 local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
206 local f, err = io.open(file);
208 local ret, err = parsers.lua.load(f:read("*a"), file, config);
209 if not ret then error(err:gsub("%[string.-%]", file), 0); end
211 if not f then error("Error loading included "..file..": "..err, 0); end
215 env.include = env.Include;
217 function env.RunScript(file)
218 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
221 local chunk, err = envload(data, "@"..config_file, env);
227 local ok, err = pcall(chunk);