#!/usr/bin/perl -w

# Usage: procreturns.pl

# David Harris, Version of 13 May 2005 pm 3:00

# Goes thru the returned emails in all the files, extracting email names and
# specifying the action to be taken [possibly in SQL for MySQL database]
$DIRNAME = "C:\\Documents and Settings\\User 2\\Application Data\\Thunderbird\\Profiles\\nijx0stm.default\\Mail\\pop.darwindaycelebration-1.org\\Returns.sbd";
$hit_end = 0; #FALSE
$inputline = '';
$lastreturntype = '';

#sub get1msg (PROTO);  # Subroutine to read 1 msg into array for examination
                      # and extraction of essential type-specific data.
#sub checkline (PROTO); # TO retrieve 1 line from the array.

sub get1msg {  # argsin: $DIRNAME, $returntype
               # argsout: $end_of_this_kind
  $i = 0; # Line pointer for array that holds 1 message.
  # Did the last time in this routine end with a saved "From" line?
  if ($inputline =~ m/^From - /) {
    $msgarray[$i] = $inputline;
    $i++;
  }
  if ($hit_end) { return $end_of_this_kind = 1 }  # $hit_end set on previous call
  $MAILFILE = "$DIRNAME\\$returntype";
  if (-z $MAILFILE) { return $end_of_this_kind = 1 } # If an empty file.
  # Is this a new file to start on?
  if ($returntype ne $lastreturntype) {
  #We're in a new return type, hold its name for next call:
  $lastreturntype = $returntype;
  # First message in file
  open MAILFILE, "<$MAILFILE" or die "procreturns.pl: Can't open $MAILFILE\n";
  }
  # Read the first line (which has "From - ")
  $inputline = <MAILFILE>;
  print $inputline;
  $msgarray[$i] = $inputline;
  $i++;
  # Now read lines into array until hit the first line of next message or EOF:

  while ($inputline = <MAILFILE>) {
    if (eof MAILFILE) {
      $hit_end = 1; #TRUE flag for next call
      return $end_of_this_kind = 1;
    }
    if ($inputline !~ m/^From - /) {
      print $inputline;
      $msgarray[$i] = $inputline;
      $i++;
    } else { # Hit a "From - " line
       return $end_of_this_kind = 0;
       $i--; # Point to last line of array.
    }  #Working here!!!!!!!!!!!!!!!!!!
  } # Closing while loop
  $a++; #Would an EOF read go here?
}
#-----------------------------------------

# Main routine starts here.
opendir RETMAILDIR, "$DIRNAME" or die "procreturns.pl: Can't read that directory!";
    @allfiles = grep !/\./, readdir RETMAILDIR; # Removes all '.'-containing filenames
closedir RETMAILDIR;
    print "\n\n@allfiles\n";

# One variety of return messages at a time:
ONEKIND: foreach $returntype (@allfiles) {
  $end_of_this_kind = get1msg($DIRNAME, $returntype);
  if (! $end_of_this_kind) {
    SWITCH: {
      if ($returntype eq "AOLUnknown") { print "$returntype\n";
        last SWITCH; }
      if ($returntype eq "Blockedbybulk") { print "$returntype\n";
        last SWITCH; }
      if ($returntype eq "DeliveryStatFailure") { print "$returntype\n";
        last SWITCH; }
      if ($returntype eq "DelivStatNotifDelay") { print "$returntype\n";
        last SWITCH; }
      if ($returntype eq "ErrorDuringDelivery") { print "$returntype\n";
        last SWITCH; }
      # ...
    }  # Switch
  }  # If not end
  else {
    next ONEKIND;
  }
} # All files of returned messages have been handled by here.

exit;  # End of program
