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