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