From 9e0b69d4bb613304210daf72e47a980f7494b1b1 Mon Sep 17 00:00:00 2001 From: indiefan Date: Tue, 5 Apr 2011 15:28:40 -0700 Subject: [PATCH] Added support for ssl connections using the new tsl api. --- example/secure.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++ lib/irc.js | 51 +++++++++++++++++++++++--------------- 2 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 example/secure.js diff --git a/example/secure.js b/example/secure.js new file mode 100644 index 00000000..80b7bce4 --- /dev/null +++ b/example/secure.js @@ -0,0 +1,63 @@ +// Make sure the irc lib is available +require.paths.unshift(__dirname + '/../lib'); + +var sys = require('sys'); +var irc = require(__dirname + '/../lib/irc'); +/* +* To set the key/cert explicitly, you could do the following +var fs = require('fs'); + +var options = { + key: fs.readFileSync('privkey.pem'), + cert: fs.readFileSync('certificate.crt') +}; +*/ + +// Or to just use defaults +var options = true; + +var bot = new irc.Client('irc.dollyfish.net.nz', 'nodebot', { + port: 7000, + secure: options, + channels: ['#blah', '#test'], +}); + +bot.addListener('error', function(message) { + sys.puts('ERROR: ' + message.command + ': ' + message.args.join(' ')); +}); + +bot.addListener('message#blah', function (from, message) { + sys.puts('<' + from + '> ' + message); +}); + +bot.addListener('message', function (from, to, message) { + sys.puts(from + ' => ' + to + ': ' + message); + + if ( to.match(/^[#&]/) ) { + // channel message + if ( message.match(/hello/i) ) { + bot.say(to, 'Hello there ' + from); + } + if ( message.match(/dance/) ) { + setTimeout(function () { bot.say(to, "\u0001ACTION dances: :D\\-<\u0001") }, 1000); + setTimeout(function () { bot.say(to, "\u0001ACTION dances: :D|-<\u0001") }, 2000); + setTimeout(function () { bot.say(to, "\u0001ACTION dances: :D/-<\u0001") }, 3000); + setTimeout(function () { bot.say(to, "\u0001ACTION dances: :D|-<\u0001") }, 4000); + } + } + else { + // private message + } +}); +bot.addListener('pm', function(nick, message) { + sys.puts('Got private message from ' + nick + ': ' + message); +}); +bot.addListener('join', function(channel, who) { + sys.puts(who + ' has joined ' + channel); +}); +bot.addListener('part', function(channel, who, reason) { + sys.puts(who + ' has left ' + channel + ': ' + reason); +}); +bot.addListener('kick', function(channel, who, by, reason) { + sys.puts(who + ' was kicked from ' + channel + ' by ' + by + ': ' + reason); +}); diff --git a/lib/irc.js b/lib/irc.js index 5e0c47f8..7fd9ee68 100644 --- a/lib/irc.js +++ b/lib/irc.js @@ -21,6 +21,7 @@ exports.Client = Client; var sys = require('sys'); var net = require('net'); +var tls = require('tls'); const replyFor = { // {{{ "200" : { @@ -788,27 +789,37 @@ Client.prototype.connect = function ( retryCount ) { // {{{ retryCount = retryCount || 0; var self = this; self.chans = {}; - self.conn = net.createConnection(self.opt.port, self.opt.server); - self.conn.requestedDisconnect = false; + // try to connect to the server if (self.opt.secure) { - // Set up a secure (SSL) connection - if (typeof self.opt.secure !== 'object') { - // Do not verify connection - self.opt.secure = require('crypto').createCredentials({}); - } else { - // Verify connection - self.conn.addListener("secure", function(){ - if(!self.conn.verifyPeer()) { - if (self.opt.debug) { - sys.log('Warning: failed to verify SSL peer certificate'+ - ' -- aborting connection'); - } - this.end(); - } - }); - } - self.conn.setSecure(self.opt.secure); - } + var creds = self.opt.secure; + if (typeof self.opt.secure !== 'object') { + creds = {}; + } + + self.conn = tls.connect(self.opt.port, self.opt.server, creds, function() { + // callback called only after successful socket connection + self.conn.connected = true; + if (self.conn.authorized) { + // authorization successful + self.conn.setEncoding('utf-8'); + + if ( self.opt.password !== null ) { + self.send( "PASS", self.opt.password ); + } + console.log('Sending irc NICK/USER'); + self.send("NICK", self.opt.nick); + self.nick = self.opt.nick; + self.send("USER", self.opt.userName, 8, "*", self.opt.realName); + self.emit("connect"); + } else { + // authorization failed + console.log(self.conn.authorizationError); + } + }); + }else { + self.conn = net.createConnection(self.opt.port, self.opt.server); + } + self.conn.requestedDisconnect = false; self.conn.setTimeout(0); self.conn.setEncoding('utf8'); self.conn.addListener("connect", function () {