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 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 5 deletions(-) (limited to 'lib') 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); -- cgit v1.2.3