2010-11-09

Irssi notifications (and more)



This explains how to get desktop notifications and sound from irssi when you receive a private message, a DCC request, og someone says your nick in a channel.
Most of it was taken from:
http://jaredquinn.info/2007/09/using-irssi-with-libnotify-over-secure-shell/
I made some small modifications to get sound as well, and the settings for Xterm didn't work, but as I use urxvt anyways, I made it work for that instead.


Two scripts need to be added to your system, and you need to add a line to ~/.Xdefaults :

### Script ~/.irssi/scripts/libnotify.pl ###

## Put me in ~/.irssi/scripts, and then execute the following in irssi:
##
##       /load perl
##       /script load libnotify
##

# Obtained from: http://jaredquinn.info/wp-content/uploads/2009/11/libnotify.pl_.txt
# Needs: http://jaredquinn.info/wp-content/uploads/2009/11/notifier.sh.txt
# See: http://jaredquinn.info/2007/09/using-irssi-with-libnotify-over-secure-shell/
# Odd Eivind Ebbesen @ 2010-11-09 12:55:51

use strict;
use Irssi;
use vars qw($VERSION %IRSSI);

$VERSION = "0.01";
%IRSSI = (
    authors     => 'Jared Quinn',
    origauthors => 'Luke Macken, Paul W. Frields, ',
    contact     => 'jared@jaredquinn.info',
    name        => 'notify.pl',
    description => 'Use libnotify to alert user to hilighted messages',
    license     => 'GNU General Public License',
    url         => 'http://jaredquinn.info/irssi',
);

Irssi::settings_add_str('notify', 'notify_icon', 'gtk-dialog-info');
Irssi::settings_add_str('notify', 'notify_time', '5000');
Irssi::settings_add_str('notify', 'notify_command', 'remote-notify-send');

sub notify {
    my ($server, $summary, $message) = @_;
    # Make the message entity-safe
    $message =~ s/&/&/g; # That could have been done better.
    $message =~ s/    $message =~ s/>/>/g;
    $message =~ s/'/'/g;

    print STDERR "\033[5i";
    print STDERR "TYPE IRC\n";
    print STDERR "ICON " . Irssi::settings_get_str('notify_icon') . "\n";
    print STDERR "SHOWFOR " . Irssi::settings_get_str('notify_time') . "\n";
    print STDERR "SUBJECT " . $summary . "\n";
    print STDERR "CONTENT " . $message . "\n";
    print STDERR "\033[4i";
}

sub print_text_notify {
    my ($dest, $text, $stripped) = @_;
    my $server = $dest->{server};
    return if (!$server || !($dest->{level} & MSGLEVEL_HILIGHT));
    my $sender = $stripped;
    $sender =~ s/^\<.([^\>]+)\>.+/\1/ ;
    $stripped =~ s/^\<.[^\>]+\>.// ;
    my $summary = "Hilight in " . $dest->{target};
    notify($server, $summary, $stripped);
}

sub message_private_notify {
    my ($server, $msg, $nick, $address) = @_;
    return if (!$server);
    notify($server, "Private message from ".$nick, $msg);
}

sub dcc_request_notify {
    my ($dcc, $sendaddr) = @_;
    my $server = $dcc->{server};

    return if (!$dcc);
    notify($server, "DCC ".$dcc->{type}." request", $dcc->{nick});
}

Irssi::signal_add('print text', 'print_text_notify');
Irssi::signal_add('message private', 'message_private_notify');
Irssi::signal_add('dcc request', 'dcc_request_notify');

### END Script ~/.irssi/scripts/libnotify.pl ###

### Script ~/.irssi/scripts/notifier.sh ###

#!/bin/bash
# Obtained from: http://jaredquinn.info/wp-content/uploads/2009/11/notifier.sh.txt
# Used with: http://jaredquinn.info/wp-content/uploads/2009/11/libnotify.pl_.txt
# See: http://jaredquinn.info/2007/09/using-irssi-with-libnotify-over-secure-shell/

# Added the line with canberra-gtk-play for sound.
# Odd Eivind Ebbesen @ 2010-11-09 12:58:09

cat - | {
    nt_icon="gtk-dialog-info"
    nt_time=5000
    nt_head="Notify"
    nt_text="Error Occured"
    nt_type="Message"
    while read k v; do
        case $k in
            TYPE) nt_type=$v;;
            ICON) nt_icon=$v;;
            CONTENT) nt_text=$v;;
            TIMEOUT) nt_time=$v;;
            SUBJECT) nt_head=$v;;
        esac
    done
    notify-send -i "$nt_icon" -c "$nt_type" -t $nt_time -- "$nt_head" "$nt_text"
}
/usr/bin/canberra-gtk-play --id="message" >/dev/null 2>&1

### END Script ~/.irssi/scripts/notifier.sh ###

### Modification ~/.Xdefaults ###

urxvt*print-pipe: ~/.irssi/scripts/notifier.sh

### END Modification ~/.Xdefaults ###



The above is all that is needed to make this work on a single machine. As I use several machines all the time, I've made my setup partially mirrored over machines using Dropbox and symlinks, so I have most settings for apps (like irssi), and utility scripts in the same location on all machines, and they're synced at all times.

The files/dirs involved for this setup to be perfect for me, are:
~/Dropbox/bin/oscr.sh
~/.irssi -> Dropbox/dotfiles/.irssi.odd-t500
~/.ssh/config -> ../Dropbox/dotfiles/.ssh_config.odd-t500
~/.Xdefaults -> Dropbox/dotfiles/.Xdefaults.odd-t500
~/.zsh_aliases -> Dropbox/dotfiles/.zsh_aliases.odd-eee
oscr.sh is a small shell script that simplifies creation/reattaching of
screens:
### Script oscr.sh ###

