=head1 NAME
mimedump - dump out the contents of a parsed MIME message
=head1 DESCRIPTION
Read a MIME stream from the stdin, and dump its contents to the stdout.
=head1 SYNOPSIS
mimedump < mime.msg
=head1 AUTHOR
Andreas Koenig suggested this, and wrote the original code.
Eryq perverted the hell out of it.
=cut
use MIME::Parser;
sub dump_entity {
my ($entity, $name) = @_;
defined($name) or $name = "'anonymous'";
my $IO;
print "\n", '=' x 60, "\n";
print "Message $name: ";
print "\n", '=' x 60, "\n\n";
print $entity->head->original_text;
print "\n";
my @parts = $entity->parts;
if (@parts) { my $i;
foreach $i (0 .. $ dump_entity($parts[$i], ("$name, part ".(1+$i)));
}
}
else {
my ($type, $subtype) = split('/', $entity->head->mime_type);
my $body = $entity->bodyhandle;
if ($type =~ /^(text|message)$/) { if ($IO = $body->open("r")) {
print $_ while (defined($_ = $IO->getline));
$IO->close;
}
else { print "$0: couldn't find/open '$name': $!";
}
}
else { my $path = $body->path;
my $size = ($path ? (-s $path) : '???');
print ">>> This is a non-text message, $size bytes long.\n";
print ">>> It is stored in ", ($path ? "'$path'" : 'core'),".\n\n";
}
}
1;
}
sub main {
print STDERR "(reading from stdin)\n" if (-t STDIN);
my $parser = new MIME::Parser;
(-d "mimedump-tmp") or mkdir "mimedump-tmp",0755 or die "mkdir: $!";
(-w "mimedump-tmp") or die "can't write to directory";
$parser->output_dir("mimedump-tmp");
$entity = $parser->read(\*STDIN) or die "couldn't parse MIME stream";
dump_entity($entity);
}
exit(&main ? 0 : -1);
1;