]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/MIME.pm
* Fix synatx error in scripts/process.in
[debbugs.git] / Debbugs / MIME.pm
1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later
3 # version at your option.
4 # See the file README and COPYING for more information.
5 #
6 # [Other people have contributed to this file; their copyrights should
7 # go here too.]
8 # Copyright 2006 by Don Armstrong <don@donarmstrong.com>.
9
10
11 package Debbugs::MIME;
12
13 use strict;
14
15 use base qw(Exporter);
16 use vars qw($VERSION @EXPORT_OK);
17
18 BEGIN {
19     $VERSION = 1.00;
20
21     @EXPORT_OK = qw(parse decode_rfc1522 encode_rfc1522 convert_to_utf8 create_mime_message getmailbody);
22 }
23
24 use File::Path;
25 use MIME::Parser;
26
27 # for decode_rfc1522
28 use MIME::WordDecoder qw();
29 use Encode qw(decode encode encode_utf8 decode_utf8 is_utf8);
30
31 # for encode_rfc1522
32 use MIME::Words qw();
33
34 sub getmailbody
35 {
36     my $entity = shift;
37     my $type = $entity->effective_type;
38     if ($type eq 'text/plain' or
39             ($type =~ m#text/# and $type ne 'text/html') or
40             $type eq 'application/pgp') {
41         return $entity->bodyhandle;
42     } elsif ($type eq 'multipart/alternative') {
43         # RFC 2046 says we should use the last part we recognize.
44         for my $part (reverse $entity->parts) {
45             my $ret = getmailbody($part);
46             return $ret if $ret;
47         }
48     } else {
49         # For other multipart types, we just pretend they're
50         # multipart/mixed and run through in order.
51         for my $part ($entity->parts) {
52             my $ret = getmailbody($part);
53             return $ret if $ret;
54         }
55     }
56     return undef;
57 }
58
59 sub parse ($)
60 {
61     # header and decoded body respectively
62     my (@headerlines, @bodylines);
63
64     my $parser = new MIME::Parser;
65     mkdir "mime.tmp.$$", 0777;
66     $parser->output_under("mime.tmp.$$");
67     my $entity = eval { $parser->parse_data($_[0]) };
68
69     if ($entity and $entity->head->tags) {
70         @headerlines = @{$entity->head->header};
71         chomp @headerlines;
72
73         my $entity_body = getmailbody($entity);
74         @bodylines = $entity_body ? $entity_body->as_lines() : ();
75         chomp @bodylines;
76     } else {
77         # Legacy pre-MIME code, kept around in case MIME::Parser fails.
78         my @msg = split /\n/, $_[0];
79         my $i;
80
81         for ($i = 0; $i <= $#msg; ++$i) {
82             $_ = $msg[$i];
83             last unless length;
84             while ($msg[$i + 1] =~ /^\s/) {
85                 ++$i;
86                 $_ .= "\n" . $msg[$i];
87             }
88             push @headerlines, $_;
89         }
90
91         @bodylines = @msg[$i .. $#msg];
92     }
93
94     rmtree "mime.tmp.$$", 0, 1;
95
96     # Remove blank lines.
97     shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
98
99     # Strip off RFC2440-style PGP clearsigning.
100     if (@bodylines and $bodylines[0] =~ /^-----BEGIN PGP SIGNED/) {
101         shift @bodylines while @bodylines and length $bodylines[0];
102         shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
103         for my $findsig (0 .. $#bodylines) {
104             if ($bodylines[$findsig] =~ /^-----BEGIN PGP SIGNATURE/) {
105                 $#bodylines = $findsig - 1;
106                 last;
107             }
108         }
109         map { s/^- // } @bodylines;
110     }
111
112     return { header => [@headerlines], body => [@bodylines]};
113 }
114
115 =head2 create_mime_message
116
117      create_mime_message([To=>'don@debian.org'],$body,[$attach1, $attach2]);
118
119 Creates a MIME encoded message with headers given by the first
120 argument, and a message given by the second.
121
122 Optional attachments can be specified in the third arrayref argument.
123
124 Headers are passed directly to MIME::Entity::build, the message is the
125 first attachment.
126
127 Each of the elements of the attachment arrayref is attached as an
128 rfc822 message if it is a scalar or an arrayref; otherwise if it is a
129 hashref, the contents are passed as an argument to
130 MIME::Entity::attach
131
132 =cut
133
134 sub create_mime_message{
135      my ($headers,$body,$attachments) = @_;
136      $attachments = [] if not defined $attachments;
137
138      die "The first argument to create_mime_message must be an arrayref" unless ref($headers) eq 'ARRAY';
139      die "The third argument to create_mime_message must be an arrayref" unless ref($attachments) eq 'ARRAY';
140
141      # Build the message
142      # MIME::Entity is stupid, and doesn't rfc1522 encode its headers, so we do it for it.
143      my $msg = MIME::Entity->build('Content-Type' => 'text/plain; charset=utf-8',
144                                    'Encoding'     => 'quoted-printable',
145                                    (map{encode_rfc1522($_)} @{$headers}),
146                                    Data    => $body
147                                   );
148
149      # Attach the attachments
150      for my $attachment (@{$attachments}) {
151           if (ref($attachment) eq 'HASH') {
152                $msg->attach(%{$attachment});
153           }
154           else {
155                # This is *craptacular*, but because various MTAs
156                # (sendmail and exim4, at least) appear to eat From
157                # lines in message/rfc822 attachments, we need eat
158                # the entire From line ourselves so the MTA doesn't
159                # leave \n detrius around.
160                if (ref($attachment) eq 'ARRAY' and $attachment->[1] =~ /^From /) {
161                     # make a copy so that we don't screw up anything
162                     # that is expecting this arrayref to stay constant
163                     $attachment = [@{$attachment}];
164                     # remove the from line
165                     splice @$attachment, 1, 1;
166                }
167                elsif (not ref($attachment)) {
168                     # It's a scalar; remove the from line
169                     $attachment =~ s/^(Received:[^\n]+\n)(From [^\n]+\n)/$1/s;
170                }
171                $msg->attach(Type => 'message/rfc822',
172                             Data => $attachment,
173                             Encoding => '7bit',
174                            );
175           }
176      }
177      return $msg->as_string;
178 }
179
180
181 # Bug #61342 et al.
182
183 sub convert_to_utf8 {
184      my ($data, $charset) = @_;
185      # raw data just gets returned (that's the charset WordDecorder
186      # uses when it doesn't know what to do)
187      return $data if $charset eq 'raw' or is_utf8($data,1);
188      my $result;
189      eval {
190           # this encode/decode madness is to make sure that the data
191           # really is valid utf8 and that the is_utf8 flag is off.
192           $result = encode("utf8",decode($charset,$data))
193      };
194      if ($@) {
195           warn "Unable to decode charset; '$charset' and '$data': $@";
196           return $data;
197      }
198      return $result;
199 }
200
201
202 =head2 decode_rfc1522
203
204     decode_rfc1522('=?iso-8859-1?Q?D=F6n_Armstr=F3ng?= <don@donarmstrong.com>')
205
206 Turn RFC-1522 names into the UTF-8 equivalent.
207
208 =cut
209
210 BEGIN {
211     # Set up the default RFC1522 decoder, which turns all charsets that
212     # are supported into the appropriate UTF-8 charset.
213     MIME::WordDecoder->default(new MIME::WordDecoder(
214         ['*' => \&convert_to_utf8,
215         ]));
216 }
217
218 sub decode_rfc1522 ($)
219 {
220     my ($string) = @_;
221
222     # this is craptacular, but leading space is hacked off by unmime.
223     # Save it.
224     my $leading_space = '';
225     $leading_space = $1 if $string =~ s/^(\s+)//;
226     # unmime calls the default MIME::WordDecoder handler set up at
227     # initialization time.
228     return $leading_space . MIME::WordDecoder::unmime($string);
229 }
230
231 =head2 encode_rfc1522
232
233      encode_rfc1522('Dön Armströng <don@donarmstrong.com>')
234
235 Encodes headers according to the RFC1522 standard by calling
236 MIME::Words::encode_mimeword on distinct words as appropriate.
237
238 =cut
239
240 # We cannot use MIME::Words::encode_mimewords because that function
241 # does not handle spaces properly at all.
242
243 sub encode_rfc1522 ($) {
244      my ($rawstr) = @_;
245
246      # handle being passed undef properly
247      return undef if not defined $rawstr;
248      # We process words in reverse so we can preserve spacing between
249      # encoded words. This regex splits on word|nonword boundaries and
250      # nonword|nonword boundaries.
251      my @words = reverse split /(?:(?<=[\s\n])|(?=[\s\n]))/m, $rawstr;
252
253      my $previous_word_encoded = 0;
254      my $string = '';
255      for my $word (@words) {
256           if ($word !~ m#[\x00-\x1F\x7F-\xFF]#o and $word ne ' ') {
257                $string = $word.$string;
258                $previous_word_encoded=0;
259           }
260           elsif ($word =~ /^[\s\n]$/) {
261                $string = $word.$string;
262                $previous_word_encoded = 0 if $word eq "\n";
263           }
264           else {
265                my $encoded = MIME::Words::encode_mimeword($word, 'q', 'UTF-8');
266                # RFC 1522 mandates that segments be at most 76 characters
267                # long. If that's the case, we split the word up into 10
268                # character pieces and encode it. We must use the Encode
269                # magic here to avoid breaking on bit boundaries here.
270                if (length $encoded > 75) {
271                     # Turn utf8 into the internal perl representation
272                     # so . is a character, not a byte.
273                     my $tempstr = decode_utf8($word,Encode::FB_DEFAULT);
274                     my @encoded;
275                     # Strip it into 10 character long segments, and encode
276                     # the segments
277                     # XXX It's possible that these segments are > 76 characters
278                     while ($tempstr =~ s/(.{1,10})$//) {
279                          # turn the character back into the utf8 representation.
280                          my $tempword = encode_utf8($1);
281                          # It may actually be better to eventually use
282                          # the base64 encoding here, but I'm not sure
283                          # if that's as widely supported as quoted
284                          # printable.
285                          unshift @encoded, MIME::Words::encode_mimeword($tempword,'q','UTF-8');
286                     }
287                     $encoded = join(" ",@encoded);
288                     # If the previous word was encoded, we must
289                     # include a trailing _ that gets encoded as a
290                     # space.
291                     $encoded =~ s/\?\=$/_\?\=/ if $previous_word_encoded;
292                     $string = $encoded.$string;
293                }
294                else {
295                     # If the previous word was encoded, we must
296                     # include a trailing _ that gets encoded as a
297                     # space.
298                     $encoded =~ s/\?\=$/_\?\=/ if $previous_word_encoded;
299                     $string = $encoded.$string;
300                }
301                $previous_word_encoded = 1;
302           }
303      }
304      return $string;
305 }
306
307 1;