configmanager: Assign a chunk name to config files loaded using the default config...
[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 = 
13                 setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type;
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 -- Built-in Lua parser
98 do
99         local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
100         local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
101         parsers.lua = {};
102         function parsers.lua.load(data, filename)
103                 local env;
104                 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
105                 env = setmetatable({ Host = true; host = true; Component = true, component = true,
106                                                         Include = true, include = true, RunScript = dofile }, { __index = function (t, k)
107                                                                                                 return rawget(_G, k) or
108                                                                                                                 function (settings_table)
109                                                                                                                         config[__currenthost or "*"][k] = settings_table;
110                                                                                                                 end;
111                                                                                 end,
112                                                                 __newindex = function (t, k, v)
113                                                                                         set(env.__currenthost or "*", "core", k, v);
114                                                                                 end});
115                 
116                 rawset(env, "__currenthost", "*") -- Default is global
117                 function env.Host(name)
118                         rawset(env, "__currenthost", name);
119                         -- Needs at least one setting to logically exist :)
120                         set(name or "*", "core", "defined", true);
121                 end
122                 env.host = env.Host;
123                 
124                 function env.Component(name)
125                         set(name, "core", "component_module", "component");
126                         -- Don't load the global modules by default
127                         set(name, "core", "load_global_modules", false);
128                         rawset(env, "__currenthost", name);
129         
130                         return function (module)
131                                         if type(module) == "string" then
132                                                 set(name, "core", "component_module", module);
133                                         end
134                                 end
135                 end
136                 env.component = env.Component;
137                 
138                 function env.Include(file)
139                         local f, err = io.open(file);
140                         if f then
141                                 local data = f:read("*a");
142                                 local ok, err = parsers.lua.load(data, file);
143                                 if not ok then error(err:gsub("%[string.-%]", file), 0); end
144                         end
145                         if not f then error("Error loading included "..file..": "..err, 0); end
146                         return f, err;
147                 end
148                 env.include = env.Include;
149                 
150                 local chunk, err = loadstring(data, "@"..filename);
151                 
152                 if not chunk then
153                         return nil, err;
154                 end
155                 
156                 setfenv(chunk, env);
157                 
158                 local ok, err = pcall(chunk);
159                 
160                 if not ok then
161                         return nil, err;
162                 end
163                 
164                 return true;
165         end
166         
167 end
168
169 return _M;