#!  /usr/local/bin/perl

$command = '/home/matt/bin/FetchOK';
$user='';
$to='';
$subject='';
$pass='';

$bHead = 1;
$body = '';

while(<stdin>)
{

if(!$bHead) {
$body .= $_;
next;
}

#if(/^From:\s*(.*<)?([^\s]*?)\+([^\s-\+]+)(-[^\s]*)?@/) {
if(/^From:\s*(.*<)?([^\s-+]*)([-+][^\s]*)?@/) {
	$user = $2;
}

if(/^OKAuth:\s*([\a\A\d\D]+?)\s*$/) {
	$pass = $1;
}

if(/^(To|B?CC):\s*(.*)?@/) {
	$to = $2;
}

if(/^Subject:\s*(.*)$/) {
	$subject = $1;
}

if(/^$/) { $bHead=0; };

}

if(($user ne "") and ($to ne "")) {
	$torun = "${command} -u '${user}' -p '${pass}' -t '${to}' -s '${subject}'";
	#print $torun ; 
	if(open INJECT, "|-", $torun) {
		print INJECT $body;
		close INJECT;
	} else {
		print "Error opening ouput.\n";
	}
}
