6 # Sanitize environent for taint
8 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
12 use POSIX qw(strftime);
18 # if we're running out of git, we want to use the git base directory as the
19 # first INC directory. If you're not running out of git, don't do that.
20 use File::Basename qw(dirname);
25 abs_path(dirname(abs_path(__FILE__)) . '/../');
26 # clear the taint; we'll assume that the absolute path to __FILE__ is the
27 # right path if there's a .git directory there
28 ($debbugs_dir) = $debbugs_dir =~ /([[:print:]]+)/;
29 if (defined $debbugs_dir and
30 -d $debbugs_dir . '/.git/') {
34 # if the first directory in @INC is not an absolute directory, assume that
35 # someone has overridden us via -I.
36 if ($INC[0] !~ /^\//) {
39 use if defined $debbugs_dir, lib => $debbugs_dir;
41 use Debbugs::Config qw(:globals :text :config);
43 # for read_log_records
44 use Debbugs::Log qw(:read);
45 use Debbugs::Log::Spam;
46 use Debbugs::CGI qw(:url :html :util :cache :usertags);
47 use Debbugs::CGI::Bugreport qw(:all);
48 use Debbugs::Common qw(buglog getmaintainers make_list bug_status package_maintainer);
49 use Debbugs::Packages qw(binary_to_source);
51 use Debbugs::Status qw(splitpackages split_status_fields get_bug_status isstrongseverity);
54 use Scalar::Util qw(looks_like_number);
56 use Debbugs::Text qw(:templates);
57 use URI::Escape qw(uri_escape_utf8);
58 use List::AllUtils qw(max);
61 if (defined $config{database}) {
62 $s = Debbugs::DB->connect($config{database}) or
63 die "Unable to connect to database";
67 my $q = new CGI::Simple;
68 # STDOUT should be using the utf8 io layer
69 binmode(STDOUT,':raw:encoding(UTF-8)');
71 my %param = cgi_parameters(query => $q,
72 single => [qw(bug msg att boring terse),
73 qw(reverse mbox mime trim),
74 qw(mboxstat mboxmaint archive),
75 qw(repeatmerged avatars),
77 default => {# msg => '',
86 repeatmerged => 'yes',
90 # This is craptacular.
92 my $ref = $param{bug} or quitcgi("No bug number", '400 Bad Request');
93 $ref =~ /(\d+)/ or quitcgi("Invalid bug number", '400 Bad Request');
96 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
97 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
98 my $boring = $param{'boring'} eq 'yes';
99 my $terse = $param{'terse'} eq 'yes';
100 my $reverse = $param{'reverse'} eq 'yes';
101 my $mbox = $param{'mbox'} eq 'yes';
102 my $mime = $param{'mime'} eq 'yes';
103 my $avatars = $param{avatars} eq 'yes';
105 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
107 my $mbox_status_message = $param{mboxstat} eq 'yes';
108 my $mbox_maint = $param{mboxmaint} eq 'yes';
109 $mbox = 1 if $mbox_status_message or $mbox_maint;
111 # Not used by this script directly, but fetch these so that pkgurl() and
112 # friends can propagate them correctly.
113 my $archive = $param{'archive'} eq 'yes';
114 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
120 my $buglog = buglog($ref);
121 my $bug_status = bug_status($ref);
122 if (not defined $buglog or not defined $bug_status) {
123 no_such_bug($q,$ref);
128 print $q->header(-status => 404,
129 -content_type => "text/html",
131 -cache_control => 'public, max-age=600',
133 print fill_in_template(template=>'cgi/no_such_bug',
134 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
141 ## calculate etag for this bugreport.cgi call
143 ## identify the files that we need to look at; if someone just wants the mbox,
144 ## they don't need to see anything but the buglog; otherwise, track what is
145 ## necessary for the usertags and things to calculate status.
147 my @dependent_files = ($buglog);
149 if (not (($mbox and not $mbox_status_message) or
150 (defined $att and defined $msg))) {
152 push @dependent_files,
154 defined $config{version_index} ? $config{version_index}:(),
155 defined $config{binary_source_map} ? $config{binary_source_map}:();
158 ## Identify the users required
159 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
160 next unless length($user);
161 push @dependent_files,Debbugs::User::usertag_file_from_email($user);
163 if (defined $param{usertag}) {
164 for my $usertag (make_list($param{usertag})) {
165 my ($user, $tag) = split /:/, $usertag, 2;
166 push @dependent_files,Debbugs::User::usertag_file_from_email($user);
170 etag_does_not_match(cgi => $q,
171 additional_data => [grep {defined $_ ? $_ :()}
174 files => [@dependent_files,
178 print $q->header(-status => 304,
179 -cache_control => 'public, max-age=600',
182 -content_type => 'text/html',
184 print "304: Not modified\n";
188 ## if they're just asking for the head, stop here.
189 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
190 print $q->header(-status => 200,
191 -cache_control => 'public, max-age=600',
194 -content_type => 'text/html',
199 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
200 next unless length($user);
201 add_user($user,\%ut,\%bugusertags,\%seen_users);
204 if (defined $param{usertag}) {
205 for my $usertag (make_list($param{usertag})) {
207 my ($u, $t) = split /:/, $usertag, 2;
208 Debbugs::User::read_usertags(\%select_ut, $u);
209 unless (defined $t && $t ne "") {
210 $t = join(",", keys(%select_ut));
212 add_user($u,\%ut,\%bugusertags,\%seen_users);
213 push @{$param{tag}}, split /,/, $t;
217 my $bug = Debbugs::Bug->new(bug => $ref,
223 %status = %{split_status_fields(get_bug_status(bug=>$ref,
224 bugusertags => \%bugusertags,
231 @records = $bug->log_records();
234 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
240 if (defined($msg) and ($msg-1) <= $#records) {
241 @records = ($records[$msg-1]);
246 binmode(STDOUT,":raw");
247 my $date = strftime "%a %b %d %T %Y", localtime;
249 print $q->header(-type => "application/mbox",
250 -cache_control => 'public, max-age=600',
252 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
257 print $q->header(-type => "message/rfc822",
258 -cache_control => 'public, max-age=86400',
260 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
263 if ($mbox_status_message and @records > 1) {
264 my $status_message='';
265 my @status_fields = (retitle => 'subject',
266 package => 'package',
267 submitter => 'originator',
268 severity => 'severity',
272 forward => 'forward',
275 while (($key,$value) = splice(@status_fields,0,2)) {
276 if (defined $status{$value} and length $status{$value}) {
277 $status_message .= qq($key $ref $status{$value}\n);
280 print STDOUT qq(From unknown $date\n),
281 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
282 To => "$gBug#$ref <$ref\@$gEmailDomain>",
283 Subject => "Status: $status{subject}",
284 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
293 my $message_number=0;
294 my %seen_message_ids;
295 for my $record (@records) {
296 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
297 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
298 # we want to include control messages anyway
299 my $record_wanted_anyway = 0;
300 my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/im);
301 next if defined $msg_id and exists $seen_message_ids{$msg_id};
302 next if defined $msg_id and $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
303 $record_wanted_anyway = 1 if record_regex($record,qr/^Received: \(at control\)/);
304 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
305 $seen_message_ids{$msg_id} = 1 if defined $msg_id;
306 # skip spam messages if we're outputting more than one message
307 next if @records > 1 and $bug->is_spam($msg_id);
309 if ($record->{inner_file}) {
310 push @lines, scalar $record->{fh}->getline;
311 push @lines, scalar $record->{fh}->getline;
315 @lines = split( "\n", $record->{text}, -1 );
317 if ( $lines[ 1 ] =~ m/^From / ) {
318 @lines = reverse @lines;
320 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
321 unshift @lines, "From unknown $date";
323 print $lines[0]."\n";
324 print map { s/^(>*From )/>$1/; $_."\n" } @lines[ 1 .. $#lines ];
325 if ($record->{inner_file}) {
326 my $fh = $record->{fh};
327 print $_ while (<$fh>);
334 if (defined $att and defined $msg and @records) {
335 binmode(STDOUT,":raw");
337 ## allow this to be cached for a week
338 print "Status: 200 OK\n";
339 print "Cache-Control: public, max-age=604800\n";
340 print "Etag: $etag\n";
341 print handle_email_message($records[0],
346 trim_headers => $trim_headers,
351 for my $record (@records) {
357 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
358 push @log, handle_record($record,$ref,$msg_num,
360 trim_headers => $trim_headers,
363 # if we're only looking at one record, allow
365 spam => (@records > 1)?$bug:undef,
370 @log = reverse @log if $reverse;
371 $log = join("\n",@log);
374 # All of the below should be turned into a template
380 no_such_bug($q,$ref);
383 my @packages = make_list($status{package});
386 my %packages_affects;
387 for my $p_a (qw(package affects)) {
388 foreach my $pkg (make_list($status{$p_a})) {
389 if ($pkg =~ /^src\:/) {
390 my ($srcpkg) = $pkg =~ /^src:(.*)/;
391 $packages_affects{$p_a}{$pkg} =
392 {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
399 $packages_affects{$p_a}{$pkg} =
400 {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
401 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
408 # fixup various bits of the status
409 $status{tags_array} = [sort(make_list($status{tags}))];
410 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
411 $status{mergedwith_array} = [make_list($status{mergedwith})];
414 my $version_graph = '';
415 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
416 $version_graph = q(<a href=").
417 html_escape(version_url(package => $status{package},
418 found => $status{found_versions},
419 fixed => $status{fixed_versions},
422 q("><img alt="version graph" src=").
423 html_escape(version_url(package => $status{package},
424 found => $status{found_versions},
425 fixed => $status{fixed_versions},
435 my @blockedby= make_list($status{blockedby});
436 $status{blockedby_array} = [];
437 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
438 for my $b (@blockedby) {
439 my %s = %{get_bug_status($b)};
440 next if (defined $s{pending} and
441 $s{"pending"} eq 'fixed') or
443 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
447 my @blocks= make_list($status{blocks});
448 $status{blocks_array} = [];
449 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
450 for my $b (@blocks) {
451 my %s = %{get_bug_status($b)};
452 next if $s{"pending"} eq 'fixed' || length $s{done};
453 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
457 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
458 $status{archived} = 1;
461 my $descriptivehead = $indexentry;
463 print $q->header(-type => "text/html",
465 -cache_control => 'public, max-age=300',
469 print fill_in_template(template => 'cgi/bugreport',
470 variables => {bug => $bug,
472 package => $packages_affects{'package'},
473 affects => $packages_affects{'affects'},
476 version_graph => $version_graph,
478 isstrongseverity => \&Debbugs::Status::isstrongseverity,
479 html_escape => \&Debbugs::CGI::html_escape,
480 uri_escape => \&URI::Escape::uri_escape_utf8,
481 looks_like_number => \&Scalar::Util::looks_like_number,
482 make_list => \&Debbugs::Common::make_list,
484 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
485 '&bug_links' => \&Debbugs::CGI::bug_links,
486 '&version_url' => \&Debbugs::CGI::version_url,
487 '&strftime' => \&POSIX::strftime,
488 '&maybelink' => \&Debbugs::CGI::maybelink,
495 # indent-tabs-mode: nil
496 # cperl-indent-level: 4