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