Close socket to IRC server on websocket disconnect
authorJack Allnutt <m2ys4u@Gmail.com>
Tue, 31 Jul 2012 21:17:11 +0000 (22:17 +0100)
committerJack Allnutt <m2ys4u@Gmail.com>
Tue, 31 Jul 2012 21:17:11 +0000 (22:17 +0100)
server/client.js
server/irc-commands.js
server/irc-connection.js

index 0f30cc548267d3f6072298d596c956408ba97189..dd94ec4deae34be63a90c8980fd192a6e53586f7 100755 (executable)
@@ -73,19 +73,20 @@ var IRC_command = function (command, callback) {
         str += command.data.args.target + ' :'
         str += String.fromCharCode(1) + command.data.args.type + ' ';
         str += command.data.args.params + String.fromCharCode(1);
-        this.IRC_connections[command.server].send(str);
+        this.IRC_connections[command.server].write(str);
     } else if (command.data.method === 'raw') {
-        this.IRC_connections[command.server].send(command.data.args.data);
+        this.IRC_connections[command.server].write(command.data.args.data);
     } else if (command.data.method === 'kiwi') {
         // do special Kiwi stuff here
     } else {
         method = command.data.method;
         command.data = command.data.args;
-        this.IRC_connections[command.server].send(method + ((command.data.params) ? ' ' + command.data.params.join(' ') : '') + ((command.data.trailing) ? ' :' + command.data.trailing : ''), callback);
+        this.IRC_connections[command.server].write(method + ((command.data.params) ? ' ' + command.data.params.join(' ') : '') + ((command.data.trailing) ? ' :' + command.data.trailing : ''), callback);
     }
 };
 
 var kiwi_command = function (command, callback) {
+    var that = this;
     console.log(typeof callback);
     if (typeof callback !== 'function') {
         callback = function () {};
@@ -111,6 +112,10 @@ var kiwi_command = function (command, callback) {
                                con.on('error', function (err) {
                                        this.websocket.sendKiwiCommand('error', {server: con_num, error: err});
                                });
+                
+                con.on('close', function () {
+                    that.IRC_connections[con_num] = null;
+                });
                        } else {
                                return callback('Hostname, port and nickname must be specified');
                        }
@@ -127,6 +132,12 @@ var extension_command = function (command, callback) {
 };
 
 var disconnect = function () {
+    _.each(this.IRC_connections, function (irc_connection, i, cons) {
+        if (irc_connection) {
+            irc_connection.end('QUIT :Kiwi IRC');
+            cons[i] = null;
+        }
+    });
     this.emit('destroy');
 };
 
index 23667c045bdb3d694856d9c8b054b9805331bda7..36402c958a6acf2f4f325a7432c80501cc5ffc05 100755 (executable)
@@ -88,7 +88,7 @@ var listeners = {
                                                } else if (option[0] === 'CHANMODES') {
                                                        this.irc_connection.options.CHANMODES = option[1].split(',');
                         } else if (option[0] === 'NAMESX') {
-                            this.irc_connection.send('PROTOCTL NAMESX');
+                            this.irc_connection.write('PROTOCTL NAMESX');
                         }
                     }
                 }
@@ -268,7 +268,7 @@ var listeners = {
                 this.client.sendIRCCommand('topicsetby', {server: this.con_num, nick: command.params[2], channel: command.params[1], when: command.params[3]});
             },
     'PING':                 function (command) {
-                this.irc_connection.send('PONG ' + command.trailing);
+                this.irc_connection.write('PONG ' + command.trailing);
             },
     'JOIN':                 function (command) {
                                var channel;
@@ -285,7 +285,7 @@ var listeners = {
                 this.client.sendIRCCommand('join', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, channel: channel});
                 
                 if (command.nick === this.nick) {
-                    this.irc_connection.send('NAMES ' + channel);
+                    this.irc_connection.write('NAMES ' + channel);
                 }
             },
     'PART':                 function (command) {
@@ -372,7 +372,7 @@ var listeners = {
                         namespace = tmp.split(' ', 1)[0];
                         this.client.sendIRCCommand('kiwi', {server: this.con_num, namespace: namespace, data: tmp.substr(namespace.length + 1)});
                     } else if (msg.trailing.substr(1, 7) === 'VERSION') {
-                        this.irc_connection.send('NOTICE ' + command.nick + ' :' + String.fromCharCode(1) + 'VERSION KiwiIRC' + String.fromCharCode(1));
+                        this.irc_connection.write('NOTICE ' + command.nick + ' :' + String.fromCharCode(1) + 'VERSION KiwiIRC' + String.fromCharCode(1));
                     } else {
                         this.client.sendIRCCommand('ctcp_request', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], msg: command.trailing.substr(1, command.trailing.length - 2)});
                     }
index 163d69e83ca4b33e9a7adf52d4ed90e47e67830a..2f3b457fcb45db8fa5dcfb93282230b3e626f177 100755 (executable)
@@ -28,6 +28,10 @@ var IRCConnection = function (hostname, port, ssl, nick, user, pass, webirc) {
         parse.apply(that, arguments);
     });
     
+    this.socket.on('close', function () {
+        that.emit('close');
+    });
+    
     this.connected = false;
     this.registered = false;
     this.nick = nick;
@@ -42,27 +46,37 @@ var IRCConnection = function (hostname, port, ssl, nick, user, pass, webirc) {
 };
 util.inherits(IRCConnection, events.EventEmitter);
 
-IRCConnection.prototype.send = function (data, callback) {
+IRCConnection.prototype.write = function (data, callback) {
     console.log('S<--', data);
     write.call(this, data + '\r\n', 'utf-8', callback);
 };
 
+IRCConnection.prototype.end = function (data, callback) {
+    console.log('S<--', data);
+    console.log('Closing docket');
+    end.call(this, data + '\r\n', 'utf-8', callback);
+}
+
 var write = function (data, encoding, callback) {
     this.socket.write(data, encoding, callback);
 };
 
+var end = function (data, encoding, callback) {
+    this.socket.end(data, encoding, callback);
+};
+
 module.exports.IRCConnection = IRCConnection;
 
 var connect_handler = function () {
     if (this.webirc) {
-        this.send('WEBIRC ' + webirc.pass + ' KiwiIRC ' + this.user.hostname + ' ' + this.user.address);
+        this.write('WEBIRC ' + webirc.pass + ' KiwiIRC ' + this.user.hostname + ' ' + this.user.address);
     }
     if (this.password) {
-        this.send('PASS ' + password);
+        this.write('PASS ' + password);
     }
-    //this.send('CAP LS');
-    this.send('NICK ' + this.nick);
-    this.send('USER kiwi_' + this.nick.replace(/[^0-9a-zA-Z\-_.]/, '') + ' 0 0 :' + this.nick);
+    //this.write('CAP LS');
+    this.write('NICK ' + this.nick);
+    this.write('USER kiwi_' + this.nick.replace(/[^0-9a-zA-Z\-_.]/, '') + ' 0 0 :' + this.nick);
     
     this.connected = true;
     console.log("IRCConnection.emit('connected')");