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

Implement irc-message and irc-prefix-parser modules #447

Open
wants to merge 1 commit 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
55 changes: 13 additions & 42 deletions lib/parse_message.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var ircColors = require('irc-colors');
var replyFor = require('./codes');
var parse = require('irc-message').parse;
var parsePrefix = require('irc-prefix-parser');

/**
* parseMessage(line, stripColors)
Expand All @@ -11,59 +13,28 @@ var replyFor = require('./codes');
* @return {Object} A parsed message object.
*/
module.exports = function parseMessage(line, stripColors) {
var message = {};
var match;

if (stripColors) {
line = ircColors.stripColorsAndStyle(line);
}

// 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;
}
var message = parse(line);
var prefix = parsePrefix(message.prefix);
if (prefix.isServer) {
message.server = prefix.host;
} else {
message.nick = prefix.nick;
message.user = prefix.user;
message.host = prefix.host;
}

// Parse command
match = line.match(/^([^ ]+) */);
message.command = match[1];
message.rawCommand = match[1];
message.rawCommand = message.command;
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);

message.args = message.params; // backwards compatibility
delete message.params; // backwards compatibility
return message;
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"Chris Nehren <[email protected]>",
"Henri Niemeläinen <[email protected]>",
"Alex Miles <[email protected]>",
"Simmo Saan <[email protected]>"
"Simmo Saan <[email protected]>",
"Blay <[email protected]>"
],
"repository": {
"type": "git",
Expand All @@ -36,7 +37,9 @@
}
],
"dependencies": {
"irc-colors": "^1.1.0"
"irc-colors": "^1.1.0",
"irc-message": "^3.0.2",
"irc-prefix-parser": "^1.0.1"
},
"optionalDependencies": {
"iconv": "~2.1.6",
Expand Down
54 changes: 38 additions & 16 deletions test/data/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,107 +23,129 @@
},
"parse-line": {
":irc.dollyfish.net.nz 372 nodebot :The message of the day was last changed: 2012-6-16 23:57": {
"prefix": "irc.dollyfish.net.nz",
"server": "irc.dollyfish.net.nz",
"command": "rpl_motd",
"rawCommand": "372",
"commandType": "reply",
"args": ["nodebot", "The message of the day was last changed: 2012-6-16 23:57"]
"raw": ":irc.dollyfish.net.nz 372 nodebot :The message of the day was last changed: 2012-6-16 23:57",
"tags": {},
"prefix": "irc.dollyfish.net.nz",
"command": "rpl_motd",
"server": "irc.dollyfish.net.nz",
"rawCommand": "372",
"commandType": "reply",
"args": ["nodebot", "The message of the day was last changed: 2012-6-16 23:57"]
},
":[email protected] PRIVMSG #test :Hello nodebot!": {
"raw": ":[email protected] PRIVMSG #test :Hello nodebot!",
"tags": {},
"prefix": "[email protected]",
"command": "PRIVMSG",
"nick": "Ned",
"user": "~martyn",
"host": "irc.dollyfish.net.nz",
"command": "PRIVMSG",
"rawCommand": "PRIVMSG",
"commandType": "normal",
"args": ["#test", "Hello nodebot!"]
},
":[email protected] PRIVMSG #test ::-)": {
"raw": ":[email protected] PRIVMSG #test ::-)",
"tags": {},
"prefix": "[email protected]",
"command": "PRIVMSG",
"nick": "Ned",
"user": "~martyn",
"host": "irc.dollyfish.net.nz",
"command": "PRIVMSG",
"rawCommand": "PRIVMSG",
"commandType": "normal",
"args": ["#test", ":-)"]
},
":[email protected] PRIVMSG #test ::": {
"raw": ":[email protected] PRIVMSG #test ::",
"tags": {},
"prefix": "[email protected]",
"command": "PRIVMSG",
"nick": "Ned",
"user": "~martyn",
"host": "irc.dollyfish.net.nz",
"command": "PRIVMSG",
"rawCommand": "PRIVMSG",
"commandType": "normal",
"args": ["#test", ":"]
},
":[email protected] PRIVMSG #test ::^:^:": {
"raw": ":[email protected] PRIVMSG #test ::^:^:",
"tags": {},
"prefix": "[email protected]",
"command": "PRIVMSG",
"nick": "Ned",
"user": "~martyn",
"host": "irc.dollyfish.net.nz",
"command": "PRIVMSG",
"rawCommand": "PRIVMSG",
"commandType": "normal",
"args": ["#test", ":^:^:"]
},
":some.irc.net 324 webuser #channel +Cnj 5:10": {
"raw": ":some.irc.net 324 webuser #channel +Cnj 5:10",
"tags": {},
"prefix": "some.irc.net",
"command": "rpl_channelmodeis",
"server": "some.irc.net",
"command": "rpl_channelmodeis",
"rawCommand": "324",
"commandType": "reply",
"args": ["webuser", "#channel", "+Cnj", "5:10"]
},
":nick!user@host QUIT :Ping timeout: 252 seconds": {
"raw": ":nick!user@host QUIT :Ping timeout: 252 seconds",
"tags": {},
"prefix": "nick!user@host",
"command": "QUIT",
"nick": "nick",
"user": "user",
"host": "host",
"command": "QUIT",
"rawCommand": "QUIT",
"commandType": "normal",
"args": ["Ping timeout: 252 seconds"]
},
":nick!user@host PRIVMSG #channel :so : colons: :are :: not a problem ::::": {
"raw": ":nick!user@host PRIVMSG #channel :so : colons: :are :: not a problem ::::",
"tags": {},
"prefix": "nick!user@host",
"command": "PRIVMSG",
"nick": "nick",
"user": "user",
"host": "host",
"command": "PRIVMSG",
"rawCommand": "PRIVMSG",
"commandType": "normal",
"args": ["#channel", "so : colons: :are :: not a problem ::::"]
},
":nick!user@host PRIVMSG #channel :\u000314,01\u001fneither are colors or styles\u001f\u0003": {
"raw": ":nick!user@host PRIVMSG #channel :neither are colors or styles",
"tags": {},
"prefix": "nick!user@host",
"command": "PRIVMSG",
"nick": "nick",
"user": "user",
"host": "host",
"command": "PRIVMSG",
"rawCommand": "PRIVMSG",
"commandType": "normal",
"args": ["#channel", "neither are colors or styles"],
"stripColors": true
},
":nick!user@host PRIVMSG #channel :\u000314,01\u001fwe can leave styles and colors alone if desired\u001f\u0003": {
"raw": ":nick!user@host PRIVMSG #channel :\u000314,01\u001fwe can leave styles and colors alone if desired\u001f\u0003",
"tags": {},
"prefix": "nick!user@host",
"command": "PRIVMSG",
"nick": "nick",
"user": "user",
"host": "host",
"command": "PRIVMSG",
"rawCommand": "PRIVMSG",
"commandType": "normal",
"args": ["#channel", "\u000314,01\u001fwe can leave styles and colors alone if desired\u001f\u0003"],
"stripColors": false
},
":pratchett.freenode.net 324 nodebot #ubuntu +CLcntjf 5:10 #ubuntu-unregged": {
"raw": ":pratchett.freenode.net 324 nodebot #ubuntu +CLcntjf 5:10 #ubuntu-unregged",
"tags": {},
"prefix": "pratchett.freenode.net",
"command": "rpl_channelmodeis",
"server": "pratchett.freenode.net",
"command": "rpl_channelmodeis",
"rawCommand": "324",
"commandType": "reply",
"args": ["nodebot", "#ubuntu", "+CLcntjf", "5:10", "#ubuntu-unregged"]
Expand Down
2 changes: 1 addition & 1 deletion test/test-parse-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ test('irc.parseMessage', function(t) {
delete checks[line].stripColors;
}
t.equal(
JSON.stringify(checks[line]),
JSON.stringify(parseMessage(line, stripColors)),
JSON.stringify(checks[line]),
line + ' parses correctly'
);
});
Expand Down