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 local have_encodings, encodings = pcall(require, "util.encodings");
23 local nameprep = have_encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
25 module "configmanager"
27 _M.resolve_relative_path = resolve_relative_path; -- COMPAT
31 local config_mt = { __index = function (t, k) return rawget(t, "*"); end};
32 local config = setmetatable({ ["*"] = { } }, config_mt);
34 -- When host not found, use global
35 local host_mt = { __index = function(_, k) return config["*"][k] end }
41 function get(host, key, _oldkey)
43 key = _oldkey; -- COMPAT with code that still uses "core"
45 return config[host][key];
47 function _M.rawget(host, key, _oldkey)
49 key = _oldkey; -- COMPAT with code that still uses "core"
51 local hostconfig = rawget(config, host);
53 return rawget(hostconfig, key);
57 local function set(config, host, key, value)
59 local hostconfig = rawget(config, host);
60 if not hostconfig then
61 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
63 hostconfig[key] = value;
69 function _M.set(host, key, value, _oldvalue)
71 key, value = value, _oldvalue; --COMPAT with code that still uses "core"
73 return set(config, host, key, value);
76 function load(filename, format)
77 format = format or filename:match("%w+$");
79 if parsers[format] and parsers[format].load then
80 local f, err = io.open(filename);
82 local new_config = setmetatable({ ["*"] = { } }, config_mt);
83 local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
87 fire_event("config-reloaded", {
93 return ok, "parser", err;
95 return f, "file", err;
99 return nil, "file", "no parser specified";
101 return nil, "file", "no parser for "..(format);
105 function save(filename, format)
108 function addparser(format, parser)
109 if format and parser then
110 parsers[format] = parser;
114 -- _M needed to avoid name clash with local 'parsers'
115 function _M.parsers()
117 for format in pairs(parsers) do
118 table.insert(p, format);
123 -- Built-in Lua parser
125 local pcall, setmetatable = _G.pcall, _G.setmetatable;
126 local rawget = _G.rawget;
128 function parsers.lua.load(data, config_file, config)
130 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
132 Host = true, host = true, VirtualHost = true,
133 Component = true, component = true,
134 Include = true, include = true, RunScript = true }, {
135 __index = function (t, k)
136 return rawget(_G, k);
138 __newindex = function (t, k, v)
139 set(config, env.__currenthost or "*", k, v);
143 rawset(env, "__currenthost", "*") -- Default is global
144 function env.VirtualHost(name)
145 name = nameprep(name);
146 if rawget(config, name) and rawget(config[name], "component_module") then
147 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
148 name, config[name].component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
150 rawset(env, "__currenthost", name);
151 -- Needs at least one setting to logically exist :)
152 set(config, name or "*", "defined", true);
153 return function (config_options)
154 rawset(env, "__currenthost", "*"); -- Return to global scope
155 for option_name, option_value in pairs(config_options) do
156 set(config, name or "*", option_name, option_value);
160 env.Host, env.host = env.VirtualHost, env.VirtualHost;
162 function env.Component(name)
163 name = nameprep(name);
164 if rawget(config, name) and rawget(config[name], "defined") and not rawget(config[name], "component_module") then
165 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
166 name, name, name), 0);
168 set(config, name, "component_module", "component");
169 -- Don't load the global modules by default
170 set(config, name, "load_global_modules", false);
171 rawset(env, "__currenthost", name);
172 local function handle_config_options(config_options)
173 rawset(env, "__currenthost", "*"); -- Return to global scope
174 for option_name, option_value in pairs(config_options) do
175 set(config, name or "*", option_name, option_value);
179 return function (module)
180 if type(module) == "string" then
181 set(config, name, "component_module", module);
182 return handle_config_options;
184 return handle_config_options(module);
187 env.component = env.Component;
189 function env.Include(file)
190 if file:match("[*?]") then
191 local lfs = deps.softreq "lfs";
193 error(format("Error expanding wildcard pattern in Include %q - LuaFileSystem not available", file));
195 local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
196 local path = file:sub(1, math_max(path_pos-2,0));
197 local config_path = config_file:gsub("[^"..path_sep.."]+$", "");
199 path = resolve_relative_path(config_path, path);
203 local patt = glob_to_pattern(glob);
204 for f in lfs.dir(path) do
205 if f:sub(1,1) ~= "." and f:match(patt) then
206 env.Include(path..path_sep..f);
210 local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
211 local f, err = io.open(file);
213 local ret, err = parsers.lua.load(f:read("*a"), file, config);
214 if not ret then error(err:gsub("%[string.-%]", file), 0); end
216 if not f then error("Error loading included "..file..": "..err, 0); end
220 env.include = env.Include;
222 function env.RunScript(file)
223 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
226 local chunk, err = envload(data, "@"..config_file, env);
232 local ok, err = pcall(chunk);