-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from martynsmith/jirwin/factor-parse-messages…
…-to-new-file fix(parseMessage): Factor parseMessage to another file for decoupling.
- Loading branch information
Showing
3 changed files
with
71 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters