Skip to content

Instantly share code, notes, and snippets.

@nhandler
Last active April 17, 2019 15:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhandler/993ba4c9d060601d12d7 to your computer and use it in GitHub Desktop.
Save nhandler/993ba4c9d060601d12d7 to your computer and use it in GitHub Desktop.
Forward Irssi Notifications from Remote Server

notify-remote

Forward Irssi Notifications from Remote Server

Description

It is very common to run Irssi on a remote server in screen or tmux via ssh. One challenge can be getting popup notifications when you receive a hilight. Most other solutions either require the user to manually run a script each time they connect to Irssi or they stop working when Irssi is left running for extended periods of time. This approach attempts to solve those problems.

How It Works

When you run SSH to connect to the remote server, socat automatically opens up a TCP tunnel (Port 12000 by default) between your computer and the remote server. Any data that is received from the remote server is sent to the ~/bin/notify-remote shell script. This script is responsible for displaying the actual notifications.

On the remote server, the notifyfwd.pl Irssi script takes any private message or hilight and sends it over the TCP tunnel.

Setup

Take the stanza in ssh_config and put it in your ~/.ssh/config file. You will need to edit the values to contain the correct hostname, username, and SSH key to connect to the remote server.

Save notify-remote to the ~/bin directory and make it executable. Technically, this script can go anywhere on your system, you just need to update ~/.ssh/config to contain the correct path.

Next, save notifyfwd.pl in the ~/.irssi/scripts directory on the remote server. You might want to add a symlink to this script from ~/.irssi/scripts/autorun so it gets loaded automatically when Irssi starts. Finally, load the script with /script load notifyfwd. Assuming you have notify-send or terminal-notifier setup on your system, you should now start to see popup notifications when users hilight or private message you.

#!/bin/sh
delay="5000"
read line
summary="$line"
read line
msg="$line"
read line
if [ "$line" = "" ] && [ "$summary" != "" ] ; then
if [ -x "$(which notify-send)" ] ; then
notify-send -i notification-message-im -u critical -t "$delay" -- "$summary" "$msg"
elif [ -x "$(which terminal-notifier)" ] ; then
terminal-notifier -message "$msg" -title "$summary"
fi
fi
#!/usr/bin/perl
use strict;
use vars qw($VERSION %IRSSI);
use IO::Socket::INET;
use Irssi;
$VERSION = '0.0.1';
%IRSSI = (
authors => 'Nathan Handler',
contact => 'nathan.handler@gmail.com',
name => 'notifyfwd',
description => 'Forward messages via TCP',
);
sub priv_msg {
my ($server,$msg,$nick,$address,$target) = @_;
notify($nick, $msg);
}
sub hilight {
my ($dest, $text, $stripped) = @_;
if ($dest->{level} & MSGLEVEL_HILIGHT) {
my $target = $dest->{target};
if ($target =~ m/^\#/) {
my($nick,$message) = split(/> /, $stripped, 2);
$nick =~ s/[\s<>\+\@\&\%\?]//g;
notify($target,"<".$nick."> $message");
}
}
}
sub notify {
my ($title,$message) = @_;
my $port = Irssi::settings_get_int($IRSSI{'name'} . '_port');
my $socket = new IO::Socket::INET (
PeerHost => '127.0.0.1',
PeerPort => $port,
Proto => 'tcp',
);
my $req = $title . "\n" . $message . "\n\n";
$socket->send($req);
shutdown($socket, 1);
$socket->close();
}
Irssi::signal_add_last("message private", "priv_msg");
Irssi::signal_add_last("print text", "hilight");
Irssi::settings_add_int('misc', $IRSSI{'name'} . '_port', 12000);
Host irssi
HostName remote.server.tld
User nhandler
IdentityFile ~/.ssh/id_rsa
RemoteForward 12000 localhost:12000
PermitLocalCommand yes
LocalCommand socat -u tcp4-listen:12000,reuseaddr,fork,bind=127.0.0.1 exec:$HOME/bin/notify-remote 2>/dev/null &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment