Removing console.log()s
[KiwiIRC.git] / server_modules / control.js
index ac1a3e79b469aeb83eb0cb02d29b633bf911dd1f..8619be55afb52678c7b93271a2cdb72a7c9f5796 100644 (file)
@@ -4,17 +4,22 @@
  * Listens on localhost:8888 by default\r
  */\r
 \r
-var net         = require('net'),\r
-    kiwiModules = require('../server/modules'),\r
-    rehash      = require('../server/rehash.js'),\r
-    config      = require('../server/configuration.js'),\r
-    _           = require('lodash');\r
+var net                = require('net'),\r
+    kiwiModules        = require('../server/modules'),\r
+    ControlInterface   = require('../server/controlinterface.js'),\r
+    _                  = require('lodash');\r
 \r
-var module = new kiwiModules.Module('Control');\r
+var control_module = new kiwiModules.Module('Control');\r
 \r
 \r
+/**\r
+ * The socket client\r
+ */\r
 function SocketClient (socket) {\r
+    var that = this;\r
+\r
     this.socket = socket;\r
+    this.socket_closing = false;\r
 \r
     this.remoteAddress = this.socket.remoteAddress;\r
     console.log('Control connection from ' + this.socket.remoteAddress + ' opened');\r
@@ -22,85 +27,61 @@ function SocketClient (socket) {
     this.bindEvents();\r
 \r
     socket.write("\nHello, you are connected to the Kiwi server :)\n\n");\r
-    this.displayPrompt();\r
+\r
+    this.control_interface = new ControlInterface(socket);\r
+    _.each(socket_commands, function(fn, command_name) {\r
+        that.control_interface.addCommand(command_name, fn.bind(that));\r
+    });\r
 }\r
 \r
 SocketClient.prototype.bindEvents = function() {\r
     var that = this;\r
 \r
-    this.socket.on('data', function() { that.onData.apply(that, arguments); });\r
     this.socket.on('close', function() { that.onClose.apply(that, arguments); });\r
 };\r
+\r
+\r
 SocketClient.prototype.unbindEvents = function() {\r
     this.socket.removeAllListeners();\r
 };\r
 \r
 \r
+SocketClient.prototype.onClose = function() {\r
+    this.control_interface.dispose();\r
+    this.control_interface = null;\r
 \r
-SocketClient.prototype.write = function(data, append) {\r
-    if (typeof append === 'undefined') append = '\n';\r
-    this.socket.write(data + append);\r
-};\r
-SocketClient.prototype.displayPrompt = function(prompt) {\r
-    prompt = prompt || 'Kiwi > ';\r
-    this.write(prompt, '');\r
+    this.unbindEvents();\r
+    this.socket = null;\r
+\r
+    console.log('Control connection from ' + this.remoteAddress + ' closed');\r
 };\r
 \r
 \r
 \r
-SocketClient.prototype.onData = function(data) {\r
-    data = data.toString().trim();\r
-\r
-    try {\r
-        switch (data) {\r
-            case 'modules':\r
-                this.write('Loaded modules: ' + Object.keys(kiwiModules.getRegisteredModules()).join(', '));\r
-                break;\r
-\r
-            case 'stats':\r
-                this.write('Connected clients: ' + _.size(global.clients.clients).toString());\r
-                this.write('Num. remote hosts: ' + _.size(global.clients.addresses).toString());\r
-                break;\r
-\r
-            case 'rehash':\r
-                rehash.rehashAll();\r
-                this.write('Rehashed');\r
-                break;\r
-\r
-            case 'reconfig':\r
-                if (config.loadConfig()) {\r
-                    this.write('New config file loaded');\r
-                } else {\r
-                    this.write("No new config file was loaded");\r
-                }\r
-                break;\r
-\r
-            case 'exit':\r
-            case 'quit':\r
-                this.socket.destroy();\r
-                break;\r
-\r
-            default:\r
-                this.write('Unrecognised command: ' + data);\r
-        }\r
-    } catch (err) {\r
-        console.log('[Control error] ' + err);\r
-        this.write('An error occured. Check the Kiwi server log for more details');\r
+/**\r
+ * Available commands\r
+ * Each function is run in context of the SocketClient\r
+ */\r
+var socket_commands = {\r
+    quit: function(data) {\r
+        this.socket.destroy();\r
+        this.socket_closing = true;\r
+    },\r
+    exit: function(data) {\r
+        this.socket.destroy();\r
+        this.socket_closing = true;\r
     }\r
-\r
-    this.displayPrompt();\r
-};\r
-\r
-\r
-SocketClient.prototype.onClose = function() {\r
-    this.unbindEvents();\r
-    console.log('Control connection from ' + this.remoteAddress + ' closed');\r
 };\r
 \r
 \r
-\r
-\r
+/**\r
+ * Start the control socket server to serve connections\r
+ */\r
 var server = net.createServer(function (socket) {\r
     new SocketClient(socket);\r
 });\r
-server.listen(8888);
\ No newline at end of file
+server.listen(8888);\r
+\r
+control_module.on('dispose', function() {\r
+    server.close();\r
+});\r