8 use vars qw($VERSION @ISA @EXPORT_OK);
14 @EXPORT_OK = qw(parse);
21 my $type = $entity->effective_type;
22 if ($type eq 'text/plain' or
23 ($type =~ m#text/# and $type ne 'text/html') or
24 $type eq 'application/pgp') {
25 return $entity->bodyhandle;
26 } elsif ($type eq 'multipart/alternative') {
27 # RFC 2046 says we should use the last part we recognize.
28 for my $part (reverse $entity->parts) {
29 my $ret = getmailbody($part);
33 # For other multipart types, we just pretend they're
34 # multipart/mixed and run through in order.
35 for my $part ($entity->parts) {
36 my $ret = getmailbody($part);
45 # header and decoded body respectively
46 my (@headerlines, @bodylines);
48 my $parser = new MIME::Parser;
49 mkdir "mime.tmp.$$", 0777;
50 $parser->output_under("mime.tmp.$$");
51 my $entity = eval { $parser->parse_data($_[0]) };
53 if ($entity and $entity->head->tags) {
54 @headerlines = @{$entity->head->header};
57 my $entity_body = getmailbody($entity);
58 @bodylines = $entity_body ? $entity_body->as_lines() : ();
61 # Legacy pre-MIME code, kept around in case MIME::Parser fails.
62 my @msg = split /\n/, $_[0];
65 for ($i = 0; $i <= $#msg; ++$i) {
68 while ($msg[$i + 1] =~ /^\s/) {
70 $_ .= "\n" . $msg[$i];
72 push @headerlines, $_;
75 @bodylines = @msg[$i .. $#msg];
78 rmtree "mime.tmp.$$", 0, 1;
81 shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
83 # Strip off RFC2440-style PGP clearsigning.
84 if (@bodylines and $bodylines[0] =~ /^-----BEGIN PGP SIGNED/) {
85 shift @bodylines while @bodylines and length $bodylines[0];
86 shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
87 for my $findsig (0 .. $#bodylines) {
88 if ($bodylines[$findsig] =~ /^-----BEGIN PGP SIGNATURE/) {
89 $#bodylines = $findsig - 1;
93 map { s/^- // } @bodylines;
96 return { header => [@headerlines], body => [@bodylines]};