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);
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);
28 my $q = new CGI::Simple;
30 my %param = cgi_parameters(query => $q,
31 single => [qw(bug msg att boring terse),
32 qw(reverse mbox mime trim),
33 qw(mboxstat mboxmaint archive),
36 default => {# msg => '',
45 repeatmerged => 'yes',
48 # This is craptacular.
50 my $ref = $param{bug} or quitcgi("No bug number");
51 $ref =~ /(\d+)/ or quitcgi("Invalid bug number");
54 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
55 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
56 my $boring = $param{'boring'} eq 'yes';
57 my $terse = $param{'terse'} eq 'yes';
58 my $reverse = $param{'reverse'} eq 'yes';
59 my $mbox = $param{'mbox'} eq 'yes';
60 my $mime = $param{'mime'} eq 'yes';
66 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
67 next unless length($user);
68 add_user($user,\%ut,\%bugusertags,\%seen_users);
71 if (defined $param{usertag}) {
72 for my $usertag (make_list($param{usertag})) {
74 my ($u, $t) = split /:/, $usertag, 2;
75 Debbugs::User::read_usertags(\%select_ut, $u);
76 unless (defined $t && $t ne "") {
77 $t = join(",", keys(%select_ut));
79 add_user($u,\%ut,\%bugusertags,\%seen_users);
80 push @{$param{tag}}, split /,/, $t;
85 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
87 my $mbox_status_message = $param{mboxstat} eq 'yes';
88 my $mbox_maint = $param{mboxmaint} eq 'yes';
89 $mbox = 1 if $mbox_status_message or $mbox_maint;
92 # Not used by this script directly, but fetch these so that pkgurl() and
93 # friends can propagate them correctly.
94 my $archive = $param{'archive'} eq 'yes';
95 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
97 my $buglog = buglog($ref);
98 if (not defined $buglog) {
99 print $q->header(-status => "404 No such bug",
100 -type => "text/html",
103 print fill_in_template(template=>'cgi/no_such_bug',
104 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
111 my @stat = stat $buglog;
114 $mtime = strftime '%a, %d %b %Y %T GMT', gmtime($stat[9]);
117 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
118 print $q->header(-type => "text/html",
120 (length $mtime)?(-last_modified => $mtime):(),
127 if ($buglog =~ m/\.gz$/) {
128 my $oldpath = $ENV{'PATH'};
129 $ENV{'PATH'} = '/bin:/usr/bin';
130 $buglogfh = IO::File->new("zcat $buglog |") or quitcgi("open log for $ref: $!");
131 $ENV{'PATH'} = $oldpath;
133 $buglogfh = IO::File->new($buglog,'r') or quitcgi("open log for $ref: $!");
137 my %status = %{get_bug_status(bug=>$ref,
138 bugusertags => \%bugusertags,
143 @records = read_log_records($buglogfh);
146 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
154 if (defined($msg) and ($msg-1) <= $#records) {
155 @records = ($records[$msg-1]);
160 my $date = strftime "%a %b %d %T %Y", localtime;
162 print $q->header(-type => "text/plain",
163 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
164 (length $mtime)?(-last_modified => $mtime):(),
169 print $q->header(-type => "message/rfc822",
170 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
171 (length $mtime)?(-last_modified => $mtime):(),
174 if ($mbox_status_message and @records > 1) {
175 my $status_message='';
176 my @status_fields = (retitle => 'subject',
177 package => 'package',
178 submitter => 'originator',
179 severity => 'severity',
183 forward => 'forward',
186 while (($key,$value) = splice(@status_fields,0,2)) {
187 if (defined $status{$value} and length $status{$value}) {
188 $status_message .= qq($key $ref $status{$value}\n);
191 print STDOUT qq(From unknown $date\n),
192 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
193 To => "$gBug#$ref <$ref\@$gEmailDomain>",
194 Subject => "Status: $status{subject}",
195 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
204 my $message_number=0;
205 my %seen_message_ids;
206 for my $record (@records) {
207 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
208 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
209 # we want to include control messages anyway
210 my $record_wanted_anyway = 0;
211 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
212 next if exists $seen_message_ids{$msg_id};
213 next if $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
214 $record_wanted_anyway = 1 if $record->{text} =~ /^Received: \(at control\)/;
215 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
216 $seen_message_ids{$msg_id} = 1;
217 my @lines = split( "\n", $record->{text}, -1 );
218 if ( $lines[ 1 ] =~ m/^From / ) {
219 my $tmp = $lines[ 0 ];
220 $lines[ 0 ] = $lines[ 1 ];
223 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
224 unshift @lines, "From unknown $date";
226 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
227 print join( "\n", @lines ) . "\n";
233 if (defined $att and defined $msg and @records) {
235 print handle_email_message($records[0]->{text},
244 for my $record (@records) {
250 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
251 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
255 @log = reverse @log if $reverse;
256 $log = join("\n",@log);
259 # All of the below should be turned into a template
261 my %maintainer = %{getmaintainers()};
262 my %pkgsrc = %{getpkgsrc()};
270 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
273 print $q->header(-type => "text/html",
275 (length $mtime)?(-last_modified => $mtime):(),
277 print fill_in_template(template=>'cgi/no_such_bug',
278 variables => {modify_time => $dtime,
288 my @packages = splitpackages($status{package});
290 foreach my $pkg (@packages) {
291 $package{$pkg} = {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
292 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
297 # fixup various bits of the status
298 $status{tags_array} = [sort(split(/\s+/, $status{tags}))];
299 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
300 $status{mergedwith_array} = [split(/ /,$status{mergedwith})];
303 my $version_graph = '';
304 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
305 $version_graph = q(<a href=").
306 html_escape(version_url(package => $status{package},
307 found => $status{found_versions},
308 fixed => $status{fixed_versions},
311 q("><img alt="version graph" src=").
312 html_escape(version_url(package => $status{package},
313 found => $status{found_versions},
314 fixed => $status{fixed_versions},
324 my @blockedby= split(/ /, $status{blockedby});
325 $status{blockedby_array} = [];
326 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
327 for my $b (@blockedby) {
328 my %s = %{get_bug_status($b)};
329 next if $s{"pending"} eq 'fixed' || length $s{done};
330 push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
334 my @blocks= split(/ /, $status{blocks});
335 $status{blocks_array} = [];
336 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
337 for my $b (@blocks) {
338 my %s = %{get_bug_status($b)};
339 next if $s{"pending"} eq 'fixed' || length $s{done};
340 push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
344 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
345 $status{archived} = 1;
348 my $descriptivehead = $indexentry;
350 print $q->header(-type => "text/html",
352 (length $mtime)?(-last_modified => $mtime):(),
355 print fill_in_template(template => 'cgi/bugreport',
356 variables => {status => \%status,
357 package => \%package,
360 version_graph => $version_graph,
362 isstrongseverity => \&Debbugs::Status::isstrongseverity,
363 html_escape => \&Debbugs::CGI::html_escape,
364 looks_like_number => \&Scalar::Util::looks_like_number,
366 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
367 '&bug_links' => \&Debbugs::CGI::bug_links,
368 '&version_url' => \&Debbugs::CGI::version_url,
369 '&bug_url' => \&Debbugs::CGI::bug_url,
370 '&strftime' => \&POSIX::strftime,
371 '&maybelink' => \&Debbugs::CGI::maybelink,