Software /
code /
prosody
Annotate
core/configmanager.lua @ 2637:82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 14 Feb 2010 18:41:44 +0000 |
parent | 2552:8dda55217e83 |
child | 2640:2dace3e87dd1 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1504
diff
changeset
|
1 -- Prosody IM |
760
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
466
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
466
diff
changeset
|
7 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
466
diff
changeset
|
8 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
466
diff
changeset
|
9 |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local _G = _G; |
2637
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
12 local setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table, format = |
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
13 setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table, string.format; |
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
14 |
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
15 |
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
16 local trb = debug.traceback |
466
0ecfd89c2cc0
Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents:
376
diff
changeset
|
17 |
1000
a73715a9267f
core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents:
913
diff
changeset
|
18 local eventmanager = require "core.eventmanager"; |
a73715a9267f
core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents:
913
diff
changeset
|
19 |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 module "configmanager" |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 local parsers = {}; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 local config = { ["*"] = { core = {} } }; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local global_config = config["*"]; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 -- When host not found, use global |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 setmetatable(config, { __index = function () return global_config; end}); |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 local host_mt = { __index = global_config }; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 -- When key not found in section, check key in global's section |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 function section_mt(section_name) |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return { __index = function (t, k) |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local section = rawget(global_config, section_name); |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 if not section then return nil; end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 return section[k]; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 end }; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
41 function getconfig() |
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
42 return config; |
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
43 end |
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
44 |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 function get(host, section, key) |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 local sec = config[host][section]; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 if sec then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 return sec[key]; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 return nil; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 function set(host, section, key, value) |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 if host and section and key then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 local hostconfig = rawget(config, host); |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 if not hostconfig then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host]; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 if not rawget(hostconfig, section) then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 hostconfig[section] = setmetatable({}, section_mt(section)); |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 hostconfig[section][key] = value; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 return true; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 return false; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 function load(filename, format) |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
69 format = format or filename:match("%w+$"); |
466
0ecfd89c2cc0
Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents:
376
diff
changeset
|
70 |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 if parsers[format] and parsers[format].load then |
466
0ecfd89c2cc0
Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents:
376
diff
changeset
|
72 local f, err = io.open(filename); |
2552
8dda55217e83
configmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2427
diff
changeset
|
73 if f then |
1777
de86734d3f7f
configmanager: Assign a chunk name to config files loaded using the default config loader (fixes issues with some diagnostic tools).
Waqas Hussain <waqas20@gmail.com>
parents:
1615
diff
changeset
|
74 local ok, err = parsers[format].load(f:read("*a"), filename); |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 f:close(); |
1000
a73715a9267f
core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents:
913
diff
changeset
|
76 if ok then |
a73715a9267f
core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents:
913
diff
changeset
|
77 eventmanager.fire_event("config-reloaded", { filename = filename, format = format }); |
a73715a9267f
core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents:
913
diff
changeset
|
78 end |
793
55add3b87c01
Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
79 return ok, "parser", err; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 end |
793
55add3b87c01
Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
81 return f, "file", err; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end |
466
0ecfd89c2cc0
Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents:
376
diff
changeset
|
83 |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
84 if not format then |
793
55add3b87c01
Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
85 return nil, "file", "no parser specified"; |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
86 else |
793
55add3b87c01
Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
87 return nil, "file", "no parser for "..(format); |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
88 end |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 function save(filename, format) |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 function addparser(format, parser) |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 if format and parser then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 parsers[format] = parser; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 |
2427
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
100 -- _M needed to avoid name clash with local 'parsers' |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
101 function _M.parsers() |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
102 local p = {}; |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
103 for format in pairs(parsers) do |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
104 table.insert(p, format); |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
105 end |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
106 return p; |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
107 end |
343a9eb7540e
configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents:
1777
diff
changeset
|
108 |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
109 -- Built-in Lua parser |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 do |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
111 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable; |
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
112 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 parsers.lua = {}; |
1777
de86734d3f7f
configmanager: Assign a chunk name to config files loaded using the default config loader (fixes issues with some diagnostic tools).
Waqas Hussain <waqas20@gmail.com>
parents:
1615
diff
changeset
|
114 function parsers.lua.load(data, filename) |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
115 local env; |
750
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
116 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below |
794
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
117 env = setmetatable({ Host = true; host = true; Component = true, component = true, |
795
e27a48e35bbb
Add RunScript command to config to run a Lua script prior to starting the server
Matthew Wild <mwild1@gmail.com>
parents:
794
diff
changeset
|
118 Include = true, include = true, RunScript = dofile }, { __index = function (t, k) |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
119 return rawget(_G, k) or |
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
120 function (settings_table) |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 config[__currenthost or "*"][k] = settings_table; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 end; |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
123 end, |
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
124 __newindex = function (t, k, v) |
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
125 set(env.__currenthost or "*", "core", k, v); |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 end}); |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 |
1615
0e3eacf135f2
configmanager: Default options appearing before Host "*" to global (fixes potential traceback)
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
128 rawset(env, "__currenthost", "*") -- Default is global |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 function env.Host(name) |
2637
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
130 if rawget(config, name) and rawget(config[name].core, "component_module") then |
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
131 error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s", |
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
132 name, config[name].core.component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0); |
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
133 end |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
134 rawset(env, "__currenthost", name); |
750
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
135 -- Needs at least one setting to logically exist :) |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
136 set(name or "*", "core", "defined", true); |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 end |
376
6d87944df37c
New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents:
371
diff
changeset
|
138 env.host = env.Host; |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 |
750
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
140 function env.Component(name) |
2637
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
141 if rawget(config, name) and rawget(config[name].core, "defined") and not rawget(config[name].core, "component_module") then |
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
142 error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s", |
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
143 name, name, name), 0); |
82cfc1ec4a7c
configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents:
2552
diff
changeset
|
144 end |
913
3e2dac84017d
core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
145 set(name, "core", "component_module", "component"); |
3e2dac84017d
core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
146 -- Don't load the global modules by default |
1504
9d8c35e98ca2
configmanager, modulemanager: Allow components to have modules specified in the config (but don't load the global set of modules for them)
Matthew Wild <mwild1@gmail.com>
parents:
1000
diff
changeset
|
147 set(name, "core", "load_global_modules", false); |
913
3e2dac84017d
core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
148 rawset(env, "__currenthost", name); |
3e2dac84017d
core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
149 |
750
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
150 return function (module) |
857
49298263f241
core.configmanager: Small fix to check validity of Component definitions
Matthew Wild <mwild1@gmail.com>
parents:
795
diff
changeset
|
151 if type(module) == "string" then |
49298263f241
core.configmanager: Small fix to check validity of Component definitions
Matthew Wild <mwild1@gmail.com>
parents:
795
diff
changeset
|
152 set(name, "core", "component_module", module); |
49298263f241
core.configmanager: Small fix to check validity of Component definitions
Matthew Wild <mwild1@gmail.com>
parents:
795
diff
changeset
|
153 end |
750
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
154 end |
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
155 end |
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
156 env.component = env.Component; |
fbfcf8c1c830
configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
157 |
794
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
158 function env.Include(file) |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
159 local f, err = io.open(file); |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
160 if f then |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
161 local data = f:read("*a"); |
1777
de86734d3f7f
configmanager: Assign a chunk name to config files loaded using the default config loader (fixes issues with some diagnostic tools).
Waqas Hussain <waqas20@gmail.com>
parents:
1615
diff
changeset
|
162 local ok, err = parsers.lua.load(data, file); |
794
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
163 if not ok then error(err:gsub("%[string.-%]", file), 0); end |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
164 end |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
165 if not f then error("Error loading included "..file..": "..err, 0); end |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
166 return f, err; |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
167 end |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
168 env.include = env.Include; |
912dc389935a
Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents:
793
diff
changeset
|
169 |
1777
de86734d3f7f
configmanager: Assign a chunk name to config files loaded using the default config loader (fixes issues with some diagnostic tools).
Waqas Hussain <waqas20@gmail.com>
parents:
1615
diff
changeset
|
170 local chunk, err = loadstring(data, "@"..filename); |
371
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 if not chunk then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 return nil, err; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 setfenv(chunk, env); |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 local ok, err = pcall(chunk); |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 if not ok then |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 return nil, err; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 return true; |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 end |
0dc5819660e8
Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 |
466
0ecfd89c2cc0
Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents:
376
diff
changeset
|
189 return _M; |