diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2012-05-11 14:12:55 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-05-11 14:12:55 +0200 |
commit | 0448390173ed3feb3795b38496f0c0fb59adeb7b (patch) | |
tree | 43a335b25a4c1c5b00cc16c1ec059758b85d3e53 /lib | |
parent | 3a7e65ca638bfcd615dcd24d4ee800596621be8b (diff) | |
download | bitraf-bot-0448390173ed3feb3795b38496f0c0fb59adeb7b.tar.gz bitraf-bot-0448390173ed3feb3795b38496f0c0fb59adeb7b.tar.bz2 bitraf-bot-0448390173ed3feb3795b38496f0c0fb59adeb7b.tar.xz bitraf-bot-0448390173ed3feb3795b38496f0c0fb59adeb7b.zip |
o Adding ,who.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/BitrafBot.js | 74 |
1 files changed, 69 insertions, 5 deletions
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); |