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