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

Added UNIX domain socket support #511

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions docs/API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Client
{
userName: 'nodebot',
realName: 'nodeJS IRC client',
socket: false,
port: 6667,
localAddress: null,
debug: false,
Expand All @@ -44,6 +45,8 @@ Client
If you set `selfSigned` to true SSL accepts certificates from a non trusted CA.
If you set `certExpired` to true, the bot connects even if the ssl cert has expired.

Set `socket` to true to interpret `server` as a path to a UNIX domain socket.

`localAddress` is the address to bind to when connecting.

`floodProtection` queues all your messages and slowly unpacks it to make sure
Expand Down
14 changes: 10 additions & 4 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function Client(server, nick, opt) {
password: null,
userName: 'nodebot',
realName: 'nodeJS IRC client',
socket: false,
port: 6667,
localAddress: null,
debug: false,
Expand Down Expand Up @@ -396,6 +397,8 @@ function Client(server, nick, opt) {
channel = self.chanData(message.args[1]);
if (channel) {
channel.topic = message.args[2];
// channel, topic, nick
self.emit('topic', message.args[1], channel.topic, null, message)
}
break;
case 'rpl_away':
Expand Down Expand Up @@ -745,10 +748,13 @@ Client.prototype.connect = function(retryCount, callback) {
self.chans = {};

// socket opts
var connectionOpts = {
host: self.opt.server,
port: self.opt.port
};
var connectionOpts = {};
if (self.opt.socket === true) {
connectionOpts.path = self.opt.server;
} else {
connectionOpts.host = self.opt.server;
connectionOpts.port = self.opt.port;
}

// local address to bind to
if (self.opt.localAddress)
Expand Down
12 changes: 12 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var path = require('path');
var fs = require('fs');
var net = require('net');
var os = require('os');
var tls = require('tls');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
Expand Down Expand Up @@ -59,6 +60,17 @@ MockIrcd.prototype.getIncomingMsgs = function() {
return this.incoming;
};

module.exports.getTempSocket = function() {
var tempDir = os.tmpdir();
var sockPath = path.join(tempDir, 'mock_ircd.sock');
try {
fs.unlinkSync(sockPath);
} catch (e) {
// ignore
}
return sockPath;
}

var fixtures = require('./data/fixtures');
module.exports.getFixtures = function(testSuite) {
return fixtures[testSuite];
Expand Down
34 changes: 34 additions & 0 deletions test/test-socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var irc = require('../lib/irc');
var test = require('tape');

var testHelpers = require('./helpers');

test('connect and quit socket with message', function(t) {
var tempSock = testHelpers.getTempSocket();
var mock = testHelpers.MockIrcd(tempSock);
var client = new irc.Client(tempSock, 'testbot', {socket: true, debug: true});

expected = testHelpers.getFixtures('quit');

t.plan(expected.sent.length + expected.received.length + 1);

mock.server.on('connection', function() {
mock.send(':localhost 001 testbot :Welcome to the Internet Relay Chat Network testbot\r\n');
});

client.on('registered', function() {
t.equal(mock.outgoing[0], expected.received[0][0], expected.received[0][1]);
client.disconnect('quitting as a test', function() {});
});

mock.on('end', function() {
var msgs = mock.getIncomingMsgs();

t.equal(msgs.length, expected.sent.length, 'Server received the correct amount of messages.')

for (var i = 0; i < msgs.length; i++) {
t.equal(msgs[i], expected.sent[i][0], expected.sent[i][1]);
}
mock.close();
});
});
Loading