ChatBuffer-KeyUp: Suppress browser's default behavior; leave cursor at the end of...
[KiwiIRC.git] / server / kiwi.js
index 36c2c5bd1de5cc5cad4d453990d6f075055519b9..6e697f75a7b306c780af3cc6f3bd003abb455568 100755 (executable)
@@ -3,7 +3,8 @@ var fs          = require('fs'),
     WebListener = require('./weblistener.js'),
     config      = require('./configuration.js'),
     rehash      = require('./rehash.js'),
-    modules     = require('./modules.js');
+    modules     = require('./modules.js'),
+    Identd      = require('./identd.js');
 
 
 
@@ -13,7 +14,7 @@ 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 && global.config.log) {
+if (process.argv.indexOf('-f') === -1 && global.config && global.config.log) {
     (function () {
         var log_file_name = global.config.log;
 
@@ -108,6 +109,49 @@ global.clients = {
     }
 };
 
+global.servers = {
+    servers: Object.create(null),
+    
+    addConnection: function (connection) {
+        var host = connection.irc_host.hostname;
+        if (!this.servers[host]) {
+            this.servers[host] = [];
+        }
+        this.servers[host].push(connection);
+    },
+    
+    removeConnection: function (connection) {
+        var host = connection.irc_host.hostname
+        if (this.servers[host]) {
+            this.servers[host] = _.without(this.servers[host], connection);
+            if (this.servers[host].length === 0) {
+                delete this.servers[host];
+            }
+        }
+    },
+    
+    numOnHost: function (host) {
+        if (this.servers[host]) {
+            return this.servers[host].length;
+        } else {
+            return 0;
+        }
+    }
+};
+
+
+
+
+/*
+ * Identd server
+ */
+if (global.config.identd && global.config.identd.enabled) {
+    new Identd({
+        bind_addr: global.config.identd.address,
+        bind_port: global.config.identd.port
+    }).start();
+}
+
 
 
 
@@ -124,11 +168,20 @@ _.each(global.config.servers, function (server) {
         clients.add(client);
     });
 
-    wl.on('destroy', function (client) {
+    wl.on('client_dispose', function (client) {
         clients.remove(client);
     });
 
-    wl.on('listening', webListenerRunning);
+    wl.on('listening', function () {
+        console.log('Listening on %s:%s %s SSL', server.address, server.port, (server.ssl ? 'with' : 'without'));
+        webListenerRunning();
+    });
+
+    wl.on('error', function (err) {
+        console.log('Error listening on %s:%s: %s', server.address, server.port, err.code);
+        // TODO: This should probably be refactored. ^JA
+        webListenerRunning();
+    });
 });
 
 // Once all the listeners are listening, set the processes UID/GID
@@ -164,6 +217,7 @@ function setProcessUid() {
 // Make sure Kiwi doesn't simply quit on an exception
 process.on('uncaughtException', function (e) {
     console.log('[Uncaught exception] ' + e);
+    console.log(e.stack);
 });