6 # Sanitize environent for taint
8 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
12 use POSIX qw(strftime);
18 use Debbugs::Config qw(:globals :text :config);
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);
30 use Scalar::Util qw(looks_like_number);
32 use Debbugs::Text qw(:templates);
34 use List::Util qw(max);
38 my $q = new CGI::Simple;
39 # STDOUT should be using the utf8 io layer
40 binmode(STDOUT,':raw:encoding(UTF-8)');
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),
48 default => {# msg => '',
57 repeatmerged => 'yes',
61 # This is craptacular.
63 my $ref = $param{bug} or quitcgi("No bug number", '400 Bad Request');
64 $ref =~ /(\d+)/ or quitcgi("Invalid bug number", '400 Bad Request');
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';
76 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
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;
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';
91 my $buglog = buglog($ref);
92 my $bug_status = bug_status($ref);
93 if (not defined $buglog or not defined $bug_status) {
99 print $q->header(-status => 404,
100 -content_type => "text/html",
102 -cache_control => 'public, max-age=600',
104 print fill_in_template(template=>'cgi/no_such_bug',
105 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
112 ## calculate etag for this bugreport.cgi call
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.
118 my @dependent_files = ($buglog);
120 if (not (($mbox and not $mbox_status_message) or
121 (defined $att and defined $msg))) {
123 push @dependent_files,
125 defined $config{version_index} ? $config{version_index}:(),
126 defined $config{binary_source_map} ? $config{binary_source_map}:();
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);
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);
141 etag_does_not_match(cgi => $q,
142 additional_data => [grep {defined $_ ? $_ :()}
145 files => [@dependent_files,
149 print $q->header(-status => 304,
150 -cache_control => 'public, max-age=600',
153 -content_type => 'text/html',
155 print "304: Not modified\n";
159 ## if they're just asking for the head, stop here.
160 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
161 print $q->header(-status => 200,
162 -cache_control => 'public, max-age=600',
165 -content_type => 'text/html',
170 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
171 next unless length($user);
172 add_user($user,\%ut,\%bugusertags,\%seen_users);
175 if (defined $param{usertag}) {
176 for my $usertag (make_list($param{usertag})) {
178 my ($u, $t) = split /:/, $usertag, 2;
179 Debbugs::User::read_usertags(\%select_ut, $u);
180 unless (defined $t && $t ne "") {
181 $t = join(",", keys(%select_ut));
183 add_user($u,\%ut,\%bugusertags,\%seen_users);
184 push @{$param{tag}}, split /,/, $t;
190 if ($buglog =~ m/\.gz$/) {
191 my $oldpath = $ENV{'PATH'};
192 $ENV{'PATH'} = '/bin:/usr/bin';
193 $buglogfh = IO::File->new("zcat $buglog |") or quitcgi("open log for $ref: $!");
194 $ENV{'PATH'} = $oldpath;
196 $buglogfh = IO::File->new($buglog,'r') or quitcgi("open log for $ref: $!");
202 %status = %{split_status_fields(get_bug_status(bug=>$ref,
203 bugusertags => \%bugusertags,
209 @records = read_log_records(logfh => $buglogfh,inner_file => 1);
212 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
220 if (defined($msg) and ($msg-1) <= $#records) {
221 @records = ($records[$msg-1]);
226 binmode(STDOUT,":raw");
227 my $date = strftime "%a %b %d %T %Y", localtime;
229 print $q->header(-type => "application/mbox",
230 -cache_control => 'public, max-age=600',
232 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
237 print $q->header(-type => "message/rfc822",
238 -cache_control => 'public, max-age=86400',
240 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
243 if ($mbox_status_message and @records > 1) {
244 my $status_message='';
245 my @status_fields = (retitle => 'subject',
246 package => 'package',
247 submitter => 'originator',
248 severity => 'severity',
252 forward => 'forward',
255 while (($key,$value) = splice(@status_fields,0,2)) {
256 if (defined $status{$value} and length $status{$value}) {
257 $status_message .= qq($key $ref $status{$value}\n);
260 print STDOUT qq(From unknown $date\n),
261 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
262 To => "$gBug#$ref <$ref\@$gEmailDomain>",
263 Subject => "Status: $status{subject}",
264 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
273 my $message_number=0;
274 my %seen_message_ids;
275 for my $record (@records) {
276 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
277 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
278 # we want to include control messages anyway
279 my $record_wanted_anyway = 0;
280 my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/im);
281 next if defined $msg_id and exists $seen_message_ids{$msg_id};
282 next if defined $msg_id and $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
283 $record_wanted_anyway = 1 if record_regex($record,qr/^Received: \(at control\)/);
284 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
285 $seen_message_ids{$msg_id} = 1 if defined $msg_id;
287 if ($record->{inner_file}) {
288 push @lines, $record->{fh}->getline;
289 push @lines, $record->{fh}->getline;
293 @lines = split( "\n", $record->{text}, -1 );
295 if ( $lines[ 1 ] =~ m/^From / ) {
296 @lines = reverse @lines;
298 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
299 unshift @lines, "From unknown $date";
301 print $lines[0]."\n";
302 print map { s/^(>*From )/>$1/; $_."\n" } @lines[ 1 .. $#lines ];
303 if ($record->{inner_file}) {
304 my $fh = $record->{fh};
305 print $_ while (<$fh>);
312 if (defined $att and defined $msg and @records) {
313 binmode(STDOUT,":raw");
315 ## allow this to be cached for a week
316 print "Status: 200 OK\n";
317 print "Cache-Control: public, max-age=604800\n";
318 print "Etag: $etag\n";
319 print handle_email_message($records[0],
324 trim_headers => $trim_headers,
329 for my $record (@records) {
335 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
336 push @log, handle_record($record,$ref,$msg_num,
338 trim_headers => $trim_headers,
344 @log = reverse @log if $reverse;
345 $log = join("\n",@log);
348 # All of the below should be turned into a template
350 my %maintainer = %{getmaintainers()};
351 my %pkgsrc = %{getpkgsrc()};
359 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
362 no_such_bug($q,$ref);
368 my @packages = make_list($status{package});
371 my %packages_affects;
372 for my $p_a (qw(package affects)) {
373 foreach my $pkg (make_list($status{$p_a})) {
374 if ($pkg =~ /^src\:/) {
375 my ($srcpkg) = $pkg =~ /^src:(.*)/;
376 $packages_affects{$p_a}{$pkg} =
377 {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
384 $packages_affects{$p_a}{$pkg} =
385 {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
386 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
393 # fixup various bits of the status
394 $status{tags_array} = [sort(make_list($status{tags}))];
395 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
396 $status{mergedwith_array} = [make_list($status{mergedwith})];
399 my $version_graph = '';
400 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
401 $version_graph = q(<a href=").
402 html_escape(version_url(package => $status{package},
403 found => $status{found_versions},
404 fixed => $status{fixed_versions},
407 q("><img alt="version graph" src=").
408 html_escape(version_url(package => $status{package},
409 found => $status{found_versions},
410 fixed => $status{fixed_versions},
420 my @blockedby= make_list($status{blockedby});
421 $status{blockedby_array} = [];
422 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
423 for my $b (@blockedby) {
424 my %s = %{get_bug_status($b)};
425 next if $s{"pending"} eq 'fixed' || length $s{done};
426 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
430 my @blocks= make_list($status{blocks});
431 $status{blocks_array} = [];
432 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
433 for my $b (@blocks) {
434 my %s = %{get_bug_status($b)};
435 next if $s{"pending"} eq 'fixed' || length $s{done};
436 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
440 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
441 $status{archived} = 1;
444 my $descriptivehead = $indexentry;
446 print $q->header(-type => "text/html",
448 -cache_control => 'public, max-age=300',
452 print fill_in_template(template => 'cgi/bugreport',
453 variables => {status => \%status,
454 package => $packages_affects{'package'},
455 affects => $packages_affects{'affects'},
458 version_graph => $version_graph,
460 isstrongseverity => \&Debbugs::Status::isstrongseverity,
461 html_escape => \&Debbugs::CGI::html_escape,
462 looks_like_number => \&Scalar::Util::looks_like_number,
463 make_list => \&Debbugs::Common::make_list,
465 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
466 '&bug_links' => \&Debbugs::CGI::bug_links,
467 '&version_url' => \&Debbugs::CGI::version_url,
468 '&bug_url' => \&Debbugs::CGI::bug_url,
469 '&strftime' => \&POSIX::strftime,
470 '&maybelink' => \&Debbugs::CGI::maybelink,