Skip to content

Instantly share code, notes, and snippets.

@stagas
Created December 31, 2011 11:41
Show Gist options
  • Save stagas/1543755 to your computer and use it in GitHub Desktop.
Save stagas/1543755 to your computer and use it in GitHub Desktop.
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