From: Darren Date: Mon, 22 Oct 2012 13:13:11 +0000 (+0100) Subject: Global config created, correct quit message. #94 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1360a4540247e507a9acf5c24042b00f919a3916;p=KiwiIRC.git Global config created, correct quit message. #94 --- diff --git a/server/client.js b/server/client.js index fb326e8..28e6f05 100755 --- a/server/client.js +++ b/server/client.js @@ -1,6 +1,7 @@ var util = require('util'), events = require('events'), _ = require('underscore'), + config = require('./configuration.js'), IrcConnection = require('./irc/connection.js').IrcConnection, IrcCommands = require('./irc/commands.js'), ClientCommands = require('./clientcommands.js'); @@ -132,7 +133,7 @@ function kiwiCommand(command, callback) { function websocketDisconnect() { _.each(this.irc_connections, function (irc_connection, i, cons) { if (irc_connection) { - irc_connection.end('QUIT :Kiwi IRC'); + irc_connection.end('QUIT :' + (config.get().quit_message || '')); cons[i] = null; } }); diff --git a/server/config.js b/server/config.js index 550e4d7..7a8ec27 100644 --- a/server/config.js +++ b/server/config.js @@ -72,7 +72,7 @@ conf.transports = [ // Default quit message -conf.quit_message = "KiwiIRC"; +conf.quit_message = "http://www.kiwiirc.com/ - A hand-crafted IRC client"; diff --git a/server/configuration.js b/server/configuration.js new file mode 100644 index 0000000..99581c0 --- /dev/null +++ b/server/configuration.js @@ -0,0 +1,60 @@ +var fs = require('fs'); + +var config_filename = 'config.js', + config_dirs = ['/etc/kiwiirc/', __dirname + '/'], + environment = 'production', + loaded_config = Object.create(null); + + +function loadConfig() { + var new_config, + conf_filepath; + + // Loop through the possible config paths and find a usable one + for (var i in config_dirs) { + conf_filepath = config_dirs[i] + config_filename; + + try { + if (fs.lstatSync(conf_filepath).isFile() === true) { + // Clear the loaded config cache + delete require.cache[require.resolve(conf_filepath)]; + + // Try load the new config file + new_config = require(conf_filepath); + console.log('Loaded config file ' + config_dirs[i] + config_filename); + break; + } + } catch (e) { + switch (e.code) { + case 'ENOENT': // No file/dir + break; + default: + console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message); + return false; + } + continue; + } + } + + if (new_config) { + loaded_config = new_config; + return loaded_config; + } else { + return false; + } +} + + + +module.exports.setEnvironment = function (new_environment) { + environment = new_environment; +}; + +// Get the current config. Optionally for a different environment than currently set +module.exports.get = function (specific_environment) { + specific_environment = specific_environment || environment; + + return loaded_config[specific_environment]; +}; + +module.exports.loadConfig = loadConfig; \ No newline at end of file diff --git a/server/kiwi.js b/server/kiwi.js index ad620e8..9cc2a17 100755 --- a/server/kiwi.js +++ b/server/kiwi.js @@ -1,61 +1,21 @@ var fs = require('fs'), _ = require('underscore'), - WebListener = require('./weblistener.js'); + WebListener = require('./weblistener.js'), + config = require('./configuration.js'); -/* - * Config loading - */ - -var config_filename = 'config.js', - config_dirs = ['/etc/kiwiirc/', __dirname + '/']; - -var config; - -function loadConfig() { - var new_config, - conf_filepath; - - // Loop through the possible config paths and find a usable one - for (var i in config_dirs) { - conf_filepath = config_dirs[i] + config_filename; - - try { - if (fs.lstatSync(conf_filepath).isFile() === true) { - // Clear the loaded config cache - delete require.cache[require.resolve(conf_filepath)]; - - // Try load the new config file - new_config = require(conf_filepath).production; - console.log('Loaded config file ' + config_dirs[i] + config_filename); - break; - } - } catch (e) { - switch (e.code) { - case 'ENOENT': // No file/dir - break; - default: - console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message); - return false; - } - continue; - } - } - - return new_config; -} -config = loadConfig() || Object.create(null); +config.loadConfig(); // Make sure we have a valid config file and at least 1 server -if (Object.keys(config).length === 0) { +if (Object.keys(config.get()).length === 0) { console.log('Couldn\'t find a valid config file!'); process.exit(1); } -if ((!config.servers) || (config.servers.length < 1)) { +if ((!config.get().servers) || (config.get().servers.length < 1)) { console.log('No servers defined in config file'); process.exit(2); } @@ -74,8 +34,8 @@ if ((!config.servers) || (config.servers.length < 1)) { var clients = []; // Start up a weblistener for each found in the config -_.each(config.servers, function (server) { - var wl = new WebListener(server, config.transports); +_.each(config.get().servers, function (server) { + var wl = new WebListener(server, config.get().transports); wl.on('connection', function (client) { clients.push(client); }); @@ -98,10 +58,10 @@ _.each(config.servers, function (server) { process.title = 'kiwiirc'; // Change UID/GID -if ((config.user) && (config.user !== '')) { +if ((config.get().user) && (config.get().user !== '')) { process.setuid(config.user); } -if ((config.group) && (config.group !== '')) { +if ((config.get().group) && (config.get().group !== '')) { process.setgid(config.group); } @@ -122,9 +82,7 @@ process.stdin.on('data', function (buffered) { case 'reconfig': (function () { - var new_conf = loadConfig(); - if (new_conf) { - config = new_conf; + if (config.loadConfig()) { console.log('New config file loaded'); } else { console.log("No new config file was loaded");