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