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 print "304: Not modified\n";
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',
159 -content_type => 'text/html',
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);
169 if (defined $param{usertag}) {
170 for my $usertag (make_list($param{usertag})) {
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));
177 add_user($u,\%ut,\%bugusertags,\%seen_users);
178 push @{$param{tag}}, split /,/, $t;
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;
190 $buglogfh = IO::File->new($buglog,'r') or quitcgi("open log for $ref: $!");
196 %status = %{split_status_fields(get_bug_status(bug=>$ref,
197 bugusertags => \%bugusertags,
203 @records = read_log_records(logfh => $buglogfh,inner_file => 1);
206 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
214 if (defined($msg) and ($msg-1) <= $#records) {
215 @records = ($records[$msg-1]);
220 binmode(STDOUT,":raw");
221 my $date = strftime "%a %b %d %T %Y", localtime;
223 print $q->header(-type => "application/mbox",
224 -cache_control => 'public, max-age=600',
226 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
231 print $q->header(-type => "message/rfc822",
232 -cache_control => 'public, max-age=86400',
234 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
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',
246 forward => 'forward',
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);
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>",
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;
281 if ($record->{inner_file}) {
282 push @lines, $record->{fh}->getline;
283 push @lines, $record->{fh}->getline;
287 @lines = split( "\n", $record->{text}, -1 );
289 if ( $lines[ 1 ] =~ m/^From / ) {
290 @lines = reverse @lines;
292 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
293 unshift @lines, "From unknown $date";
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>);
306 if (defined $att and defined $msg and @records) {
307 binmode(STDOUT,":raw");
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],
318 trim_headers => $trim_headers,
323 for my $record (@records) {
329 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
330 push @log, handle_record($record,$ref,$msg_num,
332 trim_headers => $trim_headers,
338 @log = reverse @log if $reverse;
339 $log = join("\n",@log);
342 # All of the below should be turned into a template
344 my %maintainer = %{getmaintainers()};
345 my %pkgsrc = %{getpkgsrc()};
353 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
356 no_such_bug($q,$ref);
362 my @packages = make_list($status{package});
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)',
378 $packages_affects{$p_a}{$pkg} =
379 {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
380 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
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})];
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},
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},
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};
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};
434 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
435 $status{archived} = 1;
438 my $descriptivehead = $indexentry;
440 print $q->header(-type => "text/html",
442 -cache_control => 'public, max-age=300',
446 print fill_in_template(template => 'cgi/bugreport',
447 variables => {status => \%status,
448 package => $packages_affects{'package'},
449 affects => $packages_affects{'affects'},
452 version_graph => $version_graph,
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,
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,