Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated ssl support to work with tsl #8

Merged
merged 1 commit into from
Apr 29, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions example/secure.js
Original file line number Diff line number Diff line change
@@ -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);
});
51 changes: 31 additions & 20 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports.Client = Client;

var sys = require('sys');
var net = require('net');
var tls = require('tls');

const replyFor = { // {{{
"200" : {
Expand Down Expand Up @@ -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 () {
Expand Down