Preparation Code Preparation HTML (this will be inserted in the <body>
of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries)
Setup JS var str = ":ricki!RickA@kim.dot.com PRIVMSG #expr :Hey expr, just wondering, how often is it that this code actually works?!" ;
var IRCMessage, Message , exports ;
IRCMessage = (function ( ) {
function IRCMessage (line ) {
var nextspace, pair, position, rawTags, tag, _i, _len;
this .tags = {};
this .prefix = "" ;
this .command = "" ;
this .params = [];
position = 0 ;
nextspace = 0 ;
if (line.charAt (0 ) === "@" ) {
nextspace = line.indexOf (" " );
if (nextspace === -1 ) {
return new Object ;
}
rawTags = line.slice (1 , nextspace).split (";" );
for (_i = 0 , _len = rawTags.length ; _i < _len; _i++) {
tag = rawTags[_i];
pair = tag.split ("=" );
this .tags [pair[0 ]] = pair[1 ] || true ;
}
position = nextspace + 1 ;
}
while (line.charAt (position) === " " ) {
position++;
}
if (line.charAt (position) === ":" ) {
nextspace = line.indexOf (" " , position);
if (nextspace === -1 ) {
return new Object ;
}
this .prefix = line.slice (position + 1 , nextspace);
position = nextspace + 1 ;
while (line.charAt (position) === " " ) {
position++;
}
}
nextspace = line.indexOf (" " , position);
if (nextspace === -1 ) {
if (line.length > position) {
this .command = line.slice (position);
return ;
} else {
return ;
}
}
this .command = line.slice (position, nextspace);
position = nextspace + 1 ;
while (line.charAt (position) === " " ) {
position++;
}
while (position < line.length ) {
nextspace = line.indexOf (" " , position);
if (line.charAt (position) === ":" ) {
this .params .push (line.slice (position + 1 ));
break ;
}
if (nextspace !== -1 ) {
this .params .push (line.slice (position, nextspace));
position = nextspace + 1 ;
while (line.charAt (position) === " " ) {
position++;
}
continue ;
}
if (nextspace === -1 ) {
this .params .push (line.slice (position));
break ;
}
}
return ;
}
IRCMessage.prototype .toString = function ( ) {
var param, string, tag, value, _i, _len, _ref, _ref1;
string = "" ;
if (Object .keys (this .tags ).length !== 0 ) {
string += "@" ;
_ref = this .tags ;
for (tag in _ref) {
value = _ref[tag];
if (value !== true ) {
string += "" + tag + "=" + value + ";" ;
} else {
string += "" + tag + ";" ;
}
}
string = string.slice (0 , -1 ) + " " ;
}
if (this .prefix .length !== 0 ) {
string += ":" + this .prefix + " " ;
}
if (this .command .length !== 0 ) {
string += "" + this .command + " " ;
}
if (this .params .length !== 0 ) {
_ref1 = this .params ;
for (_i = 0 , _len = _ref1.length ; _i < _len; _i++) {
param = _ref1[_i];
if (param.indexOf (" " ) === -1 ) {
string += "" + param + " " ;
} else {
string += ":" + param + " " ;
}
}
}
string = string.slice (0 , -1 );
return string;
};
IRCMessage.prototype .prefixIsHostmask = function ( ) {
return this .prefix .indexOf ("@" ) !== -1 && this .prefix .indexOf ("!" ) !== -1 ;
};
IRCMessage.prototype .prefixIsServer = function ( ) {
return this .prefix .indexOf ("@" ) === -1 && this .prefix .indexOf ("!" ) === -1 && this .prefix .indexOf ("." ) !== -1 ;
};
IRCMessage.prototype .parseHostmaskFromPrefix = function ( ) {
var hostname, nickname, username, _ref;
if (this .prefixIsHostmask ()) {
_ref = this .prefix .split (/[!@]/ ), nickname = _ref[0 ], username = _ref[1 ], hostname = _ref[2 ];
return {
nickname : nickname,
username : username,
hostname : hostname
};
} else {
return null ;
}
};
return IRCMessage;
})();
Message = function (line ) {
var message;
message = new IRCMessage (line);
if (Object .getOwnPropertyNames (message).length === 0 ) {
return null ;
} else {
return message;
}
};
var ircee = function protocol_parse (line ) {
var m = line.match (/^(:(\S+))?\s*([^:]\S+)\s+([^:]+)?(\s*:(.+))?/ );
var raw = m[0 ], source = m[2 ], cmd = m[3 ],
params = m[4 ], text = m[6 ];
if (params) params = params.trim ().split (' ' );
var target = params ? params[0 ] : null ;
var user = source ? source.match ('(.+)!(.+)@(.+)' ) : null ;
return {
raw : raw,
source : source,
cmd : cmd,
user : user ? {
address : user[0 ],
nick : user[1 ],
user : user[2 ],
host : user[3 ]
} : null ,
params : params,
target : target,
text : text
}
}
var construct = function protocol_construct (args ) {
args = [].slice .call (args);
var last = args.length - 1 ;
if (args[last] !== null )
args[last] = ':' + args[last];
else
delete args[last];
return args.join (' ' ) + '\n' ;
}
Teardown JS