// 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') , _ = 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); var checkins = -1, lastCheckin; collection.mapItemData(function(data) { checkins = parseInt(data['checkins']); lastCheckin = data['last-checkin']; }); if(checkins != -1) { return 'checkins: ' + checkins + ', last checkin: ' + (typeof lastCheckin === 'undefined' ? 'unknown' : lastCheckin); } else { return 'Could not find any checkins'; } } 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; 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.notice(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 () { var s = onCheckinsEnd(chunks); irc.notice(to, s); }); }).on('error', function(e) { irc.notice(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.notice(to, s); }); }).on('error', function(e) { irc.notice(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 = BitrafBot;