Skip to content

Commit

Permalink
Cleanup of tests; Started on Chunked Message Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Havvy committed Mar 12, 2013
1 parent 38de58e commit 332f664
Show file tree
Hide file tree
Showing 14 changed files with 428 additions and 428 deletions.
3 changes: 3 additions & 0 deletions lib/bisubscriber.js
Expand Up @@ -30,6 +30,9 @@
* - once(events: string, listener: function): undefined
*/

/*
*/

var BiEventSubscriber = function (primary, secondary) {
this._primary = primary;
this._secondary = secondary;
Expand Down
24 changes: 24 additions & 0 deletions lib/chunked-message-parser.js
@@ -0,0 +1,24 @@
var util = require('util');
var events = require('events');

var ChunkedMessageParser = function (messageParser) {
if (messageParser) {
this.listen(messageParser);
}
};

ChunkedMessageParser.prototype = new events.EventEmitter();

ChunkedMessageParser.prototype.parse = function (message) {
this.defaultAction(message);
};

ChunkedMessageParser.prototype.listen = function (messageParser) {
messageParser.on('_message', this.parse.bind(this));
};

ChunkedMessageParser.prototype.defaultAction = function (message) {
this.emit(message.name, message);
};

module.exports = ChunkedMessageParser;
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -9,7 +9,7 @@ var index = {
Socket : 'socket',
OutputSocket : 'output-socket',
MessageParser : 'irc-message-emitter',
// ChunkedMessageParser: 'chunked-message-parser',
ChunkedMessageParser: 'chunked-message-parser',
CommandParser : 'commander',
Modules : 'modules',
Bisubscriber : 'bisubscriber'
Expand Down
23 changes: 17 additions & 6 deletions lib/message-parser.js
Expand Up @@ -7,13 +7,16 @@
* 005
*
* The constructor takes two arguments:
* 1. socket - Implementing the IrcSocket interface.
* 2. receiver - The nominal receiver of the object.
* 1. receiver - The nominal receiver of the object.
* 2. socket - EventEmitter emitting ('data', RFC1459Message) <Optional>
*
* If you do not attach a socket to it, you can manually feed messages with
* the parse method.
*
* You can add multiple input sockets with the listen method.
*/

/*
This is very close to a pure actor.
Just need class syntax and fat arrow syntax and this will look beautiful.
*/

var events = require('events');
Expand All @@ -23,16 +26,24 @@ var Message = require('./structures/message');

var MessageParser = function (receiver, socket) {
this.receiver = receiver;
socket.on('data', this._onData.bind(this));

if (socket) {
this.listen(socket);
}
};

MessageParser.prototype = new events.EventEmitter();
MessageParser.prototype.constructor = MessageParser;

MessageParser.prototype._onData = function (raw) {
MessageParser.prototype.parse = function (raw) {
var message = Object.freeze(new Message(raw, this.receiver));
this.emit(message.name, message);
this.emit("_message", message);
return message;
};

MessageParser.prototype.listen = function (socket) {
socket.on('data', this.parse.bind(this));
};

MessageParser.prototype.toString = function () {
Expand Down
2 changes: 1 addition & 1 deletion lib/nrc.js
Expand Up @@ -21,7 +21,7 @@ var defaultFactoryConfiguration = {
'IrcSocket' : require('./socket'),
'IrcOutputSocket' : require('./output-socket'),
'MessageParser' : require('./message-parser'),
// 'ChunkedMessageParser' : require('./chunked-message-parser'),
'ChunkedMessageParser' : require('./chunked-message-parser'),
'CommandParser' : require('./command-parser'),
'Modules' : require('./modules'),
'BiSubscriber' : require('./bisubscriber')
Expand Down
63 changes: 21 additions & 42 deletions lib/structures/hostmask.js
@@ -1,54 +1,33 @@
/**
* A hostmask is the combination of a nickname, a username, and a host. Every IRC user
* has one that can be obtained from the WHOIS message.
* A hostmask is the combination of a nickname, a username, and a host.
* Every IRC user has one that can be obtained from the USERHOST message.
*
* A hostmask's value properties (nick, user, and host) are guaranteed to be final.
* This object is frozen upon creation.
*
* @author havvy
* @param {String} asString String representation of a hostmask. nick!user@host
* Constructor: Takes one argument, a hostmask. For example "nick!user@host";
*/

nullHostmask = {
nick : '',
user : '',
host : ''
};
/*
*/

var Hostmask = function (asString) {
if (!asString) {
return nullHostmask;
}

var nickEnd = asString.indexOf("!");
var userEnd = asString.indexOf("@");

var nick = asString.slice(0, nickEnd);
var user = asString.slice(nickEnd + 1, userEnd);
var host = asString.slice(userEnd + 1);

Object.defineProperties(this, {
"nick" : {
"get" : function () { return nick; }
},
"user" : {
"get" : function () { return user; }
},
"host" : {
"get" : function () { return host; }
}
});
var nickEnd = asString.indexOf("!");
var userEnd = asString.indexOf("@");

this.nick = asString.slice(0, nickEnd);
this.user = asString.slice(nickEnd + 1, userEnd);
this.host = asString.slice(userEnd + 1);

Object.freeze(this);
};

Hostmask.prototype.equals = function (that) {
return this.nick === that.nick && this.user === that.user &&
this.host === that.host;
};

Hostmask.prototype = {
equals : function (other) {
return this.nick === other.nick && this.user === other.user &&
this.host === other.host;
},

toString : function () {
return [this.nick, '!', this.user, '@', this.host].join('');
}
Hostmask.prototype.toString = function () {
return this.nick + '!' + this.user + '@' + this.host;
};

nullHostmask.prototype = Hostmask;
module.exports = Hostmask;

0 comments on commit 332f664

Please sign in to comment.