Skip to content

Instantly share code, notes, and snippets.

Created May 14, 2010 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/401322 to your computer and use it in GitHub Desktop.
Save anonymous/401322 to your computer and use it in GitHub Desktop.
require.paths.unshift(__dirname + '/../lib');
var sys = require('sys');
var irc = require('irc');
var http = require('http');
var fs = require('fs');
var bot = new irc.Client('irc.freenode.net', 'iShouldChangeBotName', {
channels: ['#node.js']
});
var users = {};
fs.readFile('cache.json', function (err, data) {
if (err) {
throw err;
}
users = eval('(' + data.toString() + ')');
setInterval(function () {
fs.writeFile('cache.json', JSON.stringify(users), function (err) {});
}, 10000);
});
bot.addListener('message', function (from, to, message) {
if (!users[from]) {
users[from] = {};
}
var userKeys = Object.keys(users).sort().reverse();
var userMatch = new RegExp('^(' + (userKeys.join('|') || 'noDefaultMatch') + ')');
if (message.indexOf('!register') !== -1) {
var mail = message.replace('!register ', '').trim();
users[from].mail = mail;
bot.send('PRIVMSG', to, 'Registered: ' + mail);
}
if (message.match(userMatch)) {
var matches = message.match(userMatch);
if (!users[from].connections) {
users[from].connections = {};
}
if (!users[from].connections[matches[0]]) {
users[from].connections[matches[0]] = {
'lastMention': new Date().getTime(),
'mentions': 0
};
}
else {
users[from].connections[matches[0]].lastMention = new Date().getTime();
users[from].connections[matches[0]].mentions += 1;
}
}
});
http.createServer(function (req, res) {
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify(users));
}).listen(6669);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment