summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-06-04 00:08:25 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-06-04 00:08:25 +0200
commit18c5b3ceac115200f994c440b78ceccda9a9862d (patch)
treea406719d5b1d59993df805d6668baa7dca9ba240
parent1886a4eae42d0c76a2405b75170daa1ed8251227 (diff)
downloaddynobot-irc-18c5b3ceac115200f994c440b78ceccda9a9862d.tar.gz
dynobot-irc-18c5b3ceac115200f994c440b78ceccda9a9862d.tar.bz2
dynobot-irc-18c5b3ceac115200f994c440b78ceccda9a9862d.tar.xz
dynobot-irc-18c5b3ceac115200f994c440b78ceccda9a9862d.zip
wip
-rw-r--r--atom-bot/index.js (renamed from lib/arktekk-bot.js)1
-rw-r--r--atom-bot/package.json27
-rw-r--r--dynobot-irc.js61
-rw-r--r--echo-bot.js17
-rw-r--r--echo-bot/index.js22
-rw-r--r--echo-bot/package.json27
-rw-r--r--irc-client.js103
-rw-r--r--package.json8
8 files changed, 229 insertions, 37 deletions
diff --git a/lib/arktekk-bot.js b/atom-bot/index.js
index 3a00c7f..37d073c 100644
--- a/lib/arktekk-bot.js
+++ b/atom-bot/index.js
@@ -19,6 +19,7 @@
* just update it.
*/
+require('tinycolor');
var node_irc = require('../node_modules/node-irc/IRC.js')
, cron = require('cron').CronJob
, parser = require('blindparser')
diff --git a/atom-bot/package.json b/atom-bot/package.json
new file mode 100644
index 0000000..a060290
--- /dev/null
+++ b/atom-bot/package.json
@@ -0,0 +1,27 @@
+{
+ "author": {
+ "name": "Trygve Laugstøl",
+ "email": "trygvis@inamo.no",
+ "url": "https://github.com/trygvis"
+ },
+ "name": "atom-bot",
+ "description": "IRC Bot that changes topic to the latest <entry> in a set of atom feeds",
+ "keywords": [
+ "irc",
+ "chat"
+ ],
+ "version": "0.0.1",
+ "engines": {
+ "node": ">=0.6.18"
+ },
+ "dependencies": {
+ "tinycolor": "0.0.1",
+ "underscore": "1.3.3"
+ },
+ "licenses": [
+ {
+ "type": "Apache Software License, version 2",
+ "url": "http://apache.org/licenses/LICENSE-2.0"
+ }
+ ]
+}
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);
diff --git a/echo-bot.js b/echo-bot.js
deleted file mode 100644
index 2d721e4..0000000
--- a/echo-bot.js
+++ /dev/null
@@ -1,17 +0,0 @@
-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);
diff --git a/echo-bot/index.js b/echo-bot/index.js
new file mode 100644
index 0000000..8c932a8
--- /dev/null
+++ b/echo-bot/index.js
@@ -0,0 +1,22 @@
+require('tinycolor');
+var irc = function() {
+ var Proxy = require('../node_modules/dynobot/proxy');
+ var Channel = require('../node_modules/dynobot/channel');
+ var IrcClient = require('../irc-client.js').IrcClient;
+
+ var channel = new Channel();
+ return new Proxy(IrcClient.prototype, 'irc', channel);
+}();
+
+irc.on('privmsg', function(nick, channel, message) {
+// console.log('privmsg: ', arguments);
+ irc.notice(channel, nick + ': ' + message);
+});
+
+irc.join('#bitraf2', function() {
+ console.log('joined channel!'.green);
+});
+
+console.log("echo bot started".green);
+
+// process.exit(0);
diff --git a/echo-bot/package.json b/echo-bot/package.json
new file mode 100644
index 0000000..6ca0096
--- /dev/null
+++ b/echo-bot/package.json
@@ -0,0 +1,27 @@
+{
+ "author": {
+ "name": "Trygve Laugstøl",
+ "email": "trygvis@inamo.no",
+ "url": "https://github.com/trygvis"
+ },
+ "name": "echo-bot",
+ "description": "Example bot that sends a NOTICE for each PRIVMSG",
+ "keywords": [
+ "irc",
+ "chat"
+ ],
+ "version": "0.0.1",
+ "engines": {
+ "node": ">=0.6.18"
+ },
+ "dependencies": {
+ "tinycolor": "0.0.1",
+ "underscore": "1.3.3"
+ },
+ "licenses": [
+ {
+ "type": "Apache Software License, version 2",
+ "url": "http://apache.org/licenses/LICENSE-2.0"
+ }
+ ]
+}
diff --git a/irc-client.js b/irc-client.js
new file mode 100644
index 0000000..bd8edb8
--- /dev/null
+++ b/irc-client.js
@@ -0,0 +1,103 @@
+var IRC = require('./node_modules/node-irc/irc').IRC;
+var util = require('util');
+var events = require('events');
+var _ = require('underscore');
+
+function info() {
+ var copy = [].slice.apply(arguments)
+ console.log('IrcClient: ' + copy.join(' '));
+}
+
+function Channel(name) {
+ this.name = name;
+ this.listeners = [];
+}
+
+Channel.prototype.addListener = function(f) {
+ this.listeners.push(f);
+};
+
+Channel.prototype.fire = function(event) {
+ _.each(this.listeners, function(f) {
+ f(event);
+ });
+};
+
+function IrcClient(nick, realName, ident, server, port, password) {
+ this.nick = nick;
+ this.realName = realName;
+ this.ident = ident;
+ this.server = server;
+ this.port = port;
+ this.password = password;
+
+ this.channels = {};
+ this.connected = false;
+ this.debugLevel = undefined;
+ this.irc = undefined;
+}
+util.inherits(IrcClient, events.EventEmitter);
+
+IrcClient.prototype.init = function(irc) {
+ // TODO: hmm..
+ var client = this;
+ this.debugLevel && irc.setDebugLevel(this.debugLevel);
+ irc.on('connected', function() {
+ info('Connected, channels: ' + _.size(client.channels));
+ _.each(client.channels, function(channel) {
+ info('JOINing ' + channel.name);
+ irc.join(channel.name);
+ });
+ });
+ irc.on('disconnected', function() {
+ info('Disconnected, reconnecting...');
+ client.connect();
+ });
+ irc.on('join', function(nick, channelName) {
+ _.each(client.channels, function(channel) {
+ info('JOINed ' + channel.name);
+ channel.fire(channelName);
+ });
+ });
+ irc.on('privmsg', function(nick, channel, message) {
+ client.emit('privmsg', nick, channel, message);
+ });
+ return irc;
+}
+
+IrcClient.prototype.getChannel = function(name) {
+ var c = this.channels[name];
+
+ if(typeof c == "undefined") {
+ c = new Channel(name);
+ this.channels[name] = c;
+ }
+
+ return c;
+}
+
+IrcClient.prototype.setDebugLevel = function(level) {
+ this.debugLevel = level;
+ this.irc && this.irc.setDebugLevel(level);
+}
+
+IrcClient.prototype.connect = function() {
+ this.irc = this.init(new IRC(this.server, this.port, this.password));
+ this.irc.connect(this.nick, this.realName, this.ident);
+}
+
+IrcClient.prototype.join = function(name, cb) {
+ var c = this.getChannel(name);
+ if(typeof cb != "undefined") {
+ c.addListener(cb);
+ }
+ if(this.connected) {
+ c.fire(name);
+ }
+}
+
+IrcClient.prototype.notice = function(to, message) {
+ this.irc.notice(to, message);
+}
+
+module.exports.IrcClient = IrcClient;
diff --git a/package.json b/package.json
index d45104d..948dea7 100644
--- a/package.json
+++ b/package.json
@@ -4,8 +4,8 @@
"email": "trygvis@inamo.no",
"url": "https://github.com/trygvis"
},
- "name": "bitraf-irc-client",
- "description": "Bitraf's IRC client",
+ "name": "arktekk-irc-client",
+ "description": "Arktekk's IRC client",
"keywords": [
"irc",
"chat"
@@ -16,13 +16,15 @@
"node": ">=0.4.7"
},
"dependencies": {
- "blindparser": "0.0.13"
+ "blindparser": "0.0.13",
"cmdopt": "0.2.0",
"cron": "~0.3.2",
"date": "1.0.2",
+ "dynobot": "https://github.com/einaros/dynobot/tarball/master",
"getit": "~0.1.7",
"mocha": "~1.0.3",
"node-irc": "https://github.com/einaros/node-irc/tarball/master",
+ "tinycolor": "0.0.1"
"underscore": "1.3.3",
},
"devDependencies": {},