]> git.donarmstrong.com Git - debbugs.git/blob - cgi/bugreport.cgi
Do not escape From and use .eml when returning a single message
[debbugs.git] / cgi / bugreport.cgi
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 # Sanitize environent for taint
7 BEGIN{
8     delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
9 }
10
11
12 use POSIX qw(strftime);
13 use MIME::Parser;
14 use MIME::Decoder;
15 use IO::Scalar;
16 use IO::File;
17
18 use Debbugs::Config qw(:globals :text :config);
19
20 # for read_log_records
21 use Debbugs::Log qw(:read);
22 use Debbugs::Log::Spam;
23 use Debbugs::CGI qw(:url :html :util :cache :usertags);
24 use Debbugs::CGI::Bugreport qw(:all);
25 use Debbugs::Common qw(buglog getmaintainers make_list bug_status);
26 use Debbugs::Packages qw(getpkgsrc);
27 use Debbugs::Status qw(splitpackages split_status_fields get_bug_status isstrongseverity);
28
29 use Scalar::Util qw(looks_like_number);
30
31 use Debbugs::Text qw(:templates);
32 use URI::Escape qw(uri_escape_utf8);
33 use List::AllUtils qw(max);
34
35
36 use CGI::Simple;
37 my $q = new CGI::Simple;
38 # STDOUT should be using the utf8 io layer
39 binmode(STDOUT,':raw:encoding(UTF-8)');
40
41 my %param = cgi_parameters(query => $q,
42                            single => [qw(bug msg att boring terse),
43                                       qw(reverse mbox mime trim),
44                                       qw(mboxstat mboxmaint archive),
45                                       qw(repeatmerged avatars),
46                                      ],
47                            default => {# msg       => '',
48                                        boring    => 'no',
49                                        terse     => 'no',
50                                        reverse   => 'no',
51                                        mbox      => 'no',
52                                        mime      => 'no',
53                                        mboxstat  => 'no',
54                                        mboxmaint => 'no',
55                                        archive   => 'no',
56                                        repeatmerged => 'yes',
57                                        avatars   => 'yes',
58                                       },
59                           );
60 # This is craptacular.
61
62 my $ref = $param{bug} or quitcgi("No bug number", '400 Bad Request');
63 $ref =~ /(\d+)/ or quitcgi("Invalid bug number", '400 Bad Request');
64 $ref = $1;
65 my $short = "#$ref";
66 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
67 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
68 my $boring = $param{'boring'} eq 'yes';
69 my $terse = $param{'terse'} eq 'yes';
70 my $reverse = $param{'reverse'} eq 'yes';
71 my $mbox = $param{'mbox'} eq 'yes';
72 my $mime = $param{'mime'} eq 'yes';
73 my $avatars = $param{avatars} eq 'yes';
74
75 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
76
77 my $mbox_status_message = $param{mboxstat} eq 'yes';
78 my $mbox_maint = $param{mboxmaint} eq 'yes';
79 $mbox = 1 if $mbox_status_message or $mbox_maint;
80
81 # Not used by this script directly, but fetch these so that pkgurl() and
82 # friends can propagate them correctly.
83 my $archive = $param{'archive'} eq 'yes';
84 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
85
86 my %bugusertags;
87 my %ut;
88 my %seen_users;
89
90 my $buglog = buglog($ref);
91 my $bug_status = bug_status($ref);
92 if (not defined $buglog or not defined $bug_status) {
93     no_such_bug($q,$ref);
94 }
95
96 sub no_such_bug {
97     my ($q,$ref) = @_;
98     print $q->header(-status => 404,
99                      -content_type => "text/html",
100                      -charset => 'utf-8',
101                      -cache_control => 'public, max-age=600',
102                     );
103     print fill_in_template(template=>'cgi/no_such_bug',
104                            variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
105                                          bug_num     => $ref,
106                                         },
107                           );
108     exit 0;
109 }
110
111 ## calculate etag for this bugreport.cgi call
112 my $etag;
113 ## identify the files that we need to look at; if someone just wants the mbox,
114 ## they don't need to see anything but the buglog; otherwise, track what is
115 ## necessary for the usertags and things to calculate status.
116
117 my @dependent_files = ($buglog);
118 my $need_status = 0;
119 if (not (($mbox and not $mbox_status_message) or
120          (defined $att and defined $msg))) {
121     $need_status = 1;
122     push @dependent_files,
123         $bug_status,
124         defined $config{version_index} ? $config{version_index}:(),
125         defined $config{binary_source_map} ? $config{binary_source_map}:();
126 }
127
128 ## Identify the users required
129 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
130     next unless length($user);
131     push @dependent_files,Debbugs::User::usertag_file_from_email($user);
132 }
133 if (defined $param{usertag}) {
134     for my $usertag (make_list($param{usertag})) {
135         my ($user, $tag) = split /:/, $usertag, 2;
136         push @dependent_files,Debbugs::User::usertag_file_from_email($user);
137     }
138 }
139 $etag =
140     etag_does_not_match(cgi => $q,
141                         additional_data => [grep {defined $_ ? $_ :()}
142                                             values %param
143                                            ],
144                         files => [@dependent_files,
145                                  ],
146                        );
147 if (not $etag) {
148     print $q->header(-status => 304,
149                      -cache_control => 'public, max-age=600',
150                      -etag => $etag,
151                      -charset => 'utf-8',
152                      -content_type => 'text/html',
153                     );
154     print "304: Not modified\n";
155     exit 0;
156 }
157
158 ## if they're just asking for the head, stop here.
159 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
160     print $q->header(-status => 200,
161                      -cache_control => 'public, max-age=600',
162                      -etag => $etag,
163                      -charset => 'utf-8',
164                      -content_type => 'text/html',
165                     );
166      exit 0;
167 }
168
169 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
170     next unless length($user);
171     add_user($user,\%ut,\%bugusertags,\%seen_users);
172 }
173
174 if (defined $param{usertag}) {
175      for my $usertag (make_list($param{usertag})) {
176           my %select_ut = ();
177           my ($u, $t) = split /:/, $usertag, 2;
178           Debbugs::User::read_usertags(\%select_ut, $u);
179           unless (defined $t && $t ne "") {
180                $t = join(",", keys(%select_ut));
181           }
182           add_user($u,\%ut,\%bugusertags,\%seen_users);
183           push @{$param{tag}}, split /,/, $t;
184      }
185 }
186
187 my %status;
188 if ($need_status) {
189     %status = %{split_status_fields(get_bug_status(bug=>$ref,
190                                                    bugusertags => \%bugusertags,
191                                                   ))}
192 }
193
194 my @records;
195 my $spam;
196 eval{
197     @records = read_log_records(bug_num => $ref,inner_file => 1);
198     $spam = Debbugs::Log::Spam->new(bug_num => $ref);
199 };
200 if ($@) {
201      quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
202 }
203
204 my $log='';
205 my $msg_num = 0;
206 my $skip_next = 0;
207 if (defined($msg) and ($msg-1) <= $#records) {
208      @records = ($records[$msg-1]);
209      $msg_num = $msg - 1;
210 }
211 my @log;
212 if ( $mbox ) {
213      binmode(STDOUT,":raw");
214      my $date = strftime "%a %b %d %T %Y", localtime;
215      my $multiple_messages = @records > 1;
216      if ($multiple_messages) {
217          print $q->header(-type => "application/mbox",
218                           -cache_control => 'public, max-age=600',
219                           -etag => $etag,
220                           content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
221                          );
222      }
223      else {
224           $msg_num++;
225           print $q->header(-type => "message/rfc822",
226                            -cache_control => 'public, max-age=86400',
227                            -etag => $etag,
228                            content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.eml"),
229                           );
230      }
231      if ($mbox_status_message and $multiple_messages) {
232           my $status_message='';
233           my @status_fields = (retitle   => 'subject',
234                                package   => 'package',
235                                submitter => 'originator',
236                                severity  => 'severity',
237                                tag       => 'tags',
238                                owner     => 'owner',
239                                blocks    => 'blocks',
240                                forward   => 'forward',
241                               );
242           my ($key,$value);
243           while (($key,$value) = splice(@status_fields,0,2)) {
244                if (defined $status{$value} and length $status{$value}) {
245                     $status_message .= qq($key $ref $status{$value}\n);
246                }
247           }
248           print STDOUT qq(From unknown $date\n),
249                create_mime_message([From       => "$gBug#$ref <$ref\@$gEmailDomain>",
250                                     To         => "$gBug#$ref <$ref\@$gEmailDomain>",
251                                     Subject    => "Status: $status{subject}",
252                                     "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
253                                    ],
254                                    <<END,);
255 $status_message
256 thanks
257
258
259 END
260      }
261      my $message_number=0;
262      my %seen_message_ids;
263      for my $record (@records) {
264           next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
265           my $wanted_type = $mbox_maint?'recips':'incoming-recv';
266           # we want to include control messages anyway
267           my $record_wanted_anyway = 0;
268           my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/im);
269           next if defined $msg_id and exists $seen_message_ids{$msg_id};
270           next if defined $msg_id and $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
271           $record_wanted_anyway = 1 if record_regex($record,qr/^Received: \(at control\)/);
272           next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
273           $seen_message_ids{$msg_id} = 1 if defined $msg_id;
274           # skip spam messages if we're outputting more than one message
275           next if $multiple_messages and $bug->is_spam($msg_id);
276       my @lines;
277       if ($record->{inner_file}) {
278           push @lines, scalar $record->{fh}->getline;
279           push @lines, scalar $record->{fh}->getline;
280           chomp $lines[0];
281           chomp $lines[1];
282       } else {
283           @lines = split( "\n", $record->{text}, -1 );
284       }
285           if ( $lines[ 1 ] =~ m/^From / ) {
286           @lines = reverse @lines;
287           }
288           if ( !( $lines[ 0 ] =~ m/^From / ) ) {
289                unshift @lines, "From unknown $date";
290        }
291       print $lines[0]."\n";
292           print map { s/^(>*From )/>$1/ if $multiple_messages;
293                       $_."\n" } @lines[ 1 .. $#lines ];
294       if ($record->{inner_file}) {
295           my $fh = $record->{fh};
296           local $/;
297           while (<$fh>) {
298               s/^(>*From )/>$1/gm if $multiple_messages;
299               print $_;
300           }
301       }
302      }
303      exit 0;
304 }
305
306 else {
307      if (defined $att and defined $msg and @records) {
308          binmode(STDOUT,":raw");
309          $msg_num++;
310          ## allow this to be cached for a week
311          print "Status: 200 OK\n";
312          print "Cache-Control: public, max-age=604800\n";
313          print "Etag: $etag\n";
314           print handle_email_message($records[0],
315                                      ref => $ref,
316                                      msg_num => $msg_num,
317                                      att => $att,
318                                      msg => $msg,
319                                      trim_headers => $trim_headers,
320                                     );
321           exit 0;
322      }
323      my %seen_msg_ids;
324      for my $record (@records) {
325           $msg_num++;
326           if ($skip_next) {
327                $skip_next = 0;
328                next;
329           }
330           $skip_next = 1 if $record->{type} eq 'html' and not $boring;
331           push @log, handle_record($record,$ref,$msg_num,
332                                    \%seen_msg_ids,
333                                    trim_headers => $trim_headers,
334                                    avatars => $avatars,
335                                    terse => $terse,
336                                    # if we're only looking at one record, allow
337                                    # spam to be output
338                                    spam  => (@records > 1)?$spam:undef,
339                                   );
340      }
341 }
342
343 @log = reverse @log if $reverse;
344 $log = join("\n",@log);
345
346
347 # All of the below should be turned into a template
348
349 my %maintainer = %{getmaintainers()};
350 my %pkgsrc = %{getpkgsrc()};
351
352 my $indexentry;
353 my $showseverity;
354
355 my $tpack;
356 my $tmain;
357
358 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
359
360 unless (%status) {
361     no_such_bug($q,$ref);
362 }
363
364 #$|=1;
365
366
367 my @packages = make_list($status{package});
368
369
370 my %packages_affects;
371 for my $p_a (qw(package affects)) {
372     foreach my $pkg (make_list($status{$p_a})) {
373         if ($pkg =~ /^src\:/) {
374             my ($srcpkg) = $pkg =~ /^src:(.*)/;
375             $packages_affects{$p_a}{$pkg} =
376                {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
377                 source     => $srcpkg,
378                 package    => $pkg,
379                 is_source  => 1,
380                };
381         }
382         else {
383             $packages_affects{$p_a}{$pkg} =
384                {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
385                 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
386                 package    => $pkg,
387                };
388         }
389     }
390 }
391
392 # fixup various bits of the status
393 $status{tags_array} = [sort(make_list($status{tags}))];
394 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
395 $status{mergedwith_array} = [make_list($status{mergedwith})];
396
397
398 my $version_graph = '';
399 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
400      $version_graph = q(<a href=").
401           html_escape(version_url(package => $status{package},
402                                   found => $status{found_versions},
403                                   fixed => $status{fixed_versions},
404                                  )
405                      ).
406           q("><img alt="version graph" src=").
407           html_escape(version_url(package => $status{package},
408                                   found => $status{found_versions},
409                                   fixed => $status{fixed_versions},
410                                   width => 2,
411                                   height => 2,
412                                  )
413                      ).
414           qq{"></a>};
415 }
416
417
418
419 my @blockedby= make_list($status{blockedby});
420 $status{blockedby_array} = [];
421 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
422     for my $b (@blockedby) {
423         my %s = %{get_bug_status($b)};
424         next if (defined $s{pending} and
425                  $s{"pending"} eq 'fixed') or
426                      length $s{done};
427         push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
428    }
429 }
430
431 my @blocks= make_list($status{blocks});
432 $status{blocks_array} = [];
433 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
434     for my $b (@blocks) {
435         my %s = %{get_bug_status($b)};
436         next if $s{"pending"} eq 'fixed' || length $s{done};
437         push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
438     }
439 }
440
441 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
442      $status{archived} = 1;
443 }
444
445 my $descriptivehead = $indexentry;
446
447 print $q->header(-type => "text/html",
448                  -charset => 'utf-8',
449                  -cache_control => 'public, max-age=300',
450                  -etag => $etag,
451                 );
452
453 print fill_in_template(template => 'cgi/bugreport',
454                        variables => {status => \%status,
455                                      package => $packages_affects{'package'},
456                                      affects => $packages_affects{'affects'},
457                                      log           => $log,
458                                      bug_num       => $ref,
459                                      version_graph => $version_graph,
460                                      msg           => $msg,
461                                      isstrongseverity => \&Debbugs::Status::isstrongseverity,
462                                      html_escape   => \&Debbugs::CGI::html_escape,
463                                      uri_escape    => \&URI::Escape::uri_escape_utf8,
464                                      looks_like_number => \&Scalar::Util::looks_like_number,
465                                      make_list        => \&Debbugs::Common::make_list,
466                                     },
467                        hole_var  => {'&package_links' => \&Debbugs::CGI::package_links,
468                                      '&bug_links'     => \&Debbugs::CGI::bug_links,
469                                      '&version_url'   => \&Debbugs::CGI::version_url,
470                                      '&strftime'      => \&POSIX::strftime,
471                                      '&maybelink'     => \&Debbugs::CGI::maybelink,
472                                     },
473                       );
474
475 __END__
476
477 # Local Variables:
478 # indent-tabs-mode: nil
479 # cperl-indent-level: 4
480 # End: