channel:join + channel:leave plugin events
[KiwiIRC.git] / server / configuration.js
index bc05e6ebd669adb00dda21f3dafa184d86fac109..12959c686083e5fc79575ff6c97f042ce3f090f2 100644 (file)
@@ -1,6 +1,8 @@
 var fs      = require('fs'),
     events  = require('events'),
-    util    = require('util');
+    util    = require('util'),
+    path    = require('path'),
+    winston = require('winston');
 
 var config_filename = 'config.js',
     config_dirs = ['/etc/kiwiirc/', __dirname + '/../'],
@@ -12,33 +14,57 @@ var Config = function () {
 };
 util.inherits(Config, events.EventEmitter);
 
-Config.prototype.loadConfig = function () {
+Config.prototype.loadConfig = function (manual_config_file) {
     var new_config,
         conf_filepath,
         i;
 
-    // Loop through the possible config paths and find a usable one
-    for (i = 0; i < config_dirs.length; i++) {
-        conf_filepath = config_dirs[i] + config_filename;
+    if ((manual_config_file) || (this.manual_config_file)) {
+        manual_config_file =  path.resolve(path.normalize(manual_config_file || this.manual_config_file));
+        if (fs.existsSync(manual_config_file)) {
+            try {
+                if (fs.lstatSync(manual_config_file).isFile() === true) {
+                    // Clear the loaded config cache
+                    delete require.cache[require.resolve(manual_config_file)];
 
-        try {
-            if (fs.lstatSync(conf_filepath).isFile() === true) {
-                // Clear the loaded config cache
-                delete require.cache[require.resolve(conf_filepath)];
+                    // Try load the new config file
+                    new_config = require(manual_config_file);
 
-                // Try load the new config file
-                new_config = require(conf_filepath);
-                break;
+                    // Save location of configuration file so that we can re-load it later
+                    this.manual_config_file = manual_config_file;
+                }
+            } catch (e) {
+                winston.error('An error occured parsing the config file %s: %s', manual_config_file, e.message);
+                process.exit(1);
             }
-        } catch (e) {
-            switch (e.code) {
-            case 'ENOENT':      // No file/dir
-                break;
-            default:
-                console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message);
-                return false;
+        } else {
+            winston.error('Could not find config file %s', manual_config_file);
+            process.exit(1);
+        }
+    } else {
+        // Loop through the possible config paths and find a usable one
+        for (i = 0; i < config_dirs.length; i++) {
+            conf_filepath = config_dirs[i] + config_filename;
+
+            try {
+                if (fs.lstatSync(conf_filepath).isFile() === true) {
+                    // Clear the loaded config cache
+                    delete require.cache[require.resolve(conf_filepath)];
+
+                    // Try load the new config file
+                    new_config = require(conf_filepath);
+                    break;
+                }
+            } catch (e) {
+                switch (e.code) {
+                case 'ENOENT':      // No file/dir
+                    break;
+                default:
+                    winston.warn('An error occured parsing the config file %s%s: %s', config_dirs[i], config_filename, e.message);
+                    return false;
+                }
+                continue;
             }
-            continue;
         }
     }
 
@@ -61,7 +87,7 @@ Config.prototype.setEnvironment = function (new_environment) {
 // Get the current config. Optionally for a different environment than currently set
 Config.prototype.get = function (specific_environment) {
     specific_environment = specific_environment || environment;
-    
+
     return loaded_config[specific_environment] || {};
 };