From: Darren Date: Fri, 30 Nov 2012 16:19:42 +0000 (+0000) Subject: Server modules location in config. Control module included X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d8002ae04c5f02b2df1f9cf17737d3e8e3e854c7;p=KiwiIRC.git Server modules location in config. Control module included --- diff --git a/config.example.js b/config.example.js index e17c8f3..08d4877 100644 --- a/config.example.js +++ b/config.example.js @@ -63,7 +63,7 @@ conf.cap_options = []; // Directory to find the server modules -conf.module_dir = "./kiwi_modules/"; +conf.module_dir = "../server_modules/"; // Which modules to load conf.modules = []; diff --git a/server/kiwi.js b/server/kiwi.js index 1c7520d..36c2c5b 100755 --- a/server/kiwi.js +++ b/server/kiwi.js @@ -63,14 +63,15 @@ global.modules = new modules.Publisher(); modules.registerPublisher(global.modules); // Load any modules in the config -(global.config.modules || []).forEach(function (module_name) { - if (modules.load('../server_modules/' + module_name + '.js')) { - console.log('Module ' + module_name + ' loaded successfuly'); - } else { - console.log('Module ' + module_name + ' failed to load'); - } -}); - +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'); + } + }); +} diff --git a/server_modules/control.js b/server_modules/control.js new file mode 100644 index 0000000..ac1a3e7 --- /dev/null +++ b/server_modules/control.js @@ -0,0 +1,106 @@ +/** + * Server control via TCP socket + * + * Listens on localhost:8888 by default + */ + +var net = require('net'), + kiwiModules = require('../server/modules'), + rehash = require('../server/rehash.js'), + config = require('../server/configuration.js'), + _ = require('lodash'); + +var module = new kiwiModules.Module('Control'); + + +function SocketClient (socket) { + this.socket = socket; + + this.remoteAddress = this.socket.remoteAddress; + console.log('Control connection from ' + this.socket.remoteAddress + ' opened'); + + this.bindEvents(); + + socket.write("\nHello, you are connected to the Kiwi server :)\n\n"); + this.displayPrompt(); +} + +SocketClient.prototype.bindEvents = function() { + var that = this; + + this.socket.on('data', function() { that.onData.apply(that, arguments); }); + this.socket.on('close', function() { that.onClose.apply(that, arguments); }); +}; +SocketClient.prototype.unbindEvents = function() { + this.socket.removeAllListeners(); +}; + + + +SocketClient.prototype.write = function(data, append) { + if (typeof append === 'undefined') append = '\n'; + this.socket.write(data + append); +}; +SocketClient.prototype.displayPrompt = function(prompt) { + prompt = prompt || 'Kiwi > '; + this.write(prompt, ''); +}; + + + +SocketClient.prototype.onData = function(data) { + data = data.toString().trim(); + + try { + switch (data) { + case 'modules': + this.write('Loaded modules: ' + Object.keys(kiwiModules.getRegisteredModules()).join(', ')); + break; + + case 'stats': + this.write('Connected clients: ' + _.size(global.clients.clients).toString()); + this.write('Num. remote hosts: ' + _.size(global.clients.addresses).toString()); + break; + + case 'rehash': + rehash.rehashAll(); + this.write('Rehashed'); + break; + + case 'reconfig': + if (config.loadConfig()) { + this.write('New config file loaded'); + } else { + this.write("No new config file was loaded"); + } + break; + + case 'exit': + case 'quit': + this.socket.destroy(); + break; + + default: + this.write('Unrecognised command: ' + data); + } + } catch (err) { + console.log('[Control error] ' + err); + this.write('An error occured. Check the Kiwi server log for more details'); + } + + this.displayPrompt(); +}; + + +SocketClient.prototype.onClose = function() { + this.unbindEvents(); + console.log('Control connection from ' + this.remoteAddress + ' closed'); +}; + + + + +var server = net.createServer(function (socket) { + new SocketClient(socket); +}); +server.listen(8888); \ No newline at end of file