home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-osu / mail-headers.spaf < prev    next >
Encoding:
Text File  |  1992-02-17  |  3.3 KB  |  117 lines

  1. Path: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!think.com!wupost!darwin.sura.net!gatech!purdue!spaf
  2. From: spaf@cs.purdue.EDU (Gene Spafford)
  3. Newsgroups: comp.lang.perl
  4. Subject: Mail handling routine
  5. Message-ID: <17833@ector.cs.purdue.edu>
  6. Date: 17 Feb 92 13:54:12 GMT
  7. Sender: news@cs.purdue.EDU
  8. Organization: Department of Computer Science, Purdue University
  9. Lines: 105
  10.  
  11. For the heck of it, enclosed is a Perl package I use to manipulate
  12. mail and news articles.
  13.  
  14. There are two external calls: one slurps up the header into a $
  15. variable, breaks up the entries (including continuation lines) into a
  16. @ entry, and builds an % array indexed by the header keywords.
  17.  
  18. This allows you lookup entries from the header, print the unmodified
  19. header, or otherwise dink around with the contents.
  20.  
  21. I've got about a dozen different things using this, including a mail
  22. server, a bunch of mailing list programs, and other stuff working with
  23. it for the last few months.  It appears quite stable.
  24.  
  25. The second routine attempts to figure out a return address based on
  26. the contents of the header.  It uses the value of the headers (in
  27. order of preference): Reply-to, From, Return-path, Apparently-from. 
  28.  
  29. Comments, if any, welcome.
  30.  
  31. #  Routines to parse out an RFC 822 mailheader
  32. #     E. H. Spafford,  last mod: 11/91
  33. #  
  34. #  ParseMailHeader breaks out the header into an % array
  35. #    indexed by a lower-cased keyword, e.g.
  36. #       &ParseMailHeader(STDIN, *Array);
  37. #    use $Array{'subject'}
  38. #
  39. #    Note that some duplicate lines (like "Received:") will get joined
  40. #     into a single entry in %Array; use @Array if you want them separate
  41. #    $Array will contain the unprocessed header, with embedded
  42. #     newlines
  43. #    @Array will contain the header, one line per entry
  44. #
  45. #  RetMailAddr tries to pull out the "preferred" return address
  46. #    based on the presence or absence of various return-reply fields
  47.  
  48. package MailStuff;
  49.  
  50. #  Call as &ParseMailHeader(FileHandle, *array)
  51.  
  52. sub main'ParseMailHeader  ## Public
  53. {
  54.     local($save1, $save2) = ($*, $/);
  55.     local($FH, *array) =  @_;
  56.     local ($keyw, $val);
  57.  
  58.     %array = ();
  59.  
  60. # force unqualified filehandles into callers' package
  61.     local($package) = caller;
  62.     $FH =~ s/^[^']+$/$package'$&/;
  63.  
  64.     ($*, $/) = (1, '');
  65.     $array = $_ = <$FH>;
  66.     s/\n\s+/ /g;
  67.        
  68.     @array = split('\n');
  69.     foreach $_ (@array)
  70.     {
  71.     ($keyw, $val) = m/^([^:]+):\s*(.*\S)\s*$/g;
  72.     $keyw =~ y/A-Z/a-z/;
  73.     if (defined($array{$keyw})) {
  74.         $array{$keyw} .= ", $val";
  75.     } else {
  76.         $array{$keyw} = $val;
  77.     }
  78.     }
  79.     ($*, $/) = ($save1, $save2); 
  80. }
  81.  
  82.  
  83. #  Call as $addr = &RetMailAddr(*array)
  84. #    This assumes that the header is in RFC 822 format
  85.  
  86. sub main'RetMailAddr  ## Public
  87. {
  88.     local(*array) = @_;
  89.  
  90.     local($ReplyTo) = defined($array{'reply-to'}) ?
  91.         $array{'reply-to'} : $array{'from'};
  92.  
  93.     $ReplyTo = $array{'return-path'} unless $ReplyTo;
  94.     $ReplyTo = $array{'apparently-from'} unless $ReplyTo;
  95.  
  96.     &CleanAddr($ReplyTo) if $ReplyTo;
  97.     $ReplyTo;
  98. }
  99.  
  100. sub CleanAddr   ## Private
  101. {
  102.     local($_) = @_;
  103.     s/\s*\(.*\)\s*//;
  104.     1 while s/.*<(.*)>.*/\1/;
  105.     s/^\s+//;
  106.     s/\s+$//;
  107.     $_;
  108. }
  109.  
  110. 1;
  111. -- 
  112. Gene Spafford
  113. NSF/Purdue/U of Florida  Software Engineering Research Center,
  114. Dept. of Computer Sciences, Purdue University, W. Lafayette IN 47907-1398
  115. Internet:  spaf@cs.purdue.edu    phone:  (317) 494-7825
  116.  
  117.