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