#!/usr/bin/perl -w # # mail-summary - display a summary of an mbox in a fixed number of lines # Copyright (C) 2000 Tim Waugh # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA use Getopt::Std; getopts('f:n:e:h', \%opts); if ($opts{h}) { print "Usage: $0 [-f filename] [-n number] [-e email] [-h]\n"; print "\t-f filename\tuse filename in stead of /var/spool/mail/\$LOGNAME\n"; print "\t-n number\tnumber of lines to output (default 5)\n"; print "\t-e email\tuse email instead of default\n"; print "\t-h\t\tshow this message\n"; exit 0; }; $myemail= $opts{e} || "twaugh\@redhat.com:tim\@cyberelk.net"; $beep = 0; $lines = $opts{n} || 5; exit 0 if $lines < 1; $spoolfile = $opts{f} || "/var/spool/mail/$ENV{LOGNAME}"; die "No spool file\n" unless (-f $spoolfile); ($r,$w) = (stat($spoolfile))[8,9]; open (SPOOL,"<$spoolfile") or die "$spoolfile: $!\n"; $/ = ''; @SPOOL = ; close SPOOL; utime ($r,$w,$spoolfile); $email = quotemeta $myemail; $email =~ s/\\:/:/g; @SUMMARY=(); $message = 0; foreach (@SPOOL) { next unless (/^From /m); next if (/^From MAILER-DAEMON /m); $message++; next if (/^status:.*r/mi); $N = "N"; $N = "O" if (/^status:.*o/mi); $Z = "4"; foreach my $addr (split (':', $email)) { if (/^cc:.*$addr/mi) { if (/^to:.*$addr/mi) { $Z = "2"; } else { $Z = "3"; } } elsif (/^to.*$addr/mi) { if (/^cc:/mi) { $Z = "2"; } else { $Z = "1"; } } } if ($Z != "4") { $beep = 1; } ($from) = /^from: *(.*)/mi; $from =~ s/[<(]?[\w\-.]+\@[\w\-.]+[>)]?// unless ($from =~ /^[<(]?[\w\-.]+\@[\w\-.]+[>)]?$/); $from =~ s/^ *//; $from =~ s/ *$//; $from =~ s/\"//g; ($subj) = /^subject: *(.*)/mi; $from = "(no sender)" unless $from; $subj = "(no subject)" unless $subj; push (@SUMMARY, sprintf ("%4d $N $Z %-15.15s %-54.54s\n", $message, $from, $subj)); } @temp = map { [ substr ($_,7,1), $_ ] } @SUMMARY; @temp = sort { $a->[0] cmp $b->[0] || $b->[1] cmp $a->[1] } @temp; @SUMMARY = map { $_->[1] } @temp; $more = 1 + $#SUMMARY - $lines; $count = 0; @code = (undef, '+', 'T', 'C', ' '); foreach (@SUMMARY) { s/^(.......)(.)/$1$code[$2]/; $line = $_; if ($more > 0 and $count == ($lines - 1)) { $moretext = " ($more more...)\n"; $morelen = length $moretext; $linelen = length $line; substr($line, $linelen - $morelen, $morelen) = $moretext; print $line; last; } else { print; } $count++; } print "\a" if $beep;