#! /usr/bin/perl 
#
# 
# script demonstrating how to build a vortex based analyzer to extract emails for further analysis

#warnings are usually helpful
use strict;



my $filename;
my $parser_status = 0;
# 0 => looking for DATA
# 1 => looking for .
my $line;
my $email_index = 0;
my $data_buffer;
my $output_dir;
my $output_filename;


if ( @ARGV > 0 )
{
	$output_dir = $ARGV[0];
}

#print("Using output dir: $output_dir\n");

while(<STDIN>)
{
	$filename = $_;
	chomp($filename);
	$email_index = 0;
	$parser_status = 0;
	$data_buffer = "";
	
	if (open(TO_SERVER_STREAM, $filename))
	{
		while(<TO_SERVER_STREAM>)
		{
			$line = $_;
			if ($parser_status == 0)
			{
				#TODO: Some amount of envelope collection for smtp logging.
				
				
				if ($line =~ m/^DATA\s{1,2}/)
				{
					$parser_status = 1;
				}	
			} elsif ($parser_status == 1 )
			{
				if ($line =~ m/^\.\s{1,2}/)
				{
					$output_filename = $output_dir."/".substr($filename, (rindex($filename,"/") + 1))."_smtp-".$email_index;
					if (open(EMAIL_OUTPUT, '>', $output_filename))
					{
						print EMAIL_OUTPUT $data_buffer;
						close(EMAIL_OUTPUT);
						print($output_filename."\n");
					} else
					{
						warn("Couldn't open output file: $output_filename");
					}
					#print("$filename $email_index ".length($data_buffer)."\n");
					#TODO add some amount of logging here
					$parser_status = 0;
					$email_index++;
					$data_buffer = "";
				} else
				{
					$data_buffer .= $line;		
				}				
			}		
		}
		close(TO_SERVER_STREAM);
		if ( $parser_status != 0 )
		{
			warn("Unterminated message in: $filename");
		}
	} else
	{
		warn("Couldn't open $filename");	
	}
	unlink($filename);
}
