home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3397 < prev    next >
Encoding:
Internet Message Format  |  1991-05-22  |  4.0 KB

  1. From: gamin@ireq-robot.hydro.qc.ca (Martin Boyer)
  2. Newsgroups: comp.lang.perl,alt.sources
  3. Subject: Re: perl version of from(1)
  4. Message-ID: <GAMIN.91May21172212@pellan.ireq-robot.hydro.qc.ca>
  5. Date: 21 May 91 21:22:12 GMT
  6.     d'Hydro-Quebec
  7. Lines: 150
  8.  
  9. jv@mh.nl (Johan Vromans) writes:
  10.  
  11.    > From: subbarao@phoenix.Princeton.EDU (Kartik Subbarao)
  12.    > Subject: perl version of from(1)
  13.    > Date: 19 May 91 23:54:24 GMT
  14.    > Organization: American Chemical Society
  15.  
  16.    > Anyone know of a cooler way to do this?
  17.  
  18.    Depends on what you call 'cool'    :-) .
  19.  
  20.    [shar of from.pl 1.5 deleted]
  21.  
  22. Here's my version modified to:
  23.  
  24.     handle multiple mailboxes (default is $HOME/INBOX, created by vm)
  25.     reset message counter for each mailbox
  26.     test for presence of mailbox instead of spool directory in BSD systems
  27.  
  28.  
  29. #!/usr/bin/perl
  30. #----Le laboratoire de robotique de l'Institut de recherche d'Hydro-Quebec-----
  31. # Nom     : from [-n] [MAILBOX] [MAILBOX...]
  32. # Fonction: Imprime un sommaire de la boite postale,
  33. #        avec -n, imprime les numeros de message aussi.
  34. # Fichiers: from, /usr/spool/mail, ~/INBOX
  35. # Notes   : Requiert perl version 3.0, patchlevel 4 ou mieux.
  36. # Cree    :    1989  ------------ Johan Vromans <jv@mh.nl>
  37. # Modifie : 21 mai 91 ---------1- Martin Boyer <mboyer@ireq-robot.hydro.qc.ca>
  38. #        Copyright 1989, 1990 Johan Vromans <jv@mh.nl>, no rights reserved.
  39. # Historique: 
  40. # 21 mai 91 ---------1- Martin Boyer <mboyer@ireq-robot.hydro.qc.ca>
  41. #     Modifie la version 1.5 de Johan Vromans: ajoute INBOX
  42. #     et remis le compteur de message a zero pour chaque fichier.
  43. #------------------------------------------------------------------------------
  44.  
  45. $inbox = "$ENV{'HOME'}/INBOX";
  46.  
  47. # Default output format
  48. format =
  49. @<<<<<<<<<<< "@<<<<<<<<<<<<" ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<..
  50. $date,        $from,         $subj
  51. .
  52.  
  53. # Output format when sequence numbers are requested
  54. format format_n =
  55. @>: @<<<<<<<<<<< "@<<<<<<<<<<<<" ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<..
  56. $seq, $date,      $from,         $subj
  57. .
  58.  
  59. # Parse and stash away -n switch, if provided
  60. if ($#ARGV >= 0 && $ARGV[0] eq '-n') {
  61.   shift (@ARGV);
  62.   $~ = "format_n";
  63. }
  64.  
  65. # Use system mailbox and user INBOX 
  66. # if none was specified on the command line
  67. if ( $#ARGV < 0 ) {
  68.     if ( ! ($user = getlogin)) {
  69.     @a = getpwuid($<);
  70.     $user = $a[0];
  71.     }
  72.     if ( -r "/usr/mail/$user" ) {        # System V
  73.     @ARGV = ("/usr/mail/$user");
  74.     }
  75.     elsif ( -r "/usr/spool/mail/$user" ) {  # BSD
  76.     @ARGV = ("/usr/spool/mail/$user");
  77.     }
  78.     if ( $inbox && -r $inbox ) {
  79.     @ARGV = ($inbox, @ARGV);
  80.     }
  81.     if ($#ARGV < 0)  {
  82.     printf STDERR "No mail for $user.\n";
  83.     exit 1;
  84.     }
  85. }
  86. if ( $#ARGV < 0 ) {
  87.   if ( ! ($user = getlogin)) {
  88.     @a = getpwuid($<);
  89.     $user = $a[0];
  90.   }
  91.   if ( -r "/usr/mail/$user" ) {        
  92.     @ARGV = ("/usr/mail/$user");
  93.   }
  94.   elsif ( -r "/usr/spool/mail/$user" ) {    # BSD
  95.     @ARGV = ("/usr/spool/mail/$user");
  96.   }
  97.   else {
  98.     printf STDERR "No mail for $user.\n";
  99.     exit 1;
  100.   }
  101. }
  102.   
  103. # Read through input file(s)
  104. while (<>) {
  105.  
  106.   if (eof) {$seq = 0;}
  107.  
  108.   # Look for a "From_" header (See RFC822 and associated documents).
  109.   next unless /^From\s+(\S+)\s+.*(\w{3}\s+\d+\s+\d+:\d+)/;
  110.  
  111.   chop;
  112.   $from = $1;  
  113.   $date = $2;
  114.   if ( $date eq "" || $from eq "" ) {
  115.     print STDERR "Possible garbage: $_\n";
  116.     next;
  117.   }
  118.  
  119.   $seq++;
  120.   # Get user name from uucp path
  121.   $from = $1 if $from =~ /.*!(.+)/;
  122.  
  123.   # Now, scan for Subject or empty line
  124.   $subj = "";
  125.   while ( <> ) {
  126.     chop ($_);
  127.  
  128.     if ( /^$/ || /^From / ) {
  129.       # force fall-though
  130.       $subj = "<none>" unless $subj;
  131.     }
  132.     else {
  133.       $subj = $1 if /^Subject\s*:\s*(.*)/i;
  134.       if ( /^From\s*:\s*/ ) {
  135.         $_ = $';
  136.         if ( /\((.+)\)/i ) { $from = $1; } 
  137.         elsif ( /^\s*(.+)\s*<.+>/i ) { $from = $1; } 
  138.         elsif ( /^<.+>\s*(.+)/i ) { $from = $1; } 
  139.       }
  140.     }
  141.  
  142.     # do we have enough info?
  143.     if ( $from && $subj ) {
  144.       write;
  145.       last;
  146.     }
  147.   }
  148. }
  149.  
  150. --
  151. Martin Boyer                            mboyer@ireq-robot.hydro.qc.ca
  152. Institut de recherche d'Hydro-Quebec    mboyer@ireq-robot.uucp
  153. Varennes, QC, Canada   J3X 1S1
  154. +1 514 652-8412
  155.