#!/usr/bin/perl

use warnings;
use strict;
use lib '..', '.';
use Mail::Box::Manager;

#use Mail::Box::Search::Grep;
die "Usage: $0 folderfile\n" unless @ARGV==1;
my $filename = shift @ARGV;
my $mgr    = Mail::Box::Manager->new;
my $folder = $mgr->open
   ( $filename,extract=>'LAZY');
 
die "Cannot open $filename: $!\n" unless defined $folder;
 
my @messages = $folder->messages;
my $count=0;

foreach my $msg (@messages)
	{  
        $count=$count+1;
        print "message $count\n";
	my ($part, $junk, $to, $from);
$msg->printStructure;
	if($msg->isMultipart) 
		{
		foreach $part ($msg->body->parts) 
			{
			#$part->print;
			my $attachhead = $part->head;
			my $attachbody = $part->body;
			if ( $attachhead =~ "Content-Type: message/rfc822")			
				{
				#print "head is \n";
				#print $attachhead ;
				#print "body is \n";
				#print $attachbody ;
		                ($junk,$to)= split /for /,$attachbody,2;
		                ($to,$junk)= split /\;/,$to,2;
				print "$to\n";
				my @lines=$part->lines;
				my $max=scalar @lines;
				#print "scalar is $max \n";
				my $index;
				for ($index=0; $index<$max; $index++)
					{
					#print "line $index i$max \n";
					#print "$lines[$index]";
					if ($lines[$index] =~ /From:/)
						{
		                		($junk,$from)= split /From:/,$lines[$index],2;
						print "FROM $from \n" ;
						$index=$max;
						}	
					}
				#print @lines;
				}
			}
		}
	}
$folder->close;
