]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/CGI/Bugreport.pm
decode_utf8 the html record before calling decode_rfc1522; use Debbugs::UTF8
[debbugs.git] / Debbugs / CGI / Bugreport.pm
1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later version. See the
3 # file README and COPYING for more information.
4 #
5 # [Other people have contributed to this file; their copyrights should
6 # be listed here too.]
7 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
8
9
10 package Debbugs::CGI::Bugreport;
11
12 =head1 NAME
13
14 Debbugs::CGI::Bugreport -- specific routines for the bugreport cgi script
15
16 =head1 SYNOPSIS
17
18
19 =head1 DESCRIPTION
20
21
22 =head1 BUGS
23
24 None known.
25
26 =cut
27
28 use warnings;
29 use strict;
30 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
31 use base qw(Exporter);
32
33 use IO::Scalar;
34 use Params::Validate qw(validate_with :types);
35 use Debbugs::MIME qw(decode_rfc1522 create_mime_message);
36 use Debbugs::CGI qw(:url :html :util);
37 use Debbugs::Common qw(globify_scalar english_join);
38 use Debbugs::UTF8;
39 use Debbugs::Config qw(:config);
40 use POSIX qw(strftime);
41 use Encode qw(decode_utf8);
42
43 BEGIN{
44      ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
45      $DEBUG = 0 unless defined $DEBUG;
46
47      @EXPORT = ();
48      %EXPORT_TAGS = ();
49      @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
50      Exporter::export_ok_tags(keys %EXPORT_TAGS);
51      $EXPORT_TAGS{all} = [@EXPORT_OK];
52 }
53
54
55
56 =head2 display_entity
57
58      display_entity(entity      => $entity,
59                     bug_num     => $ref,
60                     outer       => 1,
61                     msg_num     => $msg_num,
62                     attachments => \@attachments,
63                     output      => \$output);
64
65
66 =over
67
68 =item entity -- MIME::Parser entity
69
70 =item bug_num -- Bug number
71
72 =item outer -- Whether this is the outer entity; defaults to 1
73
74 =item msg_num -- message number in the log
75
76 =item attachments -- arrayref of attachments
77
78 =item output -- scalar reference for output
79
80 =back
81
82 =cut
83
84 sub display_entity {
85     my %param = validate_with(params => \@_,
86                               spec   => {entity      => {type => OBJECT,
87                                                         },
88                                          bug_num     => {type => SCALAR,
89                                                          regex => qr/^\d+$/,
90                                                         },
91                                          outer       => {type => BOOLEAN,
92                                                          default => 1,
93                                                         },
94                                          msg_num     => {type => SCALAR,
95                                                         },
96                                          attachments => {type => ARRAYREF,
97                                                          default => [],
98                                                         },
99                                          output      => {type => SCALARREF|HANDLE,
100                                                          default => \*STDOUT,
101                                                         },
102                                          terse       => {type => BOOLEAN,
103                                                          default => 0,
104                                                         },
105                                          msg         => {type => SCALAR,
106                                                          optional => 1,
107                                                         },
108                                          att         => {type => SCALAR,
109                                                          optional => 1,
110                                                         },
111                                          trim_headers => {type => BOOLEAN,
112                                                           default => 1,
113                                                          },
114                                         }
115                              );
116
117     $param{output} = globify_scalar($param{output});
118     my $entity = $param{entity};
119     my $ref = $param{bug_num};
120     my $top = $param{outer};
121     my $xmessage = $param{msg_num};
122     my $attachments = $param{attachments};
123
124     my $head = $entity->head;
125     my $disposition = $head->mime_attr('content-disposition');
126     $disposition = 'inline' if not defined $disposition or $disposition eq '';
127     my $type = $entity->effective_type;
128     my $filename = $entity->head->recommended_filename;
129     $filename = '' unless defined $filename;
130     $filename = decode_rfc1522($filename);
131
132     if ($param{outer} and
133         not $param{terse} and
134         not exists $param{att}) {
135          my $header = $entity->head;
136          print {$param{output}} "<pre class=\"headers\">\n";
137          if ($param{trim_headers}) {
138               my @headers;
139               foreach (qw(From To Cc Subject Date)) {
140                    my $head_field = $head->get($_);
141                    next unless defined $head_field and $head_field ne '';
142                    push @headers, qq(<b>$_:</b> ) . html_escape(decode_rfc1522($head_field));
143               }
144               print {$param{output}} join(qq(), @headers);
145          } else {
146               print {$param{output}} html_escape(decode_rfc1522($entity->head->stringify));
147          }
148          print {$param{output}} "</pre>\n";
149     }
150
151     if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
152              or $type =~ m{^multipart/}
153             )) {
154         push @$attachments, $param{entity};
155         # output this attachment
156         if (exists $param{att} and
157             $param{att} == $#$attachments) {
158             my $head = $entity->head;
159             chomp(my $type = $entity->effective_type);
160             my $body = $entity->stringify_body;
161             # this attachment has its own content type, so we must not
162             # try to convert it to UTF-8 or do anything funky.
163             my @layers = PerlIO::get_layers($param{output});
164             binmode($param{output},':raw');
165             print {$param{output}} "Content-Type: $type";
166             my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
167             print {$param{output}} qq(; charset="$charset") if defined $charset;
168             print {$param{output}} "\n";
169             if ($filename ne '') {
170                 my $qf = $filename;
171                 $qf =~ s/"/\\"/g;
172                 $qf =~ s[.*/][];
173                 print {$param{output}} qq{Content-Disposition: inline; filename="$qf"\n};
174             }
175             print {$param{output}} "\n";
176             my $decoder = MIME::Decoder->new($head->mime_encoding);
177             $decoder->decode(IO::Scalar->new(\$body), $param{output});
178             if (grep {/utf8/} @layers) {
179                 binmode($param{output},':utf8');
180             }
181             return;
182         }
183         elsif (not exists $param{att}) {
184              my @dlargs = (msg=>$xmessage, att=>$#$attachments);
185              push @dlargs, (filename=>$filename) if $filename ne '';
186              my $printname = $filename;
187              $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
188              print {$param{output}} '<pre class="mime">[<a href="' .
189                   html_escape(bug_links(bug => $ref,
190                                         links_only => 1,
191                                         options => {@dlargs})
192                              ) . qq{">$printname</a> } .
193                                   "($type, $disposition)]</pre>\n";
194         }
195     }
196
197     return if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
198     return unless ($type =~ m[^text/?] and
199                    $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
200                   $type =~ m[^application/pgp(?:;|$)] or
201                   $entity->parts;
202
203     if ($entity->is_multipart) {
204         my @parts = $entity->parts;
205         foreach my $part (@parts) {
206             display_entity(entity => $part,
207                            bug_num => $ref,
208                            outer => 0,
209                            msg_num => $xmessage,
210                            output => $param{output},
211                            attachments => $attachments,
212                            terse => $param{terse},
213                            exists $param{msg}?(msg=>$param{msg}):(),
214                            exists $param{att}?(att=>$param{att}):(),
215                           );
216             # print {$param{output}} "\n";
217         }
218     } elsif ($entity->parts) {
219         # We must be dealing with a nested message.
220          if (not exists $param{att}) {
221               print {$param{output}} "<blockquote>\n";
222          }
223         my @parts = $entity->parts;
224         foreach my $part (@parts) {
225             display_entity(entity => $part,
226                            bug_num => $ref,
227                            outer => 1,
228                            msg_num => $xmessage,
229                            output => $param{output},
230                            attachments => $attachments,
231                            terse => $param{terse},
232                            exists $param{msg}?(msg=>$param{msg}):(),
233                            exists $param{att}?(att=>$param{att}):(),
234                           );
235             # print {$param{output}} "\n";
236         }
237          if (not exists $param{att}) {
238               print {$param{output}} "</blockquote>\n";
239          }
240     } elsif (not $param{terse}) {
241          my $content_type = $entity->head->get('Content-Type:') || "text/html";
242          my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
243          my $body = $entity->bodyhandle->as_string;
244          $body = convert_to_utf8($body,$charset//'utf8');
245          $body = html_escape($body);
246          # Attempt to deal with format=flowed
247          if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
248               $body =~ s{^\ }{}mgo;
249               # we ignore the other things that you can do with
250               # flowed e-mails cause they don't really matter.
251          }
252          # Add links to URLs
253          # We don't html escape here because we escape above;
254          # wierd terminators are because of that
255          $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
256                     ((?:\&gt\;)?[)]?(?:'|\&\#39\;)?[:.\,]?(?:\s|$)) # terminators
257               }{<a href=\"$1\">$1</a>$2}gox;
258          # Add links to bug closures
259          $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)]
260                    [my $temp = $1;
261                     $temp =~ s{(\d+)}
262                               {bug_links(bug=>$1)}ge;
263                     $temp;]gxie;
264          if (defined $config{cve_tracker} and
265              length $config{cve_tracker}
266             ) {
267              # Add links to CVE vulnerabilities (closes #568464)
268              $body =~ s{(^|\s)(CVE-\d{4}-\d{4,})(\s|[,.-\[\]]|$)}
269                        {$1<a href="http://$config{cve_tracker}$2">$2</a>$3}gxm;
270          }
271          if (not exists $param{att}) {
272               print {$param{output}} qq(<pre class="message">$body</pre>\n);
273          }
274     }
275 }
276
277
278 =head2 handle_email_message
279
280      handle_email_message($record->{text},
281                           ref        => $bug_number,
282                           msg_num => $msg_number,
283                          );
284
285 Returns a decoded e-mail message and displays entities/attachments as
286 appropriate.
287
288
289 =cut
290
291 sub handle_email_message{
292      my ($email,%param) = @_;
293
294      # output needs to have the is_utf8 flag on to avoid double
295      # encoding
296      my $output = decode_utf8('');
297      my $parser = MIME::Parser->new();
298      # Because we are using memory, not tempfiles, there's no need to
299      # clean up here like in Debbugs::MIME
300      $parser->tmp_to_core(1);
301      $parser->output_to_core(1);
302      my $entity = $parser->parse_data( $email);
303      my @attachments = ();
304      display_entity(entity  => $entity,
305                     bug_num => $param{ref},
306                     outer   => 1,
307                     msg_num => $param{msg_num},
308                     output => \$output,
309                     attachments => \@attachments,
310                     terse       => $param{terse},
311                     exists $param{msg}?(msg=>$param{msg}):(),
312                     exists $param{att}?(att=>$param{att}):(),
313                     exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
314                    );
315      return $output;
316
317 }
318
319 =head2 handle_record
320
321      push @log, handle_record($record,$ref,$msg_num);
322
323 Deals with a record in a bug log as returned by
324 L<Debbugs::Log::read_log_records>; returns the log information that
325 should be output to the browser.
326
327 =cut
328
329 sub handle_record{
330      my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
331
332      # output needs to have the is_utf8 flag on to avoid double
333      # encoding
334      my $output = decode_utf8('');
335      local $_ = $record->{type};
336      if (/html/) {
337          # $record->{text} is not in perl's internal encoding; convert it
338          my $text = decode_rfc1522(decode_utf8($record->{text}));
339           my ($time) = $text =~ /<!--\s+time:(\d+)\s+-->/;
340           my $class = $text =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
341           $output .= $text;
342           # Link to forwarded http:// urls in the midst of the report
343           # (even though these links already exist at the top)
344           $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;)?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
345           # Add links to the cloned bugs
346           $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
347           # Add links to merged bugs
348           $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
349           # Add links to blocked bugs
350           $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
351                       {(defined $2?$1.bug_links(bug=>$2):'').$3.
352                            english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
353           $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
354                        (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
355                       {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
356                            english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
357           $output =~ s{([Aa]dded|[Rr]emoved)( indication that bug )(\d+)( blocks )([\d\s\,]+)}
358                       {$1.$2.(bug_links(bug=>$3)).$4.
359                            english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}eo;
360           # Add links to reassigned packages
361           $output =~ s{(Bug reassigned from package \`)([^']+?)((?:'|\&\#39;) to \`)([^']+?)((?:'|\&\#39;))}
362           {$1.q(<a href=").html_escape(package_links(package=>$2)).qq(">$2</a>).$3.q(<a href=").html_escape(package_links(package=>$4)).qq(">$4</a>).$5}eo;
363           if (defined $time) {
364                $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
365           }
366           $output .= '<a href="' .
367                html_escape(bug_links(bug => $bug_number,
368                                      options => {msg => ($msg_number+1)},
369                                      links_only => 1,
370                                     )
371                           ) . '">Full text</a> and <a href="' .
372                                html_escape(bug_links(bug => $bug_number,
373                                                      options => {msg => ($msg_number+1),
374                                                                  mbox => 'yes'},
375                                                      links_only => 1)
376                                           ) . '">rfc822 format</a> available.';
377
378           $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
379      }
380      elsif (/recips/) {
381           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
382           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
383                return ();
384           }
385           elsif (defined $msg_id) {
386                $$seen_msg_ids{$msg_id} = 1;
387           }
388           $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
389           $output .= 'View this message in <a href="' . html_escape(bug_links(bug=>$bug_number, links_only => 1, options=>{msg=>$msg_number, mbox=>'yes'})) . '">rfc822 format</a></p>';
390           $output .= handle_email_message($record->{text},
391                                           ref     => $bug_number,
392                                           msg_num => $msg_number,
393                                          );
394      }
395      elsif (/autocheck/) {
396           # Do nothing
397      }
398      elsif (/incoming-recv/) {
399           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
400           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
401                return ();
402           }
403           elsif (defined $msg_id) {
404                $$seen_msg_ids{$msg_id} = 1;
405           }
406           # Incomming Mail Message
407           my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
408           $output .= qq|<hr><p class="msgreceived"><a name="$msg_number"></a><a name="msg$msg_number"></a><a href="#$msg_number">Message #$msg_number</a> received at |.
409                html_escape("$received\@$hostname") .
410                     q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
411                          q|, <a href="| . html_escape(bug_links(bug => $bug_number,
412                                                                 links_only => 1,
413                                                                 options => {msg=>$msg_number,
414                                                                             mbox=>'yes'}
415                                                                )
416                                                      ) .'">mbox</a>)'.":</p>\n";
417           $output .= handle_email_message($record->{text},
418                                           ref     => $bug_number,
419                                           msg_num => $msg_number,
420                                          );
421      }
422      else {
423           die "Unknown record type $_";
424      }
425      return $output;
426 }
427
428
429
430 1;
431
432
433 __END__
434
435
436
437
438
439