6 use vars qw($VERSION @EXPORT_OK);
11 @EXPORT_OK = qw(parse decode_rfc1522 encode_rfc1522 convert_to_utf8);
18 use MIME::WordDecoder qw();
19 use Encode qw(decode encode encode_utf8 decode_utf8 is_utf8);
28 my $type = $entity->effective_type;
29 if ($type eq 'text/plain' or
30 ($type =~ m#text/# and $type ne 'text/html') or
31 $type eq 'application/pgp') {
32 return $entity->bodyhandle;
33 } elsif ($type eq 'multipart/alternative') {
34 # RFC 2046 says we should use the last part we recognize.
35 for my $part (reverse $entity->parts) {
36 my $ret = getmailbody($part);
40 # For other multipart types, we just pretend they're
41 # multipart/mixed and run through in order.
42 for my $part ($entity->parts) {
43 my $ret = getmailbody($part);
52 # header and decoded body respectively
53 my (@headerlines, @bodylines);
55 my $parser = new MIME::Parser;
56 mkdir "mime.tmp.$$", 0777;
57 $parser->output_under("mime.tmp.$$");
58 my $entity = eval { $parser->parse_data($_[0]) };
60 if ($entity and $entity->head->tags) {
61 @headerlines = @{$entity->head->header};
64 my $entity_body = getmailbody($entity);
65 @bodylines = $entity_body ? $entity_body->as_lines() : ();
68 # Legacy pre-MIME code, kept around in case MIME::Parser fails.
69 my @msg = split /\n/, $_[0];
72 for ($i = 0; $i <= $#msg; ++$i) {
75 while ($msg[$i + 1] =~ /^\s/) {
77 $_ .= "\n" . $msg[$i];
79 push @headerlines, $_;
82 @bodylines = @msg[$i .. $#msg];
85 rmtree "mime.tmp.$$", 0, 1;
88 shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
90 # Strip off RFC2440-style PGP clearsigning.
91 if (@bodylines and $bodylines[0] =~ /^-----BEGIN PGP SIGNED/) {
92 shift @bodylines while @bodylines and length $bodylines[0];
93 shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
94 for my $findsig (0 .. $#bodylines) {
95 if ($bodylines[$findsig] =~ /^-----BEGIN PGP SIGNATURE/) {
96 $#bodylines = $findsig - 1;
100 map { s/^- // } @bodylines;
103 return { header => [@headerlines], body => [@bodylines]};
108 sub convert_to_utf8 {
109 my ($data, $charset) = @_;
110 # raw data just gets returned (that's the charset WordDecorder
111 # uses when it doesn't know what to do)
112 return $data if $charset eq 'raw' or is_utf8($data,1);
115 # this encode/decode madness is to make sure that the data
116 # really is valid utf8 and that the is_utf8 flag is off.
117 $result = encode("utf8",decode($charset,$data))
120 warn "Unable to decode charset; '$charset' and '$data': $@";
127 =head2 decode_rfc1522
129 decode_rfc1522('=?iso-8859-1?Q?D=F6n_Armstr=F3ng?= <don@donarmstrong.com>')
131 Turn RFC-1522 names into the UTF-8 equivalent.
136 # Set up the default RFC1522 decoder, which turns all charsets that
137 # are supported into the appropriate UTF-8 charset.
138 MIME::WordDecoder->default(new MIME::WordDecoder(
139 ['*' => \&convert_to_utf8,
143 sub decode_rfc1522 ($)
147 # unmime calls the default MIME::WordDecoder handler set up at
148 # initialization time.
149 return MIME::WordDecoder::unmime($string);
152 =head2 encode_rfc1522
154 encode_rfc1522('Dön Armströng <don@donarmstrong.com>')
156 Encodes headers according to the RFC1522 standard by calling
157 MIME::Words::encode_mimeword on distinct words as appropriate.
161 # We cannot use MIME::Words::encode_mimewords because that function
162 # does not handle spaces properly at all.
164 sub encode_rfc1522 ($) {
167 # We process words in reverse so we can preserve spacing between
168 # encoded words. This regex splits on word|nonword boundaries and
169 # nonword|nonword boundaries.
170 my @words = reverse split /(?:(?<=[\s\n])|(?=[\s\n]))/m, $rawstr;
172 my $previous_word_encoded = 0;
174 for my $word (@words) {
175 if ($word !~ m#[\x00-\x1F\x7F-\xFF]#o and $word ne ' ') {
176 $string = $word.$string;
177 $previous_word_encoded=0;
179 elsif ($word =~ /^[\s\n]$/) {
180 $string = $word.$string;
181 $previous_word_encoded = 0 if $word eq "\n";
184 my $encoded = MIME::Words::encode_mimeword($word, 'q', 'UTF-8');
185 # RFC 1522 mandates that segments be at most 76 characters
186 # long. If that's the case, we split the word up into 10
187 # character pieces and encode it. We must use the Encode
188 # magic here to avoid breaking on bit boundaries here.
189 if (length $encoded > 75) {
190 # Turn utf8 into the internal perl representation
191 # so . is a character, not a byte.
192 my $tempstr = decode_utf8($word,Encode::FB_DEFAULT);
194 # Strip it into 10 character long segments, and encode
196 # XXX It's possible that these segments are > 76 characters
197 while ($tempstr =~ s/(.{1,10})$//) {
198 # turn the character back into the utf8 representation.
199 my $tempword = encode_utf8($1);
200 # It may actually be better to eventually use
201 # the base64 encoding here, but I'm not sure
202 # if that's as widely supported as quoted
204 unshift @encoded, MIME::Words::encode_mimeword($tempword,'q','UTF-8');
206 $encoded = join(" ",@encoded);
207 # If the previous word was encoded, we must
208 # include a trailing _ that gets encoded as a
210 $encoded =~ s/\?\=$/_\?\=/ if $previous_word_encoded;
211 $string = $encoded.$string;
214 # If the previous word was encoded, we must
215 # include a trailing _ that gets encoded as a
217 $encoded =~ s/\?\=$/_\?\=/ if $previous_word_encoded;
218 $string = $encoded.$string;
220 $previous_word_encoded = 1;