#!/usr/bin/perl
#
# ol2sanity - Outlook replies to Sanity :)
#
# input, an outlook mail ready to be replied to with headers... note this
#   is not the original outlook mail, but the one after choosing to reply
#   to it.

$H = 0;
$R = 1;
$OH = 2;
$OD = 3;

$state = $H;
$sender = "";

@headers;
@reply;
@oheaders;
@orig;
@mymesg;

while($line=<>) {
    if($state == $H) {
	if($line =~ /^$/) {
	    $state = $R;
	} else {
	    if($line =~ /^Subject: RE/) {
		$line =~ s/RE:/Re:/;
	    }
	    push @headers, $line;
	}
    } elsif($state == $R) {
	if($line =~ /^> -----\ ?Original Message\ ?-----/) {
	    $state = $OH;
	} elsif($line =~ /.*wrote:$/i) {
	    $sender = $line;
	} else {
	    push @reply, $line;
	}
    } elsif($state == $OH) {
	if(($line =~ /^> From:/)||
	   ($line =~ /^> Sent:/)||
	   ($line =~ /^> To:/)||
	   ($line =~ /^> Cc:/)||
	   ($line =~ /^> Subject:/)||
	   ($line =~ /^> $/)) {
	    if($line =~ /From:/) {
		$replier = $line;
		$replier =~ s/.*From: (.*) \[.*/$1/;
		chomp($replier);
	    }
	    push @oheaders, $line;
	} else {
	    push @orig, "> ".$line;
	    $state = $OD;
	}
    } else {
	if($line =~ /^>/) {
	    push @orig, "> ".$line;
	} else {
	    push @mymesg, $line;
	}
    }
}

print @headers;
print "\n";
print $sender;
print "> $replier wrote:\n";
print @orig;
print ">\n";
print @reply;
print "\n";
print @mymesg;

exit(0);

