Using irssi with screen

Like a lot of other geeks, I’m still somewhat active on IRC. It may be handy to stay online all the time, just in case something interesting passes by when you’re not around. That’s why I installed ZNC on one of my servers.
ZNC is an IRC bouncer, that stays connected to IRC, so when an IRC client disconnects/reconnects, you don’t lose your chat session. As IRC client, I’m found of irssi, which is a terminal based IRC client. So every time I connect from my desktop or laptop, I would just connect to ZNC where I could review previous conversations if they happened while I was gone.

As I used this setup for several years, it’s kind of overkill, since I’m the only one connecting to it. And since I use irssi, I could simplify everything, by just installing irssi on my server and run it in a screen session. This way, I can just detach the screen when I log off so I can login again and reattach it later.

Installation

So first of all, let’s make sure everything is installed properly. Since I’m running Ubuntu on my server, this is pretty easy through aptitude.

sudo apt-get install irssi irssi-scripts screen

Now to make things go smoothly, we want to connect from a remote machine, onto our server, creating a screen session which starts irssi. In case there is already a screen session active, we want of course to attach the existing screen so we don’t end up with multiple new sessions running at the same time and hence lose the ability to review conversations when we are away.

So we need to run irssi as a user we can ssh into, preferably with keys. I won’t explain this here, since I hope you already work this way by disabling password logins through ssh and only allow pub-private key authentication.

Screen

The first thing we need to do is make sure you have the following script available to start a new screen session, or attach an existing screen when you want to connect to IRC. Make sure this script is executable by the user that will be connecting to the machine. I have chosen to store the script in my irssi config folder ( ~/.irssi/screen ) :

#!/bin/bash
# Thx to Serge van Ginderachter for sharing this script :p
if screen -list | grep "\.irssi" > /dev/null
then
  screen -Rx irssi
else
  screen -S irssi -t irssi irssi
fi
exit 0

 

So what does this script do? When you execute it, it will check if there is already a screen session running with the name irssi. If such a session exist, it will attempt to resume the first detached screen session it finds and attach to a not detached screen session. If no session session is found, it will try to start irssi in a new session called irssi with the title irssi.

Connecting

Now of course, we want to connect from our local machines. You might be wondering how we can just connect to irssi as simply as possible, without having to login and attach to the session manually all the time. This is fairly simple by applying some bash magic. Simply add a command alias to do everything for you. So add the following line to your .bashrc file on your local system. You can also have a look at my personal dot files, containing different configuration settings.

If you want to use the irssi alias, make sure irssi is no longer installed on your local system. Otherwise, you might get some strange results.

alias irssi='ssh remoteserver -t .irssi/screen'

Don’t forget to reload your bash settings after you added this line, or just close and restart terminal.

I created a new alias called irssi. From now on, whenever I type irssi in my prompt and hit enter, it will ssh into my remote server and execute the remote screen script. This way, I’ll get automatically connected to the right screen if one exists and be connected to IRC.

Now add this alias to all your workstations, and you’re good to go.

Disconnecting

Now you don’t want to exit irssi the normal way, I you will get disconnected from IRC. Instead, detach the screen by pressing ctrl+a followed by d ( to detach ) .