7 use POSIX qw(strftime tzset);
13 use Debbugs::Config qw(:globals);
14 #require '/usr/lib/debbugs/errorlib';
15 require './common.pl';
17 require '/etc/debbugs/text';
19 # for read_log_records
21 use Debbugs::MIME qw(convert_to_utf8 decode_rfc1522 create_mime_message);
22 use Debbugs::CGI qw(:url :html);
24 use Scalar::Util qw(looks_like_number);
26 my %param = readparse();
30 my $ref = $param{'bug'} || quitcgi("No bug number");
31 $ref =~ /(\d+)/ or quitcgi("Invalid bug number");
34 my $msg = $param{'msg'} || "";
35 my $att = $param{'att'};
36 my $boring = ($param{'boring'} || 'no') eq 'yes';
37 my $terse = ($param{'terse'} || 'no') eq 'yes';
38 my $reverse = ($param{'reverse'} || 'no') eq 'yes';
39 my $mbox = ($param{'mbox'} || 'no') eq 'yes';
40 my $mime = ($param{'mime'} || 'yes') eq 'yes';
42 my $trim_headers = ($param{trim} || ($msg?'no':'yes')) eq 'yes';
44 my $mbox_status_message = ($param{mboxstat}||'no') eq 'yes';
45 my $mbox_maint = ($param{mboxmaint}||'no') eq 'yes';
46 $mbox = 1 if $mbox_status_message or $mbox_maint;
49 # Not used by this script directly, but fetch these so that pkgurl() and
50 # friends can propagate them correctly.
51 my $archive = ($param{'archive'} || 'no') eq 'yes';
52 my $repeatmerged = ($param{'repeatmerged'} || 'yes') eq 'yes';
53 set_option('archive', $archive);
54 set_option('repeatmerged', $repeatmerged);
56 my $buglog = buglog($ref);
58 if (defined $ENV{REQUEST_METHOD} and $ENV{REQUEST_METHOD} eq 'HEAD' and not defined($att) and not $mbox) {
59 print "Content-Type: text/html; charset=utf-8\n";
60 my @stat = stat $buglog;
62 my $mtime = strftime '%a, %d %b %Y %T GMT', gmtime($stat[9]);
63 print "Last-Modified: $mtime\n";
69 sub display_entity ($$$$\$\@);
70 sub display_entity ($$$$\$\@) {
76 my $attachments = shift;
78 my $head = $entity->head;
79 my $disposition = $head->mime_attr('content-disposition');
80 $disposition = 'inline' if not defined $disposition or $disposition eq '';
81 my $type = $entity->effective_type;
82 my $filename = $entity->head->recommended_filename;
83 $filename = '' unless defined $filename;
84 $filename = decode_rfc1522($filename);
86 if ($top and not $terse) {
87 my $header = $entity->head;
88 $$this .= "<pre class=\"headers\">\n";
91 foreach (qw(From To Cc Subject Date)) {
92 my $head_field = $head->get($_);
93 next unless defined $head_field and $head_field ne '';
94 push @headers, qq(<b>$_:</b> ) . htmlsanit(decode_rfc1522($head_field));
96 $$this .= join(qq(), @headers) unless $terse;
98 $$this .= htmlsanit(decode_rfc1522($entity->head->stringify));
100 $$this .= "</pre>\n";
103 unless (($top and $type =~ m[^text(?:/plain)?(?:;|$)]) or
104 ($type =~ m[^multipart/])) {
105 push @$attachments, $entity;
106 my @dlargs = ($ref, "msg=$xmessage", "att=$#$attachments");
107 push @dlargs, "filename=$filename" if $filename ne '';
108 my $printname = $filename;
109 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
110 $$this .= '<pre class="mime">[<a href="' . bugurl(@dlargs) . qq{">$printname</a> } .
111 "($type, $disposition)]</pre>\n";
113 if ($msg and defined($att) and $att eq $#$attachments) {
114 my $head = $entity->head;
115 chomp(my $type = $entity->effective_type);
116 my $body = $entity->stringify_body;
117 print "Content-Type: $type";
118 my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
119 print qq(; charset="$charset") if defined $charset;
121 if ($filename ne '') {
125 print qq{Content-Disposition: inline; filename="$qf"\n};
128 my $decoder = new MIME::Decoder($head->mime_encoding);
129 $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
134 return if not $top and $disposition eq 'attachment' and not defined($att);
135 return unless ($type =~ m[^text/?] and
136 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
137 $type =~ m[^application/pgp(?:;|$)] or
140 if ($entity->is_multipart) {
141 my @parts = $entity->parts;
142 foreach my $part (@parts) {
143 display_entity($part, $ref, 0, $xmessage,
144 $$this, @$attachments);
147 } elsif ($entity->parts) {
148 # We must be dealing with a nested message.
149 $$this .= "<blockquote>\n";
150 my @parts = $entity->parts;
151 foreach my $part (@parts) {
152 display_entity($part, $ref, 1, $xmessage,
153 $$this, @$attachments);
156 $$this .= "</blockquote>\n";
159 my $content_type = $entity->head->get('Content-Type:') || "text/html";
160 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
161 my $body = $entity->bodyhandle->as_string;
162 $body = convert_to_utf8($body,$charset) if defined $charset;
163 $body = htmlsanit($body);
165 $body =~ s,((ftp|http|https)://[\S~-]+?/?)((\>\;)?[)]?[']?[:.\,]?(\s|$)),<a href=\"$1\">$1</a>$3,go;
166 # Add links to bug closures
167 $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)
168 ][my $temp = $1; $temp =~ s{(\d+)}{qq(<a href=").bugurl($1).qq(">$1</a>)}ge; $temp;]gxie;
169 $$this .= qq(<pre class="message">$body</pre>\n);
174 my %maintainer = %{getmaintainers()};
175 my %pkgsrc = %{getpkgsrc()};
186 my $dtime = strftime "%a, %e %b %Y %T UTC", localtime;
187 $tail_html = $debbugs::gHTMLTail;
188 $tail_html =~ s/SUBSTITUTE_DTIME/$dtime/;
190 my %status = %{getbugstatus($ref)};
193 Content-Type: text/html; charset=utf-8
195 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
197 <head><title>$short - $debbugs::gProject $debbugs::gBug report logs</title></head>
199 <h1>$debbugs::gProject $debbugs::gBug report logs - $short</h1>
200 <p>There is no record of $debbugs::gBug $short.
201 Try the <a href="http://$gWebDomain/">search page</a> instead.</p>
202 $tail_html</body></html>
209 $tpack = lc $status{'package'};
210 my @tpacks = splitpackages($tpack);
212 if ($status{severity} eq 'normal') {
214 } elsif (isstrongseverity($status{severity})) {
215 $showseverity = "Severity: <em class=\"severity\">$status{severity}</em>;\n";
217 $showseverity = "Severity: $status{severity};\n";
220 $indexentry .= "<div class=\"msgreceived\">\n";
221 $indexentry .= htmlpackagelinks($status{package}, 0) . ";\n";
223 foreach my $pkg (@tpacks) {
224 my $tmaint = defined($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)';
225 my $tsrc = defined($pkgsrc{$pkg}) ? $pkgsrc{$pkg} : '(unknown)';
228 htmlmaintlinks(sub { $_[0] == 1 ? "Maintainer for $pkg is\n"
229 : "Maintainers for $pkg are\n" },
231 $indexentry .= ";\nSource for $pkg is\n".
232 '<a href="'.srcurl($tsrc)."\">$tsrc</a>" if ($tsrc ne "(unknown)");
233 $indexentry .= ".\n";
236 $indexentry .= "<br>";
237 $indexentry .= htmladdresslinks("Reported by: ", \&submitterurl,
238 $status{originator}) . ";\n";
239 $indexentry .= sprintf "Date: %s.\n",
240 (strftime "%a, %e %b %Y %T UTC", localtime($status{date}));
242 $indexentry .= "<br>Owned by: " . htmlsanit($status{owner}) . ".\n"
243 if length $status{owner};
245 $indexentry .= "</div>\n";
249 $indexentry .= "<h3>$showseverity";
250 $indexentry .= sprintf "Tags: %s;\n",
251 htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
252 if length($status{tags});
253 $indexentry .= "<br>" if (length($showseverity) or length($status{tags}));
255 my @merged= split(/ /,$status{mergedwith});
257 my $descmerged = 'Merged with ';
259 for my $m (@merged) {
260 $descmerged .= $mseparator."<a href=\"" . bugurl($m) . "\">#$m</a>";
263 push @descstates, $descmerged;
266 if (@{$status{found_versions}}) {
267 my $foundtext = 'Found in ';
268 $foundtext .= (@{$status{found_versions}} == 1) ? 'version ' : 'versions ';
269 $foundtext .= join ', ', map htmlsanit($_), @{$status{found_versions}};
270 push @descstates, $foundtext;
273 if (@{$status{fixed_versions}}) {
274 my $fixedtext = '<strong>Fixed</strong> in ';
275 $fixedtext .= (@{$status{fixed_versions}} == 1) ? 'version ' : 'versions ';
276 $fixedtext .= join ', ', map htmlsanit($_), @{$status{fixed_versions}};
277 if (length($status{done})) {
278 $fixedtext .= ' by ' . htmlsanit(decode_rfc1522($status{done}));
280 push @descstates, $fixedtext;
281 } elsif (length($status{done})) {
282 push @descstates, "<strong>Done:</strong> ".htmlsanit(decode_rfc1522($status{done}));
283 } elsif (length($status{forwarded})) {
284 push @descstates, "<strong>Forwarded</strong> to ".maybelink($status{forwarded});
288 my @blockedby= split(/ /, $status{blockedby});
289 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
290 for my $b (@blockedby) {
291 my %s = %{getbugstatus($b)};
292 next if $s{"pending"} eq 'fixed' || length $s{done};
293 push @descstates, "Fix blocked by <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
297 my @blocks= split(/ /, $status{blocks});
298 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
299 for my $b (@blocks) {
300 my %s = %{getbugstatus($b)};
301 next if $s{"pending"} eq 'fixed' || length $s{done};
302 push @descstates, "Blocking fix for <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
306 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
307 push @descstates, "Bug is archived. No further changes may be made";
310 $indexentry .= join(";\n<br>", @descstates) . ".\n" if @descstates;
311 $indexentry .= "</h3>\n";
313 my $descriptivehead = $indexentry;
316 if ($buglog =~ m/\.gz$/) {
317 my $oldpath = $ENV{'PATH'};
318 $ENV{'PATH'} = '/bin:/usr/bin';
319 $buglogfh = new IO::File "zcat $buglog |" or &quitcgi("open log for $ref: $!");
320 $ENV{'PATH'} = $oldpath;
322 $buglogfh = new IO::File "<$buglog" or &quitcgi("open log for $ref: $!");
328 @records = read_log_records($buglogfh);
331 quitcgi("Bad bug log for $debbugs::gBug $ref. Unable to read records: $@");
335 =head2 handle_email_message
337 handle_email_message($record->{text},
339 msg_number => $msg_number,
342 Returns a decoded e-mail message and displays entities/attachments as
348 sub handle_email_message{
349 my ($email,%options) = @_;
352 my $parser = new MIME::Parser;
353 # Because we are using memory, not tempfiles, there's no need to
354 # clean up here like in Debbugs::MIME
355 $parser->tmp_to_core(1);
356 $parser->output_to_core(1);
357 my $entity = $parser->parse_data( $email);
358 my @attachments = ();
359 display_entity($entity, $options{ref}, 1, $options{msg_number}, $output, @attachments);
367 bug_links($starting_bug,$stoping_bugs,);
369 Creates a set of links to bugs, starting with bug number
370 $starting_bug, and finishing with $stoping_bug; if only one bug is
371 passed, makes a link to only a single bug.
373 The content of the link is the bug number.
378 my ($start,$stop,$query_arguments) = @_;
379 $stop = $stop || $start;
380 $query_arguments ||= '';
382 for my $bug ($start..$stop) {
383 push @output,'<a href="'.bugurl($bug,'').qq(">$bug</a>);
385 return join(', ',@output);
390 push @log, handle_record($record,$ref,$msg_num);
392 Deals with a record in a bug log as returned by
393 L<Debbugs::Log::read_log_records>; returns the log information that
394 should be output to the browser.
399 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
402 local $_ = $record->{type};
404 $output .= decode_rfc1522($record->{text});
405 # Link to forwarded http:// urls in the midst of the report
406 # (even though these links already exist at the top)
407 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
408 # Add links to the cloned bugs
409 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links($2).$3.bug_links($4,$5)}eo;
410 # Add links to merged bugs
411 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links($_)} (split /\s+/, $1))}eo;
412 # Add links to reassigned packages
413 $output =~ s{(Bug reassigned from package \`)([^\']+)(' to \`)([^\']+)(')}
414 {$1.q(<a href=").pkgurl($2).qq(">$2</a>).$3.q(<a href=").pkgurl($4).qq(">$4</a>).$5}eo;
415 $output .= '<a href="' . bugurl($ref, 'msg='.($msg_number+1)) . '">Full text</a> and <a href="' .
416 bugurl($ref, 'msg='.($msg_number+1), 'mbox') . '">rfc822 format</a> available.';
418 $output = qq(<div class="msgreceived">\n<a name="$msg_number">\n) . $output . "</div>\n";
421 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
422 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
425 elsif (defined $msg_id) {
426 $$seen_msg_ids{$msg_id} = 1;
428 $output .= qq(<a name="$msg_number">\n);
429 $output .= 'View this message in <a href="' . bugurl($ref, "msg=$msg_number", "mbox") . '">rfc822 format</a>';
430 $output .= handle_email_message($record->{text},
432 msg_number => $msg_number,
435 elsif (/autocheck/) {
438 elsif (/incoming-recv/) {
439 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
440 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
443 elsif (defined $msg_id) {
444 $$seen_msg_ids{$msg_id} = 1;
446 # Incomming Mail Message
447 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
448 $output .= qq|<p class="msgreceived"><a name="$msg_number"><a name="msg$msg_number">Message received</a> at |.
449 htmlsanit("$received\@$hostname") . q| (<a href="| . bugurl($ref, "msg=$msg_number") . '">full text</a>'.q|, <a href="| . bugurl($ref, "msg=$msg_number") . ';mbox=yes">mbox</a>)'.":</p>\n";
450 $output .= handle_email_message($record->{text},
452 msg_number => $msg_number,
456 die "Unknown record type $_";
464 if (looks_like_number($msg) and ($msg-1) <= $#records) {
465 @records = ($records[$msg-1]);
470 my $date = strftime "%a %b %d %T %Y", localtime;
472 print qq(Content-Disposition: attachment; filename="bug_${ref}.mbox"\n);
473 print "Content-Type: text/plain\n\n";
477 print qq(Content-Disposition: attachment; filename="bug_${ref}_message_${msg_num}.mbox"\n);
478 print "Content-Type: message/rfc822\n\n";
480 if ($mbox_status_message and @records > 1) {
481 my $status_message='';
482 my @status_fields = (retitle => 'subject',
483 package => 'package',
484 submitter => 'originator',
485 severity => 'severity',
489 forward => 'forward',
492 while (($key,$value) = splice(@status_fields,0,2)) {
493 if (defined $status{$value} and length $status{$value}) {
494 $status_message .= qq($key $ref $status{$value}\n);
497 print STDOUT qq(From unknown $date\n),
498 create_mime_message([From => "$debbugs::gBug#$ref <$ref\@$debbugs::gEmailDomain>",
499 To => "$debbugs::gBug#$ref <$ref\@$debbugs::gEmailDomain>",
500 Subject => "Status: $status{subject}",
501 "Reply-To" => "$debbugs::gBug#$ref <$ref\@$debbugs::gEmailDomain>",
510 my $message_number=0;
511 my %seen_message_ids;
512 for my $record (@records) {
513 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
514 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
515 # we want to include control messages anyway
516 my $record_wanted_anyway = 0;
517 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
518 next if exists $seen_message_ids{$msg_id};
519 $seen_message_ids{$msg_id} = 1;
520 next if $msg_id =~/handler\..+\.ack(?:info)?\@/;
521 $record_wanted_anyway = 1 if $record->{text} =~ /^Received: \(at control\)/;
522 next if not $boring and $record->{type} ne $wanted_type and not $record_wanted_anyway and @records > 1;
523 my @lines = split( "\n", $record->{text}, -1 );
524 if ( $lines[ 1 ] =~ m/^From / ) {
525 my $tmp = $lines[ 0 ];
526 $lines[ 0 ] = $lines[ 1 ];
529 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
530 unshift @lines, "From unknown $date";
532 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
533 print join( "\n", @lines ) . "\n";
540 for my $record (@records) {
546 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
547 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
551 @log = reverse @log if $reverse;
552 $log = join('<hr>',@log);
555 print "Content-Type: text/html; charset=utf-8\n\n";
557 my $title = htmlsanit($status{subject});
559 my $dummy2 = $debbugs::gWebHostBugDir;
561 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
562 print "<HTML><HEAD>\n" .
563 "<TITLE>$short - $title - $debbugs::gProject $debbugs::gBug report logs</TITLE>\n" .
564 '<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'.
565 "<link rel=\"stylesheet\" href=\"$debbugs::gWebHostBugDir/css/bugs.css\" type=\"text/css\">" .
569 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
570 "<BR>" . $title . "</H1>\n";
572 print "$descriptivehead\n";
573 printf qq(<div class="msgreceived"><p>View this report as an <a href="%s">mbox folder</a>,).
574 qq(<a href="%s">status mbox</a>, <a href="%s">maintainer mbox</a></p></div>\n),
575 html_escape(bug_url($ref, mbox=>'yes')),
576 html_escape(bug_url($ref, mbox=>'yes',mboxstatus=>'yes')),
577 html_escape(bug_url($ref, mbox=>'yes',mboxmaint=>'yes'));
581 print "<p class=\"msgreceived\">Send a report that <a href=\"/cgi-bin/bugspam.cgi?bug=$ref\">this bug log contains spam</a>.</p>\n<HR>\n";
584 print "</BODY></HTML>\n";