Skip to content

Instantly share code, notes, and snippets.

@stagas stagas/protocol.js forked from jfhbrook/protocol.js
Created Dec 31, 2011

Embed
What would you like to do?
Thinking about a human-readable multi-line pubsub api for irc bots...
var EventEmitter = require('events').EventEmitter,
util = require('util');
// TODO:
// * tags (BEGIN somehashhere\nsomehashhere data)
// * line numbers (begin somehash\nsomehash:12 data)
var Buffer = exports.Buffer = function (o) {
o = o || {};
this.buffer = [];
this.buffering = false;
this.name = o.name || 'bot';
var self = this;
};
util.inherits(Buffer, EventEmitter);
Buffer.prototype.push = function (data) {
var self = this;
if (Buffer.isBuffer(data)) {
data = data.toString();
}
else if (typeof data !== 'string') {
try {
data = String(data);
}
catch (e) {
cb(e);
}
}
data.split('\n').forEach(function (line) {
if (self.buffer.length === 0) {
if (line.match(new RegExp('^'+self.name+': '))) {
self.buffer.push(line);
if (line.match(/BEGIN$/)) {
self.buffering = true;
}
}
}
else if (self.buffering) {
self.buffer.push(line);
if (line.match(/END$/)) {
self.buffering = false;
}
}
if (!self.buffering) {
self.emit('data', self.buffer.join('\n'));
self.buffer = [];
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.