CTCP TIME fix
[KiwiIRC.git] / server / kiwi.js
index 30031a9e01577b134f797ef7636a6723764d0cb9..1361071e396b50a6b80e04a05deb35d15a3ba997 100755 (executable)
@@ -1,8 +1,9 @@
 var fs          = require('fs'),
-    _           = require('underscore'),
+    _           = require('lodash'),
     WebListener = require('./weblistener.js'),
     config      = require('./configuration.js'),
-    rehash      = require('./rehash.js');
+    rehash      = require('./rehash.js'),
+    modules     = require('./modules.js');
 
 
 
@@ -42,8 +43,8 @@ if (process.argv.indexOf('-f') === -1 && global.config.log) {
 
 
 // Make sure we have a valid config file and at least 1 server
-if (Object.keys(global.config).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);
 }
 
@@ -55,6 +56,25 @@ if ((!global.config.servers) || (global.config.servers.length < 1)) {
 
 
 
+// Create a plugin interface
+global.modules = new modules.Publisher();
+
+// Register as the active interface
+modules.registerPublisher(global.modules);
+
+// Load any modules in the config
+if (global.config.module_dir) {
+    (global.config.modules || []).forEach(function (module_name) {
+        if (modules.load(global.config.module_dir + module_name + '.js')) {
+            console.log('Module ' + module_name + ' loaded successfuly');
+        } else {
+            console.log('Module ' + module_name + ' failed to load');
+        }
+    });
+}
+
+
+
 
 // Holder for all the connected clients
 global.clients = {
@@ -107,8 +127,18 @@ _.each(global.config.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 +151,23 @@ _.each(global.config.servers, function (server) {
 process.title = 'kiwiirc';
 
 // Change UID/GID
-if ((global.config.group) && (global.config.group !== '')) {
-    process.setgid(global.config.group);
-}
-if ((global.config.user) && (global.config.user !== '')) {
-    process.setuid(global.config.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);
+    console.log(e.stack);
+});
+
+
 process.on('SIGUSR1', function() {
     if (config.loadConfig()) {
         console.log('New config file loaded');