Merge 0.10->trunk
[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 local encodings = deps.softreq"util.encodings";
23 local nameprep = encodings and encodings.stringprep.nameprep or function (host) return host:lower(); end
24
25 local _M = {};
26 local _ENV = nil;
27
28 _M.resolve_relative_path = resolve_relative_path; -- COMPAT
29
30 local parsers = {};
31
32 local config_mt = { __index = function (t, _) return rawget(t, "*"); end};
33 local config = setmetatable({ ["*"] = { } }, config_mt);
34
35 -- When host not found, use global
36 local host_mt = { __index = function(_, k) return config["*"][k] end }
37
38 function _M.getconfig()
39         return config;
40 end
41
42 function _M.get(host, key, _oldkey)
43         if key == "core" then
44                 key = _oldkey; -- COMPAT with code that still uses "core"
45         end
46         return config[host][key];
47 end
48 function _M.rawget(host, key, _oldkey)
49         if key == "core" then
50                 key = _oldkey; -- COMPAT with code that still uses "core"
51         end
52         local hostconfig = rawget(config, host);
53         if hostconfig then
54                 return rawget(hostconfig, key);
55         end
56 end
57
58 local function set(config_table, host, key, value)
59         if host and key then
60                 local hostconfig = rawget(config_table, host);
61                 if not hostconfig then
62                         hostconfig = rawset(config_table, host, setmetatable({}, host_mt))[host];
63                 end
64                 hostconfig[key] = value;
65                 return true;
66         end
67         return false;
68 end
69
70 function _M.set(host, key, value, _oldvalue)
71         if key == "core" then
72                 key, value = value, _oldvalue; --COMPAT with code that still uses "core"
73         end
74         return set(config, host, key, value);
75 end
76
77 function _M.load(filename, config_format)
78         config_format = config_format or filename:match("%w+$");
79
80         if parsers[config_format] and parsers[config_format].load then
81                 local f, err = io.open(filename);
82                 if f then
83                         local new_config = setmetatable({ ["*"] = { } }, config_mt);
84                         local ok, err = parsers[config_format].load(f:read("*a"), filename, new_config);
85                         f:close();
86                         if ok then
87                                 config = new_config;
88                                 fire_event("config-reloaded", {
89                                         filename = filename,
90                                         format = config_format,
91                                         config = config
92                                 });
93                         end
94                         return ok, "parser", err;
95                 end
96                 return f, "file", err;
97         end
98
99         if not config_format then
100                 return nil, "file", "no parser specified";
101         else
102                 return nil, "file", "no parser for "..(config_format);
103         end
104 end
105
106 function _M.addparser(config_format, parser)
107         if config_format and parser then
108                 parsers[config_format] = parser;
109         end
110 end
111
112 -- _M needed to avoid name clash with local 'parsers'
113 function _M.parsers()
114         local p = {};
115         for config_format in pairs(parsers) do
116                 table.insert(p, config_format);
117         end
118         return p;
119 end
120
121 -- Built-in Lua parser
122 do
123         local pcall = _G.pcall;
124         parsers.lua = {};
125         function parsers.lua.load(data, config_file, config_table)
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 (_, k)
133                                         return rawget(_G, k);
134                                 end,
135                                 __newindex = function (_, k, v)
136                                         set(config_table, env.__currenthost or "*", k, v);
137                                 end
138                 });
139
140                 rawset(env, "__currenthost", "*") -- Default is global
141                 function env.VirtualHost(name)
142                         name = nameprep(name);
143                         if rawget(config_table, name) and rawget(config_table[name], "component_module") then
144                                 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
145                                         name, config_table[name].component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
146                         end
147                         rawset(env, "__currenthost", name);
148                         -- Needs at least one setting to logically exist :)
149                         set(config_table, name or "*", "defined", true);
150                         return function (config_options)
151                                 rawset(env, "__currenthost", "*"); -- Return to global scope
152                                 for option_name, option_value in pairs(config_options) do
153                                         set(config_table, name or "*", option_name, option_value);
154                                 end
155                         end;
156                 end
157                 env.Host, env.host = env.VirtualHost, env.VirtualHost;
158
159                 function env.Component(name)
160                         name = nameprep(name);
161                         if rawget(config_table, name) and rawget(config_table[name], "defined") and not rawget(config_table[name], "component_module") then
162                                 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
163                                         name, name, name), 0);
164                         end
165                         set(config_table, name, "component_module", "component");
166                         -- Don't load the global modules by default
167                         set(config_table, name, "load_global_modules", false);
168                         rawset(env, "__currenthost", name);
169                         local function handle_config_options(config_options)
170                                 rawset(env, "__currenthost", "*"); -- Return to global scope
171                                 for option_name, option_value in pairs(config_options) do
172                                         set(config_table, name or "*", option_name, option_value);
173                                 end
174                         end
175
176                         return function (module)
177                                         if type(module) == "string" then
178                                                 set(config_table, name, "component_module", module);
179                                                 return handle_config_options;
180                                         end
181                                         return handle_config_options(module);
182                                 end
183                 end
184                 env.component = env.Component;
185
186                 function env.Include(file)
187                         -- Check whether this is a wildcard Include
188                         if file:match("[*?]") then
189                                 local lfs = deps.softreq "lfs";
190                                 if not lfs then
191                                         error(format("Error expanding wildcard pattern in Include %q - LuaFileSystem not available", file));
192                                 end
193                                 local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
194                                 local path = file:sub(1, math_max(path_pos-2,0));
195                                 local config_path = config_file:gsub("[^"..path_sep.."]+$", "");
196                                 if #path > 0 then
197                                         path = resolve_relative_path(config_path, path);
198                                 else
199                                         path = config_path;
200                                 end
201                                 local patt = glob_to_pattern(glob);
202                                 for f in lfs.dir(path) do
203                                         if f:sub(1,1) ~= "." and f:match(patt) then
204                                                 env.Include(path..path_sep..f);
205                                         end
206                                 end
207                                 return;
208                         end
209                         -- Not a wildcard, so resolve (potentially) relative path and run through config parser
210                         file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
211                         local f, err = io.open(file);
212                         if f then
213                                 local ret, err = parsers.lua.load(f:read("*a"), file, config_table);
214                                 if not ret then error(err:gsub("%[string.-%]", file), 0); end
215                         end
216                         if not f then error("Error loading included "..file..": "..err, 0); end
217                         return f, err;
218                 end
219                 env.include = env.Include;
220
221                 function env.RunScript(file)
222                         return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
223                 end
224
225                 local chunk, err = envload(data, "@"..config_file, env);
226
227                 if not chunk then
228                         return nil, err;
229                 end
230
231                 local ok, err = pcall(chunk);
232
233                 if not ok then
234                         return nil, err;
235                 end
236
237                 return true;
238         end
239
240 end
241
242 return _M;