6 # Sanitize environent for taint
8 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
11 use POSIX qw(strftime);
17 use Debbugs::Config qw(:globals :text);
19 # for read_log_records
20 use Debbugs::Log qw(read_log_records);
21 use Debbugs::CGI qw(:url :html :util);
22 use Debbugs::CGI::Bugreport qw(:all);
23 use Debbugs::Common qw(buglog getmaintainers make_list bug_status);
24 use Debbugs::Packages qw(getpkgsrc);
25 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);
33 use List::Util qw(max);
37 my $q = new CGI::Simple;
39 my %param = cgi_parameters(query => $q,
40 single => [qw(bug msg att boring terse),
41 qw(reverse mbox mime trim),
42 qw(mboxstat mboxmaint archive),
45 default => {# msg => '',
54 repeatmerged => 'yes',
57 # This is craptacular.
59 my $ref = $param{bug} or quitcgi("No bug number");
60 $ref =~ /(\d+)/ or quitcgi("Invalid bug number");
63 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
64 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
65 my $boring = $param{'boring'} eq 'yes';
66 my $terse = $param{'terse'} eq 'yes';
67 my $reverse = $param{'reverse'} eq 'yes';
68 my $mbox = $param{'mbox'} eq 'yes';
69 my $mime = $param{'mime'} eq 'yes';
75 my $buglog = buglog($ref);
76 my $bug_status = bug_status($ref);
77 if (not defined $buglog or not defined $bug_status) {
78 print $q->header(-status => "404 No such bug",
82 print fill_in_template(template=>'cgi/no_such_bug',
83 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
90 # the log should almost always be newer, but just in case
91 my $log_mtime = +(stat $buglog)[9] || time;
92 my $status_mtime = +(stat $bug_status)[9] || time;
93 my $mtime = strftime '%a, %d %b %Y %T GMT', gmtime(max($status_mtime,$log_mtime));
95 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
96 print $q->header(-type => "text/html",
98 (length $mtime)?(-last_modified => $mtime):(),
103 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
104 next unless length($user);
105 add_user($user,\%ut,\%bugusertags,\%seen_users);
108 if (defined $param{usertag}) {
109 for my $usertag (make_list($param{usertag})) {
111 my ($u, $t) = split /:/, $usertag, 2;
112 Debbugs::User::read_usertags(\%select_ut, $u);
113 unless (defined $t && $t ne "") {
114 $t = join(",", keys(%select_ut));
116 add_user($u,\%ut,\%bugusertags,\%seen_users);
117 push @{$param{tag}}, split /,/, $t;
122 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
124 my $mbox_status_message = $param{mboxstat} eq 'yes';
125 my $mbox_maint = $param{mboxmaint} eq 'yes';
126 $mbox = 1 if $mbox_status_message or $mbox_maint;
129 # Not used by this script directly, but fetch these so that pkgurl() and
130 # friends can propagate them correctly.
131 my $archive = $param{'archive'} eq 'yes';
132 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
137 if ($buglog =~ m/\.gz$/) {
138 my $oldpath = $ENV{'PATH'};
139 $ENV{'PATH'} = '/bin:/usr/bin';
140 $buglogfh = IO::File->new("zcat $buglog |") or quitcgi("open log for $ref: $!");
141 $ENV{'PATH'} = $oldpath;
143 $buglogfh = IO::File->new($buglog,'r') or quitcgi("open log for $ref: $!");
148 %{split_status_fields(get_bug_status(bug=>$ref,
149 bugusertags => \%bugusertags,
154 @records = read_log_records($buglogfh);
157 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
165 if (defined($msg) and ($msg-1) <= $#records) {
166 @records = ($records[$msg-1]);
171 my $date = strftime "%a %b %d %T %Y", localtime;
173 print $q->header(-type => "text/plain",
174 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
175 (length $mtime)?(-last_modified => $mtime):(),
180 print $q->header(-type => "message/rfc822",
181 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
182 (length $mtime)?(-last_modified => $mtime):(),
185 if ($mbox_status_message and @records > 1) {
186 my $status_message='';
187 my @status_fields = (retitle => 'subject',
188 package => 'package',
189 submitter => 'originator',
190 severity => 'severity',
194 forward => 'forward',
197 while (($key,$value) = splice(@status_fields,0,2)) {
198 if (defined $status{$value} and length $status{$value}) {
199 $status_message .= qq($key $ref $status{$value}\n);
202 print STDOUT qq(From unknown $date\n),
203 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
204 To => "$gBug#$ref <$ref\@$gEmailDomain>",
205 Subject => "Status: $status{subject}",
206 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
215 my $message_number=0;
216 my %seen_message_ids;
217 for my $record (@records) {
218 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
219 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
220 # we want to include control messages anyway
221 my $record_wanted_anyway = 0;
222 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
223 next if exists $seen_message_ids{$msg_id};
224 next if $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
225 $record_wanted_anyway = 1 if $record->{text} =~ /^Received: \(at control\)/;
226 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
227 $seen_message_ids{$msg_id} = 1;
228 my @lines = split( "\n", $record->{text}, -1 );
229 if ( $lines[ 1 ] =~ m/^From / ) {
230 my $tmp = $lines[ 0 ];
231 $lines[ 0 ] = $lines[ 1 ];
234 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
235 unshift @lines, "From unknown $date";
237 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
238 print join( "\n", @lines ) . "\n";
244 if (defined $att and defined $msg and @records) {
246 print handle_email_message($records[0]->{text},
251 trim_headers => $trim_headers,
256 for my $record (@records) {
262 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
263 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
267 @log = reverse @log if $reverse;
268 $log = join("\n",@log);
271 # All of the below should be turned into a template
273 my %maintainer = %{getmaintainers()};
274 my %pkgsrc = %{getpkgsrc()};
282 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
285 print $q->header(-type => "text/html",
287 (length $mtime)?(-last_modified => $mtime):(),
289 print fill_in_template(template=>'cgi/no_such_bug',
290 variables => {modify_time => $dtime,
300 my @packages = make_list($status{package});
302 foreach my $pkg (@packages) {
303 if ($pkg =~ /^src\:/) {
304 my ($srcpkg) = $pkg =~ /^src:(.*)/;
305 $package{$pkg} = {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
312 $package{$pkg} = {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
313 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
319 # fixup various bits of the status
320 $status{tags_array} = [sort(make_list($status{tags}))];
321 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
322 $status{mergedwith_array} = [make_list($status{mergedwith})];
325 my $version_graph = '';
326 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
327 $version_graph = q(<a href=").
328 html_escape(version_url(package => $status{package},
329 found => $status{found_versions},
330 fixed => $status{fixed_versions},
333 q("><img alt="version graph" src=").
334 html_escape(version_url(package => $status{package},
335 found => $status{found_versions},
336 fixed => $status{fixed_versions},
346 my @blockedby= make_list($status{blockedby});
347 $status{blockedby_array} = [];
348 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
349 for my $b (@blockedby) {
350 my %s = %{get_bug_status($b)};
351 next if $s{"pending"} eq 'fixed' || length $s{done};
352 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
356 my @blocks= make_list($status{blocks});
357 $status{blocks_array} = [];
358 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
359 for my $b (@blocks) {
360 my %s = %{get_bug_status($b)};
361 next if $s{"pending"} eq 'fixed' || length $s{done};
362 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
366 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
367 $status{archived} = 1;
370 my $descriptivehead = $indexentry;
372 print $q->header(-type => "text/html",
374 (length $mtime)?(-last_modified => $mtime):(),
377 print fill_in_template(template => 'cgi/bugreport',
378 variables => {status => \%status,
379 package => \%package,
382 version_graph => $version_graph,
384 isstrongseverity => \&Debbugs::Status::isstrongseverity,
385 html_escape => \&Debbugs::CGI::html_escape,
386 looks_like_number => \&Scalar::Util::looks_like_number,
387 make_list => \&Debbugs::Common::make_list,
389 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
390 '&bug_links' => \&Debbugs::CGI::bug_links,
391 '&version_url' => \&Debbugs::CGI::version_url,
392 '&bug_url' => \&Debbugs::CGI::bug_url,
393 '&strftime' => \&POSIX::strftime,
394 '&maybelink' => \&Debbugs::CGI::maybelink,