Running WeeChat on a Server for IRC Backlogs

Calle Erlandsson

In a previous blog post we walked you through setting up and configuring the text based chat client WeeChat for use with the Slack IRC gateway.

The blog post also explained how to set up Slacklog - a WeeChat script for retrieving backlog from the Slack API. This works great, but if you want more backlog or want backlog for channels on other IRC servers a good solution is to run WeeChat on a server. By doing this you won’t miss anything that happens in the channels you are connected to, even when you’re not at your computer or connected to the Internet. When you reconnect you can go through messages that were meant for you or that you’re interested in.

This tutorial assumes that you have a server running Debian 7.0 (Wheezy) that you can access over SSH using an account with sudo privileges. If you’re using a different setup you may have to alter the instructions. If you don’t have a server but would like to set one up, I recently wrote a blog post explaining how to set up a basic Debian server.

Setting up WeeChat

Connect to your server using ssh:

ssh YOURIP

To get the latest version of WeeChat you have to install it from the Wheezy backports. If you haven’t already, add the Wheezy backports to your /etc/apt/sources.list file:

sudo sh -c 'echo deb http://http.debian.net/debian wheezy-backports main >> /etc/apt/sources.list'

Update your package index and install WeeChat using apt-get:

sudo apt-get update
sudo apt-get -t wheezy-backports install weechat-curses

You can now start WeeChat using the weechat-curses command:

weechat-curses

To exit type /quit and press Enter.

If you disconnect from your server by typing Ctrl+D, you can now reconnect to the server and run weechat-curses by running this command locally:

ssh YOURIP -t weechat-curses

When you pass a command to ssh it will run the command on the remote machine instead of running your normal shell. When it does so, it will also skip the allocation of a pseudo-tty. This works fine for one-off commands with no output, but since WeeChat is an interactive program, it needs a tty to connect to. The -t flag in the above command forces the allocation of a pseudo-tty.

When you exit out of WeeChat, ssh will automatically disconnect from the server.

Great! We can now connect to and run WeeChat on our server in one command. One problem with this approach is that WeeChat won’t continue to run after you disconnect. This means that you will miss anything that happens in the channels you were connected to since you’re simply no longer connected. Always being connected means that you will have access to the full backlog.

To fix this we will have to run WeeChat using a terminal multiplexer (like GNU Screen or tmux) that has support for running programs in background sessions that can be detached from and reattached to.

Setting up GNU Screen

We will use GNU Screen in this tutorial mainly because a lot of us here at thoughtbot use tmux locally and nesting tmux sessions can cause problems. If you’re not using tmux locally, using tmux on your server is perfectly fine.

Reconnect to your server and install GNU Screen using apt-get:

ssh YOURIP
sudo apt-get install screen

If you disconnect from your server you can now reconnect and run weechat-curses in a screen session using this command:

ssh YOURIP -t screen -D -RR weechat weechat-curses

This command will log in to your server and look for a screen session named “weechat”. If a session with that name exists, it will reattach to it. If not it will create a new session and run the weechat-curses command inside of it.

To disconnect from the server without quitting WeeChat press Ctrl+A and then D.

Awesome! We can now enjoy the upside of always being online even when we’re not.

There is still one problem though. When you’re done for the day, close your laptop and head home, your SSH connection will eventually time out. When you get home and open up your laptop, the terminal running the ssh command will be frozen and the only way to quit it will be by pressing ~ and .. To continue chatting you’ll then have to reconnect. This will also happen if you suddenly lose your Internet connection for a few minutes and it can be really annoying.

Wouldn’t it be simpler if you were reconnected automatically as soon as you were reconnected to the Internet?

Setting up Mosh

Mosh stands for “mobile shell” and is a remote terminal application that allows roaming and intermittent connectivity. In our case mosh will replace ssh. Under the hood Mosh still uses ssh to log in to the server to then start a mosh-server that you can communicate with over UDP.

Mosh connections will not freeze after losing the Internet connection but will instead show a timer that tells how long ago it last got a response from the server. As soon as you come online it will reconnect and you can continue chatting.

Install Mosh locally using your favorite package manager.

On Debian:

sudo apt-get install mosh

On Arch Linux:

sudo pacman -S mosh

On Mac OS X (using Homebrew):

brew install mobile-shell

Installation instructions for other systems can be found on the Mosh website.

Reconnect to your server and install Mosh using apt-get:

ssh YOURIP
sudo apt-get install mosh

By default, mosh-server binds to a UDP port between 60000 and 61000. If you’re running a firewall, you have to open these ports to be able to connect. If you are using UFW to manage your firewall you can run this command to open the ports:

sudo ufw allow 60000:61000/udp

If you disconnect from your server, you can now reconnect to and attach to your WeeChat screen session using mosh:

mosh YOURIP -- screen -D -RR weechat weechat-curses

There we go! All set up.

Connecting to servers and joining channels

You can now connect to an IRC server using WeeChat:

/connect chat.freenode.net

Once connected you can join your favorite channel:

/join #ruby

For more information on how to use WeeChat check out WeeChat for Slack’s IRC Gateway.