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 version_url);
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()};
183 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
184 $tail_html = $debbugs::gHTMLTail;
185 $tail_html =~ s/SUBSTITUTE_DTIME/$dtime/;
187 my %status = %{getbugstatus($ref)};
190 Content-Type: text/html; charset=utf-8
192 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
194 <head><title>$short - $debbugs::gProject $debbugs::gBug report logs</title></head>
196 <h1>$debbugs::gProject $debbugs::gBug report logs - $short</h1>
197 <p>There is no record of $debbugs::gBug $short.
198 Try the <a href="http://$gWebDomain/">search page</a> instead.</p>
199 $tail_html</body></html>
206 $tpack = lc $status{'package'};
207 my @tpacks = splitpackages($tpack);
209 if ($status{severity} eq 'normal') {
211 } elsif (isstrongseverity($status{severity})) {
212 $showseverity = "Severity: <em class=\"severity\">$status{severity}</em>;\n";
214 $showseverity = "Severity: $status{severity};\n";
217 $indexentry .= "<div class=\"msgreceived\">\n";
218 $indexentry .= htmlpackagelinks($status{package}, 0) . ";\n";
220 foreach my $pkg (@tpacks) {
221 my $tmaint = defined($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)';
222 my $tsrc = defined($pkgsrc{$pkg}) ? $pkgsrc{$pkg} : '(unknown)';
225 htmlmaintlinks(sub { $_[0] == 1 ? "Maintainer for $pkg is\n"
226 : "Maintainers for $pkg are\n" },
228 $indexentry .= ";\nSource for $pkg is\n".
229 '<a href="'.srcurl($tsrc)."\">$tsrc</a>" if ($tsrc ne "(unknown)");
230 $indexentry .= ".\n";
233 $indexentry .= "<br>";
234 $indexentry .= htmladdresslinks("Reported by: ", \&submitterurl,
235 $status{originator}) . ";\n";
236 $indexentry .= sprintf "Date: %s.\n",
237 (strftime "%a, %e %b %Y %T UTC", localtime($status{date}));
239 $indexentry .= "<br>Owned by: " . htmlsanit($status{owner}) . ".\n"
240 if length $status{owner};
242 $indexentry .= "</div>\n";
246 $indexentry .= "<h3>$showseverity";
247 $indexentry .= sprintf "Tags: %s;\n",
248 htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
249 if length($status{tags});
250 $indexentry .= "<br>" if (length($showseverity) or length($status{tags}));
252 my @merged= split(/ /,$status{mergedwith});
254 my $descmerged = 'Merged with ';
256 for my $m (@merged) {
257 $descmerged .= $mseparator."<a href=\"" . bugurl($m) . "\">#$m</a>";
260 push @descstates, $descmerged;
263 if (@{$status{found_versions}}) {
264 my $foundtext = 'Found in ';
265 $foundtext .= (@{$status{found_versions}} == 1) ? 'version ' : 'versions ';
266 $foundtext .= join ', ', map htmlsanit($_), @{$status{found_versions}};
267 push @descstates, $foundtext;
270 if (@{$status{fixed_versions}}) {
271 my $fixedtext = '<strong>Fixed</strong> in ';
272 $fixedtext .= (@{$status{fixed_versions}} == 1) ? 'version ' : 'versions ';
273 $fixedtext .= join ', ', map htmlsanit($_), @{$status{fixed_versions}};
274 if (length($status{done})) {
275 $fixedtext .= ' by ' . htmlsanit(decode_rfc1522($status{done}));
277 push @descstates, $fixedtext;
278 push @descstates, '<a href="'.
279 version_url($status{package},
280 $status{found_versions},
281 $status{fixed_versions},
282 ).qq{">Version Graph</a>};
284 } elsif (length($status{done})) {
285 push @descstates, "<strong>Done:</strong> ".htmlsanit(decode_rfc1522($status{done}));
286 } elsif (length($status{forwarded})) {
287 push @descstates, "<strong>Forwarded</strong> to ".maybelink($status{forwarded});
291 my @blockedby= split(/ /, $status{blockedby});
292 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
293 for my $b (@blockedby) {
294 my %s = %{getbugstatus($b)};
295 next if $s{"pending"} eq 'fixed' || length $s{done};
296 push @descstates, "Fix blocked by <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
300 my @blocks= split(/ /, $status{blocks});
301 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
302 for my $b (@blocks) {
303 my %s = %{getbugstatus($b)};
304 next if $s{"pending"} eq 'fixed' || length $s{done};
305 push @descstates, "Blocking fix for <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
309 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
310 push @descstates, "Bug is archived. No further changes may be made";
313 $indexentry .= join(";\n<br>", @descstates) . ".\n" if @descstates;
314 $indexentry .= "</h3>\n";
316 my $descriptivehead = $indexentry;
319 if ($buglog =~ m/\.gz$/) {
320 my $oldpath = $ENV{'PATH'};
321 $ENV{'PATH'} = '/bin:/usr/bin';
322 $buglogfh = new IO::File "zcat $buglog |" or &quitcgi("open log for $ref: $!");
323 $ENV{'PATH'} = $oldpath;
325 $buglogfh = new IO::File "<$buglog" or &quitcgi("open log for $ref: $!");
331 @records = read_log_records($buglogfh);
334 quitcgi("Bad bug log for $debbugs::gBug $ref. Unable to read records: $@");
338 =head2 handle_email_message
340 handle_email_message($record->{text},
342 msg_number => $msg_number,
345 Returns a decoded e-mail message and displays entities/attachments as
351 sub handle_email_message{
352 my ($email,%options) = @_;
355 my $parser = new MIME::Parser;
356 # Because we are using memory, not tempfiles, there's no need to
357 # clean up here like in Debbugs::MIME
358 $parser->tmp_to_core(1);
359 $parser->output_to_core(1);
360 my $entity = $parser->parse_data( $email);
361 my @attachments = ();
362 display_entity($entity, $options{ref}, 1, $options{msg_number}, $output, @attachments);
369 push @log, handle_record($record,$ref,$msg_num);
371 Deals with a record in a bug log as returned by
372 L<Debbugs::Log::read_log_records>; returns the log information that
373 should be output to the browser.
378 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
381 local $_ = $record->{type};
383 my $class = $record->{text} =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/ ? 'infmessage':'msgreceived';
384 $output .= decode_rfc1522($record->{text});
385 # Link to forwarded http:// urls in the midst of the report
386 # (even though these links already exist at the top)
387 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
388 # Add links to the cloned bugs
389 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links($2).$3.bug_links($4,$5)}eo;
390 # Add links to merged bugs
391 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links($_)} (split /\s+/, $1))}eo;
392 # Add links to blocked bugs
393 $output =~ s{(?<=Blocking bugs)(?:(of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
394 {(defined $2?$1.bug_links($2):'').$3.
395 join(' ',map {bug_links($_)} (split /\,?\s+/, $4))}eo;
396 # Add links to reassigned packages
397 $output =~ s{(Bug reassigned from package \`)([^\']+)(' to \`)([^\']+)(')}
398 {$1.q(<a href=").pkgurl($2).qq(">$2</a>).$3.q(<a href=").pkgurl($4).qq(">$4</a>).$5}eo;
399 $output .= '<a href="' . bugurl($ref, 'msg='.($msg_number+1)) . '">Full text</a> and <a href="' .
400 bugurl($ref, 'msg='.($msg_number+1), 'mbox') . '">rfc822 format</a> available.';
402 $output = qq(<div class="$class"><hr>\n<a name="$msg_number">\n) . $output . "</div>\n";
405 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
406 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
409 elsif (defined $msg_id) {
410 $$seen_msg_ids{$msg_id} = 1;
412 $output .= qq(<hr><a name="$msg_number">\n);
413 $output .= 'View this message in <a href="' . bugurl($ref, "msg=$msg_number", "mbox") . '">rfc822 format</a>';
414 $output .= handle_email_message($record->{text},
416 msg_number => $msg_number,
419 elsif (/autocheck/) {
422 elsif (/incoming-recv/) {
423 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
424 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
427 elsif (defined $msg_id) {
428 $$seen_msg_ids{$msg_id} = 1;
430 # Incomming Mail Message
431 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
432 $output .= qq|<hr><p class="msgreceived"><a name="$msg_number"><a name="msg$msg_number">Message received</a> at |.
433 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";
434 $output .= handle_email_message($record->{text},
436 msg_number => $msg_number,
440 die "Unknown record type $_";
448 if (looks_like_number($msg) and ($msg-1) <= $#records) {
449 @records = ($records[$msg-1]);
454 my $date = strftime "%a %b %d %T %Y", localtime;
456 print qq(Content-Disposition: attachment; filename="bug_${ref}.mbox"\n);
457 print "Content-Type: text/plain\n\n";
461 print qq(Content-Disposition: attachment; filename="bug_${ref}_message_${msg_num}.mbox"\n);
462 print "Content-Type: message/rfc822\n\n";
464 if ($mbox_status_message and @records > 1) {
465 my $status_message='';
466 my @status_fields = (retitle => 'subject',
467 package => 'package',
468 submitter => 'originator',
469 severity => 'severity',
473 forward => 'forward',
476 while (($key,$value) = splice(@status_fields,0,2)) {
477 if (defined $status{$value} and length $status{$value}) {
478 $status_message .= qq($key $ref $status{$value}\n);
481 print STDOUT qq(From unknown $date\n),
482 create_mime_message([From => "$debbugs::gBug#$ref <$ref\@$debbugs::gEmailDomain>",
483 To => "$debbugs::gBug#$ref <$ref\@$debbugs::gEmailDomain>",
484 Subject => "Status: $status{subject}",
485 "Reply-To" => "$debbugs::gBug#$ref <$ref\@$debbugs::gEmailDomain>",
494 my $message_number=0;
495 my %seen_message_ids;
496 for my $record (@records) {
497 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
498 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
499 # we want to include control messages anyway
500 my $record_wanted_anyway = 0;
501 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
502 next if exists $seen_message_ids{$msg_id};
503 $seen_message_ids{$msg_id} = 1;
504 next if $msg_id =~/handler\..+\.ack(?:info)?\@/;
505 $record_wanted_anyway = 1 if $record->{text} =~ /^Received: \(at control\)/;
506 next if not $boring and $record->{type} ne $wanted_type and not $record_wanted_anyway and @records > 1;
507 my @lines = split( "\n", $record->{text}, -1 );
508 if ( $lines[ 1 ] =~ m/^From / ) {
509 my $tmp = $lines[ 0 ];
510 $lines[ 0 ] = $lines[ 1 ];
513 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
514 unshift @lines, "From unknown $date";
516 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
517 print join( "\n", @lines ) . "\n";
524 for my $record (@records) {
530 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
531 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
535 @log = reverse @log if $reverse;
536 $log = join("\n",@log);
539 print "Content-Type: text/html; charset=utf-8\n\n";
541 my $title = htmlsanit($status{subject});
543 my $dummy2 = $debbugs::gWebHostBugDir;
545 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
548 <TITLE>$short - $title - $debbugs::gProject $debbugs::gBug report logs</TITLE>
549 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
550 <link rel="stylesheet" href="$debbugs::gWebHostBugDir/css/bugs.css" type="text/css">
551 <script type="text/javascript">
553 function toggle_infmessages(){
554 var styles = document.styleSheets;
556 for (var i = 0; i < styles.length; i++) {
557 for (var j = 0; j < styles[i].cssRules.length; j++) {
558 if (styles[i].cssRules[j].cssText == ".infmessage { display: none; }") {
559 styles[i].deleteRule(j);
565 styles[0].insertRule(".infmessage { display: none; }",0);
573 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
574 "<BR>" . $title . "</H1>\n";
576 print "$descriptivehead\n";
577 print qq(<p><a href="mailto:$ref\@$debbugs::gEmailDomain">Reply</a> ),
578 qq(or <a href="mailto:$ref-subscribe\@$debbugs::gEmailDomain">subscribe</a> ),
579 qq(to this bug.</p>\n);
580 print qq(<p><a href="javascript:toggle_infmessages();">Show useless messages</a></p>);
581 printf qq(<div class="msgreceived"><p>View this report as an <a href="%s">mbox folder</a>, ).
582 qq(<a href="%s">status mbox</a>, <a href="%s">maintainer mbox</a></p></div>\n),
583 html_escape(bug_url($ref, mbox=>'yes')),
584 html_escape(bug_url($ref, mbox=>'yes',mboxstatus=>'yes')),
585 html_escape(bug_url($ref, mbox=>'yes',mboxmaint=>'yes'));
588 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";
591 print "</BODY></HTML>\n";