#!/usr/local/bin/perl # From: print out one line for each mail message in the system mailbox # indicating who the mail is from and the subject # # Written by Dave Ratner # # This program is shareware. It can be freely distributed # as long as my name and this notice remains as is. $debug = 0; # # Parse options # -d debug # while ($#ARGV >= 0) { if ($ARGV[0] eq '-d') { $debug++; shift(@ARGV); } else { last; }; }; # # Find that system mailbox # if ($#ARGV >= 0) { $mbox = shift @ARGV; } else { $mbox = "/var/spool/mail/$ENV{'LOGNAME'}"; # $mbox = "/usr/spool/mail/$ENV{'LOGNAME'}" } die "You have no mail\n" if ( ! -s $mbox); open(MBOX, "< $mbox") || die "Can't open $mbox: $!\n"; # # Get the from and subject lines # sub getmessage { local ($from, $subject, $header); # Check the line we saved from the last email message $header = $getmessage_savedline; if ($header =~ s/^Subject: //) { $subject = $header; } elsif ($header =~ s/^From: //) { $from = $header; } # # Read the header looking for "From:" and "Subject:" # while () { $header = $_; if ($header =~ s/^Subject: //) { $subject = $header; } elsif ($header =~ s/^From: //) { $from = $header; } last if (/^$/); }; # # If we are at the end of the file, exit early # return () if (!defined($from) && !defined($subject)); # # Skip the message body # while () { last if (/^From /); }; # save the first line of the next email header $getmessage_savedline = $_; # remove newlines chop $from; chop $subject; # edit the from line so if they have a name and email address, we # just use the name # # NEEDSWORK: this section can be condensed # # sometimes its "name " # sometimes its "email address (name)" # sometimes its just "email address" $from =~ s/<.*@.*>//; # filter out email addr from "name " $from =~ s/"//g; # filter out quotes from around name, if present. if ($from !~ /^.*@[^(]*$/) { # if not just an email address # filter out email addr from "addr (name)" and remove the parens $from =~ s/^.*@[^(]*// && $from =~ s/[()]//g; } # some subject lines have unnecessary whitespace at the beginning $subject =~ /^\s*(.*)$/ && ($subject = $1); return ($from, $subject); } # # Mainline # $message_number = 0; # # Loop until no more messages # while (($from, $subject) = &getmessage) { if ($debug) { print "\nMessage " . $message_number++ . "\n"; print "******\n" . $header . "*****\n"; } $subject = "" if (!defined($subject)); # # Print the data; prune "from" to 18 characters so everything lines up # since from is 18 characters, max subject line should be 55 (assume # a standard 80 column window) # printf("%-18.18s \t %.55s\n", $from, $subject); } exit 0;