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