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::Log::Spam;
23 use Debbugs::CGI qw(:url :html :util :cache :usertags);
24 use Debbugs::CGI::Bugreport qw(:all);
25 use Debbugs::Common qw(buglog getmaintainers make_list bug_status);
26 use Debbugs::Packages qw(getpkgsrc);
28 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);
33 use URI::Escape qw(uri_escape_utf8);
34 use List::AllUtils qw(max);
37 if (defined $config{database}) {
38 $s = Debbugs::DB->connect($config{database}) or
39 die "Unable to connect to database";
43 my $q = new CGI::Simple;
44 # STDOUT should be using the utf8 io layer
45 binmode(STDOUT,':raw:encoding(UTF-8)');
47 my %param = cgi_parameters(query => $q,
48 single => [qw(bug msg att boring terse),
49 qw(reverse mbox mime trim),
50 qw(mboxstat mboxmaint archive),
51 qw(repeatmerged avatars),
53 default => {# msg => '',
62 repeatmerged => 'yes',
66 # This is craptacular.
68 my $ref = $param{bug} or quitcgi("No bug number", '400 Bad Request');
69 $ref =~ /(\d+)/ or quitcgi("Invalid bug number", '400 Bad Request');
72 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
73 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
74 my $boring = $param{'boring'} eq 'yes';
75 my $terse = $param{'terse'} eq 'yes';
76 my $reverse = $param{'reverse'} eq 'yes';
77 my $mbox = $param{'mbox'} eq 'yes';
78 my $mime = $param{'mime'} eq 'yes';
79 my $avatars = $param{avatars} eq 'yes';
81 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
83 my $mbox_status_message = $param{mboxstat} eq 'yes';
84 my $mbox_maint = $param{mboxmaint} eq 'yes';
85 $mbox = 1 if $mbox_status_message or $mbox_maint;
87 # Not used by this script directly, but fetch these so that pkgurl() and
88 # friends can propagate them correctly.
89 my $archive = $param{'archive'} eq 'yes';
90 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
96 my $buglog = buglog($ref);
97 my $bug_status = bug_status($ref);
98 if (not defined $buglog or not defined $bug_status) {
104 print $q->header(-status => 404,
105 -content_type => "text/html",
107 -cache_control => 'public, max-age=600',
109 print fill_in_template(template=>'cgi/no_such_bug',
110 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
117 ## calculate etag for this bugreport.cgi call
119 ## identify the files that we need to look at; if someone just wants the mbox,
120 ## they don't need to see anything but the buglog; otherwise, track what is
121 ## necessary for the usertags and things to calculate status.
123 my @dependent_files = ($buglog);
125 if (not (($mbox and not $mbox_status_message) or
126 (defined $att and defined $msg))) {
128 push @dependent_files,
130 defined $config{version_index} ? $config{version_index}:(),
131 defined $config{binary_source_map} ? $config{binary_source_map}:();
134 ## Identify the users required
135 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
136 next unless length($user);
137 push @dependent_files,Debbugs::User::usertag_file_from_email($user);
139 if (defined $param{usertag}) {
140 for my $usertag (make_list($param{usertag})) {
141 my ($user, $tag) = split /:/, $usertag, 2;
142 push @dependent_files,Debbugs::User::usertag_file_from_email($user);
146 etag_does_not_match(cgi => $q,
147 additional_data => [grep {defined $_ ? $_ :()}
150 files => [@dependent_files,
154 print $q->header(-status => 304,
155 -cache_control => 'public, max-age=600',
158 -content_type => 'text/html',
160 print "304: Not modified\n";
164 ## if they're just asking for the head, stop here.
165 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
166 print $q->header(-status => 200,
167 -cache_control => 'public, max-age=600',
170 -content_type => 'text/html',
175 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
176 next unless length($user);
177 add_user($user,\%ut,\%bugusertags,\%seen_users);
180 if (defined $param{usertag}) {
181 for my $usertag (make_list($param{usertag})) {
183 my ($u, $t) = split /:/, $usertag, 2;
184 Debbugs::User::read_usertags(\%select_ut, $u);
185 unless (defined $t && $t ne "") {
186 $t = join(",", keys(%select_ut));
188 add_user($u,\%ut,\%bugusertags,\%seen_users);
189 push @{$param{tag}}, split /,/, $t;
195 %status = %{split_status_fields(get_bug_status(bug=>$ref,
196 bugusertags => \%bugusertags,
197 defined $s?(schema => $s):(),
204 @records = read_log_records(bug_num => $ref,inner_file => 1);
205 $spam = Debbugs::Log::Spam->new(bug_num => $ref);
208 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;
280 # skip spam messages if we're outputting more than one message
281 next if @records > 1 and $spam->is_spam($msg_id);
283 if ($record->{inner_file}) {
284 push @lines, scalar $record->{fh}->getline;
285 push @lines, scalar $record->{fh}->getline;
289 @lines = split( "\n", $record->{text}, -1 );
291 if ( $lines[ 1 ] =~ m/^From / ) {
292 @lines = reverse @lines;
294 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
295 unshift @lines, "From unknown $date";
297 print $lines[0]."\n";
298 print map { s/^(>*From )/>$1/; $_."\n" } @lines[ 1 .. $#lines ];
299 if ($record->{inner_file}) {
300 my $fh = $record->{fh};
301 print $_ while (<$fh>);
308 if (defined $att and defined $msg and @records) {
309 binmode(STDOUT,":raw");
311 ## allow this to be cached for a week
312 print "Status: 200 OK\n";
313 print "Cache-Control: public, max-age=604800\n";
314 print "Etag: $etag\n";
315 print handle_email_message($records[0],
320 trim_headers => $trim_headers,
325 for my $record (@records) {
331 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
332 push @log, handle_record($record,$ref,$msg_num,
334 trim_headers => $trim_headers,
337 # if we're only looking at one record, allow
339 spam => (@records > 1)?$spam:undef,
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 (defined $s{pending} and
426 $s{"pending"} eq 'fixed') or
428 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
432 my @blocks= make_list($status{blocks});
433 $status{blocks_array} = [];
434 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
435 for my $b (@blocks) {
436 my %s = %{get_bug_status($b)};
437 next if $s{"pending"} eq 'fixed' || length $s{done};
438 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
442 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
443 $status{archived} = 1;
446 my $descriptivehead = $indexentry;
448 print $q->header(-type => "text/html",
450 -cache_control => 'public, max-age=300',
454 print fill_in_template(template => 'cgi/bugreport',
455 variables => {status => \%status,
456 package => $packages_affects{'package'},
457 affects => $packages_affects{'affects'},
460 version_graph => $version_graph,
462 isstrongseverity => \&Debbugs::Status::isstrongseverity,
463 html_escape => \&Debbugs::CGI::html_escape,
464 uri_escape => \&URI::Escape::uri_escape_utf8,
465 looks_like_number => \&Scalar::Util::looks_like_number,
466 make_list => \&Debbugs::Common::make_list,
468 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
469 '&bug_links' => \&Debbugs::CGI::bug_links,
470 '&version_url' => \&Debbugs::CGI::version_url,
471 '&strftime' => \&POSIX::strftime,
472 '&maybelink' => \&Debbugs::CGI::maybelink,
479 # indent-tabs-mode: nil
480 # cperl-indent-level: 4