mod_s2s_auth_certs: Pick a logging function once and stick with it
[prosody.git] / core / configmanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local _G = _G;
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;
13
14 local fire_event = prosody and prosody.events.fire_event or function () end;
15
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);
21
22 module "configmanager"
23
24 _M.resolve_relative_path = resolve_relative_path; -- COMPAT
25
26 local parsers = {};
27
28 local config_mt = { __index = function (t, k) return rawget(t, "*"); end};
29 local config = setmetatable({ ["*"] = { } }, config_mt);
30
31 -- When host not found, use global
32 local host_mt = { __index = function(_, k) return config["*"][k] end }
33
34 function getconfig()
35         return config;
36 end
37
38 function get(host, key, _oldkey)
39         if key == "core" then
40                 key = _oldkey; -- COMPAT with code that still uses "core"
41         end
42         return config[host][key];
43 end
44 function _M.rawget(host, key, _oldkey)
45         if key == "core" then
46                 key = _oldkey; -- COMPAT with code that still uses "core"
47         end
48         local hostconfig = rawget(config, host);
49         if hostconfig then
50                 return rawget(hostconfig, key);
51         end
52 end
53
54 local function set(config, host, key, value)
55         if host and key then
56                 local hostconfig = rawget(config, host);
57                 if not hostconfig then
58                         hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
59                 end
60                 hostconfig[key] = value;
61                 return true;
62         end
63         return false;
64 end
65
66 function _M.set(host, key, value, _oldvalue)
67         if key == "core" then
68                 key, value = value, _oldvalue; --COMPAT with code that still uses "core"
69         end
70         return set(config, host, key, value);
71 end
72
73 function load(filename, format)
74         format = format or filename:match("%w+$");
75
76         if parsers[format] and parsers[format].load then
77                 local f, err = io.open(filename);
78                 if f then
79                         local new_config = setmetatable({ ["*"] = { } }, config_mt);
80                         local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
81                         f:close();
82                         if ok then
83                                 config = new_config;
84                                 fire_event("config-reloaded", {
85                                         filename = filename,
86                                         format = format,
87                                         config = config
88                                 });
89                         end
90                         return ok, "parser", err;
91                 end
92                 return f, "file", err;
93         end
94
95         if not format then
96                 return nil, "file", "no parser specified";
97         else
98                 return nil, "file", "no parser for "..(format);
99         end
100 end
101
102 function save(filename, format)
103 end
104
105 function addparser(format, parser)
106         if format and parser then
107                 parsers[format] = parser;
108         end
109 end
110
111 -- _M needed to avoid name clash with local 'parsers'
112 function _M.parsers()
113         local p = {};
114         for format in pairs(parsers) do
115                 table.insert(p, format);
116         end
117         return p;
118 end
119
120 -- Built-in Lua parser
121 do
122         local pcall, setmetatable = _G.pcall, _G.setmetatable;
123         local rawget = _G.rawget;
124         parsers.lua = {};
125         function parsers.lua.load(data, config_file, config)
126                 local env;
127                 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
128                 env = setmetatable({
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);
134                                 end,
135                                 __newindex = function (t, k, v)
136                                         set(config, env.__currenthost or "*", k, v);
137                                 end
138                 });
139
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);
145                         end
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);
153                                 end
154                         end;
155                 end
156                 env.Host, env.host = env.VirtualHost, env.VirtualHost;
157
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);
162                         end
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);
171                                 end
172                         end
173
174                         return function (module)
175                                         if type(module) == "string" then
176                                                 set(config, name, "component_module", module);
177                                                 return handle_config_options;
178                                         end
179                                         return handle_config_options(module);
180                                 end
181                 end
182                 env.component = env.Component;
183
184                 function env.Include(file)
185                         if file:match("[*?]") then
186                                 local lfs = deps.softreq "lfs";
187                                 if not lfs then
188                                         error(format("Error expanding wildcard pattern in Include %q - LuaFileSystem not available", file));
189                                 end
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.."]+$", "");
193                                 if #path > 0 then
194                                         path = resolve_relative_path(config_path, path);
195                                 else
196                                         path = config_path;
197                                 end
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);
202                                         end
203                                 end
204                         else
205                                 local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
206                                 local f, err = io.open(file);
207                                 if f then
208                                         local ret, err = parsers.lua.load(f:read("*a"), file, config);
209                                         if not ret then error(err:gsub("%[string.-%]", file), 0); end
210                                 end
211                                 if not f then error("Error loading included "..file..": "..err, 0); end
212                                 return f, err;
213                         end
214                 end
215                 env.include = env.Include;
216
217                 function env.RunScript(file)
218                         return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
219                 end
220
221                 local chunk, err = envload(data, "@"..config_file, env);
222
223                 if not chunk then
224                         return nil, err;
225                 end
226
227                 local ok, err = pcall(chunk);
228
229                 if not ok then
230                         return nil, err;
231                 end
232
233                 return true;
234         end
235
236 end
237
238 return _M;