forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configfile.js
146 lines (123 loc) · 3.45 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
"use strict";
// Config file loader
var fs = require('fs');
// for "ini" type files
var regex = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
param: /^\s*([\w@\._]+)\s*=\s*(.*)\s*$/,
comment: /^\s*[;#].*$/,
line: /^\s*(.*)\s*$/,
blank: /^\s*$/
};
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
if (curr.mtime.getTime() !== prev.mtime.getTime()) {
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) {
return JSON.parse(fs.readFileSync(name));
}
cfreader.load_ini_config = function(name) {
var result = cfreader.empty_config('ini');
var current_sect = result.main;
var data = fs.readFileSync(name, "UTF-8");
var lines = data.split(/\r\n|\r|\n/);
var match;
lines.forEach( function(line) {
if (regex.comment.test(line)) {
return;
}
else if (regex.blank.test(line)) {
return;
}
else 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 if (match = regex.section.exec(line)) {
current_sect = result[match[1]] = {};
}
else {
// error ?
};
});
return result;
};
cfreader.load_flat_config = function(name, type) {
var result = [];
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]);
}
});
return result;
};