#!/usr/bin/perl -w

# Usage: unsub.extractor.pl  <FILOUT>
#
# Example:  unsub.extractor.pl unsubs.9nov05.txt
#
# David Harris, Version of 6 Jan 2006 19:00 PT

# ------------------------------------------------

# Check the number of arguments on the command line:
if ($#ARGV+1 != 1) {
  die "unsub.extractor.pl needs <FILOUT> to use";
};

# Hard coded full path to the unsubscribers file, which is assumed "Compact Files" processed
$filein = "C:\\Documents and Settings\\User 2\\Application Data\\Thunderbird\\Profiles\\nijx0stm.default\\Mail\\pop.earthlink.net\\Inbox";
open FILIN, "$filein"
  or die "unsub.extractor.pl <FILIN> file not found.\n";
open FILOUT, ">$ARGV[0]"
  or die "unsub.extractor.pl <FILOUT> file not made.\n";

#Read through FILIN file:
while ($lineread = <FILIN>) {
  #$lineread =~ s/\[mailto:.+$//;  #Strip off the "[mailto:rstephens..." stuff
  chomp $lineread;
  if (($lineread =~ m/^From: / ) && !($lineread =~ s/\[mailto:.+$//)) {
    $atsign = index($lineread,"@",5);
    if ($atsign > -1) {  #May contain an email near '@'
      #Seek backward to start of email address:
      $loc = 0;
      while ( (substr($lineread, $atsign-$loc, 1) ne " ") 
          &&  (substr($lineread, $atsign-$loc, 1) ne "<") ) {
        $thechar = substr($lineread,$atsign-$loc, 1);
        $loc++;
      }
      $emailstart = $atsign-$loc+1;
      #Seek forward to end of email address:
      $loc = 0;
      while ( (substr($lineread, $atsign+$loc, 1) ne " ")
          &&  (substr($lineread, $atsign+$loc, 1) ne ">")
          &&  ($atsign+$loc < (length $lineread))) {
        $thechar = substr($lineread,$atsign+$loc, 1);
        $loc++;
      }
      $emailend = $atsign+$loc;
    }
    if (($emailend-$emailstart) < 5) {
      $a=$a;
    }
    $email = lc substr($lineread,$emailstart,$emailend-$emailstart);
    print FILOUT "$email\n" ;
  };
}  
close FILOUT;
close FILIN;
exit;