20 September 2008

New mail script

I used to use this little program called "nfrm" (or frm -n) to tell me on log in to a unix system if I had new mail. Unfortunately what worked in the past no longer was keeping accurate lists in the world where I was also checking my mail via IMAP and the iPhone, where there's a lot of mail I've been informed (in some way) about, but have never actually read.

Here's a quick Perl script that I wrote which checks three folders (my inbox, a work-related box, and mailing lists) for new messages. It requires Mail::MboxParser (install via "cpan" then "install Mail::MboxParser" or have your sysadmin do it for you):

#!/usr/bin/perl

use Mail::MboxParser;
use Term::ReadKey;
use strict;

my ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();
my $available_space = $wchar - 13;
my $from_length = int($available_space * .33);
my $subject_length = int($available_space * .67);

my @mailboxes = @ARGV;
if (scalar(@mailboxes) == 0) {
@mailboxes = (["**** ", $ENV{'MAIL'}],
["NBER ", "/homes/nber/cuthbert/mail/nber"],
["Lists", "/homes/nber/cuthbert/mail/lists"]);
}

my $parseropts = {
enable_cache => 1,
enable_grep => 1,
cache_file_name => '/tmp/msc-mbox-cache-file',
};

foreach my $box (@mailboxes) {
next unless -e $box->[1];
my $mb = Mail::MboxParser->new($box->[1],
decode => 'ALL',
parseropts => $parseropts);
while (my $msg = $mb->next_message) {
if ($msg->header->{status} !~ /R/) {
my $name = $msg->from->{name};
if (!$name) { $name = $msg->from->{email} }
$name =~ s/\"//g;
$name =~ s/^\s+//;
$name = substr($name, 0, $from_length);

my $subject = $msg->header->{subject};
$subject = substr($subject, 0, $subject_length);

printf("%-8s %-${from_length}s %-${subject_length}s\n", $box->[0], $name, $subject);
}
}
}

No comments: