Comparison

core/configmanager.lua @ 5776:bd0ff8ae98a8

Remove all trailing whitespace
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 09 Aug 2013 17:48:21 +0200
parent 5413:0bf5e90be086
child 5814:5cf1c08805fb
comparison
equal deleted inserted replaced
5775:a6c2b8933507 5776:bd0ff8ae98a8
1 -- Prosody IM 1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild 2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain 3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local _G = _G; 9 local _G = _G;
71 function resolve_relative_path(parent_path, path) 71 function resolve_relative_path(parent_path, path)
72 if path then 72 if path then
73 -- Some normalization 73 -- Some normalization
74 parent_path = parent_path:gsub("%"..path_sep.."+$", ""); 74 parent_path = parent_path:gsub("%"..path_sep.."+$", "");
75 path = path:gsub("^%.%"..path_sep.."+", ""); 75 path = path:gsub("^%.%"..path_sep.."+", "");
76 76
77 local is_relative; 77 local is_relative;
78 if path_sep == "/" and path:sub(1,1) ~= "/" then 78 if path_sep == "/" and path:sub(1,1) ~= "/" then
79 is_relative = true; 79 is_relative = true;
80 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" or path:sub(2,3) ~= ":/")) then 80 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" or path:sub(2,3) ~= ":/")) then
81 is_relative = true; 81 is_relative = true;
83 if is_relative then 83 if is_relative then
84 return parent_path..path_sep..path; 84 return parent_path..path_sep..path;
85 end 85 end
86 end 86 end
87 return path; 87 return path;
88 end 88 end
89 end 89 end
90 90
91 -- Helper function to convert a glob to a Lua pattern 91 -- Helper function to convert a glob to a Lua pattern
92 local function glob_to_pattern(glob) 92 local function glob_to_pattern(glob)
93 return "^"..glob:gsub("[%p*?]", function (c) 93 return "^"..glob:gsub("[%p*?]", function (c)
165 end, 165 end,
166 __newindex = function (t, k, v) 166 __newindex = function (t, k, v)
167 set(config, env.__currenthost or "*", k, v); 167 set(config, env.__currenthost or "*", k, v);
168 end 168 end
169 }); 169 });
170 170
171 rawset(env, "__currenthost", "*") -- Default is global 171 rawset(env, "__currenthost", "*") -- Default is global
172 function env.VirtualHost(name) 172 function env.VirtualHost(name)
173 if rawget(config, name) and rawget(config[name], "component_module") then 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", 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); 175 name, config[name].component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
183 set(config, name or "*", option_name, option_value); 183 set(config, name or "*", option_name, option_value);
184 end 184 end
185 end; 185 end;
186 end 186 end
187 env.Host, env.host = env.VirtualHost, env.VirtualHost; 187 env.Host, env.host = env.VirtualHost, env.VirtualHost;
188 188
189 function env.Component(name) 189 function env.Component(name)
190 if rawget(config, name) and rawget(config[name], "defined") and not rawget(config[name], "component_module") then 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", 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); 192 name, name, name), 0);
193 end 193 end
199 rawset(env, "__currenthost", "*"); -- Return to global scope 199 rawset(env, "__currenthost", "*"); -- Return to global scope
200 for option_name, option_value in pairs(config_options) do 200 for option_name, option_value in pairs(config_options) do
201 set(config, name or "*", option_name, option_value); 201 set(config, name or "*", option_name, option_value);
202 end 202 end
203 end 203 end
204 204
205 return function (module) 205 return function (module)
206 if type(module) == "string" then 206 if type(module) == "string" then
207 set(config, name, "component_module", module); 207 set(config, name, "component_module", module);
208 return handle_config_options; 208 return handle_config_options;
209 end 209 end
210 return handle_config_options(module); 210 return handle_config_options(module);
211 end 211 end
212 end 212 end
213 env.component = env.Component; 213 env.component = env.Component;
214 214
215 function env.Include(file) 215 function env.Include(file)
216 if file:match("[*?]") then 216 if file:match("[*?]") then
217 local path_pos, glob = file:match("()([^"..path_sep.."]+)$"); 217 local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
218 local path = file:sub(1, math_max(path_pos-2,0)); 218 local path = file:sub(1, math_max(path_pos-2,0));
219 local config_path = config_file:gsub("[^"..path_sep.."]+$", ""); 219 local config_path = config_file:gsub("[^"..path_sep.."]+$", "");
238 if not f then error("Error loading included "..file..": "..err, 0); end 238 if not f then error("Error loading included "..file..": "..err, 0); end
239 return f, err; 239 return f, err;
240 end 240 end
241 end 241 end
242 env.include = env.Include; 242 env.include = env.Include;
243 243
244 function env.RunScript(file) 244 function env.RunScript(file)
245 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file)); 245 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
246 end 246 end
247 247
248 local chunk, err = envload(data, "@"..config_file, env); 248 local chunk, err = envload(data, "@"..config_file, env);
249 249
250 if not chunk then 250 if not chunk then
251 return nil, err; 251 return nil, err;
252 end 252 end
253 253
254 local ok, err = pcall(chunk); 254 local ok, err = pcall(chunk);
255 255
256 if not ok then 256 if not ok then
257 return nil, err; 257 return nil, err;
258 end 258 end
259 259
260 return true; 260 return true;
261 end 261 end
262 262
263 end 263 end
264 264
265 return _M; 265 return _M;