6 use POSIX qw(strftime);
12 use Debbugs::Config qw(:globals :text);
14 # for read_log_records
15 use Debbugs::Log qw(read_log_records);
16 use Debbugs::CGI qw(:url :html :util);
17 use Debbugs::CGI::Bugreport qw(:all);
18 use Debbugs::Common qw(buglog getmaintainers make_list bug_status);
19 use Debbugs::Packages qw(getpkgsrc);
20 use Debbugs::Status qw(splitpackages split_status_fields get_bug_status isstrongseverity);
22 use Scalar::Util qw(looks_like_number);
24 use Debbugs::Text qw(:templates);
26 use List::Util qw(max);
30 my $q = new CGI::Simple;
32 my %param = cgi_parameters(query => $q,
33 single => [qw(bug msg att boring terse),
34 qw(reverse mbox mime trim),
35 qw(mboxstat mboxmaint archive),
38 default => {# msg => '',
47 repeatmerged => 'yes',
50 # This is craptacular.
52 my $ref = $param{bug} or quitcgi("No bug number");
53 $ref =~ /(\d+)/ or quitcgi("Invalid bug number");
56 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
57 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
58 my $boring = $param{'boring'} eq 'yes';
59 my $terse = $param{'terse'} eq 'yes';
60 my $reverse = $param{'reverse'} eq 'yes';
61 my $mbox = $param{'mbox'} eq 'yes';
62 my $mime = $param{'mime'} eq 'yes';
68 my $buglog = buglog($ref);
69 my $bug_status = bug_status($ref);
70 if (not defined $buglog or not defined $bug_status) {
71 print $q->header(-status => "404 No such bug",
75 print fill_in_template(template=>'cgi/no_such_bug',
76 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
83 # the log should almost always be newer, but just in case
84 my $log_mtime = +(stat $buglog)[9] || time;
85 my $status_mtime = +(stat $bug_status)[9] || time;
86 my $mtime = strftime '%a, %d %b %Y %T GMT', gmtime(max($status_mtime,$log_mtime));
88 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
89 print $q->header(-type => "text/html",
91 (length $mtime)?(-last_modified => $mtime):(),
96 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
97 next unless length($user);
98 add_user($user,\%ut,\%bugusertags,\%seen_users);
101 if (defined $param{usertag}) {
102 for my $usertag (make_list($param{usertag})) {
104 my ($u, $t) = split /:/, $usertag, 2;
105 Debbugs::User::read_usertags(\%select_ut, $u);
106 unless (defined $t && $t ne "") {
107 $t = join(",", keys(%select_ut));
109 add_user($u,\%ut,\%bugusertags,\%seen_users);
110 push @{$param{tag}}, split /,/, $t;
115 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
117 my $mbox_status_message = $param{mboxstat} eq 'yes';
118 my $mbox_maint = $param{mboxmaint} eq 'yes';
119 $mbox = 1 if $mbox_status_message or $mbox_maint;
122 # Not used by this script directly, but fetch these so that pkgurl() and
123 # friends can propagate them correctly.
124 my $archive = $param{'archive'} eq 'yes';
125 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
130 if ($buglog =~ m/\.gz$/) {
131 my $oldpath = $ENV{'PATH'};
132 $ENV{'PATH'} = '/bin:/usr/bin';
133 $buglogfh = IO::File->new("zcat $buglog |") or quitcgi("open log for $ref: $!");
134 $ENV{'PATH'} = $oldpath;
136 $buglogfh = IO::File->new($buglog,'r') or quitcgi("open log for $ref: $!");
141 %{split_status_fields(get_bug_status(bug=>$ref,
142 bugusertags => \%bugusertags,
147 @records = read_log_records($buglogfh);
150 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
158 if (defined($msg) and ($msg-1) <= $#records) {
159 @records = ($records[$msg-1]);
164 my $date = strftime "%a %b %d %T %Y", localtime;
166 print $q->header(-type => "text/plain",
167 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
168 (length $mtime)?(-last_modified => $mtime):(),
173 print $q->header(-type => "message/rfc822",
174 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
175 (length $mtime)?(-last_modified => $mtime):(),
178 if ($mbox_status_message and @records > 1) {
179 my $status_message='';
180 my @status_fields = (retitle => 'subject',
181 package => 'package',
182 submitter => 'originator',
183 severity => 'severity',
187 forward => 'forward',
190 while (($key,$value) = splice(@status_fields,0,2)) {
191 if (defined $status{$value} and length $status{$value}) {
192 $status_message .= qq($key $ref $status{$value}\n);
195 print STDOUT qq(From unknown $date\n),
196 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
197 To => "$gBug#$ref <$ref\@$gEmailDomain>",
198 Subject => "Status: $status{subject}",
199 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
208 my $message_number=0;
209 my %seen_message_ids;
210 for my $record (@records) {
211 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
212 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
213 # we want to include control messages anyway
214 my $record_wanted_anyway = 0;
215 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
216 next if exists $seen_message_ids{$msg_id};
217 next if $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
218 $record_wanted_anyway = 1 if $record->{text} =~ /^Received: \(at control\)/;
219 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
220 $seen_message_ids{$msg_id} = 1;
221 my @lines = split( "\n", $record->{text}, -1 );
222 if ( $lines[ 1 ] =~ m/^From / ) {
223 my $tmp = $lines[ 0 ];
224 $lines[ 0 ] = $lines[ 1 ];
227 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
228 unshift @lines, "From unknown $date";
230 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
231 print join( "\n", @lines ) . "\n";
237 if (defined $att and defined $msg and @records) {
239 print handle_email_message($records[0]->{text},
244 trim_headers => $trim_headers,
249 for my $record (@records) {
255 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
256 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
260 @log = reverse @log if $reverse;
261 $log = join("\n",@log);
264 # All of the below should be turned into a template
266 my %maintainer = %{getmaintainers()};
267 my %pkgsrc = %{getpkgsrc()};
275 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
278 print $q->header(-type => "text/html",
280 (length $mtime)?(-last_modified => $mtime):(),
282 print fill_in_template(template=>'cgi/no_such_bug',
283 variables => {modify_time => $dtime,
293 my @packages = splitpackages($status{package});
295 foreach my $pkg (@packages) {
296 if ($pkg =~ /^src\:/) {
297 my ($srcpkg) = $pkg =~ /^src:(.*)/;
298 $package{$pkg} = {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
305 $package{$pkg} = {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
306 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
312 # fixup various bits of the status
313 $status{tags_array} = [sort(make_list($status{tags}))];
314 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
315 $status{mergedwith_array} = [make_list($status{mergedwith})];
318 my $version_graph = '';
319 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
320 $version_graph = q(<a href=").
321 html_escape(version_url(package => $status{package},
322 found => $status{found_versions},
323 fixed => $status{fixed_versions},
326 q("><img alt="version graph" src=").
327 html_escape(version_url(package => $status{package},
328 found => $status{found_versions},
329 fixed => $status{fixed_versions},
339 my @blockedby= make_list($status{blockedby});
340 $status{blockedby_array} = [];
341 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
342 for my $b (@blockedby) {
343 my %s = %{get_bug_status($b)};
344 next if $s{"pending"} eq 'fixed' || length $s{done};
345 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
349 my @blocks= make_list($status{blocks});
350 $status{blocks_array} = [];
351 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
352 for my $b (@blocks) {
353 my %s = %{get_bug_status($b)};
354 next if $s{"pending"} eq 'fixed' || length $s{done};
355 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
359 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
360 $status{archived} = 1;
363 my $descriptivehead = $indexentry;
365 print $q->header(-type => "text/html",
367 (length $mtime)?(-last_modified => $mtime):(),
370 print fill_in_template(template => 'cgi/bugreport',
371 variables => {status => \%status,
372 package => \%package,
375 version_graph => $version_graph,
377 isstrongseverity => \&Debbugs::Status::isstrongseverity,
378 html_escape => \&Debbugs::CGI::html_escape,
379 looks_like_number => \&Scalar::Util::looks_like_number,
380 make_list => \&Debbugs::Common::make_list,
382 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
383 '&bug_links' => \&Debbugs::CGI::bug_links,
384 '&version_url' => \&Debbugs::CGI::version_url,
385 '&bug_url' => \&Debbugs::CGI::bug_url,
386 '&strftime' => \&POSIX::strftime,
387 '&maybelink' => \&Debbugs::CGI::maybelink,