Skip to content

Instantly share code, notes, and snippets.

@chtitux
Created October 30, 2012 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chtitux/3981226 to your computer and use it in GitHub Desktop.
Save chtitux/3981226 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use File::stat;
use Time::localtime;
use List::MoreUtils 'any';
use Time::HiRes qw(usleep);
use XML::Simple;
use Data::Dumper;
use Gtk2::Notify;
use utf8;
use Encode;
$| = 1;
# apt-get install libgtk2-notify-perl libxml-simple-perl liblist-moreutils-perl inotify-tools
my %hashTime = ();
my $lastDate;
# --------------------------------------------------------
# ---------------- CUSTOMIZE HERE ------------------------
# --------------------------------------------------------
my @ignore = (
"Théophile Helleboid:",
"theophile.helleboid:"
);
my $homedir = ` echo \$HOME `;
chomp($homedir);
my $initialPath = "$homedir/.purple/logs/jabber";
my $buddyList = "$homedir/.purple/blist.xml";
my $iconPath = "$homedir/.purple/icons";
# --------------------------------------------------------
# --------------------------------------------------------
# --------------------------------------------------------
# ----- INIT ICONS ------
my $icons;
my $contactsList = XMLin("$buddyList");
my $groups = $contactsList->{blist}->{group};
foreach my $group (keys %$groups) {
my $source = $contactsList->{blist}->{group}->{$group}->{contact};
if( ref($source) eq 'ARRAY') {
foreach my $buddy (@$source)
{
my $key;
if($buddy->{buddy}->{setting}->{servernick}->{content} ne "") {
$key = $buddy->{buddy}->{setting}->{servernick}->{content};
}
else {
if($buddy->{buddy}->{alias} ne "") {
$key = $buddy->{buddy}->{alias};
}
else {
$key = $buddy->{buddy}->{name};
}
}
$icons->{encode_utf8($key) . ':'} = $buddy->{buddy}->{setting}->{buddy_icon}->{content};
}
}
}
# ----- INIT ICONS ------
sub display()
{
my $line = shift;
my $pseudo;
if( $line =~ /^<font color.*/)
{
$line =~ s/\(\d{2}:\d{2}:\d{2} [AP]M\)//g;
$line =~ s/<b>(.*)<\/b>//g;
$pseudo = $1;
}
elsif( $line =~ /^<msg.*/)
{
$line =~ s/nick="(\S*)"//g;
$pseudo = $1;
if( $pseudo eq "" ) {
return;
}
}
else
{
return;
}
$line =~ s/<\/?\w*[^>]*>\s?//g;
my $matchSearch = any { /$pseudo/ } @ignore;
if ( !$matchSearch )
{
my $notification;
if( $icons->{"$pseudo"} ne "") {
$notification = Gtk2::Notify->new(decode_utf8($pseudo), decode_utf8($line), $iconPath . "/" . $icons->{$pseudo});
}
elsif( $icons->{substr($pseudo, 0, -1) . '@jabber.ovh.net:'} ne "") {
$notification = Gtk2::Notify->new(decode_utf8($pseudo), decode_utf8($line), $iconPath . "/" . $icons->{substr($pseudo, 0, -1) . '@jabber.ovh.net:'});
}
else {
print "pb icon '$pseudo'\n";
$notification = Gtk2::Notify->new(decode_utf8($pseudo), decode_utf8($line));
}
$notification->show;
}
}
my $inwtPid;
my $fromInwt;
sub clientQuit {
print "Caught a signal. Shutting down.\n";
#print STDOUT "inwtPid is $inwtPid\n";
kill(2, $inwtPid);
close($fromInwt);
waitpid($inwtPid, 0);
exit(0);
}
$inwtPid = open($fromInwt, '-|');
if ($inwtPid == 0) {
# Parent wants all our output on the single filehandle $fromInwt.
open(STDERR, ">&", STDOUT);
my @args = ('inotifywait',
'-e modify', '-e create',
'-r', '-m', '--format', "%w%f",
"/home/thellebo/.purple/logs" );
#exec(@args);
exec("inotifywait -r -m /home/thellebo/.purple/logs -e MODIFY --format '%w%f' ");
}
<$fromInwt>; # `Watches established'
<$fromInwt>; # `Watches established'
$SIG{INT} = \&clientQuit;
print "Continuously mirroring. Give me a SIGINT when you want me to quit.\n";
for (;;) {
my $file;
chomp($file = <$fromInwt>);
print "File modified : $file\n";
($file =~ /^\s*$/) and next;
my $line = `tail -n 1 $file`;
print "------------ $line -----------";
print time."\n";
&display( $line );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment