Article Listing 1 jun2005.tar

Listing 1 badadd.pl

#!/usr/bin/perl -w

#PART ONE: locate and extract the lines containing the addresses for 
#each error code

#Define my input file $infile as the mail file (inbox) for the user to 
#whom all the bounces come
$infile="feedback_mailfile";
$x=0;

#define a separate search for each of my 6 defined error codes
$search1 = 'Status: 5.0.0';
$search2 = 'Status: 5.1.1';
$search3 = 'Status: 5.1.2';
$search4 = 'Status: 5.1.3';
$search5 = 'Status: 5.1.6';
$search6 = 'Status: 5.1.8';

$status="";
$i=0;
open (MAILFILE, $infile) || die("Could not open input file");

#Open a file for each of my searches - .tmp because what is extracted 
#still needs some processing
open (temp1, '>bad-500.tmp') || die("Could not open output file");
open (temp2, '>bad-511.tmp') || die("Could not open output file");
open (temp3, '>bad-512.tmp') || die("Could not open output file");
open (temp4, '>bad-513.tmp') || die("Could not open output file");
open (temp5, '>bad-516.tmp') || die("Could not open output file");
open (temp6, '>bad-518.tmp') || die("Could not open output file");

while(<MAILFILE>){
        $addi = 'Final-Recipient:';        # the Final Recipient 
                                           # contains the address
        $status = $_;
        if ($status =~ /$addi/){
                $address = $_;
                for ($x=1;$x<3;$x++){   # look up two lines from the 
                                          # line containing the error code
                        $_=<MAILFILE>;
                        $status = $_;
                 if ($status =~ /$search1/){
                                print temp1 "$address";
                        } # end if 
                 if ($status =~ /$search2/){
                                print temp2 "$address";
                        } # end if 
                 if ($status =~ /$search3/){
                                print temp3 "$address";
                        } # end if 
                 if ($status =~ /$search4/){
                                print temp4 "$address";
                        } # end if 
                 if ($status =~ /$search5/){
                                print temp5 "$address";
                        } # end if 
                 if ($status =~ /$search6/){
                                print temp6 "$address";
                        } # end if 
                } # end for     
        } # end if              
        $i++;
        $status="";
} # end while
close MAILFILE;
close temp1, temp2, temp3, temp4, temp5, temp6;

#PART TWO: remove extraneous content from each of the 6 tmp files

$inputfile="bad-500.tmp";    # define the files and call the 
                              # addressonly subroutine
$outfile="bad-500.txt";
&addressonly;

$inputfile="bad-511.tmp";
$outfile="bad-511.txt";
&addressonly;

$inputfile="bad-512.tmp";
$outfile="bad-512.txt";
&addressonly;

$inputfile="bad-513.tmp";
$outfile="bad-513.txt";
&addressonly;

$inputfile="bad-516.tmp";
$outfile="bad-516.txt";
&addressonly;

$inputfile="bad-518.tmp";
$outfile="bad-518.txt";
&addressonly;

sub addressonly{

open (INFILE, $inputfile) || die("Could not open input file!");
open (BOUNCE, ">$outfile") || die("Could not open input file!");

$i=0;
while(<INFILE>){
        $line = $_;
        @a = split(" ", $line);    # split the line into fields
        @ra = reverse(@a);         # reverse the fields so the address 
                                   # is the first field
        @sra = shift(@ra);         # shift off everything after the 
                                   # address
        print BOUNCE "@sra \n";
        $i++;                  # move to the next line in the tmp file
} #end while
}
close INFILE, BOUNCE;

# One last thing - remove the temp files

unlink("bad-500.tmp", "bad-511.tmp", "bad-512.tmp", "bad-513.tmp", \
  "bad-516.tmp", "bad-518.tmp") or die "Couldn't unlink $!\n";

#end of script