010b307652b4b9115b3343477913f56938b130f7
[prosody.git] / core / configmanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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
10
11 local _G = _G;
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;
14
15
16 local trb = debug.traceback
17
18 local eventmanager = require "core.eventmanager";
19
20 module "configmanager"
21
22 local parsers = {};
23
24 local config = { ["*"] = { core = {} } };
25
26 local global_config = config["*"];
27
28 -- When host not found, use global
29 setmetatable(config, { __index = function () return global_config; end});
30 local host_mt = { __index = global_config };
31
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
37                                                                         return section[k];
38                                                         end };
39 end
40
41 function getconfig()
42         return config;
43 end
44
45 function get(host, section, key)
46         local sec = config[host][section];
47         if sec then
48                 return sec[key];
49         end
50         return nil;
51 end
52
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];
58                 end
59                 if not rawget(hostconfig, section) then
60                         hostconfig[section] = setmetatable({}, section_mt(section));
61                 end
62                 hostconfig[section][key] = value;
63                 return true;
64         end
65         return false;
66 end
67
68 function load(filename, format)
69         format = format or filename:match("%w+$");
70
71         if parsers[format] and parsers[format].load then
72                 local f, err = io.open(filename);
73                 if f then
74                         local ok, err = parsers[format].load(f:read("*a"), filename);
75                         f:close();
76                         if ok then
77                                 eventmanager.fire_event("config-reloaded", { filename = filename, format = format });
78                         end
79                         return ok, "parser", err;
80                 end
81                 return f, "file", err;
82         end
83
84         if not format then
85                 return nil, "file", "no parser specified";
86         else
87                 return nil, "file", "no parser for "..(format);
88         end
89 end
90
91 function save(filename, format)
92 end
93
94 function addparser(format, parser)
95         if format and parser then
96                 parsers[format] = parser;
97         end
98 end
99
100 -- _M needed to avoid name clash with local 'parsers'
101 function _M.parsers()
102         local p = {};
103         for format in pairs(parsers) do
104                 table.insert(p, format);
105         end
106         return p;
107 end
108
109 -- Built-in Lua parser
110 do
111         local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
112         local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
113         parsers.lua = {};
114         function parsers.lua.load(data, filename)
115                 local env;
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;
122                                                                                                                 end;
123                                                                                 end,
124                                                                 __newindex = function (t, k, v)
125                                                                                         set(env.__currenthost or "*", "core", k, v);
126                                                                                 end});
127                 
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);
133                         end
134                         rawset(env, "__currenthost", name);
135                         -- Needs at least one setting to logically exist :)
136                         set(name or "*", "core", "defined", true);
137                 end
138                 env.host = env.Host;
139                 
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);
144                         end
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);
149         
150                         return function (module)
151                                         if type(module) == "string" then
152                                                 set(name, "core", "component_module", module);
153                                         end
154                                 end
155                 end
156                 env.component = env.Component;
157                 
158                 function env.Include(file)
159                         local f, err = io.open(file);
160                         if f then
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
164                         end
165                         if not f then error("Error loading included "..file..": "..err, 0); end
166                         return f, err;
167                 end
168                 env.include = env.Include;
169                 
170                 local chunk, err = loadstring(data, "@"..filename);
171                 
172                 if not chunk then
173                         return nil, err;
174                 end
175                 
176                 setfenv(chunk, env);
177                 
178                 local ok, err = pcall(chunk);
179                 
180                 if not ok then
181                         return nil, err;
182                 end
183                 
184                 return true;
185         end
186         
187 end
188
189 return _M;