require('tinycolor'); var Channel = require('dynobot/channel.js'); var EventEmitter = require('events').EventEmitter; var FakeSocket = require('./FakeSocket.js'); var IrcClient = require('./irc-client').IrcClient; var Service = require('dynobot/service.js'); var _ = require('underscore'); var cmdopt = require('cmdopt') var cp = require('child_process'); var os = require('os') var util = require('util'); var watchTree = require("fs-watch-tree").watchTree; var parser = new cmdopt.Parser(); parser.option("-h, --help", "Show this help"); parser.option("-s, --server=HOST", "Server to join"); parser.option("-p, --port=PORT", "Port to use"); parser.option("-n, --nick=NICK", "Nickname of the bot"); parser.option("-c, --channel=CHANNEL", "IRC channel to join"); var args = process.argv.slice(2); var defaults = { server: "irc.freenode.org", port: "6667", nick: os.hostname(), channel: "#dynobot" }; try { var config = parser.parse(args); if (config.help) { console.log(parser.help()); return; } config = _.defaults(config, defaults); console.log("config", config); } catch (ex) { if (ex instanceof cmdopt.ParseError) { process.stderr.write(ex.message + "\n"); process.exit(1); } throw ex; } var StdinPipe = function() { EventEmitter.call(this); } util.inherits(StdinPipe, EventEmitter); StdinPipe.prototype.setEncoding = function() {} StdinPipe.prototype.send = function(msg) { console.log(msg.blue); stdinPipe.emit('data', msg + '\r\n'); } var stdinPipe = new StdinPipe(); var irc; var realName = 'bots\'r\'us'; var ident = 'ident'; function info(a, b, c) { console.log.apply(null, arguments); } var reallyConnect = true; if(reallyConnect) { irc = new IrcClient(config.nick, realName, ident, config.server, config.port); } else { var msgHandler = function(line) { line = line.trim(); var prefix = ':' + nick + '!' + ident + '@example.org'; var parts = line.split(' '); switch(parts[0]) { case 'JOIN': stdinPipe.send(prefix + ' JOIN ' + parts[1]); } } irc = new IrcClient(nick, realName, ident, new FakeSocket(stdinPipe, process.stdout, msgHandler)); } var ircService = new Service('irc', irc); irc.setDebugLevel(3); function Plugin(dir, config) { this.dir = dir; this.config = config; this.channel = undefined; // this.watch = undefined; // TODO: Resolve plugin.script. Dir is relative to the plugin's // installation directory. this.script = 'index.js'; } Plugin.prototype.start = function(name) { info('Starting ' + name); // TODO: set cwd option to the plugin's directory. var args = []; var options = { silent: true, cwd: this.dir }; var child = cp.fork(this.script, args, options); var channel = new Channel(child); ircService.handle(channel); child.on('exit', function() { info(name + ' exited..'); ircService.release(channel); channel.close(); }); // TODO: set up consumers for child.stdout and color it before // piping to process.stdout // this.watch = watchTree(this.dir); info(name + ' started..'); } Plugin.prototype.stop = function(name) { log("Stooping pulugin " + name); // this.watch && this.watch.end(); } var plugins = { // new Plugin('echo', './echo-bot') atom: new Plugin('./atom-bot') }; _.each(plugins, function(plugin, name) { plugin.start(name); }); irc.join(config.channel, function(name) { info('joined ' + name + ' from dynobot.'); }); info('colors: ' + 'from bot'.red + ', ' + 'to bot'.blue); irc.connect(); var repl = require('./Repl.js'); repl.init(); if(reallyConnect) { repl.setupRepl(); } else { repl.setupSimulatorRepl(); }