22
33var _ = require ( 'lodash' ) ;
44var MessageTags = require ( './messagetags' ) ;
5+ var IrcMessage = require ( './ircmessage' ) ;
56
67module . exports = parseIrcLine ;
78
@@ -13,38 +14,31 @@ module.exports = parseIrcLine;
1314var parse_regex = / ^ (?: @ ( [ ^ ] + ) ) ? (?: : ( (?: (?: ( [ ^ \s ! @ ] + ) (?: ! ( [ ^ \s @ ] + ) ) ? ) @ ) ? ( \S + ) ) ) ? ( (?: [ a - z A - Z ] + ) | (?: [ 0 - 9 ] { 3 } ) ) (?: ( [ ^ : ] .* ?) ) ? (?: : ( .* ) ) ? $ / i;
1415
1516function parseIrcLine ( line ) {
16- var msg ;
17- var tags = Object . create ( null ) ;
18- var msg_obj ;
19-
2017 // Parse the complete line, removing any carriage returns
21- msg = parse_regex . exec ( line . replace ( / ^ \r + | \r + $ / , '' ) ) ;
22-
23- if ( ! msg ) {
18+ let matches = parse_regex . exec ( line . replace ( / ^ \r + | \r + $ / , '' ) ) ;
19+ if ( ! matches ) {
2420 // The line was not parsed correctly, must be malformed
2521 return ;
2622 }
2723
28- // Extract any tags (msg[1])
29- if ( msg [ 1 ] ) {
30- tags = MessageTags . decode ( msg [ 1 ] ) ;
24+ let msg = new IrcMessage ( ) ;
25+
26+ if ( matches [ 1 ] ) {
27+ msg . tags = MessageTags . decode ( matches [ 1 ] ) ;
3128 }
3229
33- // Nick value will be in the prefix slot if a full user mask is not used
34- msg_obj = {
35- tags : tags ,
36- prefix : msg [ 2 ] ,
37- nick : msg [ 3 ] || msg [ 2 ] ,
38- ident : msg [ 4 ] || '' ,
39- hostname : msg [ 5 ] || '' ,
40- command : msg [ 6 ] ,
41- params : msg [ 7 ] ? msg [ 7 ] . split ( / + / ) : [ ]
42- } ;
30+ msg . prefix = matches [ 2 ] ;
31+ // Nick will be in the prefix slot if a full user mask is not used
32+ msg . nick = matches [ 3 ] || matches [ 2 ] ;
33+ msg . ident = matches [ 4 ] || '' ;
34+ msg . hostname = matches [ 5 ] || '' ;
35+ msg . command = matches [ 6 ] ;
36+ msg . params = matches [ 7 ] ? matches [ 7 ] . split ( / + / ) : [ ] ;
4337
4438 // Add the trailing param to the params list
45- if ( typeof msg [ 8 ] !== 'undefined' ) {
46- msg_obj . params . push ( _ . trimEnd ( msg [ 8 ] ) ) ;
39+ if ( typeof matches [ 8 ] !== 'undefined' ) {
40+ msg . params . push ( _ . trimEnd ( matches [ 8 ] ) ) ;
4741 }
4842
49- return msg_obj ;
43+ return msg ;
5044}
0 commit comments