+process.chdir(__dirname + '/../');
+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) {
+ (function () {
+ var log_file_name = global.config.log;
+
+ if (log_file_name[0] !== '/') {
+ log_file_name = __dirname + '/../' + log_file_name;
+ }
+
+
+
+ console.log = function() {
+ var logfile = fs.openSync(log_file_name, 'a'),
+ out;
+
+ out = Array.prototype.join.apply(arguments, [' ']);
+
+ // Make sure we out somthing to log and we have an open file
+ if (!out || !logfile) return;
+
+ out += '\n';
+ fs.writeSync(logfile, out, null);
+
+ fs.closeSync(logfile);
+ };
+ })();
+}
-config.loadConfig();
// Make sure we have a valid config file and at least 1 server
-if (Object.keys(config.get()).length === 0) {
+if (Object.keys(global.config).length === 0) {
console.log('Couldn\'t find a valid config file!');
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);
}
+// Holder for all the connected clients
+global.clients = {
+ clients: Object.create(null),
+ addresses: Object.create(null),
+
+ add: function (client) {
+ this.clients[client.hash] = client;
+ if (typeof this.addresses[client.real_address] === 'undefined') {
+ this.addresses[client.real_address] = Object.create(null);
+ }
+ this.addresses[client.real_address][client.hash] = client;
+ },
+
+ remove: function (client) {
+ if (typeof this.clients[client.hash] !== 'undefined') {
+ delete this.clients[client.hash];
+ delete this.addresses[client.real_address][client.hash];
+ if (Object.keys(this.addresses[client.real_address]).length < 1) {
+ delete this.addresses[client.real_address];
+ }
+ }
+ },
+
+ numOnAddress: function (addr) {
+ if (typeof this.addresses[addr] !== 'undefined') {
+ return Object.keys(this.addresses[addr]).length;
+ } else {
+ return 0;
+ }
+ }
+};
+
+
+
+
/*
* Web listeners
*/
-// Holder for all the connected clients
-// TODO: Change from an array to an object. Generate sha1 hash within the client
-// and use that as the key. (Much less work involved in removing a client)
-var 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.push(client);
+ clients.add(client);
});
- wl.on('destroy', function (client) {
- clients = _.reject(clients, function (c) {
- if (client === c) {
- c.dispose();
- return true;
- }
- return false;
- });
+ wl.on('destroy', function (client) {
+ clients.remove(client);
});
});
process.title = 'kiwiirc';
// Change UID/GID
-if ((config.get().group) && (config.get().group !== '')) {
- process.setgid(config.get().group);
+if ((global.config.group) && (global.config.group !== '')) {
+ process.setgid(global.config.group);
}
-if ((config.get().user) && (config.get().user !== '')) {
- process.setuid(config.get().user);
+if ((global.config.user) && (global.config.user !== '')) {
+ process.setuid(global.config.user);
}
+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
switch (data) {
case 'stats':
- console.log('Connected clients: ' + _.size(clients).toString());
+ console.log('Connected clients: ' + _.size(global.clients.clients).toString());
+ console.log('Num. remote hosts: ' + _.size(global.clients.addresses).toString());
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;