Skip to content
Browse files

Added support for ssl connections using the new tsl api.

  • Loading branch information...
1 parent fce73bd commit 9e0b69d4bb613304210daf72e47a980f7494b1b1 @indiefan indiefan committed Apr 5, 2011
Showing with 94 additions and 20 deletions.
  1. +63 −0 example/secure.js
  2. +31 −20 lib/irc.js
View
63 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);
+});
View
51 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 () {

0 comments on commit 9e0b69d

Please sign in to comment.
Something went wrong with that request. Please try again.