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);
49 use Debbugs::Packages qw(getpkgsrc);
51 use Debbugs::Status qw(splitpackages split_status_fields get_bug_status isstrongseverity);
53 use Scalar::Util qw(looks_like_number);
55 use Debbugs::Text qw(:templates);
56 use URI::Escape qw(uri_escape_utf8);
57 use List::AllUtils qw(max);
60 if (defined $config{database}) {
61 $s = Debbugs::DB->connect($config{database}) or
62 die "Unable to connect to database";
66 my $q = new CGI::Simple;
67 # STDOUT should be using the utf8 io layer
68 binmode(STDOUT,':raw:encoding(UTF-8)');
70 my %param = cgi_parameters(query => $q,
71 single => [qw(bug msg att boring terse),
72 qw(reverse mbox mime trim),
73 qw(mboxstat mboxmaint archive),
74 qw(repeatmerged avatars),
76 default => {# msg => '',
85 repeatmerged => 'yes',
89 # This is craptacular.
91 my $ref = $param{bug} or quitcgi("No bug number", '400 Bad Request');
92 $ref =~ /(\d+)/ or quitcgi("Invalid bug number", '400 Bad Request');
95 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
96 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
97 my $boring = $param{'boring'} eq 'yes';
98 my $terse = $param{'terse'} eq 'yes';
99 my $reverse = $param{'reverse'} eq 'yes';
100 my $mbox = $param{'mbox'} eq 'yes';
101 my $mime = $param{'mime'} eq 'yes';
102 my $avatars = $param{avatars} eq 'yes';
104 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
106 my $mbox_status_message = $param{mboxstat} eq 'yes';
107 my $mbox_maint = $param{mboxmaint} eq 'yes';
108 $mbox = 1 if $mbox_status_message or $mbox_maint;
110 # Not used by this script directly, but fetch these so that pkgurl() and
111 # friends can propagate them correctly.
112 my $archive = $param{'archive'} eq 'yes';
113 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
119 my $buglog = buglog($ref);
120 my $bug_status = bug_status($ref);
121 if (not defined $buglog or not defined $bug_status) {
122 no_such_bug($q,$ref);
127 print $q->header(-status => 404,
128 -content_type => "text/html",
130 -cache_control => 'public, max-age=600',
132 print fill_in_template(template=>'cgi/no_such_bug',
133 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
140 ## calculate etag for this bugreport.cgi call
142 ## identify the files that we need to look at; if someone just wants the mbox,
143 ## they don't need to see anything but the buglog; otherwise, track what is
144 ## necessary for the usertags and things to calculate status.
146 my @dependent_files = ($buglog);
148 if (not (($mbox and not $mbox_status_message) or
149 (defined $att and defined $msg))) {
151 push @dependent_files,
153 defined $config{version_index} ? $config{version_index}:(),
154 defined $config{binary_source_map} ? $config{binary_source_map}:();
157 ## Identify the users required
158 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
159 next unless length($user);
160 push @dependent_files,Debbugs::User::usertag_file_from_email($user);
162 if (defined $param{usertag}) {
163 for my $usertag (make_list($param{usertag})) {
164 my ($user, $tag) = split /:/, $usertag, 2;
165 push @dependent_files,Debbugs::User::usertag_file_from_email($user);
169 etag_does_not_match(cgi => $q,
170 additional_data => [grep {defined $_ ? $_ :()}
173 files => [@dependent_files,
177 print $q->header(-status => 304,
178 -cache_control => 'public, max-age=600',
181 -content_type => 'text/html',
183 print "304: Not modified\n";
187 ## if they're just asking for the head, stop here.
188 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
189 print $q->header(-status => 200,
190 -cache_control => 'public, max-age=600',
193 -content_type => 'text/html',
198 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
199 next unless length($user);
200 add_user($user,\%ut,\%bugusertags,\%seen_users);
203 if (defined $param{usertag}) {
204 for my $usertag (make_list($param{usertag})) {
206 my ($u, $t) = split /:/, $usertag, 2;
207 Debbugs::User::read_usertags(\%select_ut, $u);
208 unless (defined $t && $t ne "") {
209 $t = join(",", keys(%select_ut));
211 add_user($u,\%ut,\%bugusertags,\%seen_users);
212 push @{$param{tag}}, split /,/, $t;
218 %status = %{split_status_fields(get_bug_status(bug=>$ref,
219 bugusertags => \%bugusertags,
220 defined $s?(schema => $s):(),
227 @records = read_log_records(bug_num => $ref,inner_file => 1);
228 $spam = Debbugs::Log::Spam->new(bug_num => $ref);
231 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
237 if (defined($msg) and ($msg-1) <= $#records) {
238 @records = ($records[$msg-1]);
243 binmode(STDOUT,":raw");
244 my $date = strftime "%a %b %d %T %Y", localtime;
246 print $q->header(-type => "application/mbox",
247 -cache_control => 'public, max-age=600',
249 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
254 print $q->header(-type => "message/rfc822",
255 -cache_control => 'public, max-age=86400',
257 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
260 if ($mbox_status_message and @records > 1) {
261 my $status_message='';
262 my @status_fields = (retitle => 'subject',
263 package => 'package',
264 submitter => 'originator',
265 severity => 'severity',
269 forward => 'forward',
272 while (($key,$value) = splice(@status_fields,0,2)) {
273 if (defined $status{$value} and length $status{$value}) {
274 $status_message .= qq($key $ref $status{$value}\n);
277 print STDOUT qq(From unknown $date\n),
278 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
279 To => "$gBug#$ref <$ref\@$gEmailDomain>",
280 Subject => "Status: $status{subject}",
281 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
290 my $message_number=0;
291 my %seen_message_ids;
292 for my $record (@records) {
293 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
294 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
295 # we want to include control messages anyway
296 my $record_wanted_anyway = 0;
297 my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/im);
298 next if defined $msg_id and exists $seen_message_ids{$msg_id};
299 next if defined $msg_id and $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
300 $record_wanted_anyway = 1 if record_regex($record,qr/^Received: \(at control\)/);
301 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
302 $seen_message_ids{$msg_id} = 1 if defined $msg_id;
303 # skip spam messages if we're outputting more than one message
304 next if @records > 1 and $spam->is_spam($msg_id);
306 if ($record->{inner_file}) {
307 push @lines, scalar $record->{fh}->getline;
308 push @lines, scalar $record->{fh}->getline;
312 @lines = split( "\n", $record->{text}, -1 );
314 if ( $lines[ 1 ] =~ m/^From / ) {
315 @lines = reverse @lines;
317 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
318 unshift @lines, "From unknown $date";
320 print $lines[0]."\n";
321 print map { s/^(>*From )/>$1/; $_."\n" } @lines[ 1 .. $#lines ];
322 if ($record->{inner_file}) {
323 my $fh = $record->{fh};
324 print $_ while (<$fh>);
331 if (defined $att and defined $msg and @records) {
332 binmode(STDOUT,":raw");
334 ## allow this to be cached for a week
335 print "Status: 200 OK\n";
336 print "Cache-Control: public, max-age=604800\n";
337 print "Etag: $etag\n";
338 print handle_email_message($records[0],
343 trim_headers => $trim_headers,
348 for my $record (@records) {
354 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
355 push @log, handle_record($record,$ref,$msg_num,
357 trim_headers => $trim_headers,
360 # if we're only looking at one record, allow
362 spam => (@records > 1)?$spam:undef,
367 @log = reverse @log if $reverse;
368 $log = join("\n",@log);
371 # All of the below should be turned into a template
373 my %maintainer = %{getmaintainers()};
374 my %pkgsrc = %{getpkgsrc()};
382 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
385 no_such_bug($q,$ref);
391 my @packages = make_list($status{package});
394 my %packages_affects;
395 for my $p_a (qw(package affects)) {
396 foreach my $pkg (make_list($status{$p_a})) {
397 if ($pkg =~ /^src\:/) {
398 my ($srcpkg) = $pkg =~ /^src:(.*)/;
399 $packages_affects{$p_a}{$pkg} =
400 {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
407 $packages_affects{$p_a}{$pkg} =
408 {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
409 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
416 # fixup various bits of the status
417 $status{tags_array} = [sort(make_list($status{tags}))];
418 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
419 $status{mergedwith_array} = [make_list($status{mergedwith})];
422 my $version_graph = '';
423 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
424 $version_graph = q(<a href=").
425 html_escape(version_url(package => $status{package},
426 found => $status{found_versions},
427 fixed => $status{fixed_versions},
430 q("><img alt="version graph" src=").
431 html_escape(version_url(package => $status{package},
432 found => $status{found_versions},
433 fixed => $status{fixed_versions},
443 my @blockedby= make_list($status{blockedby});
444 $status{blockedby_array} = [];
445 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
446 for my $b (@blockedby) {
447 my %s = %{get_bug_status($b)};
448 next if (defined $s{pending} and
449 $s{"pending"} eq 'fixed') or
451 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
455 my @blocks= make_list($status{blocks});
456 $status{blocks_array} = [];
457 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
458 for my $b (@blocks) {
459 my %s = %{get_bug_status($b)};
460 next if $s{"pending"} eq 'fixed' || length $s{done};
461 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
465 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
466 $status{archived} = 1;
469 my $descriptivehead = $indexentry;
471 print $q->header(-type => "text/html",
473 -cache_control => 'public, max-age=300',
477 print fill_in_template(template => 'cgi/bugreport',
478 variables => {status => \%status,
479 package => $packages_affects{'package'},
480 affects => $packages_affects{'affects'},
483 version_graph => $version_graph,
485 isstrongseverity => \&Debbugs::Status::isstrongseverity,
486 html_escape => \&Debbugs::CGI::html_escape,
487 uri_escape => \&URI::Escape::uri_escape_utf8,
488 looks_like_number => \&Scalar::Util::looks_like_number,
489 make_list => \&Debbugs::Common::make_list,
491 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
492 '&bug_links' => \&Debbugs::CGI::bug_links,
493 '&version_url' => \&Debbugs::CGI::version_url,
494 '&strftime' => \&POSIX::strftime,
495 '&maybelink' => \&Debbugs::CGI::maybelink,
502 # indent-tabs-mode: nil
503 # cperl-indent-level: 4