diff --git a/lib/parse_message.js b/lib/parse_message.js index 698c6388..6d5d6549 100644 --- a/lib/parse_message.js +++ b/lib/parse_message.js @@ -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) @@ -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; } diff --git a/package.json b/package.json index 50617c64..afe34719 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "Chris Nehren ", "Henri Niemeläinen ", "Alex Miles ", - "Simmo Saan " + "Simmo Saan ", + "Blay " ], "repository": { "type": "git", @@ -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", diff --git a/test/data/fixtures.json b/test/data/fixtures.json index a15cce92..f7cf261c 100644 --- a/test/data/fixtures.json +++ b/test/data/fixtures.json @@ -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"] }, ":Ned!~martyn@irc.dollyfish.net.nz PRIVMSG #test :Hello nodebot!": { + "raw": ":Ned!~martyn@irc.dollyfish.net.nz PRIVMSG #test :Hello nodebot!", + "tags": {}, "prefix": "Ned!~martyn@irc.dollyfish.net.nz", + "command": "PRIVMSG", "nick": "Ned", "user": "~martyn", "host": "irc.dollyfish.net.nz", - "command": "PRIVMSG", "rawCommand": "PRIVMSG", "commandType": "normal", "args": ["#test", "Hello nodebot!"] }, ":Ned!~martyn@irc.dollyfish.net.nz PRIVMSG #test ::-)": { + "raw": ":Ned!~martyn@irc.dollyfish.net.nz PRIVMSG #test ::-)", + "tags": {}, "prefix": "Ned!~martyn@irc.dollyfish.net.nz", + "command": "PRIVMSG", "nick": "Ned", "user": "~martyn", "host": "irc.dollyfish.net.nz", - "command": "PRIVMSG", "rawCommand": "PRIVMSG", "commandType": "normal", "args": ["#test", ":-)"] }, ":Ned!~martyn@irc.dollyfish.net.nz PRIVMSG #test ::": { + "raw": ":Ned!~martyn@irc.dollyfish.net.nz PRIVMSG #test ::", + "tags": {}, "prefix": "Ned!~martyn@irc.dollyfish.net.nz", + "command": "PRIVMSG", "nick": "Ned", "user": "~martyn", "host": "irc.dollyfish.net.nz", - "command": "PRIVMSG", "rawCommand": "PRIVMSG", "commandType": "normal", "args": ["#test", ":"] }, ":Ned!~martyn@irc.dollyfish.net.nz PRIVMSG #test ::^:^:": { + "raw": ":Ned!~martyn@irc.dollyfish.net.nz PRIVMSG #test ::^:^:", + "tags": {}, "prefix": "Ned!~martyn@irc.dollyfish.net.nz", + "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"] diff --git a/test/test-parse-line.js b/test/test-parse-line.js index c0da5152..5255e0aa 100644 --- a/test/test-parse-line.js +++ b/test/test-parse-line.js @@ -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' ); });