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);
24 use Scalar::Util qw(looks_like_number);
26 use Debbugs::Text qw(:templates);
28 use List::Util qw(max);
32 my $q = new CGI::Simple;
34 my %param = cgi_parameters(query => $q,
35 single => [qw(bug msg att boring terse),
36 qw(reverse mbox mime trim),
37 qw(mboxstat mboxmaint archive),
40 default => {# msg => '',
49 repeatmerged => 'yes',
52 # This is craptacular.
54 my $ref = $param{bug} or quitcgi("No bug number");
55 $ref =~ /(\d+)/ or quitcgi("Invalid bug number");
58 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
59 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
60 my $boring = $param{'boring'} eq 'yes';
61 my $terse = $param{'terse'} eq 'yes';
62 my $reverse = $param{'reverse'} eq 'yes';
63 my $mbox = $param{'mbox'} eq 'yes';
64 my $mime = $param{'mime'} eq 'yes';
70 my $buglog = buglog($ref);
71 my $bug_status = bug_status($ref);
72 if (not defined $buglog or not defined $bug_status) {
73 print $q->header(-status => "404 No such bug",
77 print fill_in_template(template=>'cgi/no_such_bug',
78 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
85 # the log should almost always be newer, but just in case
86 my $log_mtime = +(stat $buglog)[9] || time;
87 my $status_mtime = +(stat $bug_status)[9] || time;
88 my $mtime = strftime '%a, %d %b %Y %T GMT', gmtime(max($status_mtime,$log_mtime));
90 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
91 print $q->header(-type => "text/html",
93 (length $mtime)?(-last_modified => $mtime):(),
98 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
99 next unless length($user);
100 add_user($user,\%ut,\%bugusertags,\%seen_users);
103 if (defined $param{usertag}) {
104 for my $usertag (make_list($param{usertag})) {
106 my ($u, $t) = split /:/, $usertag, 2;
107 Debbugs::User::read_usertags(\%select_ut, $u);
108 unless (defined $t && $t ne "") {
109 $t = join(",", keys(%select_ut));
111 add_user($u,\%ut,\%bugusertags,\%seen_users);
112 push @{$param{tag}}, split /,/, $t;
117 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
119 my $mbox_status_message = $param{mboxstat} eq 'yes';
120 my $mbox_maint = $param{mboxmaint} eq 'yes';
121 $mbox = 1 if $mbox_status_message or $mbox_maint;
124 # Not used by this script directly, but fetch these so that pkgurl() and
125 # friends can propagate them correctly.
126 my $archive = $param{'archive'} eq 'yes';
127 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
132 if ($buglog =~ m/\.gz$/) {
133 my $oldpath = $ENV{'PATH'};
134 $ENV{'PATH'} = '/bin:/usr/bin';
135 $buglogfh = IO::File->new("zcat $buglog |") or quitcgi("open log for $ref: $!");
136 $ENV{'PATH'} = $oldpath;
138 $buglogfh = IO::File->new($buglog,'r') or quitcgi("open log for $ref: $!");
143 %{split_status_fields(get_bug_status(bug=>$ref,
144 bugusertags => \%bugusertags,
149 @records = read_log_records($buglogfh);
152 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
160 if (defined($msg) and ($msg-1) <= $#records) {
161 @records = ($records[$msg-1]);
166 my $date = strftime "%a %b %d %T %Y", localtime;
168 print $q->header(-type => "text/plain",
169 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
170 (length $mtime)?(-last_modified => $mtime):(),
175 print $q->header(-type => "message/rfc822",
176 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
177 (length $mtime)?(-last_modified => $mtime):(),
180 if ($mbox_status_message and @records > 1) {
181 my $status_message='';
182 my @status_fields = (retitle => 'subject',
183 package => 'package',
184 submitter => 'originator',
185 severity => 'severity',
189 forward => 'forward',
192 while (($key,$value) = splice(@status_fields,0,2)) {
193 if (defined $status{$value} and length $status{$value}) {
194 $status_message .= qq($key $ref $status{$value}\n);
197 print STDOUT qq(From unknown $date\n),
198 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
199 To => "$gBug#$ref <$ref\@$gEmailDomain>",
200 Subject => "Status: $status{subject}",
201 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
210 my $message_number=0;
211 my %seen_message_ids;
212 for my $record (@records) {
213 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
214 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
215 # we want to include control messages anyway
216 my $record_wanted_anyway = 0;
217 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
218 next if exists $seen_message_ids{$msg_id};
219 next if $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
220 $record_wanted_anyway = 1 if $record->{text} =~ /^Received: \(at control\)/;
221 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
222 $seen_message_ids{$msg_id} = 1;
223 my @lines = split( "\n", $record->{text}, -1 );
224 if ( $lines[ 1 ] =~ m/^From / ) {
225 my $tmp = $lines[ 0 ];
226 $lines[ 0 ] = $lines[ 1 ];
229 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
230 unshift @lines, "From unknown $date";
232 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
233 print join( "\n", @lines ) . "\n";
239 if (defined $att and defined $msg and @records) {
241 print handle_email_message($records[0]->{text},
246 trim_headers => $trim_headers,
251 for my $record (@records) {
257 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
258 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
262 @log = reverse @log if $reverse;
263 $log = join("\n",@log);
266 # All of the below should be turned into a template
268 my %maintainer = %{getmaintainers()};
269 my %pkgsrc = %{getpkgsrc()};
277 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
280 print $q->header(-type => "text/html",
282 (length $mtime)?(-last_modified => $mtime):(),
284 print fill_in_template(template=>'cgi/no_such_bug',
285 variables => {modify_time => $dtime,
295 my @packages = make_list($status{package});
297 foreach my $pkg (@packages) {
298 if ($pkg =~ /^src\:/) {
299 my ($srcpkg) = $pkg =~ /^src:(.*)/;
300 $package{$pkg} = {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
307 $package{$pkg} = {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
308 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
314 # fixup various bits of the status
315 $status{tags_array} = [sort(make_list($status{tags}))];
316 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
317 $status{mergedwith_array} = [make_list($status{mergedwith})];
320 my $version_graph = '';
321 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
322 $version_graph = q(<a href=").
323 html_escape(version_url(package => $status{package},
324 found => $status{found_versions},
325 fixed => $status{fixed_versions},
328 q("><img alt="version graph" src=").
329 html_escape(version_url(package => $status{package},
330 found => $status{found_versions},
331 fixed => $status{fixed_versions},
341 my @blockedby= make_list($status{blockedby});
342 $status{blockedby_array} = [];
343 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
344 for my $b (@blockedby) {
345 my %s = %{get_bug_status($b)};
346 next if $s{"pending"} eq 'fixed' || length $s{done};
347 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
351 my @blocks= make_list($status{blocks});
352 $status{blocks_array} = [];
353 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
354 for my $b (@blocks) {
355 my %s = %{get_bug_status($b)};
356 next if $s{"pending"} eq 'fixed' || length $s{done};
357 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
361 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
362 $status{archived} = 1;
365 my $descriptivehead = $indexentry;
367 print $q->header(-type => "text/html",
369 (length $mtime)?(-last_modified => $mtime):(),
372 print fill_in_template(template => 'cgi/bugreport',
373 variables => {status => \%status,
374 package => \%package,
377 version_graph => $version_graph,
379 isstrongseverity => \&Debbugs::Status::isstrongseverity,
380 html_escape => \&Debbugs::CGI::html_escape,
381 looks_like_number => \&Scalar::Util::looks_like_number,
382 make_list => \&Debbugs::Common::make_list,
384 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
385 '&bug_links' => \&Debbugs::CGI::bug_links,
386 '&version_url' => \&Debbugs::CGI::version_url,
387 '&bug_url' => \&Debbugs::CGI::bug_url,
388 '&strftime' => \&POSIX::strftime,
389 '&maybelink' => \&Debbugs::CGI::maybelink,