From a55635dd4da622a82a091b13c533388e0d8a28cb Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 2 May 2012 17:24:42 +0200 Subject: o Adding last-checkin. --- lib/BitrafBot.js | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/client.js | 84 ---------------------------------------------------- main.js | 11 ++++--- 3 files changed, 96 insertions(+), 88 deletions(-) create mode 100644 lib/BitrafBot.js delete mode 100644 lib/client.js diff --git a/lib/BitrafBot.js b/lib/BitrafBot.js new file mode 100644 index 0000000..9388ddb --- /dev/null +++ b/lib/BitrafBot.js @@ -0,0 +1,89 @@ +// var IRC = require('./IRC').IRC; +var IRC = require('../node_modules/node-irc/IRC.js').IRC + , irc = new IRC('irc.freenode.net', 6667) + , url = require('url') + , http = require('http') + , _ = require('underscore'); + +var BitrafBot = function(config) { + var nick = ''; + var nickCount = 0; + var nickAdditions = [ '', '^', '-', '_', '\\', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ]; + + function findNick() { + if (nickCount == nickAdditions.length) { + nickCount = 0; + } + + return config.nick + nickAdditions[nickCount++]; + } + nick = findNick(); + + irc.on('connected', function(server) { + console.log('connected to ' + server); + irc.join(config.channel, function(error) { + irc.privmsg(config.channel, 'well hello yall'); + }); + }); + irc.on('privmsg', function(from, to, message) { + if(message == ',checkins') { + var checkinsUrl = config.checkinsUrl + console.log('checkins request. url=' + checkinsUrl); + + var options = url.parse(checkinsUrl); + options.headers = { Accept: 'application/vnd.collection+json' }; + options.method = 'GET'; + var req = http.get(options, function(res) { + var chunks = ''; + res.on('data', function (chunk) { + chunks += chunk; + }); + res.on('end', function () { + try { + data = JSON.parse(chunks); + var checkins = -1; + var lastCheckin = 'unknown'; + _.each(data['collection'].items, function(item, i) { + _.each(item.data, function(item, i) { + switch(item.name) { + case 'checkins': checkins = parseInt(item.value); break; + case 'last-checkin': lastCheckin = item.value; break; + } + }); + }); + if(checkins != -1) { + irc.privmsg(to, 'checkins: ' + checkins + ', last checkin: ' + lastCheckin); + } + else { + irc.privmsg(to, 'Could not find any checkins'); + } + } + catch(ex) { + irc.privmsg(to, 'Could not parse document'); + } + }); + }).on('error', function(e) { + irc.privmsg(to, 'Problem with request: ' + e.message); + }); + } + }); + irc.on('ping', function(from) { + console.log('ping from ' + from); + irc.ping(from); + }); + irc.on('ping-reply', function(from, ms) { + console.log('ping reply from ' + from + ': ' + ms + ' ms'); + }); + irc.on('errorcode', function(code) { + if (code == 'ERR_NICKNAMEINUSE') { + nick = findNick(); + irc.nick(nick); + } + }); + irc.connect(nick, 'The useful Bitraf bot', nick); + process.on('exit', function () { + irc.quit('bye'); + }); +} + +module.exports = BitrafBot; diff --git a/lib/client.js b/lib/client.js deleted file mode 100644 index adbc25a..0000000 --- a/lib/client.js +++ /dev/null @@ -1,84 +0,0 @@ -// var IRC = require('./IRC').IRC; -var IRC = require('../node_modules/node-irc/IRC.js').IRC - , irc = new IRC('irc.freenode.net', 6667) - , url = require('url') - , http = require('http') - , _ = require('underscore'); - -var BitrafBot = function(config) { - var nick = ''; - var nickCount = 0; - var nickAdditions = [ '', '^', '_', '\\', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ]; - - function findNick() { - if (nickCount == nickAdditions.length) { - nickCount = 0; - } - - return config.nick + nickAdditions[nickCount++]; - } - nick = findNick(); - - irc.on('connected', function(server) { - console.log('connected to ' + server); - irc.join(config.channel, function(error) { - irc.privmsg(config.channel, 'well hello yall'); - }); - }); - irc.on('privmsg', function(from, to, message) { - if(message == ',checkins') { - console.log('checkins request.'); - var now = new Date(); - var from = (now.getYear() + 1900) + "-" + (now.getMonth() + 1) + "-" + now.getDate(); - - var options = url.parse(config.checkinsUrl + '?from=' + from); - options.headers = { Accept: 'application/vnd.collection+json' }; - options.method = 'GET'; - var req = http.get(options, function(res) { - var chunks = ""; - res.on('data', function (chunk) { - chunks += chunk; - }); - res.on('end', function () { - // console.log("chunks", chunks); - data = JSON.parse(chunks); - var checkins = -1; - _.each(data["collection"].items, function(item, i) { - _.each(item.data, function(item, i) { - if(item.name === "checkins") { - checkins = parseInt(item.value); - } - }); - }); - if(checkins != -1) { - irc.privmsg(to, 'checkins: ' + checkins); - } - else { - irc.privmsg(to, 'Could not find any checkins'); - } - }); - }).on('error', function(e) { - irc.privmsg(to, 'Problem with request: ' + e.message); - }); - } - }); - irc.on('ping', function(from) { - console.log('ping from ' + from); - irc.ping(from); - }); - irc.on('ping-reply', function(from, ms) { - console.log('ping reply from ' + from + ': ' + ms + ' ms'); - }); - irc.on('errorcode', function(code) { - if (code == 'ERR_NICKNAMEINUSE') { - nick = findNick(); - irc.nick(nick); - } - }); - irc.connect(nick, 'The useful Bitraf bot', nick); - process.on('exit', function () { - irc.quit('bye'); - }); -} - -module.exports = BitrafBot; diff --git a/main.js b/main.js index 6774bae..105543d 100644 --- a/main.js +++ b/main.js @@ -12,17 +12,20 @@ parser.option("--checkinsUrl", "URL to use when looking for checkins"); var args = process.argv.slice(2); var defaults = { channel: "#bitraf", - checkinsUrl: 'http://hermes.bitraf.no/~trygvis/checkins.php', + checkinsUrl: 'http://hermes.bitraf.no/~trygvis/checkins/checkins-by-day.php?from=today', nick: os.hostname() }; try { var config = parser.parse(args); + if (config.help) { + console.log(parser.help()); + return; + } config = _.defaults(config, defaults); - console.log("config", config); - if (config.help) console.log(parser.help()); - var bot = require('./lib/client.js')(config); + console.log("configuration: ", config); + var bot = require('./lib/BitrafBot.js')(config); var r = repl.start('irc> '); r.context.bot = bot; r.context.config = config; -- cgit v1.2.3