Channel Info+admin window; RPL_CHANNEL_URL; Ban list; CSS forms refactor
[KiwiIRC.git] / server / configuration.js
1 var fs = require('fs'),
2 events = require('events'),
3 util = require('util');
4
5 var config_filename = 'config.js',
6 config_dirs = ['/etc/kiwiirc/', __dirname + '/../'],
7 environment = 'production',
8 loaded_config = Object.create(null);
9
10 var Config = function () {
11 events.EventEmitter.call(this);
12 };
13 util.inherits(Config, events.EventEmitter);
14
15 Config.prototype.loadConfig = function () {
16 var new_config,
17 conf_filepath,
18 i;
19
20 // Loop through the possible config paths and find a usable one
21 for (i = 0; i < config_dirs.length; i++) {
22 conf_filepath = config_dirs[i] + config_filename;
23
24 try {
25 if (fs.lstatSync(conf_filepath).isFile() === true) {
26 // Clear the loaded config cache
27 delete require.cache[require.resolve(conf_filepath)];
28
29 // Try load the new config file
30 new_config = require(conf_filepath);
31 break;
32 }
33 } catch (e) {
34 switch (e.code) {
35 case 'ENOENT': // No file/dir
36 break;
37 default:
38 console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message);
39 return false;
40 }
41 continue;
42 }
43 }
44
45 if (new_config) {
46 loaded_config = new_config;
47 global.config = new_config[environment] || {};
48 this.emit('loaded');
49 return loaded_config;
50 } else {
51 return false;
52 }
53 };
54
55
56
57 Config.prototype.setEnvironment = function (new_environment) {
58 environment = new_environment;
59 };
60
61 // Get the current config. Optionally for a different environment than currently set
62 Config.prototype.get = function (specific_environment) {
63 specific_environment = specific_environment || environment;
64
65 return loaded_config[specific_environment] || {};
66 };
67
68 module.exports = new Config();