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

fix(parseMessage): Factor parseMessage to another file for decoupling. #293

Merged
1 commit merged into from
Jan 11, 2015
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
68 changes: 1 addition & 67 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ var util = require('util');
var EventEmitter = require('events').EventEmitter;

var colors = require('./colors');
var parseMessage = require('./parse_message');
exports.colors = colors;

var replyFor = require('./codes');

var lineDelimiter = new RegExp('\r\n|\r|\n')

function Client(server, nick, opt) {
Expand Down Expand Up @@ -1026,68 +1025,3 @@ Client.prototype._updateMaxLineLength = function() {
// target is determined in _speak() and subtracted there
this.maxLineLength = 497 - this.nick.length - this.hostMask.length;
};
/*
* parseMessage(line, stripColors)
*
* takes a raw "line" from the IRC server and turns it into an object with
* useful keys
*/
function parseMessage(line, stripColors) {
var message = {};
var match;

if (stripColors) {
line = line.replace(/[\x02\x1f\x16\x0f]|\x03\d{0,2}(?:,\d{0,2})?/g, '');
}

// Parse prefix
match = line.match(/^:([^ ]+) +/);
if (match) {
message.prefix = match[1];
line = line.replace(/^:[^ ]+ +/, '');
match = message.prefix.match(/^([_a-zA-Z0-9\[\]\\`^{}|-]*)(!([^@]+)@(.*))?$/);
if (match) {
message.nick = match[1];
message.user = match[3];
message.host = match[4];
}
else {
message.server = message.prefix;
}
}

// Parse command
match = line.match(/^([^ ]+) */);
message.command = match[1];
message.rawCommand = match[1];
message.commandType = 'normal';
line = line.replace(/^[^ ]+ +/, '');

if (replyFor[message.rawCommand]) {
message.command = replyFor[message.rawCommand].name;
message.commandType = replyFor[message.rawCommand].type;
}

message.args = [];
var middle, trailing;

// Parse parameters
if (line.search(/^:|\s+:/) != -1) {
match = line.match(/(.*?)(?:^:|\s+:)(.*)/);
middle = match[1].trimRight();
trailing = match[2];
}
else {
middle = line;
}

if (middle.length)
message.args = middle.split(/ +/);

if (typeof (trailing) != 'undefined' && trailing.length)
message.args.push(trailing);

return message;
}

exports.parseMessage = parseMessage;
68 changes: 68 additions & 0 deletions lib/parse_message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var replyFor = require('./codes');

/**
* parseMessage(line, stripColors)
*
* takes a raw "line" from the IRC server and turns it into an object with
* useful keys
* @param {String} line Raw message from IRC server.
* @param {Boolean} stripColors If true, strip IRC colors.
* @return {Object} A parsed message object.
*/
module.exports = function parseMessage(line, stripColors) {
var message = {};
var match;

if (stripColors) {
line = line.replace(/[\x02\x1f\x16\x0f]|\x03\d{0,2}(?:,\d{0,2})?/g, '');
}

// Parse prefix
match = line.match(/^:([^ ]+) +/);
if (match) {
message.prefix = match[1];
line = line.replace(/^:[^ ]+ +/, '');
match = message.prefix.match(/^([_a-zA-Z0-9\[\]\\`^{}|-]*)(!([^@]+)@(.*))?$/);
if (match) {
message.nick = match[1];
message.user = match[3];
message.host = match[4];
}
else {
message.server = message.prefix;
}
}

// Parse command
match = line.match(/^([^ ]+) */);
message.command = match[1];
message.rawCommand = match[1];
message.commandType = 'normal';
line = line.replace(/^[^ ]+ +/, '');

if (replyFor[message.rawCommand]) {
message.command = replyFor[message.rawCommand].name;
message.commandType = replyFor[message.rawCommand].type;
}

message.args = [];
var middle, trailing;

// Parse parameters
if (line.search(/^:|\s+:/) != -1) {
match = line.match(/(.*?)(?:^:|\s+:)(.*)/);
middle = match[1].trimRight();
trailing = match[2];
}
else {
middle = line;
}

if (middle.length)
message.args = middle.split(/ +/);

if (typeof (trailing) != 'undefined' && trailing.length)
message.args.push(trailing);

return message;
}
4 changes: 2 additions & 2 deletions test/test-parse-line.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var irc = require('../lib/irc.js');
var parseMessage = require('../lib/parse_message');
var test = require('tape');

test('irc.parseMessage', function(t) {
Expand Down Expand Up @@ -92,7 +92,7 @@ test('irc.parseMessage', function(t) {
Object.keys(checks).forEach(function(line) {
t.equal(
JSON.stringify(checks[line]),
JSON.stringify(irc.parseMessage(line)),
JSON.stringify(parseMessage(line)),
line + ' parses correctly'
);
});
Expand Down