Config files now as a nodejs module
authorDarren <darren@darrenwhitlen.com>
Sun, 21 Oct 2012 23:20:16 +0000 (00:20 +0100)
committerDarren <darren@darrenwhitlen.com>
Sun, 21 Oct 2012 23:20:16 +0000 (00:20 +0100)
server/config.js [new file with mode: 0644]
server/config.json [deleted file]
server/kiwi.js

diff --git a/server/config.js b/server/config.js
new file mode 100644 (file)
index 0000000..037d1e0
--- /dev/null
@@ -0,0 +1,84 @@
+var conf = {};
+
+// Run the Kiwi server under a different user/group
+conf.user = "";
+conf.group = "";
+
+
+// Server listen blocks
+conf.servers = [];
+
+// Example SSL server block
+conf.servers.push({
+    port:     7777,
+    address: "0.0.0.0",
+
+    secure:   true,
+    ssl_key: "server.key",
+    ssl_cert: "cert.pem"
+});
+
+// Example plain-text server block
+conf.servers.push({
+    secure:   false,
+    port:   7778,
+    address: "0.0.0.0"
+});
+
+// Where the client files are
+conf.public_http = "client/";
+
+// Max connections per connection
+conf.max_client_conns = 5;
+
+// Enabled CAP extensions (See ENTER URL TO CAP INFO HERE PLS)
+conf.cap_options = [];
+
+
+
+
+// Directory to find the server modules
+conf.module_dir = "./kiwi_modules/";
+
+// Which modules to load
+conf.modules = ["spamfilter", "statistics"];
+
+
+
+
+// WebIRC passwords enabled for this server
+conf.webirc_pass = {
+    //"irc.network.com":  "configured_webirc_password",
+    //"127.0.0.1":        "foobar"
+};
+
+// Some IRCDs require the clients IP via the username/ident
+conf.ip_as_username = [
+       "irc.network.com",
+       "127.0.0.1"
+];
+
+
+
+
+// Enabled transports for the client to use
+conf.transports = [
+    "websocket",
+    "flashsocket",
+    "htmlfile",
+    "xhr-polling",
+    "jsonp-polling"
+];
+
+
+// Default quit message
+conf.quit_message = "KiwiIRC";
+
+
+
+
+
+/*
+ * Do not ammend the below lines unless you understand the changes!
+ */
+module.exports.production = conf;
\ No newline at end of file
diff --git a/server/config.json b/server/config.json
deleted file mode 100755 (executable)
index b03b861..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-{
-    "servers":            [
-                            {
-                                "secure":   true,
-                                "hsts": true,
-                                "port":   7777,
-                                "address": "0.0.0.0",
-
-                                "ssl_key": "server.key",
-                                "ssl_cert": "cert.pem"
-                            },
-                            {
-                                "secure":   false,
-                                "port":   7778,
-                                "address": "0.0.0.0"
-                            }
-                            
-                        ],
-
-    "user":             "",
-    "group":            "",
-
-    "quit_message":     "KiwiIRC",
-    "cap_options":      [],
-
-    "handle_http":      true,
-    "public_http":      "client_backbone/",
-
-    "max_client_conns": 2,
-
-    "module_dir":       "./kiwi_modules/",
-    "modules":          ["spamfilter", "statistics"],
-    
-    "webirc":           true,
-    "webirc_pass":      {
-                            "irc.example.com":  "examplepassword",
-                            "127.0.0.1":        "foobar"
-                        },
-
-    "transports":       [
-                            "websocket",
-                            "flashsocket",
-                            "htmlfile",
-                            "xhr-polling",
-                            "jsonp-polling"
-                        ],
-
-    "client_defaults":  {
-                            "server": "irc.anonnet.org",
-                            "port": 6667,
-                            "port_ssl": 6697,
-                            "ssl": false
-                        }
-}
index 5bbf5653730c4e8b2a706876cec5965882f595ad..ad620e844ea96d42466b68a71e68f6079f315822 100755 (executable)
@@ -8,29 +8,47 @@ var fs          = require('fs'),
  * Config loading
  */
 
-var config_filename = 'config.json',
+var config_filename = 'config.js',
     config_dirs = ['/etc/kiwiirc/', __dirname + '/'];
 
-var config = Object.create(null);
-for (var i in config_dirs) {
-    try {
-        if (fs.lstatSync(config_dirs[i] + config_filename).isDirectory() === false) {
-            config = JSON.parse(fs.readFileSync(config_dirs[i] + config_filename, 'utf-8'));
-            console.log('Loaded config file ' + config_dirs[i] + config_filename);
-            break;
+var config;
+
+function loadConfig() {
+    var new_config,
+        conf_filepath;
+
+    // Loop through the possible config paths and find a usable one
+    for (var i in config_dirs) {
+        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).production;
+                console.log('Loaded config file ' + config_dirs[i] + config_filename);
+                break;
+            }
+        } 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;
+            }
+            continue;
         }
-    } 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;
-        }
-        continue;
     }
+
+    return new_config;
 }
 
+
+config = loadConfig() || Object.create(null);
+
 // Make sure we have a valid config file and at least 1 server
 if (Object.keys(config).length === 0) {
     console.log('Couldn\'t find a valid config file!');
@@ -102,6 +120,19 @@ process.stdin.on('data', function (buffered) {
             console.log('Connected clients: ' + _.size(clients).toString());
             break;
 
+        case 'reconfig':
+            (function () {
+                var new_conf = loadConfig();
+                if (new_conf) {
+                    config = new_conf;
+                    console.log('New config file loaded');
+                } else {
+                    console.log("No new config file was loaded");
+                }
+            })();
+
+            break;
+
         default:
             console.log('Unrecognised command: ' + data);
     }