Merge branch 'development'
[KiwiIRC.git] / server / kiwi.js
index e2f87581fa4e63021ac1edfe4480aa2d98f3e3a8..8151db4aa9459a8c547a8ecc8e41735a80b189ed 100755 (executable)
@@ -1,5 +1,5 @@
 var fs          = require('fs'),
-    _           = require('underscore'),
+    _           = require('lodash'),
     WebListener = require('./weblistener.js'),
     config      = require('./configuration.js'),
     rehash      = require('./rehash.js');
@@ -12,9 +12,9 @@ config.loadConfig();
 
 // If we're not running in the forground and we have a log file.. switch
 // console.log to output to a file
-if (process.argv.indexOf('-f') === -1 && config.get().log) {
+if (process.argv.indexOf('-f') === -1 && global.config.log) {
     (function () {
-        var log_file_name = config.get().log;
+        var log_file_name = global.config.log;
 
         if (log_file_name[0] !== '/') {
             log_file_name = __dirname + '/../' + log_file_name;
@@ -42,12 +42,12 @@ if (process.argv.indexOf('-f') === -1 && config.get().log) {
 
 
 // Make sure we have a valid config file and at least 1 server
-if (Object.keys(config.get()).length === 0) {
-    console.log('Couldn\'t find a valid config file!');
+if (!global.config || Object.keys(global.config).length === 0) {
+    console.log('Couldn\'t find a valid config.js file (Did you copy the config.example.js file yet?)');
     process.exit(1);
 }
 
-if ((!config.get().servers) || (config.get().servers.length < 1)) {
+if ((!global.config.servers) || (global.config.servers.length < 1)) {
     console.log('No servers defined in config file');
     process.exit(2);
 }
@@ -97,8 +97,8 @@ global.clients = {
 
 
 // Start up a weblistener for each found in the config
-_.each(config.get().servers, function (server) {
-    var wl = new WebListener(server, config.get().transports);
+_.each(global.config.servers, function (server) {
+    var wl = new WebListener(server, global.config.transports);
 
     wl.on('connection', function (client) {
         clients.add(client);
@@ -107,8 +107,18 @@ _.each(config.get().servers, function (server) {
     wl.on('destroy', function (client) {
         clients.remove(client);
     });
+
+    wl.on('listening', webListenerRunning);
 });
 
+// Once all the listeners are listening, set the processes UID/GID
+var num_listening = 0;
+function webListenerRunning() {
+    num_listening++;
+    if (num_listening === global.config.servers.length) {
+        setProcessUid();
+    }
+}
 
 
 
@@ -121,14 +131,32 @@ _.each(config.get().servers, function (server) {
 process.title = 'kiwiirc';
 
 // Change UID/GID
-if ((config.get().group) && (config.get().group !== '')) {
-    process.setgid(config.get().group);
-}
-if ((config.get().user) && (config.get().user !== '')) {
-    process.setuid(config.get().user);
+function setProcessUid() {
+    if ((global.config.group) && (global.config.group !== '')) {
+        process.setgid(global.config.group);
+    }
+    if ((global.config.user) && (global.config.user !== '')) {
+        process.setuid(global.config.user);
+    }
 }
 
 
+// Make sure Kiwi doesn't simply quit on an exception
+process.on('uncaughtException', function (e) {
+    console.log('[Uncaught exception] ' + e);
+});
+
+
+process.on('SIGUSR1', function() {
+    if (config.loadConfig()) {
+        console.log('New config file loaded');
+    } else {
+        console.log("No new config file was loaded");
+    }
+});
+
+
+
 
 /*
  * Listen for runtime commands
@@ -145,13 +173,11 @@ process.stdin.on('data', function (buffered) {
             break;
 
         case 'reconfig':
-            (function () {
-                if (config.loadConfig()) {
-                    console.log('New config file loaded');
-                } else {
-                    console.log("No new config file was loaded");
-                }
-            })();
+            if (config.loadConfig()) {
+                console.log('New config file loaded');
+            } else {
+                console.log("No new config file was loaded");
+            }
 
             break;