Created
December 31, 2011 11:41
-
-
Save stagas/1543755 to your computer and use it in GitHub Desktop.
Thinking about a human-readable multi-line pubsub api for irc bots...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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