var irc = require('irc');
var api = require('./services/lib/api');
var config = api.load_config('bot.json');
var pub = api.get_redis_client(config.redis);
var sub = api.get_redis_client(config.redis);

function server_config_for(idx) {
    var cfg = config.servers[idx];
    /* server-generic options */
    cfg.debug = config.options.debug;
    cfg.floodProtection = config.options.floodProtection;
    cfg.showErrors = config.options.debug;
    cfg.stripColors = config.options.stripColors;
    return cfg;
}

function output_version_1(bot, message) {
    switch (message.type) {
    case 'privmsg':
        console.log('  privmsg');
        bot.say(message.data.to, message.data.message);
        break;
    case 'raw':
        console.log('  raw');
        bot.send(message.command);
        break;
    }
}

function setup() {
    var cfg = server_config_for(0);
    console.log('irc server: '+cfg.hostname+' nick: '+cfg.nick);
    var bot = new irc.Client(cfg.hostname, cfg.nick, cfg);

    bot.addListener('message', function(nick, to, text, message) {
        console.log(nick + ' said ' + text + ' to ' + to);
        var msg = {
            version: 1,
            type: 'privmsg',
            data: {
                sender: nick,
                channel: to,
                message: text,
            },
        };
        pub.publish('in', JSON.stringify(msg));
    });

    bot.addListener('bot_nick_change', function(oldNick, newNick) {
        pub.set('zenircbot:nick', newNick);
    });

    bot.addListener('part', function(channel, nick, reason, message) {
        console.log(nick + ' left ' + channel + ' because ' + reason);
        var msg = {
            version: 1,
            type: 'part',
            data: {
                sender: nick,
                channel: channel,
            },
        };
        pub.publish('in', JSON.stringify(msg));
    });

    bot.addListener('quit', function(nick, reason, channels, message) {
        console.log(nick + ' quit');
        var msg = {
            version: 1,
            type: 'quit',
            data: {
                sender: nick,
            },
        };
        pub.publish('in', JSON.stringify(msg));
    });

    var output_handlers = {
        1: output_version_1,
    };

    sub.subscribe('out');
    sub.on('message', function(channel, message) {
        var msg = JSON.parse(message);
        output_handlers[msg.version](bot, msg);
    });
}

setup();

@@ -984,16 +984,56 @@
 , cfg);%0A
+    pub.set('zenircbot:nick', cfg.nick);
 %0A    bot
@@ -1412,32 +1412,35 @@
 ));%0A    %7D);%0A%0A   
+ //
  bot.addListener
@@ -1487,24 +1487,27 @@
 wNick) %7B%0A   
+ //
      pub.set
@@ -1534,24 +1534,27 @@
 ewNick);%0A   
+ //
  %7D);%0A%0A    bo

send pull request to upstream before commiting