Did you know that IRC, an application protocol created in 1988, is still quite alive and well? Despite not having the gazilion users it used to have, plenty of people still use it everyday - many comunities keep it as a central place to gather, and twitch.tv uses it to power all of its site’s chats.

An integral part of IRC chatting are the bots that do stuff for you so you don’t need to leave that awesome text-only interface. So why not set up your own, and mix the old-style IRC with the new-era Node.JS and Docker?

First things first: You’ll need Node.js installed and in your path. I’m also running nodeclipse to make everything easier.

Having done that, start your node project and open package.json for editing. We’ll use node-irc (docs) and irc-colors, so set them up as dependencies:

{
  "name": "madrugabot",
    "version": "0.1.0",
    "private": true,
    "description": "A simple ircbot for eRepublik",
    "readmeFilename": "README.md",
    "author": "Marcelo <[email protected]>",
  "dependencies": {
    "irc": "0.3.x",
          "irc-colors": "1.1.x"
    }
}

And then run npm install to download.

ProTip: installing nodeclipse lets you start a terminal window inside eclipse by pressing Ctrl+Alt+T

We’ll be using the dockerfile/nodejs-runtime image, and it assumes a server.js file as entrypoint - create/rename your current file to match that. Using the library itself is quite simple if you are already familiar with asynchronous languages:

var irc = require('irc'); 
var madruga = require('./madruga'); 
    
//those are environmental variables set when starting the docker container
var client = new irc.Client(process.env.MADRUGA_HOST || 'irc.rizon.net', 
                            process.env.MADRUGA_USER || 'Madruga', {
                              channels: process.env.MADRUGA_CHANNELS ? 
                                process.env.MADRUGA_CHANNELS.split(' ') : '',
                           });
    
var identify = function(message) {
  if(process.env.MADRUGA_PASS) {
    client.say('nickserv', 'IDENTIFY ' + process.env.MADRUGA_PASS);
  }
};

var onError = function(message) {
  console.log('error: ', JSON.stringify(message));
};
    
var onMessage = function (from, to, message) {
  madruga.onMessage(from, to, message, client);
}; 
    
client.addListener('registered', identify);
client.addListener('error', onError);
client.addListener('message#', onMessage);

Just because you are running javascript does not mean you can be lazy about code organizing, so let’s create a madruga.js file to do all the command parsing and stuff.

In this case we want all our commands to start with a .; They can make the bot say a simple phrase or can contain parameters to run more complex methods.

module.exports = {
  onMessage: function (from, to, text, client) {
    if(text && /^.[a-z]+/.test(text)) {
      return parseMessage(from, to, text, client);  
    }
  } 
};
    
var parseMessage = function (from, to, text, client) {
  var parsed = /^.([a-z]+)s?(.*)/.exec(text);
  if(!parsed) { return; }
  var command = parsed[1];
  var param = parsed[2];
  if(phrases[command]) {
    client.say(to, phrases[command]);
  }
  if(cmd[command]) {
    cmd[command](from, to, param, client);  
  } 
};
    
var phrases = {
  madruga: 'A vingança nunca é plena, mata a alma e a envenena'
};
    
var cmd = {
  up: function(from, to, param, client) {
      var uptimeHours = process.uptime() / 3600;
      client.say(to, 'I've been online for ~' + uptimeHours + 'h');
  }
};

That finishes the code, but you need one more step to link it with your docker image. Create a Dockerfile text file on your root:

FROM dockerfile/nodejs-runtime

Yes, that simple - the parent image uses the ONBUILD command, so everything else that needs to be done is already defined.

Commit it to your favorite git provider (I’ve uploaded MadrugaBot to BitBucket), because we’ll use the autobuilds offered by the official Docker Registry. After setting up your account and linking it to BitBucket/GitHub, go to your repositories and add a new Automated Build. Link it with your bot’s project, and wait - this may take a couple of minutes if their server is too busy.

Once complete, just ssh into your server and things could not be simpler:

docker run -d 
  --name madruga 
  --net=host 
  marcelofs/madrugabot

Every time you commit changes to your git repository, Docker’s registry will re-generate the image with your new code. To make sure you have the latest version before restarting your container, just use docker pull marcelofs/madrugabot!