summaryrefslogtreecommitdiff
path: root/dynobot-irc.js
diff options
context:
space:
mode:
Diffstat (limited to 'dynobot-irc.js')
-rw-r--r--dynobot-irc.js61
1 files changed, 44 insertions, 17 deletions
diff --git a/dynobot-irc.js b/dynobot-irc.js
index 705e534..20f6f79 100644
--- a/dynobot-irc.js
+++ b/dynobot-irc.js
@@ -1,15 +1,19 @@
+require('tinycolor');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var cp = require('child_process');
-var IRC = require('./node_modules/node-irc/irc').IRC;
+var IrcClient = require('./irc-client').IrcClient;
var Service = require('dynobot/service.js');
var Channel = require('dynobot/channel.js');
var _ = require('underscore');
var rl = require('readline');
-function FakeSocket(stdin, stdout) {
+/************************************************************/
+
+function FakeSocket(stdin, stdout, msgHandler) {
this.stdin = stdin;
this.stdout = stdout;
+ this.msgHandler = msgHandler;
this.connectHandler = undefined;
this.errorHandler = undefined;
}
@@ -45,9 +49,12 @@ FakeSocket.prototype.connect = function(server, port) {
}
FakeSocket.prototype.write = function(chunk) {
- this.stdout.write(chunk);
+ this.stdout.write(chunk.red);
+ this.msgHandler && this.msgHandler(chunk);
}
+/************************************************************/
+
var StdinPipe = function() {
EventEmitter.call(this);
}
@@ -55,29 +62,38 @@ util.inherits(StdinPipe, EventEmitter);
StdinPipe.prototype.setEncoding = function() {}
StdinPipe.prototype.send = function(msg) {
- console.log('outgoing: ' + msg);
+ console.log(msg.blue);
stdinPipe.emit('data', msg + '\r\n');
}
+/************************************************************/
+
var stdinPipe = new StdinPipe();
var irc;
+var nick = 'dynobot';
+var realName = 'bots\'r\'us';
+var ident = 'ident';
if(false) {
- irc = new IRC('irc.freenode.net', 6667);
+ irc = new IrcClient(nick, realName, ident, 'irc.freenode.net', 6667);
}
else {
- irc = new IRC(new FakeSocket(stdinPipe, process.stdout));
+ 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));
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;
@@ -86,13 +102,18 @@ function Plugin(name, script) {
}
var plugins = [
- new Plugin('arktekk', './echo-bot.js')
+ new Plugin('echo', './echo-bot/index.js')
];
_.each(plugins, function(plugin) {
console.log('Starting ' + plugin.name);
// TODO: Resolve plugin.script
- var child = cp.fork(plugin.script);
+ // TODO: set cwd option to the plugin's directory.
+ var args = [];
+ var options = {
+ silent: true
+ };
+ var child = cp.fork(plugin.script, args, options);
var channel = new Channel(child);
ircService.handle(channel);
child.on('exit', function() {
@@ -104,8 +125,6 @@ _.each(plugins, function(plugin) {
// piping to process.stdout
});
-irc.connect('dynobot', 'real name', 'ident');
-
process.stdin.resume();
var i = rl.createInterface(process.stdin, process.stdout, null);
@@ -176,7 +195,15 @@ function setupRepl() {
updatePrompt();
}
-stdinPipe.emit('data', ':irc.foo.bar 001 this :Welcome to Some Internet Relay Chat Network this');
+irc.join('#bitraf2', function(name) {
+ console.log('joined ' + name + ' from dynobot.');
+});
+
+console.log('colors: ' + 'from bot'.red + ', ' + 'to bot'.blue);
+irc.connect();
+
setupRepl(i);
-// setInterval(function() {gc();}, 2000);
+setTimeout(function() {
+ stdinPipe.send(':irc.foo.bar 001 this :Welcome to Some Internet Relay Chat Network this');
+}, 1000);