Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 179 lines (146 sloc) 4.565 kb
ba596f3 @mwild1 Remove version number from copyright headers
mwild1 authored
1 -- Prosody IM
a4d1701 @mwild1 Update copyright notices for 2009
mwild1 authored
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
dd01eab @mwild1 Insert copyright/license headers
mwild1 authored
4 --
489e850 @mwild1 GPL->MIT!
mwild1 authored
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
dd01eab @mwild1 Insert copyright/license headers
mwild1 authored
7 --
8
9
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
10
11 local _G = _G;
4f30ec4 @mwild1 configmanager: Add parsers() method to return an array of supported conf...
mwild1 authored
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;
63cedeb @mwild1 Fix for configmanager when config file can't be found
mwild1 authored
14
03aa609 @mwild1 core.configmanager: Fire event when (re)loading config file
mwild1 authored
15 local eventmanager = require "core.eventmanager";
16
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
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
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
38 function getconfig()
39 return config;
40 end
41
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
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)
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
66 format = format or filename:match("%w+$");
63cedeb @mwild1 Fix for configmanager when config file can't be found
mwild1 authored
67
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
68 if parsers[format] and parsers[format].load then
63cedeb @mwild1 Fix for configmanager when config file can't be found
mwild1 authored
69 local f, err = io.open(filename);
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
70 if f then
db64a35 @zeen configmanager: Assign a chunk name to config files loaded using the defa...
zeen authored
71 local ok, err = parsers[format].load(f:read("*a"), filename);
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
72 f:close();
03aa609 @mwild1 core.configmanager: Fire event when (re)loading config file
mwild1 authored
73 if ok then
74 eventmanager.fire_event("config-reloaded", { filename = filename, format = format });
75 end
4be0b27 @mwild1 Report errors in the config file to the user
mwild1 authored
76 return ok, "parser", err;
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
77 end
4be0b27 @mwild1 Report errors in the config file to the user
mwild1 authored
78 return f, "file", err;
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
79 end
63cedeb @mwild1 Fix for configmanager when config file can't be found
mwild1 authored
80
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
81 if not format then
4be0b27 @mwild1 Report errors in the config file to the user
mwild1 authored
82 return nil, "file", "no parser specified";
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
83 else
4be0b27 @mwild1 Report errors in the config file to the user
mwild1 authored
84 return nil, "file", "no parser for "..(format);
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
85 end
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
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
4f30ec4 @mwild1 configmanager: Add parsers() method to return an array of supported conf...
mwild1 authored
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
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
106 -- Built-in Lua parser
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
107 do
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
108 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
109 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
110 parsers.lua = {};
db64a35 @zeen configmanager: Assign a chunk name to config files loaded using the defa...
zeen authored
111 function parsers.lua.load(data, filename)
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
112 local env;
cf0ce99 @mwild1 configmanager: Add support for defining components
mwild1 authored
113 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
41823bd @mwild1 Add Include command to include extra configuration files from the main o...
mwild1 authored
114 env = setmetatable({ Host = true; host = true; Component = true, component = true,
a1d73d9 @mwild1 Add RunScript command to config to run a Lua script prior to starting th...
mwild1 authored
115 Include = true, include = true, RunScript = dofile }, { __index = function (t, k)
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
116 return rawget(_G, k) or
117 function (settings_table)
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
118 config[__currenthost or "*"][k] = settings_table;
119 end;
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
120 end,
121 __newindex = function (t, k, v)
122 set(env.__currenthost or "*", "core", k, v);
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
123 end});
124
0bf8d8a @mwild1 configmanager: Default options appearing before Host "*" to global (fixe...
mwild1 authored
125 rawset(env, "__currenthost", "*") -- Default is global
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
126 function env.Host(name)
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
127 rawset(env, "__currenthost", name);
cf0ce99 @mwild1 configmanager: Add support for defining components
mwild1 authored
128 -- Needs at least one setting to logically exist :)
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
129 set(name or "*", "core", "defined", true);
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
130 end
cb3d819 @mwild1 New configmanager. Old-style config files still work, but will print a w...
mwild1 authored
131 env.host = env.Host;
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
132
cf0ce99 @mwild1 configmanager: Add support for defining components
mwild1 authored
133 function env.Component(name)
f82d922 @mwild1 core.configmanager: Make components use 'component' module by default if...
mwild1 authored
134 set(name, "core", "component_module", "component");
135 -- Don't load the global modules by default
30696ec @mwild1 configmanager, modulemanager: Allow components to have modules specified...
mwild1 authored
136 set(name, "core", "load_global_modules", false);
f82d922 @mwild1 core.configmanager: Make components use 'component' module by default if...
mwild1 authored
137 rawset(env, "__currenthost", name);
138
cf0ce99 @mwild1 configmanager: Add support for defining components
mwild1 authored
139 return function (module)
d032e26 @mwild1 core.configmanager: Small fix to check validity of Component definitions
mwild1 authored
140 if type(module) == "string" then
141 set(name, "core", "component_module", module);
142 end
cf0ce99 @mwild1 configmanager: Add support for defining components
mwild1 authored
143 end
144 end
145 env.component = env.Component;
146
41823bd @mwild1 Add Include command to include extra configuration files from the main o...
mwild1 authored
147 function env.Include(file)
148 local f, err = io.open(file);
149 if f then
150 local data = f:read("*a");
db64a35 @zeen configmanager: Assign a chunk name to config files loaded using the defa...
zeen authored
151 local ok, err = parsers.lua.load(data, file);
41823bd @mwild1 Add Include command to include extra configuration files from the main o...
mwild1 authored
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
db64a35 @zeen configmanager: Assign a chunk name to config files loaded using the defa...
zeen authored
159 local chunk, err = loadstring(data, "@"..filename);
b09d216 @mwild1 Import initial configmanager, not sure if it works yet, but it does pass...
mwild1 authored
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
63cedeb @mwild1 Fix for configmanager when config file can't be found
mwild1 authored
178 return _M;
Something went wrong with that request. Please try again.