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