From 0448390173ed3feb3795b38496f0c0fb59adeb7b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 11 May 2012 14:12:55 +0200 Subject: o Adding ,who. --- lib/BitrafBot.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- main.js | 2 ++ package.json | 3 ++- test/who.test.js | 31 ++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 test/who.test.js diff --git a/lib/BitrafBot.js b/lib/BitrafBot.js index 92bb8d9..b40f62e 100644 --- a/lib/BitrafBot.js +++ b/lib/BitrafBot.js @@ -1,18 +1,40 @@ // var IRC = require('./IRC').IRC; var IRC = require('../node_modules/node-irc/IRC.js').IRC + , collection_json = require('collection_json') + , http = require('http') , irc = new IRC('irc.freenode.net', 6667) , url = require('url') - , http = require('http') - , collection_json = require('collection_json') , _ = require('underscore'); +require('date'); + +function twoDigit(i) { + if(i > 10) { + return "" + i; + } + return "0" + i; +} + +function join(array, f) { + var memo = undefined; + _.each(array, function(item) { + if(memo == undefined) { + memo = item; + } + else { + memo = f(memo, item); + } + }); + + return memo; +} function onCheckinsEnd(chunks) { var collection = collection_json.fromString(chunks); -// console.log("collection.items", collection.items); +// console.log('collection.items', collection.items); var checkins = -1, lastCheckin; collection.mapItemData(function(data) { - checkins = parseInt(data["checkins"]); - lastCheckin = data["last-checkin"]; + checkins = parseInt(data['checkins']); + lastCheckin = data['last-checkin']; }); if(checkins != -1) { return 'checkins: ' + checkins + ', last checkin: ' + (typeof lastCheckin === 'undefined' ? 'unknown' : lastCheckin); @@ -23,6 +45,28 @@ function onCheckinsEnd(chunks) { } module.exports.onCheckinsEnd = onCheckinsEnd; +function onWhoEnd(chunks) { + var collection = collection_json.fromString(chunks); +// console.log('collection.items', collection.items); + var result = {}; + collection.mapItemData(function(data) { + if(typeof(result[data['account']]) === 'undefined') { + var date = Date.ISO(data['date']) + var d = twoDigit(date.getHours()) + ":" + twoDigit(date.getMinutes()); + result[data['account']] = data['account'] + ' (' + d + ')'; + } + }); + if(_.size(result) > 0) { + result = 'Checkins: ' + join(result, function(memo, num) { return memo + ', ' + num }); + console.log('result', result); + return result; + } + else { + return 'Could not find any checkins'; + } +} +module.exports.onWhoEnd = onWhoEnd; + function BitrafBot(config) { var nick = ''; var nickCount = 0; @@ -64,6 +108,26 @@ function BitrafBot(config) { irc.privmsg(to, 'Problem with request: ' + e.message); }); } + else if(message == ',who') { + var whoUrl = config.whoUrl + console.log('who request. url=' + whoUrl); + + var options = url.parse(whoUrl); + 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 () { + var s = onWhoEnd(chunks); + irc.privmsg(to, s); + }); + }).on('error', function(e) { + irc.privmsg(to, 'Problem with request: ' + e.message); + }); + } }); irc.on('ping', function(from) { console.log('ping from ' + from); diff --git a/main.js b/main.js index 6f6e620..50e19f5 100644 --- a/main.js +++ b/main.js @@ -8,11 +8,13 @@ parser.option("-h, --help", "show help"); parser.option("-c, --channel=CHANNEL", "IRC channel to join"); parser.option("-n, --nick=NICK", "Nickname of the bot"); parser.option("--checkinsUrl", "URL to use when looking for checkins"); +parser.option("--whoUrl", "URL to use when looking for who's present"); var args = process.argv.slice(2); var defaults = { channel: "#bitraf", checkinsUrl: 'http://hermes.bitraf.no/~trygvis/checkins/checkins-by-day.php?from=today', + whoUrl: 'http://hermes.bitraf.no/~trygvis/checkins/checkins.php?from=24%20hours', nick: os.hostname() }; diff --git a/package.json b/package.json index 498c473..19948da 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "dependencies": { "node-irc": "https://github.com/einaros/node-irc/tarball/master", "underscore": "1.3.3", - "cmdopt": "0.2.0" + "cmdopt": "0.2.0", + "date": "1.0.2" }, "devDependencies": {}, "optionalDependencies": {}, diff --git a/test/who.test.js b/test/who.test.js new file mode 100644 index 0000000..27a8c70 --- /dev/null +++ b/test/who.test.js @@ -0,0 +1,31 @@ +var BitrafBot = require('../lib/BitrafBot.js') + , assert = require('assert'); + +var json = JSON.stringify({ "collection" : + { + "version" : "1.0", + links:[ + {rel: "self", href: "http://hermes.bitraf.no/~trygvis/checkins/checkins.php?from=yesterday&count=0"} + ], + "items" : [ +{ data: [ {"name" : "date", "value" : "2012-05-11T08:55:00+02:00"}, {"name" : "account", "value" : "brokenpoly"} ], } +,{ data: [ {"name" : "date", "value" : "2012-05-10T20:41:35+02:00"}, {"name" : "account", "value" : "Flemm"} ], } +,{ data: [ {"name" : "date", "value" : "2012-05-10T20:29:40+02:00"}, {"name" : "account", "value" : "ccscanf"} ], } +,{ data: [ {"name" : "date", "value" : "2012-05-10T19:57:03+02:00"}, {"name" : "account", "value" : "mortehu"} ], } +,{ data: [ {"name" : "date", "value" : "2012-05-10T19:19:03+02:00"}, {"name" : "account", "value" : "haavares"} ], } +,{ data: [ {"name" : "date", "value" : "2012-05-10T18:53:39+02:00"}, {"name" : "account", "value" : "ccscanf"} ], } +,{ data: [ {"name" : "date", "value" : "2012-05-10T18:36:13+02:00"}, {"name" : "account", "value" : "trygvis"} ], } +,{ data: [ {"name" : "date", "value" : "2012-05-10T15:34:22+02:00"}, {"name" : "account", "value" : "mortehu"} ], } +,{ data: [ {"name" : "date", "value" : "2012-05-10T13:04:14+02:00"}, {"name" : "account", "value" : "brokenpoly"} ], } +,{ data: [ {"name" : "date", "value" : "2012-05-10T13:01:13+02:00"}, {"name" : "account", "value" : "odinho"} ], } +,{ data: [ {"name" : "date", "value" : "2012-05-10T11:10:38+02:00"}, {"name" : "account", "value" : "mortehu"} ], } + ], + "count": 11 } +}); + +describe('Who', function() { + it('can parse who', function() { + var str = BitrafBot.onWhoEnd(json); + assert.equal('Checkins: brokenpoly (08:55), Flemm (20:41), ccscanf (20:29), mortehu (19:57), haavares (19:19), trygvis (18:36), odinho (13:01)', str); + }); +}); -- cgit v1.2.3