#!  /usr/local/bin/perl

$command = '/home/matt/bin/JabberSend.py';

$to = '';
$subject = '';
$bHead = 1;
$body = '';

while(<stdin>)
{

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

if(/^(To|B?CC):.*<?"([^"]+)"@[a-zA-Z0-9-\.]+>?/)
{
	$to = $2;
}

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

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

}

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