diff options
-rw-r--r-- | dynobot-irc.js | 182 | ||||
-rw-r--r-- | echo-bot.js | 17 |
2 files changed, 199 insertions, 0 deletions
diff --git a/dynobot-irc.js b/dynobot-irc.js new file mode 100644 index 0000000..705e534 --- /dev/null +++ b/dynobot-irc.js @@ -0,0 +1,182 @@ +var EventEmitter = require('events').EventEmitter; +var util = require('util'); +var cp = require('child_process'); +var IRC = require('./node_modules/node-irc/irc').IRC; +var Service = require('dynobot/service.js'); +var Channel = require('dynobot/channel.js'); +var _ = require('underscore'); +var rl = require('readline'); + +function FakeSocket(stdin, stdout) { + this.stdin = stdin; + this.stdout = stdout; + 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); +} + +var StdinPipe = function() { + EventEmitter.call(this); +} +util.inherits(StdinPipe, EventEmitter); + +StdinPipe.prototype.setEncoding = function() {} +StdinPipe.prototype.send = function(msg) { + console.log('outgoing: ' + msg); + stdinPipe.emit('data', msg + '\r\n'); +} + +var stdinPipe = new StdinPipe(); +var irc; + +if(false) { + irc = new IRC('irc.freenode.net', 6667); +} +else { + irc = new IRC(new FakeSocket(stdinPipe, process.stdout)); + process.stdin.resume(); +} + +var ircService = new Service('irc', irc); + +irc.setDebugLevel(3); +irc.on('connected', function() { + irc.join('#bitraf2', function(channel) { + irc.notice(channel, 'well hello yall'); + }); +}); + +function Plugin(name, script) { + this.name = name; + this.script = script; + this.channel = undefined; +} + +var plugins = [ + new Plugin('arktekk', './echo-bot.js') +]; + +_.each(plugins, function(plugin) { + console.log('Starting ' + plugin.name); + // TODO: Resolve plugin.script + var child = cp.fork(plugin.script); + var channel = new Channel(child); + ircService.handle(channel); + child.on('exit', function() { + console.log(plugin.name + ' exited..'); + ircService.release(channel); + channel.close(); + }); + // TODO: set up consumers for child.stdout and color it before + // piping to process.stdout +}); + +irc.connect('dynobot', 'real name', 'ident'); + +process.stdin.resume(); +var i = rl.createInterface(process.stdin, process.stdout, null); + +function quit() { + i.close(); + process.stdin.destroy(); + process.exit(0); +} + +function setupRepl() { + var from = 'alice!~alice@example.com'; + var channel = '#bitraf2'; + + function updatePrompt() { + var prefix = 'from=' + from; + prefix += channel ? ', channel=' + channel : ''; + prefix += '> '; + i.setPrompt(prefix, prefix.length); + i.prompt(); + } + + i.on('line', function (cmd) { + cmd = cmd.trim(); + if(cmd.length == 0) { + i.prompt(); + return; + } + var parts = cmd.split(' '); + switch(parts[0]) { + case '/quit': + case '/q': + quit(); + break; + case '/raw': + case '/r': + parts.shift(); + stdinPipe.send(parts.join(' ')); + updatePrompt(); + break; + case '/join': + case '/j': + channel = parts[1]; + updatePrompt(); + break; + case '/from': + case '/f': + from = parts[1]; + updatePrompt(); + break; + case '/notice': + case '/n': + if(channel) { + parts.shift(); + stdinPipe.send(':' + from + ' NOTICE ' + channel + ' :' + parts.join(' ')); + } + else { + console.log('You have to /j a channel first'); + } + break; + default: + stdinPipe.send(':' + from + ' PRIVMSG ' + channel + ' :' + parts.join(' ')); + updatePrompt(); + break; + } + }).on('close', function() { + quit(); + }); + updatePrompt(); +} + +stdinPipe.emit('data', ':irc.foo.bar 001 this :Welcome to Some Internet Relay Chat Network this'); +setupRepl(i); + +// setInterval(function() {gc();}, 2000); diff --git a/echo-bot.js b/echo-bot.js new file mode 100644 index 0000000..2d721e4 --- /dev/null +++ b/echo-bot.js @@ -0,0 +1,17 @@ +var irc = function() { + var Proxy = require('dynobot/proxy'); + var Channel = require('dynobot/channel'); + var IRC = require('./node_modules/node-irc/irc').IRC; + + var channel = new Channel(); + return new Proxy(IRC.prototype, 'irc', channel); +}(); + +irc.on('privmsg', function(nick, channel, message) { + console.log('privmsg: ', arguments); + irc.notice(channel, nick + ': ' + message); +}); + +console.log("echo bot started"); + +// process.exit(0); |