#! /usr/bin/perl -w
#
# Extract the body of a mail message and feed it to another script.
#

use strict;
use MIME::Parser;
use Getopt::Long;

my $PROG;

GetOptions('e|exec=s' => \$PROG);

my $parser = new MIME::Parser;
$parser->output_to_core(10000000);
$parser->output_dir('/tmp/');
eval {
    $parser->tmp_recycling(0);
};
my $entity = $parser->read(\*STDIN)
    or die("Couldn't get message from stdin!");

#
# Get the body.
#
my $body = $entity->bodyhandle()->as_string();
if ($PROG) {
    #
    # Pipe it.
    #
    open(PROG, "| $PROG") or die("Can't execute '$PROG'");
    print PROG $body;
    close(PROG);
}
else {
    print $body;
}
