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 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: $!");
140 my %status = %{get_bug_status(bug=>$ref,
141 bugusertags => \%bugusertags,
146 @records = read_log_records($buglogfh);
149 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
157 if (defined($msg) and ($msg-1) <= $#records) {
158 @records = ($records[$msg-1]);
163 my $date = strftime "%a %b %d %T %Y", localtime;
165 print $q->header(-type => "text/plain",
166 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
167 (length $mtime)?(-last_modified => $mtime):(),
172 print $q->header(-type => "message/rfc822",
173 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
174 (length $mtime)?(-last_modified => $mtime):(),
177 if ($mbox_status_message and @records > 1) {
178 my $status_message='';
179 my @status_fields = (retitle => 'subject',
180 package => 'package',
181 submitter => 'originator',
182 severity => 'severity',
186 forward => 'forward',
189 while (($key,$value) = splice(@status_fields,0,2)) {
190 if (defined $status{$value} and length $status{$value}) {
191 $status_message .= qq($key $ref $status{$value}\n);
194 print STDOUT qq(From unknown $date\n),
195 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
196 To => "$gBug#$ref <$ref\@$gEmailDomain>",
197 Subject => "Status: $status{subject}",
198 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
207 my $message_number=0;
208 my %seen_message_ids;
209 for my $record (@records) {
210 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
211 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
212 # we want to include control messages anyway
213 my $record_wanted_anyway = 0;
214 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
215 next if exists $seen_message_ids{$msg_id};
216 next if $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
217 $record_wanted_anyway = 1 if $record->{text} =~ /^Received: \(at control\)/;
218 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
219 $seen_message_ids{$msg_id} = 1;
220 my @lines = split( "\n", $record->{text}, -1 );
221 if ( $lines[ 1 ] =~ m/^From / ) {
222 my $tmp = $lines[ 0 ];
223 $lines[ 0 ] = $lines[ 1 ];
226 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
227 unshift @lines, "From unknown $date";
229 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
230 print join( "\n", @lines ) . "\n";
236 if (defined $att and defined $msg and @records) {
238 print handle_email_message($records[0]->{text},
243 trim_headers => $trim_headers,
248 for my $record (@records) {
254 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
255 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
259 @log = reverse @log if $reverse;
260 $log = join("\n",@log);
263 # All of the below should be turned into a template
265 my %maintainer = %{getmaintainers()};
266 my %pkgsrc = %{getpkgsrc()};
274 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
277 print $q->header(-type => "text/html",
279 (length $mtime)?(-last_modified => $mtime):(),
281 print fill_in_template(template=>'cgi/no_such_bug',
282 variables => {modify_time => $dtime,
292 my @packages = splitpackages($status{package});
294 foreach my $pkg (@packages) {
295 if ($pkg =~ /^src\:/) {
296 my ($srcpkg) = $pkg =~ /^src:(.*)/;
297 $package{$pkg} = {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
304 $package{$pkg} = {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
305 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
311 # fixup various bits of the status
312 $status{tags_array} = [sort(split(/\s+/, $status{tags}))];
313 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
314 $status{mergedwith_array} = [split(/ /,$status{mergedwith})];
317 my $version_graph = '';
318 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
319 $version_graph = q(<a href=").
320 html_escape(version_url(package => $status{package},
321 found => $status{found_versions},
322 fixed => $status{fixed_versions},
325 q("><img alt="version graph" src=").
326 html_escape(version_url(package => $status{package},
327 found => $status{found_versions},
328 fixed => $status{fixed_versions},
338 my @blockedby= split(/ /, $status{blockedby});
339 $status{blockedby_array} = [];
340 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
341 for my $b (@blockedby) {
342 my %s = %{get_bug_status($b)};
343 next if $s{"pending"} eq 'fixed' || length $s{done};
344 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
348 my @blocks= split(/ /, $status{blocks});
349 $status{blocks_array} = [];
350 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
351 for my $b (@blocks) {
352 my %s = %{get_bug_status($b)};
353 next if $s{"pending"} eq 'fixed' || length $s{done};
354 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
358 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
359 $status{archived} = 1;
362 my $descriptivehead = $indexentry;
364 print $q->header(-type => "text/html",
366 (length $mtime)?(-last_modified => $mtime):(),
369 print fill_in_template(template => 'cgi/bugreport',
370 variables => {status => \%status,
371 package => \%package,
374 version_graph => $version_graph,
376 isstrongseverity => \&Debbugs::Status::isstrongseverity,
377 html_escape => \&Debbugs::CGI::html_escape,
378 looks_like_number => \&Scalar::Util::looks_like_number,
380 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
381 '&bug_links' => \&Debbugs::CGI::bug_links,
382 '&version_url' => \&Debbugs::CGI::version_url,
383 '&bug_url' => \&Debbugs::CGI::bug_url,
384 '&strftime' => \&POSIX::strftime,
385 '&maybelink' => \&Debbugs::CGI::maybelink,