#!/bin/bash
# oscr.sh ("Odds screen")
# Some wrappers for running screen quicker.
#
# Odd Eivind Ebbesen, 2010-11-08 20:16:43

sessions=$(screen -ls | tail -n +2 | head -n -2 | awk '{print $1}')
hostname=$(hostname)
cmd=
session_name=
session_default=
p_rs="-dRAa"
p_cr="-aAUOS"

for s in $sessions; do
    if [[ $s =~ $hostname ]]; then
        session_default="$s"
    fi
    if [[ -n $1 && $s =~ "$1" ]]; then
        session_name="$s"
    fi
done

if [[ -n $session_name ]]; then
    cmd="screen $p_rs $session_name"
elif [[ -z $session_name && -n $1 ]]; then
    cmd="screen $p_cr $1"
elif [[ -z $1 && -n $session_default ]]; then
    cmd="screen $p_rs $session_default"
elif [[ -z $1 && -z $session_default ]]; then
    cmd="screen $p_cr $hostname"
fi

$cmd

### END Script oscr.sh ###

I have setup host aliases and options in ~/.ssh/config, and in ~/.zsh_aliases I have aliased oscr="~/Dropbox/bin/oscr.sh ". This means I can now on my main machine start a screen dedicated to irssi with $oscr irssi and then $irssi inside screen (I know I could have automated even more). I then use irc, MSN & Jabber (via Minbif), and Twitter (via Twirssi) all in irssi, with popups and sound. If I detach screen, I'm automatically set as away (screen_away.pl), and I don't get any notifications (which is reversed back if I reattach). If I
then move away (to the couch or the other side of the world, for that matter..), I just type in from my netbook: $ssh odd-t500 and $oscr ir (oscr matches "ir" with the screen session name "irssi" set earlier), and I'm back to where I was, getting notifications and sound on my netbook instead.

Happy chatting!

2010-05-26

Ubuntu service management

Once upon a time, service management in Ubuntu, like other Debian derivates, was easy. Now, in the days of 10.04 recently released, I find it confusing to the point where I felt the need to write down some notes on the subject.

I'm a control freak, but as the years fly by, I'm also getting increasingly lazy and obsessed with getting things done efficiently, and not manually by hand for stuff I do often. I started off with Slackware and Gentoo, but during the latest years working with VoIP, I'ved used Ubuntu more and more of the time. So, I've learned to love Ubuntu for how easy it is, and how quickly I can go from installing it on a machine to working efficiently with it, but also hate what Ubuntu does different from more manual distros like Arch, and the two aforementioned, when it comes to easily deciding for yourself what to enable or disable.

As an example; I do some web development now and then, but it's far from often. So I want MySQL, Apache2 and PHP installed, but I don't want MySQL and Apache2 to start other than if I choose to start them manually myself.
As Ubuntu sets them to start automatically at boot, in a different way for each of them, you also need to do it in different ways for both of them in order to disable them.

First look in /etc/init.d/ to see the installed services and their init-scripts. You should see that some of them are scripts, and some are symlinks. Those that are actual files/scripts, are the old-style sysvinit scripts, and you enable/disable them with update-rc.d. This means Apache2 can be disabled by typing:

# update-rc.d -f apache2 remove

as root, which will remove all symlinks in the /etc/rc?.d/ which in effect will make the init scripts to not pick up on starting them automatically, and so they are disabled.


With the new style upstart init scripts, which is event-based, things are done differently, and that's the case with MySQL. A listing of /etc/init.d/ will show you that mysql is actually just a symlink to /lib/init/upstart-job. The upstart-job script does some common initialisation, and looks in /etc/init/ for files ending in .conf, and reads those files, which specify which events that may trigger the startup of the service, and some other details. If you edit the file corresponding to the service you wish to disable, like in this case, /etc/init/mysql.conf, you'll find some lines in the beginning of the file, looking like this:


# MySQL Service

description     "MySQL Server"
author          "Mario Limonciello "

start on (net-device-up
          and local-filesystems)
stop on runlevel [016]

respawn
...


The parameters to "start_on" define which events will trigger this service to start. If you specify the event "never", which will never be triggered, the effect is, due to the expression being AND-ed, that the evaluation always returns false, and the service can only be started by calling it through "service ". So, if you modify the mysql.conf file to look like this in the beginning:


# MySQL Service

description     "MySQL Server"
author          "Mario Limonciello "

start on (never
          and net-device-up
          and local-filesystems)
stop on runlevel [016]

respawn
...


the result is that the service can only be started manually, which is just how I want it.

So, what I do now, when I need to start or stop the services, is (as root):


# /etc/init.d/apache2 start|stop|restart
# service mysql start|stop|restart


Hope this can be of use to someone, or at least myself, if I should forget how to. Enjoy deciding for youself, and not letting Canonical/Ubuntu make all choices for you!

2009-11-01

About

I'm really not a blogger. I don't think the world is interested in what I had for dinner today, or at what time I got up a given day. But, through my years of small scale hacking, programming and general computer (mis)use, I've found incredibly much useful info on how to do this or that on various blogs, and since I'm a big fan of sharing knowledge, I think it's time I try to start documenting stuff I've learned at a place where others can see and learn (or teach me better ways), so here I go!

PS. This blog might be about non-computer stuff as well, but probably not...