prosody: Read list of possible config extensions from configmanager, removes old...
[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 =
13                 setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table;
14
15 local eventmanager = require "core.eventmanager";
16
17 module "configmanager"
18
19 local parsers = {};
20
21 local config = { ["*"] = { core = {} } };
22
23 local global_config = config["*"];
24
25 -- When host not found, use global
26 setmetatable(config, { __index = function () return global_config; end});
27 local host_mt = { __index = global_config };
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(global_config, section_name);
33                                                                         if not section then return nil; end
34                                                                         return section[k];
35                                                         end };
36 end
37
38 function getconfig()
39         return config;
40 end
41
42 function get(host, section, key)
43         local sec = config[host][section];
44         if sec then
45                 return sec[key];
46         end
47         return nil;
48 end
49
50 function set(host, section, key, value)
51         if host and section and key then
52                 local hostconfig = rawget(config, host);
53                 if not hostconfig then
54                         hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
55                 end
56                 if not rawget(hostconfig, section) then
57                         hostconfig[section] = setmetatable({}, section_mt(section));
58                 end
59                 hostconfig[section][key] = value;
60                 return true;
61         end
62         return false;
63 end
64
65 function load(filename, format)
66         format = format or filename:match("%w+$");
67
68         if parsers[format] and parsers[format].load then
69                 local f, err = io.open(filename);
70                 if f then 
71                         local ok, err = parsers[format].load(f:read("*a"), filename);
72                         f:close();
73                         if ok then
74                                 eventmanager.fire_event("config-reloaded", { filename = filename, format = format });
75                         end
76                         return ok, "parser", err;
77                 end
78                 return f, "file", err;
79         end
80
81         if not format then
82                 return nil, "file", "no parser specified";
83         else
84                 return nil, "file", "no parser for "..(format);
85         end
86 end
87
88 function save(filename, format)
89 end
90
91 function addparser(format, parser)
92         if format and parser then
93                 parsers[format] = parser;
94         end
95 end
96
97 -- _M needed to avoid name clash with local 'parsers'
98 function _M.parsers()
99         local p = {};
100         for format in pairs(parsers) do
101                 table.insert(p, format);
102         end
103         return p;
104 end
105
106 -- Built-in Lua parser
107 do
108         local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
109         local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
110         parsers.lua = {};
111         function parsers.lua.load(data, filename)
112                 local env;
113                 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
114                 env = setmetatable({ Host = true; host = true; Component = true, component = true,
115                                                         Include = true, include = true, RunScript = dofile }, { __index = function (t, k)
116                                                                                                 return rawget(_G, k) or
117                                                                                                                 function (settings_table)
118                                                                                                                         config[__currenthost or "*"][k] = settings_table;
119                                                                                                                 end;
120                                                                                 end,
121                                                                 __newindex = function (t, k, v)
122                                                                                         set(env.__currenthost or "*", "core", k, v);
123                                                                                 end});
124                 
125                 rawset(env, "__currenthost", "*") -- Default is global
126                 function env.Host(name)
127                         rawset(env, "__currenthost", name);
128                         -- Needs at least one setting to logically exist :)
129                         set(name or "*", "core", "defined", true);
130                 end
131                 env.host = env.Host;
132                 
133                 function env.Component(name)
134                         set(name, "core", "component_module", "component");
135                         -- Don't load the global modules by default
136                         set(name, "core", "load_global_modules", false);
137                         rawset(env, "__currenthost", name);
138         
139                         return function (module)
140                                         if type(module) == "string" then
141                                                 set(name, "core", "component_module", module);
142                                         end
143                                 end
144                 end
145                 env.component = env.Component;
146                 
147                 function env.Include(file)
148                         local f, err = io.open(file);
149                         if f then
150                                 local data = f:read("*a");
151                                 local ok, err = parsers.lua.load(data, file);
152                                 if not ok then error(err:gsub("%[string.-%]", file), 0); end
153                         end
154                         if not f then error("Error loading included "..file..": "..err, 0); end
155                         return f, err;
156                 end
157                 env.include = env.Include;
158                 
159                 local chunk, err = loadstring(data, "@"..filename);
160                 
161                 if not chunk then
162                         return nil, err;
163                 end
164                 
165                 setfenv(chunk, env);
166                 
167                 local ok, err = pcall(chunk);
168                 
169                 if not ok then
170                         return nil, err;
171                 end
172                 
173                 return true;
174         end
175         
176 end
177
178 return _M;