User:Optimix/Irssi guifications

From Wikipedia, the free encyclopedia

Irssi plugin that pops up a gtk2 window when someone says your name in a chat or when you receive a private message.

It sucks, but it works and I like it.

#!/usr/bin/perl
use vars qw(%IRSSI
            $total_popups
            $popup_vertical_pos
            $first_popup_closed);

#gtk and glib
use Gtk2 '-init';
use Glib;

#constants
use constant TRUE  => 1;
use constant FALSE => 0;

%IRSSI = (
    authors     => 'Munteanu Alexandru Ionut',
    contact     => 'io_alex_2002 AT yahoo DOT fr',
    name        => 'irssi_guifications',
    description => 'pop-up gtk window on incoming private message',
    version     => '0.1',
    license     => 'GPLv2',
    year        => '2006',
    url         => '',
);

$total_popups = 0;
$popup_vertical_pos = 30;
$first_popup_closed = FALSE;

#contains arrays of {'nick' =>,'number' =>}
@popups = ();

#popups the gtk window that expires in $seconds (see below)
sub show_popup($$$$$$)
{
    #if private_message is true, then we have received a private
    #message, otherwise a public message
    my ($private_message,$server, $msg, $nick, $address, $target) = @_;
    
    my $current_popup;
    
    #security check
    if ($total_popups < 0) 
    {
        $total_popups = 0;
    }
    $current_popup = $total_popups;
    $total_popups++;
    
    #number of seconds to show the notification ares
    my $seconds = 6;
    
    #creates new gtk window
    my $window = Gtk2::Window->new('popup');
    $window->set_default_size(150,30);
    
    #click event
    my $click_event = Gtk2::EventBox->new;
    $click_event->signal_connect(button_press_event => sub { 
        $total_popups--;
        #if we are the first popup,
        if ($current_popup == 0)
        {
            $new_popup_vertical_pos = 30;
            $total_popups = 0;
        }
        $window->destroy();
        });
    #we add the click event to the main window
    $window->add($click_event);
    
    #main vertical box
    my $main_vbox = Gtk2::VBox->new;
    #we add the main vertical vbox to the click event
    $click_event->add($main_vbox);
    
    #vertical box
    my $vbox = Gtk2::VBox->new;
    
    #we put the vbox inside the main vbox
    $main_vbox->pack_start($vbox,TRUE,TRUE,10);
    
    #blue color
    my $gdk_blue_color;
    $gdk_blue_color = Gtk2::Gdk::Color->parse("blue");
    
    #the nick label
    my $nick_label = Gtk2::Label->new($nick);
    $nick_label->modify_fg(GTK_STATE_NORMAL,$gdk_blue_color);
    #"has messaged you" label
    my $has_messaged_you_label;
    #the channel where our name was pronounced
    my $channel_label;
    my $network_name_label;
    $network_name_label = Gtk2::Label->new($server->{'chatnet'});
    $network_name_label->modify_fg(GTK_STATE_NORMAL,$gdk_blue_color);
    
    #ŧhe "has messaged you" message
    if ($private_message)
    {
        $has_messaged_you_label = Gtk2::Label->new("has messaged you on");
    }
    else
    {
        $has_messaged_you_label = 
            Gtk2::Label->new("said your name on");
        $channel_label = Gtk2::Label->new($target);
        $channel_label->modify_fg(GTK_STATE_NORMAL,$gdk_blue_color);
    }
    
    #we put the nick in the box
    $vbox->pack_start($nick_label,FALSE,FALSE,0);
    #we put the "has messaged you" label in the main vbox
    $vbox->pack_start($has_messaged_you_label,FALSE,FALSE,0);    
    #we put the network name
    $vbox->pack_start($network_name_label,FALSE,FALSE,0);
    #if public message, put channel
    if (!$private_message)
    {
        $vbox->pack_start($channel_label,FALSE,FALSE,0);
    }
    
    #we show the popup
    $window->show_all;
    
    #we move the window
    $window->move(0,$popup_vertical_pos+2);
    
    if (($popup_vertical_pos != 30)
        || ($current_popup == 0))
    {
        #we get the vertical size of the window
        #and stock it for future popups
        my $window_y_pos, $window_x_pos;
        ($window_x_pos, $window_y_pos) = $window->get_position;
        my $window_width, $window_height;
        ($window_width, $window_height) = $window->get_size;
        $popup_vertical_pos = $window_y_pos + $window_height;
    }
    
    #we destroy the window after $seconds
    Glib::Timeout->add($seconds * 1000, sub {
        $total_popups--;
        #if we are the first popup,
        if ($current_popup == 0)
        {
            $popup_vertical_pos = 30;
            $total_popups = 0;
        }
        $window->destroy();
    });
}

#when we receive a private message, we execute this
sub sig_message_private ($$$$$)
{
    my ($server, $msg, $nick, $address,$target) = @_;    
    show_popup(TRUE,$server, $msg, $nick, $address,$target);
}

#we receive a public message
sub sig_message_public ($$$$$)
{
    my ($server, $msg, $nick, $address,$target) = @_;
    
    #if our nick is pronounced in the public chat
    if ($msg =~ /.*$server->{nick}.*/)
    {
        show_popup(FALSE,$server, $msg, $nick, $address, $target);
    }
}

#when we receile a private message
Irssi::signal_add('message private', \&sig_message_private);
Irssi::signal_add('message public', \&sig_message_public);

#show message on plugin load
print CLIENTCRAP '%B>>%n '.$IRSSI{name}.' v'.$IRSSI{version}.
    ' loaded - '.$IRSSI{license}." license"." - ".
    $IRSSI{authors}, " - ".$IRSSI{year};