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);
27 use Debbugs::Status qw(splitpackages split_status_fields get_bug_status isstrongseverity);
29 use Scalar::Util qw(looks_like_number);
31 use Debbugs::Text qw(:templates);
32 use URI::Escape qw(uri_escape_utf8);
33 use List::AllUtils qw(max);
35 if (defined $ENV{REMOTE_ADDR} and $ENV{REMOTE_ADDR} =~ /(?:218\.175\.56\.14|64\.126\
36 .93\.93|72\.17\.168\.57|208\.138\.29\.104|66\.63\.250\.28|71\.70\.91\.207)/) {
37 print "Content-Type: text/html\n\nGo away.";
45 my $q = new CGI::Simple;
46 # STDOUT should be using the utf8 io layer
47 binmode(STDOUT,':raw:encoding(UTF-8)');
49 my %param = cgi_parameters(query => $q,
50 single => [qw(bug msg att boring terse),
51 qw(reverse mbox mime trim),
52 qw(mboxstat mboxmaint archive),
53 qw(repeatmerged avatars),
55 default => {# msg => '',
64 repeatmerged => 'yes',
68 # This is craptacular.
70 my $ref = $param{bug} or quitcgi("No bug number", '400 Bad Request');
71 $ref =~ /(\d+)/ or quitcgi("Invalid bug number", '400 Bad Request');
74 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
75 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
76 my $boring = $param{'boring'} eq 'yes';
77 my $terse = $param{'terse'} eq 'yes';
78 my $reverse = $param{'reverse'} eq 'yes';
79 my $mbox = $param{'mbox'} eq 'yes';
80 my $mime = $param{'mime'} eq 'yes';
81 my $avatars = $param{avatars} eq 'yes';
83 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
85 my $mbox_status_message = $param{mboxstat} eq 'yes';
86 my $mbox_maint = $param{mboxmaint} eq 'yes';
87 $mbox = 1 if $mbox_status_message or $mbox_maint;
89 # Not used by this script directly, but fetch these so that pkgurl() and
90 # friends can propagate them correctly.
91 my $archive = $param{'archive'} eq 'yes';
92 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
98 my $buglog = buglog($ref);
99 my $bug_status = bug_status($ref);
100 if (not defined $buglog or not defined $bug_status) {
101 no_such_bug($q,$ref);
106 print $q->header(-status => 404,
107 -content_type => "text/html",
109 -cache_control => 'public, max-age=600',
111 print fill_in_template(template=>'cgi/no_such_bug',
112 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
119 ## calculate etag for this bugreport.cgi call
121 ## identify the files that we need to look at; if someone just wants the mbox,
122 ## they don't need to see anything but the buglog; otherwise, track what is
123 ## necessary for the usertags and things to calculate status.
125 my @dependent_files = ($buglog);
127 if (not (($mbox and not $mbox_status_message) or
128 (defined $att and defined $msg))) {
130 push @dependent_files,
132 defined $config{version_index} ? $config{version_index}:(),
133 defined $config{binary_source_map} ? $config{binary_source_map}:();
136 ## Identify the users required
137 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
138 next unless length($user);
139 push @dependent_files,Debbugs::User::usertag_file_from_email($user);
141 if (defined $param{usertag}) {
142 for my $usertag (make_list($param{usertag})) {
143 my ($user, $tag) = split /:/, $usertag, 2;
144 push @dependent_files,Debbugs::User::usertag_file_from_email($user);
148 etag_does_not_match(cgi => $q,
149 additional_data => [grep {defined $_ ? $_ :()}
152 files => [@dependent_files,
156 print $q->header(-status => 304,
157 -cache_control => 'public, max-age=600',
160 -content_type => 'text/html',
162 print "304: Not modified\n";
166 ## if they're just asking for the head, stop here.
167 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
168 print $q->header(-status => 200,
169 -cache_control => 'public, max-age=600',
172 -content_type => 'text/html',
177 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
178 next unless length($user);
179 add_user($user,\%ut,\%bugusertags,\%seen_users);
182 if (defined $param{usertag}) {
183 for my $usertag (make_list($param{usertag})) {
185 my ($u, $t) = split /:/, $usertag, 2;
186 Debbugs::User::read_usertags(\%select_ut, $u);
187 unless (defined $t && $t ne "") {
188 $t = join(",", keys(%select_ut));
190 add_user($u,\%ut,\%bugusertags,\%seen_users);
191 push @{$param{tag}}, split /,/, $t;
197 %status = %{split_status_fields(get_bug_status(bug=>$ref,
198 bugusertags => \%bugusertags,
205 @records = read_log_records(bug_num => $ref,inner_file => 1);
206 $spam = Debbugs::Log::Spam->new(bug_num => $ref);
209 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
215 if (defined($msg) and ($msg-1) <= $#records) {
216 @records = ($records[$msg-1]);
221 binmode(STDOUT,":raw");
222 my $date = strftime "%a %b %d %T %Y", localtime;
223 my $multiple_messages = @records > 1;
224 if ($multiple_messages) {
225 print $q->header(-type => "application/mbox",
226 -cache_control => 'public, max-age=600',
228 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
233 print $q->header(-type => "message/rfc822",
234 -cache_control => 'public, max-age=86400',
236 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.eml"),
239 if ($mbox_status_message and $multiple_messages) {
240 my $status_message='';
241 my @status_fields = (retitle => 'subject',
242 package => 'package',
243 submitter => 'originator',
244 severity => 'severity',
248 forward => 'forward',
251 while (($key,$value) = splice(@status_fields,0,2)) {
252 if (defined $status{$value} and length $status{$value}) {
253 $status_message .= qq($key $ref $status{$value}\n);
256 print STDOUT qq(From unknown $date\n),
257 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
258 To => "$gBug#$ref <$ref\@$gEmailDomain>",
259 Subject => "Status: $status{subject}",
260 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
269 my $message_number=0;
270 my %seen_message_ids;
271 for my $record (@records) {
272 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
273 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
274 # we want to include control messages anyway
275 my $record_wanted_anyway = 0;
276 my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/im);
277 next if defined $msg_id and exists $seen_message_ids{$msg_id};
278 next if not defined $msg and defined $msg_id and $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
279 $record_wanted_anyway = 1 if record_regex($record,qr/^Received: \(at control\)/);
280 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
281 $seen_message_ids{$msg_id} = 1 if defined $msg_id;
282 # skip spam messages if we're outputting more than one message
283 next if $multiple_messages and $spam->is_spam($msg_id);
285 if ($record->{inner_file}) {
286 push @lines, scalar $record->{fh}->getline;
287 push @lines, scalar $record->{fh}->getline;
291 @lines = split( "\n", $record->{text}, -1 );
293 if ( $lines[ 1 ] =~ m/^From / ) {
294 @lines = reverse @lines;
296 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
297 unshift @lines, "From unknown $date";
299 print $lines[0]."\n";
300 print map { s/^(>*From )/>$1/ if $multiple_messages;
301 $_."\n" } @lines[ 1 .. $#lines ];
302 if ($record->{inner_file}) {
303 my $fh = $record->{fh};
306 s/^(>*From )/>$1/gm if $multiple_messages;
315 if (defined $att and defined $msg and @records) {
316 binmode(STDOUT,":raw");
318 ## allow this to be cached for a week
319 print "Status: 200 OK\n";
320 print "Cache-Control: public, max-age=604800\n";
321 print "Etag: $etag\n";
322 print handle_email_message($records[0],
327 trim_headers => $trim_headers,
332 for my $record (@records) {
338 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
339 push @log, handle_record($record,$ref,$msg_num,
341 trim_headers => $trim_headers,
344 # if we're only looking at one record, allow
346 spam => (@records > 1)?$spam:undef,
351 @log = reverse @log if $reverse;
352 $log = join("\n",@log);
355 # All of the below should be turned into a template
357 my %maintainer = %{getmaintainers()};
358 my %pkgsrc = %{getpkgsrc()};
366 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
369 no_such_bug($q,$ref);
375 my @packages = make_list($status{package});
378 my %packages_affects;
379 for my $p_a (qw(package affects)) {
380 foreach my $pkg (make_list($status{$p_a})) {
381 if ($pkg =~ /^src\:/) {
382 my ($srcpkg) = $pkg =~ /^src:(.*)/;
383 $packages_affects{$p_a}{$pkg} =
384 {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
391 $packages_affects{$p_a}{$pkg} =
392 {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
393 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
400 # fixup various bits of the status
401 $status{tags_array} = [sort(make_list($status{tags}))];
402 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
403 $status{mergedwith_array} = [make_list($status{mergedwith})];
406 my $version_graph = '';
407 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
408 $version_graph = q(<a href=").
409 html_escape(version_url(package => $status{package},
410 found => $status{found_versions},
411 fixed => $status{fixed_versions},
414 q("><img alt="version graph" src=").
415 html_escape(version_url(package => $status{package},
416 found => $status{found_versions},
417 fixed => $status{fixed_versions},
427 my @blockedby= make_list($status{blockedby});
428 $status{blockedby_array} = [];
429 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
430 for my $b (@blockedby) {
431 my %s = %{get_bug_status($b)};
432 next if (defined $s{pending} and
433 $s{"pending"} eq 'fixed') or
435 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
439 my @blocks= make_list($status{blocks});
440 $status{blocks_array} = [];
441 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
442 for my $b (@blocks) {
443 my %s = %{get_bug_status($b)};
444 next if $s{"pending"} eq 'fixed' || length $s{done};
445 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
449 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
450 $status{archived} = 1;
453 my $descriptivehead = $indexentry;
455 print $q->header(-type => "text/html",
457 -cache_control => 'public, max-age=300',
461 print fill_in_template(template => 'cgi/bugreport',
462 variables => {status => \%status,
463 package => $packages_affects{'package'},
464 affects => $packages_affects{'affects'},
467 version_graph => $version_graph,
469 isstrongseverity => \&Debbugs::Status::isstrongseverity,
470 html_escape => \&Debbugs::CGI::html_escape,
471 uri_escape => \&URI::Escape::uri_escape_utf8,
472 looks_like_number => \&Scalar::Util::looks_like_number,
473 make_list => \&Debbugs::Common::make_list,
475 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
476 '&bug_links' => \&Debbugs::CGI::bug_links,
477 '&version_url' => \&Debbugs::CGI::version_url,
478 '&strftime' => \&POSIX::strftime,
479 '&maybelink' => \&Debbugs::CGI::maybelink,
486 # indent-tabs-mode: nil
487 # cperl-indent-level: 4