From: Jack Allnutt Date: Tue, 31 Jul 2012 21:17:11 +0000 (+0100) Subject: Close socket to IRC server on websocket disconnect X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b8e4d9f7d1b3945998a53feca3e0972f026fec26;p=KiwiIRC.git Close socket to IRC server on websocket disconnect --- diff --git a/server/client.js b/server/client.js index 0f30cc5..dd94ec4 100755 --- a/server/client.js +++ b/server/client.js @@ -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'); }; diff --git a/server/irc-commands.js b/server/irc-commands.js index 23667c0..36402c9 100755 --- a/server/irc-commands.js +++ b/server/irc-commands.js @@ -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)}); } diff --git a/server/irc-connection.js b/server/irc-connection.js index 163d69e..2f3b457 100755 --- a/server/irc-connection.js +++ b/server/irc-connection.js @@ -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')");