Backed out changeset c75c22c316d6 (the XMPP spec now gives a better way to do this)
[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 eventmanager = require "core.eventmanager";
17
18 module "configmanager"
19
20 local parsers = {};
21
22 local config = { ["*"] = { core = {} } };
23
24 local global_config = config["*"];
25
26 -- When host not found, use global
27 setmetatable(config, { __index = function () return global_config; end});
28 local host_mt = { __index = global_config };
29
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
35                                                                         return section[k];
36                                                         end };
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 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];
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 load(filename, format)
67         format = format or filename:match("%w+$");
68
69         if parsers[format] and parsers[format].load then
70                 local f, err = io.open(filename);
71                 if f then
72                         local ok, err = parsers[format].load(f:read("*a"), filename);
73                         f:close();
74                         if ok then
75                                 eventmanager.fire_event("config-reloaded", { filename = filename, format = format });
76                         end
77                         return ok, "parser", err;
78                 end
79                 return f, "file", err;
80         end
81
82         if not format then
83                 return nil, "file", "no parser specified";
84         else
85                 return nil, "file", "no parser for "..(format);
86         end
87 end
88
89 function save(filename, format)
90 end
91
92 function addparser(format, parser)
93         if format and parser then
94                 parsers[format] = parser;
95         end
96 end
97
98 -- _M needed to avoid name clash with local 'parsers'
99 function _M.parsers()
100         local p = {};
101         for format in pairs(parsers) do
102                 table.insert(p, format);
103         end
104         return p;
105 end
106
107 -- Built-in Lua parser
108 do
109         local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
110         local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
111         parsers.lua = {};
112         function parsers.lua.load(data, filename)
113                 local env;
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;
120                                                                                                                 end;
121                                                                                 end,
122                                                                 __newindex = function (t, k, v)
123                                                                                         set(env.__currenthost or "*", "core", k, v);
124                                                                                 end});
125                 
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);
131                         end
132                         rawset(env, "__currenthost", name);
133                         -- Needs at least one setting to logically exist :)
134                         set(name or "*", "core", "defined", true);
135                 end
136                 env.host = env.Host;
137                 
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);
142                         end
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);
147         
148                         return function (module)
149                                         if type(module) == "string" then
150                                                 set(name, "core", "component_module", module);
151                                         end
152                                 end
153                 end
154                 env.component = env.Component;
155                 
156                 function env.Include(file)
157                         local f, err = io.open(file);
158                         if f then
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
162                         end
163                         if not f then error("Error loading included "..file..": "..err, 0); end
164                         return f, err;
165                 end
166                 env.include = env.Include;
167                 
168                 local chunk, err = loadstring(data, "@"..filename);
169                 
170                 if not chunk then
171                         return nil, err;
172                 end
173                 
174                 setfenv(chunk, env);
175                 
176                 local ok, err = pcall(chunk);
177                 
178                 if not ok then
179                         return nil, err;
180                 end
181                 
182                 return true;
183         end
184         
185 end
186
187 return _M;