Skip to content

Instantly share code, notes, and snippets.

Created May 14, 2010 15:51
Show Gist options
  • Select an option

  • Save anonymous/401322 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/401322 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist May 14, 2010.
    66 changes: 66 additions & 0 deletions botthingy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    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);