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_file_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_file_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 %status = %{split_status_fields(get_bug_status(bug=>$ref,
191 bugusertags => \%bugusertags,
197 @records = read_log_records(bug_num => $ref,inner_file => 1);
200 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
206 if (defined($msg) and ($msg-1) <= $#records) {
207 @records = ($records[$msg-1]);
212 binmode(STDOUT,":raw");
213 my $date = strftime "%a %b %d %T %Y", localtime;
215 print $q->header(-type => "application/mbox",
216 -cache_control => 'public, max-age=600',
218 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
223 print $q->header(-type => "message/rfc822",
224 -cache_control => 'public, max-age=86400',
226 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
229 if ($mbox_status_message and @records > 1) {
230 my $status_message='';
231 my @status_fields = (retitle => 'subject',
232 package => 'package',
233 submitter => 'originator',
234 severity => 'severity',
238 forward => 'forward',
241 while (($key,$value) = splice(@status_fields,0,2)) {
242 if (defined $status{$value} and length $status{$value}) {
243 $status_message .= qq($key $ref $status{$value}\n);
246 print STDOUT qq(From unknown $date\n),
247 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
248 To => "$gBug#$ref <$ref\@$gEmailDomain>",
249 Subject => "Status: $status{subject}",
250 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
259 my $message_number=0;
260 my %seen_message_ids;
261 for my $record (@records) {
262 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
263 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
264 # we want to include control messages anyway
265 my $record_wanted_anyway = 0;
266 my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/im);
267 next if defined $msg_id and exists $seen_message_ids{$msg_id};
268 next if defined $msg_id and $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
269 $record_wanted_anyway = 1 if record_regex($record,qr/^Received: \(at control\)/);
270 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
271 $seen_message_ids{$msg_id} = 1 if defined $msg_id;
273 if ($record->{inner_file}) {
274 push @lines, $record->{fh}->getline;
275 push @lines, $record->{fh}->getline;
279 @lines = split( "\n", $record->{text}, -1 );
281 if ( $lines[ 1 ] =~ m/^From / ) {
282 @lines = reverse @lines;
284 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
285 unshift @lines, "From unknown $date";
287 print $lines[0]."\n";
288 print map { s/^(>*From )/>$1/; $_."\n" } @lines[ 1 .. $#lines ];
289 if ($record->{inner_file}) {
290 my $fh = $record->{fh};
291 print $_ while (<$fh>);
298 if (defined $att and defined $msg and @records) {
299 binmode(STDOUT,":raw");
301 ## allow this to be cached for a week
302 print "Status: 200 OK\n";
303 print "Cache-Control: public, max-age=604800\n";
304 print "Etag: $etag\n";
305 print handle_email_message($records[0],
310 trim_headers => $trim_headers,
315 for my $record (@records) {
321 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
322 push @log, handle_record($record,$ref,$msg_num,
324 trim_headers => $trim_headers,
331 @log = reverse @log if $reverse;
332 $log = join("\n",@log);
335 # All of the below should be turned into a template
337 my %maintainer = %{getmaintainers()};
338 my %pkgsrc = %{getpkgsrc()};
346 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
349 no_such_bug($q,$ref);
355 my @packages = make_list($status{package});
358 my %packages_affects;
359 for my $p_a (qw(package affects)) {
360 foreach my $pkg (make_list($status{$p_a})) {
361 if ($pkg =~ /^src\:/) {
362 my ($srcpkg) = $pkg =~ /^src:(.*)/;
363 $packages_affects{$p_a}{$pkg} =
364 {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
371 $packages_affects{$p_a}{$pkg} =
372 {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
373 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
380 # fixup various bits of the status
381 $status{tags_array} = [sort(make_list($status{tags}))];
382 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
383 $status{mergedwith_array} = [make_list($status{mergedwith})];
386 my $version_graph = '';
387 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
388 $version_graph = q(<a href=").
389 html_escape(version_url(package => $status{package},
390 found => $status{found_versions},
391 fixed => $status{fixed_versions},
394 q("><img alt="version graph" src=").
395 html_escape(version_url(package => $status{package},
396 found => $status{found_versions},
397 fixed => $status{fixed_versions},
407 my @blockedby= make_list($status{blockedby});
408 $status{blockedby_array} = [];
409 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
410 for my $b (@blockedby) {
411 my %s = %{get_bug_status($b)};
412 next if $s{"pending"} eq 'fixed' || length $s{done};
413 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
417 my @blocks= make_list($status{blocks});
418 $status{blocks_array} = [];
419 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
420 for my $b (@blocks) {
421 my %s = %{get_bug_status($b)};
422 next if $s{"pending"} eq 'fixed' || length $s{done};
423 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
427 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
428 $status{archived} = 1;
431 my $descriptivehead = $indexentry;
433 print $q->header(-type => "text/html",
435 -cache_control => 'public, max-age=300',
439 print fill_in_template(template => 'cgi/bugreport',
440 variables => {status => \%status,
441 package => $packages_affects{'package'},
442 affects => $packages_affects{'affects'},
445 version_graph => $version_graph,
447 isstrongseverity => \&Debbugs::Status::isstrongseverity,
448 html_escape => \&Debbugs::CGI::html_escape,
449 looks_like_number => \&Scalar::Util::looks_like_number,
450 make_list => \&Debbugs::Common::make_list,
452 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
453 '&bug_links' => \&Debbugs::CGI::bug_links,
454 '&version_url' => \&Debbugs::CGI::version_url,
455 '&bug_url' => \&Debbugs::CGI::bug_url,
456 '&strftime' => \&POSIX::strftime,
457 '&maybelink' => \&Debbugs::CGI::maybelink,
464 # indent-tabs-mode: nil
465 # cperl-indent-level: 4