From dff4a1ffd7cf9ca3097ea0227a82d18184bbca25 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 4 Jun 2012 13:09:40 +0200 Subject: o Updating the atom plugin to the latest API. --- atom-bot/index.js | 100 ++++++++++++++++++++++++------------------------------ dynobot-irc.js | 20 +++++++---- irc-client.js | 25 +++++++++----- 3 files changed, 75 insertions(+), 70 deletions(-) diff --git a/atom-bot/index.js b/atom-bot/index.js index 37d073c..470786c 100644 --- a/atom-bot/index.js +++ b/atom-bot/index.js @@ -20,20 +20,37 @@ */ require('tinycolor'); -var node_irc = require('../node_modules/node-irc/IRC.js') - , cron = require('cron').CronJob +var cron = require('cron').CronJob , parser = require('blindparser') , events = require('events') , _ = require('underscore'); +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); +}(); + var parserOptions = {}; -var config; +// Config +var channel = '#bitraf2'; +var feeds = [ +// "http://search.twitter.com/search.atom?q=from:AgileBorat", +// "http://search.twitter.com/search.atom?q=from:KongenDin", +// "http://search.twitter.com/search.atom?q=from:NestenSivJensen", + "http://search.twitter.com/search.atom?q=ndc2012" +// "http://search.twitter.com/search.atom?q=awesome", +// "http://search.twitter.com/search.atom?q=eurovision", +]; var cronJobs = []; var state = { channelTopic: undefined, - irc: undefined, + topic: undefined, updatingFeed: false, feeds: [], newest: { @@ -41,35 +58,9 @@ var state = { text: undefined } }; -module.exports.state = state; var eventEmitter = new events.EventEmitter; -function startIrc() { - irc = new node_irc.IRC(config.host, config.port); - irc.on('raw', function(data) { console.log(data) }); - irc.on('connected', function(server) { - console.log('Connected to ' + server); - irc.join(config.channel, function(error) { - irc.notice(config.channel, 'well hello yall'); - }); - }); - irc.topic = function(channel, topic) { - irc._socket.write('topic ' + channel + ' :' + topic + '\r\n'); - }; - - irc.on('topic', function(channel, topic) { - console.log("new topic: " + topic); - /* If we're not storing this, it is possible for people to set - * the topic after the bot has set it and it will persist (until - * next update from the feed). - topic = t; - */ - - state.channelTopic = topic; - }); -} - function updateFeed(feedState) { console.log("Fetching " + feedState.url); if(feedState.updatingFeed) @@ -77,7 +68,7 @@ function updateFeed(feedState) { feedState.updatingFeed = true; parser.parseURL(feedState.url, parserOptions, function(err, feed) { -// console.log("Fetched " + feedState.url + ", status=" + (err ? "failure" : "success")); + console.log("Fetched " + feedState.url + ", status=" + (err ? "failure" : "success")); if(err) { console.log(err); return; @@ -108,33 +99,33 @@ function processFeed(feed) { timestamp: feed.items[0].date }; } -module.exports.processFeed = processFeed; eventEmitter.on("feedChanged", function(url, newest) { state.feeds[url] = newest; if(state.newest.timestamp >= newest.timestamp) { -// console.log("oold: " + newest.text); + console.log("oold: " + newest.text); return; } + /* + */ var text = newest.author + ": " + newest.text; console.log("New topic", newest.timestamp, url, text); state.newest = newest; - if(config.connect && state.topic != text) { - irc.topic(config.channel, text); + /* + if(state.channelTopic != text) { + irc.topic(channel, text); } -}); - -eventEmitter.on("configUpdated", function(c) { - setup(); + */ + irc.notice(channel, text); }); function setup() { console.log("Stopping " + cronJobs.length + " cron jobs"); _.each(cronJobs, function(job) { job.stop(); }); cronJobs = []; - _.each(config.feeds, function(feed) { + _.each(feeds, function(feed) { var state = { url: feed, updatingFeed: false, @@ -147,20 +138,19 @@ function setup() { }); } -function start(c) { - config = c; - startIrc(); - setup(); - if(config.connect) { - console.log('Connecting to ' + config.host + ':' + config.port); - irc.connect(config.nick); - } else { - console.log('Not connecting to IRC'); - } -} +irc.join(channel, function(c) { + irc.notice(channel, 'well hello yall: ' + c); +}); -module.exports.start = start; +irc.on('topic', function(channel, topic) { + console.log("new topic: " + topic); + /* If we're not storing this, it is possible for people to set + * the topic after the bot has set it and it will persist (until + * next update from the feed). + topic = t; + */ -module.exports.emit = function(config) { - eventEmitter.emit("configUpdated", config); -} + state.channelTopic = topic; +}); + +setup(); diff --git a/dynobot-irc.js b/dynobot-irc.js index 20f6f79..24dd2ee 100644 --- a/dynobot-irc.js +++ b/dynobot-irc.js @@ -74,7 +74,12 @@ var nick = 'dynobot'; var realName = 'bots\'r\'us'; var ident = 'ident'; -if(false) { +function info(a, b, c) { + console.log.apply(null, arguments); +} + +var reallyConnect = true; +if(reallyConnect) { irc = new IrcClient(nick, realName, ident, 'irc.freenode.net', 6667); } else { @@ -102,11 +107,12 @@ function Plugin(name, script) { } var plugins = [ - new Plugin('echo', './echo-bot/index.js') +// new Plugin('echo', './echo-bot/index.js') + new Plugin('atom', './atom-bot/index.js') ]; _.each(plugins, function(plugin) { - console.log('Starting ' + plugin.name); + info('Starting ' + plugin.name); // TODO: Resolve plugin.script // TODO: set cwd option to the plugin's directory. var args = []; @@ -117,7 +123,7 @@ _.each(plugins, function(plugin) { var channel = new Channel(child); ircService.handle(channel); child.on('exit', function() { - console.log(plugin.name + ' exited..'); + info(plugin.name + ' exited..'); ircService.release(channel); channel.close(); }); @@ -181,7 +187,7 @@ function setupRepl() { stdinPipe.send(':' + from + ' NOTICE ' + channel + ' :' + parts.join(' ')); } else { - console.log('You have to /j a channel first'); + info('You have to /j a channel first'); } break; default: @@ -196,10 +202,10 @@ function setupRepl() { } irc.join('#bitraf2', function(name) { - console.log('joined ' + name + ' from dynobot.'); + info('joined ' + name + ' from dynobot.'); }); -console.log('colors: ' + 'from bot'.red + ', ' + 'to bot'.blue); +info('colors: ' + 'from bot'.red + ', ' + 'to bot'.blue); irc.connect(); setupRepl(i); diff --git a/irc-client.js b/irc-client.js index bd8edb8..ad21a5e 100644 --- a/irc-client.js +++ b/irc-client.js @@ -43,7 +43,7 @@ IrcClient.prototype.init = function(irc) { var client = this; this.debugLevel && irc.setDebugLevel(this.debugLevel); irc.on('connected', function() { - info('Connected, channels: ' + _.size(client.channels)); + info('Connected, channels: ' + _.keys(client.channels).join(', ')); _.each(client.channels, function(channel) { info('JOINing ' + channel.name); irc.join(channel.name); @@ -54,13 +54,16 @@ IrcClient.prototype.init = function(irc) { client.connect(); }); irc.on('join', function(nick, channelName) { - _.each(client.channels, function(channel) { - info('JOINed ' + channel.name); - channel.fire(channelName); - }); + info('JOINed ' + channelName); + client.getChannel(channelName).fire(channelName); }); irc.on('privmsg', function(nick, channel, message) { - client.emit('privmsg', nick, channel, message); + client.emit.call(['privmsg'].slice.apply(arguments)); +// client.emit('privmsg', nick, channel, message); + }); + irc.on('topic', function() { + console.log('topic: ' + util.inspect(arguments)); + client.emit.call(null, ['topic'].slice.apply(arguments)); }); return irc; } @@ -92,12 +95,18 @@ IrcClient.prototype.join = function(name, cb) { c.addListener(cb); } if(this.connected) { + info('join: already connected: ' + name); c.fire(name); } } -IrcClient.prototype.notice = function(to, message) { - this.irc.notice(to, message); +// TODO: fix +function dispatch(name) { + this.irc[name].apply(this.irc, arguments); } +IrcClient.prototype.notice = function() { this.irc.notice.apply(this.irc, arguments); } +IrcClient.prototype.privmsg = function() { this.irc.privmsg.apply(this.irc, arguments); } +IrcClient.prototype.topic = function() { this.irc.topic.apply(this.irc, arguments); } + module.exports.IrcClient = IrcClient; -- cgit v1.2.3