home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2036 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  5.4 KB

  1. From: Harald.Eikrem@elab-runit.sintef.no
  2. Newsgroups: alt.sources
  3. Subject: Re: Kill file for mail.
  4. Message-ID: <90Nov8.013502*Harald.Eikrem@elab-runit.sintef.no>
  5. Date: 8 Nov 90 00:35:02 GMT
  6.  
  7. Here's another approach to sorting mailboxes.  Perl based, what else?
  8. There's no separate man page.  It does not pipe messages to programs, because
  9. it is strictly a mail sorter.  True improvements are of course welcomed.
  10.  
  11. --Harald E
  12.  
  13.  
  14. ++++Perl script follows, save as `mailsort' in your favourite bin directory++++
  15.  
  16. #!/usr/local/bin/perl
  17.  
  18. # Usage "mailsort [ MAILBOX... ]"    Requires perl 3.0 patchlevel 4 up.
  19. # Sorts Unix mailbox file(s) into mailbox folders at ~/Mail/<folder>,
  20. # default input file is /usr{/spool,}/mail/<login>.
  21. # Produces two kinds of folder summaries, one building up for each
  22. # folder (~/Mail/.sum/<folder>), and another about all new messages
  23. # from the last sort (~/Mail/.sum/NEW).
  24. # The sorting is based on a sort file (~/Mail/.fsort)
  25. # using the following conventions:
  26. #  - Each line should contain a keyword and a folder name.
  27. #  - The keyword and folder tokens are separated by one or more <tab>.
  28. #  - A blank character in the keyword matches any number of whitespaces.
  29. #  - Keyword matches are always case-INsensitive.
  30. #  - Regular expressions are valid, but take care. Not everything is
  31. #    going to work.
  32. #  - Key order might be significant.
  33. #  - Mail header reserved words should only be preceeded by a circumflex
  34. #    (^) and end in a colon (:). There is no need to insert random string
  35. #    matches in beetween the MHRW and the matching string, a single
  36. #    space is suitable, e.g. "^From: henry@vcu<tab>henry"
  37. #    will match a From: header field with "henry@vcu" anywhere on that
  38. #    line, the message is written to the folder named "henry".
  39. #    "^To:" will match both To: and Cc: header records.
  40. #  - Mail header continuation lines are not recognised on a regular basis.
  41. #  - Mail header trace lines are not rendered in the folder decisions.
  42. #  - Any other kind of keyword will match *anything* in the mail header.
  43. #  - Default folder name is "inbox".
  44. #
  45. # Written 5. Feb 90 by Harald Eikrem, SINTEF, Trondheim, Norway.
  46. # (Some ideas lent from `from' script by Johan Vromans).
  47. #      <Harald.Eikrem@elab-runit.sintef.no>
  48. # PS. I am sure someone can optimise this algorithm. A perl novice I am.
  49. #
  50. # Modified 8. Feb 90 -- redesigned key struct
  51. # Modified 21. Mar 90 -- modified "^To:" keyword to match both To: and Cc:.
  52. # Modified 1. Apr 90 -- now handles absolute or relative folder file
  53. #            referencing, e.g. /dev/null (this is no joke)
  54.  
  55. if ( $#ARGV < 0 ) {
  56.   if ( ! ($user = getlogin)) {
  57.     @a = getpwuid($<);
  58.     $user = $a[0];
  59.   }
  60.   if ( -r "/usr/spool/mail/$user" ) {
  61.     @ARGV = ("/usr/spool/mail/$user");
  62.   }
  63.   elsif ( -r "/usr/mail/$user" ) {
  64.     @ARGV = ("/usr/mail/$user");
  65.   }
  66.   else {
  67.     printf STDERR "No mail for $user.\n";
  68.     exit 1;
  69.   }
  70. }
  71.  
  72. $Maildir = $ENV{"HOME"}."/Mail";
  73.  
  74. if ( ! -d $Maildir ) {
  75.     printf STDERR "No ~/Mail directory.\n";
  76.     exit 1;
  77. }
  78.  
  79. $fsort = ".fsort";
  80.  
  81. if ( ! -r "$Maildir/$fsort" ) {
  82.     printf STDERR "No sort file ~/Mail/$fsort\n";
  83.     exit 1;
  84. }
  85.  
  86. # read sort file
  87. open(srt, "<$Maildir/$fsort");
  88. for ($index = 1; $line = <srt>; ++$index ) {
  89.   chop($line);
  90.   next if $line =~ /^#/ || $line =~ /^\s*$/;
  91.   $line =~ /^([^\t]+)\t+(\S+)\s*$/;
  92.   $key[$index] = $1;
  93.   $val[$index] = $2 ? $2 : "inbox";
  94.   $key[$index] =~ s/^\^?([\w-]+\s*:)\s*(.+)/^$1[^\\n]*$2/;
  95.   $key[$index] =~ s/\s+/\\s+/g;
  96.   $key[$index] =~ s/(\[[^\]]*)\\s+/$1 /g;
  97.   $key[$index] =~ s/\\\\s\+?/ /g;
  98.   $key[$index] =~ s/^\^/\\n/;
  99.   if (/^(\n)To(:.*)/i) { $key[++$index] = s//$1Cc$2/; }
  100. #  printf "%3d KEY = %-42s VAL = %-s\n",$index,$key[$index],$val[$index];
  101. }
  102. close(srt);
  103. $MAXKEY = $index-1;
  104.  
  105. if ( ! -d "$Maildir/.sum" ) { mkdir("$Maildir/.sum", 0755); }
  106.  
  107. # go through input file(s)
  108. while ( $line = <> ) {
  109.  
  110.   # scan until "From_" header found
  111.   if ( $line !~ /^From\s+(\S+)\s+[^\n]*(\w{3}\s+\d+\s+\d+:\d+)/ ) {
  112.     print fld $line if $folder;
  113.     next;
  114.   }
  115.  
  116.   $from = $1; $date = $2;
  117.   $full_header = $line;
  118.   $header = $line;
  119.   $Recline = 0;
  120.  
  121.   # get user name from uucp path
  122.   $from = $1 if $from =~ /.*!([^\n]+)/;
  123.  
  124.   # now, scan for Subject or empty line
  125.   $subj = "";
  126.   while ( $line = <> ) {
  127.  
  128.     if ( $line =~ /^$/ ) {
  129.       # force fall-though
  130.       $subj = "(none)" unless $subj;
  131.       last;
  132.     }
  133.     $full_header .= $line;
  134.     # Skip trace header lines
  135.     if ($Recline && $line !~ /^\s/) { $Recline = 0; }
  136.     if ($Recline || $line =~ /^Received\s*:/) { $Recline = 1; } 
  137.     else { $header .= $line; }
  138.     $subj = $1 if $line =~ /^Subject\s*:\s*([^\n]*)/;
  139.     if ( $line =~ /^From\s*:\s*/ ) {
  140.       $line = $';
  141.       if ( $line =~ /\(([^\n]+)\)/ ) { $from = $1; } 
  142.       elsif ( $line =~ /^"?([^<\n"]+)"?\s*<[^\n]+>/ ) { $from = $1; } 
  143.       elsif ( $line =~ /^<?([^>\n]+)>?/ ) { $from = $1; } 
  144.     }
  145.   }
  146.  
  147.   $folder = "";
  148.   study($header);
  149.   for ($index = 1; !$folder && $index <= $MAXKEY; ++$index ) {
  150.     $folder = $val[$index] if $header =~ /$key[$index]/i;
  151.   }
  152.   $folder = "inbox" unless $folder;
  153.   $summary{$folder} .=
  154.      sprintf(" %-12.12s \"%-17.17s\" %-.45s\n", $date,$from,$subj);
  155.   close(fld);
  156.   if ($folder =~ m#^[/.]#) {
  157.     open(fld, ">>$folder");
  158.   } else {
  159.     open(fld, ">>$Maildir/$folder");
  160.   }
  161.   print fld $full_header,"\n";
  162. }
  163.  
  164. close(fld);
  165.  
  166. open(new, ">>$Maildir/.sum/NEW");
  167.  
  168. while ( ($folder,$sumtext) = each(%summary) ) {
  169.   print new $folder,":\n",$sumtext;
  170.   open(sum, ">>$Maildir/.sum/$folder");
  171.   print sum $sumtext;
  172.   close(sum);
  173. }
  174. close(new);
  175.