diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2012-06-16 14:24:33 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-06-16 14:24:33 +0200 |
commit | 2870c4da1aedf41926972dd60227c3d62cdaa123 (patch) | |
tree | bab887acdac4969c8a4cc3fcaf827ebb81726f6c /FakeSocket.js | |
parent | e971ae61c99c8a602a1d60c95e9f8f908d4cf053 (diff) | |
download | dynobot-irc-2870c4da1aedf41926972dd60227c3d62cdaa123.tar.gz dynobot-irc-2870c4da1aedf41926972dd60227c3d62cdaa123.tar.bz2 dynobot-irc-2870c4da1aedf41926972dd60227c3d62cdaa123.tar.xz dynobot-irc-2870c4da1aedf41926972dd60227c3d62cdaa123.zip |
o Polishing the dynobot a bit.
Diffstat (limited to 'FakeSocket.js')
-rw-r--r-- | FakeSocket.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/FakeSocket.js b/FakeSocket.js new file mode 100644 index 0000000..af3d74c --- /dev/null +++ b/FakeSocket.js @@ -0,0 +1,44 @@ +function FakeSocket(stdin, stdout, msgHandler) { + this.stdin = stdin; + this.stdout = stdout; + this.msgHandler = msgHandler; + this.connectHandler = undefined; + this.errorHandler = undefined; +} + +FakeSocket.prototype.setEncoding = function(encoding) { + this.stdin.setEncoding(encoding); +} + +FakeSocket.prototype.on = function(event, cb) { + switch(event) { + case 'data': + this.stdin.on('data', function(chunk) { + cb(chunk.trim() + '\r\n'); + }); + break; + case 'connect': + this.connectHandler = cb; + break; + case 'close': + this.stdin.on('exit', cb); + break; + case 'end': + case 'error': + break; + default: + process.exit(1); + break; + } +} + +FakeSocket.prototype.connect = function(server, port) { + this.connectHandler(); +} + +FakeSocket.prototype.write = function(chunk) { + this.stdout.write(chunk.red); + this.msgHandler && this.msgHandler(chunk); +} + +module.exports = FakeSocket; |