1 #!/usr/bin/perl
  2 #deps: pkg_add p5-libwww p5-XML-Simple p5-Net-DBus
  3 
  4 #str2time
  5 use HTTP::Date;
  6 use LWP::Simple;
  7 use XML::Simple;
  8 #splitname
  9 use OpenBSD::PackageName;
 10 use Net::DBus;
 11 use strict;
 12 use warnings;
 13 
 14 my $timeout=8000;
 15 
 16 # exit if d-bus is not running
 17 exit unless defined glob "/tmp/dbus-*";
 18 # make sure we find d-bus session bus from crontab
 19 $ENV{'DBUS_SESSION_BUS_ADDRESS'}="unix:path=".glob "/tmp/dbus-*";
 20 $ENV{'DISPLAY'}=":0.0";
 21 my $dbus_session = Net::DBus->session;
 22 my $notification_service = $dbus_session->get_service('org.freedesktop.Notifications');
 23 my $notification_obj = $notification_service->get_object('/org/freedesktop/Notifications', 'org.freedesktop.Notifications');
 24 
 25 # get latest read package in previous rss
 26 my $latest;
 27 my $file ='/var/tmp/update-notifier';
 28 if (open (FH,'<', $file))
 29 {$latest = <FH> ; chomp $latest}
 30 else
 31 {$latest = 0}
 32 close (FH);
 33 
 34 # get data with LWP
 35 my $xml = XML::Simple->new(SuppressEmpty => 1);
 36 my $url="http://openports.se/rss/all";
 37 my $page = get($url);
 38 exit unless defined $page;
 39 
 40 # get XML from data
 41 my $result = $xml->XMLin($page);
 42 
 43 my (@updated, @new, @removed);
 44 my ($body, $title) = ("","");
 45 my $item;
 46 my $str;
 47 
 48 # loop/parse
 49 foreach $item (@{$result->{channel}->{'item'}})
 50 {
 51         last if (str2time($item->{'dc:date'}) <= $latest);
 52 #       print "cur:".$item->{title}."\n";
 53         if ($item->{title} =~ /Updated, (.*) to (.*)/)
 54         {
 55                 # get pkg name
 56                 my ($pkg, $ver) = OpenBSD::PackageName::splitname($1);
 57                 my (undef, $newver) = OpenBSD::PackageName::splitname($2);
 58                 $str = "<a href='$item->{link}'><i>$pkg</i></a><b> ($ver->$newver)</b>";
 59                 $str .= ":$item->{description}" if ($item->{description});
 60                 push @updated, $str;
 61         }
 62         elsif ($item->{title} =~ /Removed, (.*)/)
 63         {
 64                 my $pkg = OpenBSD::PackageName::splitname($1);
 65                 $str = "<a href='$item->{link}'><i>$pkg</i></a>";
 66                 $str .= ":$item->{description}" if ($item->{description});
 67                 push @removed, $str;
 68         }
 69         elsif ($item->{title} =~ /New package, (.*)/)
 70         {
 71                 my ($pkg, $ver) = OpenBSD::PackageName::splitname($1);
 72                 $str = "<a href='$item->{link}'><i>$pkg</i></a><b> $ver</b>";
 73                 $str .= ":$item->{description}" if ($item->{description});
 74                 push @new, $str;
 75         }
 76 }
 77 
 78 # write last read package
 79 open (FH, ">", $file) or die "can't open $file for writing: $!";
 80 print FH str2time($result->{channel}->{item}->[0]->{'dc:date'})."\n";
 81 close (FH);
 82 
 83 # create title and body
 84 $title="";
 85 if ($#updated > 0)
 86 {
 87         $title.= "$#updated updated";
 88         $body.="<b>Updated:\n</b>";
 89         $body.="$_\n" foreach @updated;
 90 }
 91 
 92 if ($#new > 0)
 93 {
 94         $title.=", " if ($title ne "");
 95         $title.= "$#new new";
 96         $body.="\n<b>New:</b>\n";
 97         $body.="$_\n" foreach @new;
 98 }
 99 
100 if ($#removed > 0)
101 {
102         $title.=", " if ($title ne "");
103         $title.= "$#removed removed";
104         $body.="\n<b>Removed:</b>\n";
105         $body.="$_\n\t" foreach @removed;
106 }
107 $title.=".";
108 
109 #print "title=$title\n\n";
110 #print "body=$body\n\n";
111 # send notification
112 #exec "/usr/local/bin/notify-send" ,"--expire-time=$timeout", "--icon=system-software-update", "$title", "$body" unless ($title eq ".");
113 $notification_obj->Notify($ARGV[0], 0, 'system-software-update', $title, $body, undef, undef, $timeout) unless ($title eq ".");