forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configfile.js
213 lines (190 loc) · 5.64 KB
/
configfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"use strict";
// Config file loader
var fs = require('fs');
var utils = require('./utils');
var logger = require('./logger');
// for "ini" type files
var regex = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
param: /^\s*([\w@\._]+)\s*=\s*(.*)\s*$/,
comment: /^\s*[;#].*$/,
line: /^\s*(.*)\s*$/,
blank: /^\s*$/,
continuation: /\\[ \t]*$/,
};
var cfreader = exports;
cfreader.watch_files = true;
cfreader._config_cache = {};
cfreader.read_config = function(name, type, cb) {
// Check cache first
if (cfreader._config_cache[name]) {
return cfreader._config_cache[name];
}
// load config file
var result = cfreader.load_config(name, type);
if (cfreader.watch_files) {
fs.unwatchFile(name);
fs.watchFile(name, function (curr, prev) {
// file has changed, or files has been removed
if (curr.mtime.getTime() !== prev.mtime.getTime() ||
curr.nlink !== prev.nlink)
{
cfreader.load_config(name, type);
if (typeof cb === 'function') cb();
}
});
}
return result;
}
cfreader.empty_config = function(type) {
if (type === 'ini') {
return { main: {} };
}
else if (type === 'json') {
return {};
}
else {
return [];
}
};
cfreader.load_config = function(name, type) {
var result;
if (type === 'ini' || /\.ini$/.test(name)) {
result = cfreader.load_ini_config(name);
}
else if (type === 'json' || /\.json$/.test(name)) {
result = cfreader.load_json_config(name);
}
else {
result = cfreader.load_flat_config(name, type);
if (result && type !== 'list' && type !== 'data') {
result = result[0];
if (/^\d+$/.test(result)) {
result = parseInt(result);
}
}
}
cfreader._config_cache[name] = result;
return result;
};
cfreader.load_json_config = function(name) {
var result = cfreader.empty_config('json');
try {
if (utils.existsSync(name)) {
result = JSON.parse(fs.readFileSync(name));
}
}
catch (err) {
if (err.code === 'EBADF') {
if (cfreader._config_cache[name]) {
return cfreader._config_cache[name];
}
}
else {
throw err;
}
}
return result;
}
cfreader.load_ini_config = function(name) {
var result = cfreader.empty_config('ini');
var current_sect = result.main;
try {
if (utils.existsSync(name)) {
var data = fs.readFileSync(name, "UTF-8");
var lines = data.split(/\r\n|\r|\n/);
var match;
var pre = '';
lines.forEach(function(line) {
if (regex.comment.test(line)) {
return;
}
else if (regex.blank.test(line)) {
return;
}
else if (match = regex.section.exec(line)) {
current_sect = result[match[1]] = {};
return;
}
else if (regex.continuation.test(line)) {
pre += line.replace(regex.continuation, '');
return;
}
line = pre + line;
pre = '';
if (match = regex.param.exec(line)) {
if (/^\d+$/.test(match[2])) {
current_sect[match[1]] = parseInt(match[2]);
}
else {
current_sect[match[1]] = match[2];
}
}
else {
logger.logerror("Unvalid line in config file '" + name + "': " + line);
};
});
}
}
catch (err) {
if (err.code === 'EBADF') {
if (cfreader._config_cache[name]) {
return cfreader._config_cache[name];
}
}
else {
throw err;
}
}
return result;
};
cfreader.load_flat_config = function(name, type) {
var result = cfreader.empty_config();
try {
if (utils.existsSync(name)) {
var data = fs.readFileSync(name, "UTF-8");
if (type === 'data') {
while (data.length > 0) {
var match = data.match(/^([^\n]*)\n?/);
result.push(match[1]);
data = data.slice(match[0].length);
}
return result;
}
var lines = data.split(/\r\n|\r|\n/);
lines.forEach( function(line) {
var line_data;
if (regex.comment.test(line)) {
return;
}
else if (regex.blank.test(line)) {
return;
}
else if (line_data = regex.line.exec(line)) {
result.push(line_data[1]);
}
});
}
}
catch (err) {
if (err.code === 'EBADF') {
if (cfreader._config_cache[name]) {
return cfreader._config_cache[name];
}
}
else {
throw err;
}
}
// Return hostname for 'me' if no result
if (/\/me$/.test(name) && !(result && result.length)) {
return [ require('os').hostname() ];
}
// For value types with no result
if (!(type && (type === 'list' || type === 'data'))) {
if (!(result && result.length)) {
return null;
}
}
return result;
};