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