2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 trb = debug.traceback
18 local eventmanager = require "core.eventmanager";
20 module "configmanager"
24 local config = { ["*"] = { core = {} } };
26 local global_config = config["*"];
28 -- When host not found, use global
29 setmetatable(config, { __index = function () return global_config; end});
30 local host_mt = { __index = global_config };
32 -- When key not found in section, check key in global's section
33 function section_mt(section_name)
34 return { __index = function (t, k)
35 local section = rawget(global_config, section_name);
36 if not section then return nil; end
45 function get(host, section, key)
46 local sec = config[host][section];
53 function set(host, section, key, value)
54 if host and section and key then
55 local hostconfig = rawget(config, host);
56 if not hostconfig then
57 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
59 if not rawget(hostconfig, section) then
60 hostconfig[section] = setmetatable({}, section_mt(section));
62 hostconfig[section][key] = value;
68 function load(filename, format)
69 format = format or filename:match("%w+$");
71 if parsers[format] and parsers[format].load then
72 local f, err = io.open(filename);
74 local ok, err = parsers[format].load(f:read("*a"), filename);
77 eventmanager.fire_event("config-reloaded", { filename = filename, format = format });
79 return ok, "parser", err;
81 return f, "file", err;
85 return nil, "file", "no parser specified";
87 return nil, "file", "no parser for "..(format);
91 function save(filename, format)
94 function addparser(format, parser)
95 if format and parser then
96 parsers[format] = parser;
100 -- _M needed to avoid name clash with local 'parsers'
101 function _M.parsers()
103 for format in pairs(parsers) do
104 table.insert(p, format);
109 -- Built-in Lua parser
111 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
112 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
114 function parsers.lua.load(data, filename)
116 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
117 env = setmetatable({ Host = true; host = true; Component = true, component = true,
118 Include = true, include = true, RunScript = dofile }, { __index = function (t, k)
119 return rawget(_G, k) or
120 function (settings_table)
121 config[__currenthost or "*"][k] = settings_table;
124 __newindex = function (t, k, v)
125 set(env.__currenthost or "*", "core", k, v);
128 rawset(env, "__currenthost", "*") -- Default is global
129 function env.Host(name)
130 if rawget(config, name) and rawget(config[name].core, "component_module") then
131 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
132 name, config[name].core.component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
134 rawset(env, "__currenthost", name);
135 -- Needs at least one setting to logically exist :)
136 set(name or "*", "core", "defined", true);
140 function env.Component(name)
141 if rawget(config, name) and rawget(config[name].core, "defined") and not rawget(config[name].core, "component_module") then
142 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
143 name, name, name), 0);
145 set(name, "core", "component_module", "component");
146 -- Don't load the global modules by default
147 set(name, "core", "load_global_modules", false);
148 rawset(env, "__currenthost", name);
150 return function (module)
151 if type(module) == "string" then
152 set(name, "core", "component_module", module);
156 env.component = env.Component;
158 function env.Include(file)
159 local f, err = io.open(file);
161 local data = f:read("*a");
162 local ok, err = parsers.lua.load(data, file);
163 if not ok then error(err:gsub("%[string.-%]", file), 0); end
165 if not f then error("Error loading included "..file..": "..err, 0); end
168 env.include = env.Include;
170 local chunk, err = loadstring(data, "@"..filename);
178 local ok, err = pcall(chunk);