X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=server%2Fkiwi.js;h=6e697f75a7b306c780af3cc6f3bd003abb455568;hb=e35f86f072142d04d00ee9e834f2ce2a3c897478;hp=39edb397d81d4d9f3b9b1d39cf938ec6a05e1390;hpb=2427e69ee0551a9445f8042a2e7cfdb8b8b06b8e;p=KiwiIRC.git diff --git a/server/kiwi.js b/server/kiwi.js index 39edb39..6e697f7 100755 --- a/server/kiwi.js +++ b/server/kiwi.js @@ -1,8 +1,10 @@ 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'), + Identd = require('./identd.js'); @@ -12,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; @@ -42,8 +44,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 +57,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 = { @@ -88,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(); +} + @@ -104,11 +168,30 @@ _.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', 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 +var num_listening = 0; +function webListenerRunning() { + num_listening++; + if (num_listening === global.config.servers.length) { + setProcessUid(); + } +} @@ -121,17 +204,20 @@ _.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); });