2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
12 local setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table, format =
13 setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table, string.format;
16 local eventmanager = require "core.eventmanager";
18 module "configmanager"
22 local config = { ["*"] = { core = {} } };
24 local global_config = config["*"];
26 -- When host not found, use global
27 setmetatable(config, { __index = function () return global_config; end});
28 local host_mt = { __index = global_config };
30 -- When key not found in section, check key in global's section
31 function section_mt(section_name)
32 return { __index = function (t, k)
33 local section = rawget(global_config, section_name);
34 if not section then return nil; end
43 function get(host, section, key)
44 local sec = config[host][section];
51 function set(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];
57 if not rawget(hostconfig, section) then
58 hostconfig[section] = setmetatable({}, section_mt(section));
60 hostconfig[section][key] = value;
66 function load(filename, format)
67 format = format or filename:match("%w+$");
69 if parsers[format] and parsers[format].load then
70 local f, err = io.open(filename);
72 local ok, err = parsers[format].load(f:read("*a"), filename);
75 eventmanager.fire_event("config-reloaded", { filename = filename, format = format });
77 return ok, "parser", err;
79 return f, "file", err;
83 return nil, "file", "no parser specified";
85 return nil, "file", "no parser for "..(format);
89 function save(filename, format)
92 function addparser(format, parser)
93 if format and parser then
94 parsers[format] = parser;
98 -- _M needed to avoid name clash with local 'parsers'
101 for format in pairs(parsers) do
102 table.insert(p, format);
107 -- Built-in Lua parser
109 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
110 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
112 function parsers.lua.load(data, filename)
114 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
115 env = setmetatable({ Host = true; host = true; Component = true, component = true,
116 Include = true, include = true, RunScript = dofile }, { __index = function (t, k)
117 return rawget(_G, k) or
118 function (settings_table)
119 config[__currenthost or "*"][k] = settings_table;
122 __newindex = function (t, k, v)
123 set(env.__currenthost or "*", "core", k, v);
126 rawset(env, "__currenthost", "*") -- Default is global
127 function env.Host(name)
128 if rawget(config, name) and rawget(config[name].core, "component_module") then
129 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
130 name, config[name].core.component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
132 rawset(env, "__currenthost", name);
133 -- Needs at least one setting to logically exist :)
134 set(name or "*", "core", "defined", true);
138 function env.Component(name)
139 if rawget(config, name) and rawget(config[name].core, "defined") and not rawget(config[name].core, "component_module") then
140 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
141 name, name, name), 0);
143 set(name, "core", "component_module", "component");
144 -- Don't load the global modules by default
145 set(name, "core", "load_global_modules", false);
146 rawset(env, "__currenthost", name);
148 return function (module)
149 if type(module) == "string" then
150 set(name, "core", "component_module", module);
154 env.component = env.Component;
156 function env.Include(file)
157 local f, err = io.open(file);
159 local data = f:read("*a");
160 local ok, err = parsers.lua.load(data, file);
161 if not ok then error(err:gsub("%[string.-%]", file), 0); end
163 if not f then error("Error loading included "..file..": "..err, 0); end
166 env.include = env.Include;
168 local chunk, err = loadstring(data, "@"..filename);
176 local ok, err = pcall(chunk);