home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / tutorial / eg / forwmail < prev    next >
Encoding:
Text File  |  1990-03-16  |  4.4 KB  |  203 lines

  1. #!/usr/bin/perl
  2.  
  3. # ===========   begin configuration section
  4.  
  5. $DOMAIN  = '\.convex\.com';     # remember to turn off regexp magic
  6. $ALIASES = '/usr/lib/aliases';
  7. $SPOOL   = '/usr/spool/mail';
  8.  
  9. # ===========   end configuration section
  10.  
  11. ($program = $0) =~ s,.*/,,;
  12.  
  13. $| = 1;
  14.  
  15.  
  16. &source('stat.pl');
  17. &source('getopts.pl');
  18.  
  19. &Getopts('nv') || die "usage: $program [-nv]\n";
  20.  
  21. $verbose = $opt_v;
  22. $doit    = !$opt_n;
  23.  
  24. chop($hostname = `hostname`);
  25. ($host = $hostname) =~ s/$DOMAIN//;  # chop domainname
  26.  
  27. print "running $program on $host";
  28. print " ($hostname)" if $host ne $hostname;
  29. print "\n";
  30.  
  31. chdir $SPOOL || die "absurd chdir failure to $SPOOL: $!";
  32.  
  33. unless (-f "$ALIASES.pag" && dbmopen(aliases, $ALIASES, 0644)) {
  34.     $slow++;
  35.     print "no dbm file: doing aliases the hard way\n";
  36.     print STDERR "can't open dbm file $ALIASES: $!\n";
  37.     open ALIASES || die "couldn't read $ALIASES: $!";
  38.     while (<ALIASES>) {
  39.     next if /^#/ || ! /^(\S+)\s*:\s+(.*)/;
  40.     $aliases{$1} = $2;
  41.     } 
  42.     close ALIASES;
  43. }
  44.  
  45. foreach $mbox ( <*> ) {
  46.     
  47.     unless (&Stat($mbox)) {
  48.     print STDERR "couldn't stat $mbox: $!\n";
  49.     next;
  50.     } 
  51.  
  52.     if (!($alias = $aliases{$slow ? $mbox : "$mbox\000" })) {
  53.     if ($st_size == 0) {
  54.         print "deleting zero-length non-mbox $mbox\n";
  55.         if ($doit) {
  56.         unlink($mbox) || print STDERR "can't unlink $mbox: $!\n";
  57.         }
  58.         next;
  59.     } 
  60.     print "forwarding bogus mbox $mbox to postmaster\n";
  61.     if ($doit) {
  62.         unless (rename($mbox, "$mbox.$$")) {
  63.         print STDERR "can't rename $mbox to $mbox.$$: $!\n";
  64.         next;
  65.         }
  66.         system "Mail -s 'bogus mailbox from $mbox@$host' postmaster < $mbox.$$";
  67.         unlink("$mbox.$$") || print STDERR "can't unlink $mbox.$$: $!\n";
  68.     }
  69.     next;
  70.     }  
  71.  
  72.     chop($alias) unless $slow;
  73.     $alias =~ s/^\s*(.*)\s*$/$1/g;
  74.     $alias =~ s/$DOMAIN//g;
  75.  
  76.     if ($alias eq "$host!$mbox"  ||     # uucp style alias to me
  77.     $alias eq "$mbox@$host"  ||     # inet style alias to me
  78.     $alias eq $mbox          ||     # no host specified
  79.     $alias eq "localhost!$host")     # strange alias
  80.     {
  81.     print "ok for $mbox to live on $host, as alias is $alias\n" 
  82.         if $verbose;
  83.     next;
  84.      } 
  85.  
  86.     if ($st_size == 0) {
  87.     print "deleting zero-length mbox $mbox\n";
  88.     if ($doit) {
  89.         unlink($mbox) || print STDERR "can't unlink $mbox: $!\n";
  90.     }
  91.     next;
  92.     } 
  93.  
  94.  
  95.     print "forwarding misdelivered $mbox to $alias "; 
  96.  
  97.     unless ($doit) {
  98.     print "\n";
  99.     next;
  100.     } 
  101.  
  102.     $alias = $mbox if $alias =~ /\|/;
  103.  
  104.     unless (rename($mbox, "$mbox.$$")) {
  105.     print STDERR "can't rename $mbox to $mbox.$$: $!\n";
  106.     next;
  107.     }
  108.  
  109.     unless (open(mbox, "$mbox.$$")) {
  110.     print STDERR "can't read $mbox: $!\n";
  111.     next;
  112.     }
  113.  
  114.     $mail = '';
  115.     $count = 0;
  116.     $status = 0;
  117.     while (<mbox>) {
  118.     if (/^From\s/) {
  119.         if ($mail) {
  120.         chop($mail);
  121.         $status |= &send($mail, $alias);
  122.         $mail = '';
  123.         } 
  124.     } else {
  125.         $mail .= $_;
  126.     } 
  127.     } 
  128.     $status |= &send($mail, $alias) if $mail;
  129.  
  130.     print "%d piece%s\n", $count, $count == 1 ? "" : "s";
  131.  
  132.     printf "\ttotal size %d, age %s\n", $st_size, &dtime(time - $st_mtime) 
  133.     if $verbose;
  134.  
  135.     if ($status) {
  136.     print STDERR "bad status from mail: $status\n";
  137.     next;
  138.     } 
  139.  
  140.     unlink("$mbox.$$") || print "$mbox.$$ disappeared!\n";
  141. }
  142.  
  143.  
  144. ###########################################################################
  145. sub source {
  146.     local($file) = @_;
  147.     local($return) = 0;
  148.  
  149.     $return = do $file;
  150.     die "couldn't parse $file: $@" if $@;
  151.     die "couldn't do $file: $!" unless defined $return;
  152.     die "couldn't run $file" unless $return;
  153.     $return;
  154. }
  155.  
  156. ###########################################################################
  157.  
  158. sub dtime {
  159.     local($seconds) = $_[0];
  160.     local($days,$hours,$minutes, $retval);
  161.  
  162.     $days =  int($seconds  / (24 * 3600));
  163.     $seconds -= $days * (24 * 3600);
  164.  
  165.     $hours =  int($seconds / 3600);
  166.     $seconds -= $hours * 3600;
  167.  
  168.     $minutes =  int($seconds / 60);
  169.     $seconds -= $minutes * 60;
  170.  
  171.     $retval =  "$days days" if $days;
  172.  
  173.     if ($hours) {
  174.     $retval .= ", " if $retval;
  175.     $retval .= "$hours hours";
  176.     }
  177.  
  178.     if ($minutes) {
  179.     $retval .= ", " if $retval;
  180.     $retval .= "$minutes minutes";
  181.     }
  182.  
  183.     if ($seconds) {
  184.     $retval .= ", " if $retval;
  185.     $retval .= "$seconds seconds";
  186.     }
  187.  
  188.     $retval;
  189.  
  190. sub send {
  191.     local($text, $whom) = @_;
  192.     $count++;
  193.  
  194.     printf "\tpiece %d, size %-5d\n", $count, length($text) if $verbose;
  195.  
  196.     open (PIPE, "|/usr/lib/sendmail -oi -t");
  197.     print PIPE "Resent-to: $alias\n";
  198.     print PIPE "Resent-comment: lost mail for $mbox found on $host\n";
  199.     print PIPE $text;
  200.     !close PIPE;
  201.