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()};
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 } elsif (length($status{done})) {
279 push @descstates, "<strong>Done:</strong> ".htmlsanit(decode_rfc1522($status{done}));
280 } elsif (length($status{forwarded})) {
281 push @descstates, "<strong>Forwarded</strong> to ".maybelink($status{forwarded});
285 my @blockedby= split(/ /, $status{blockedby});
286 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
287 for my $b (@blockedby) {
288 my %s = %{getbugstatus($b)};
289 next if $s{"pending"} eq 'fixed' || length $s{done};
290 push @descstates, "Fix blocked by <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
294 my @blocks= split(/ /, $status{blocks});
295 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
296 for my $b (@blocks) {
297 my %s = %{getbugstatus($b)};
298 next if $s{"pending"} eq 'fixed' || length $s{done};
299 push @descstates, "Blocking fix for <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
303 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
304 push @descstates, "Bug is archived. No further changes may be made";
307 $indexentry .= join(";\n<br>", @descstates) . ".\n" if @descstates;
308 $indexentry .= "</h3>\n";
310 my $descriptivehead = $indexentry;
313 if ($buglog =~ m/\.gz$/) {
314 my $oldpath = $ENV{'PATH'};
315 $ENV{'PATH'} = '/bin:/usr/bin';
316 $buglogfh = new IO::File "zcat $buglog |" or &quitcgi("open log for $ref: $!");
317 $ENV{'PATH'} = $oldpath;
319 $buglogfh = new IO::File "<$buglog" or &quitcgi("open log for $ref: $!");
325 @records = read_log_records($buglogfh);
328 quitcgi("Bad bug log for $debbugs::gBug $ref. Unable to read records: $@");
332 =head2 handle_email_message
334 handle_email_message($record->{text},
336 msg_number => $msg_number,
339 Returns a decoded e-mail message and displays entities/attachments as
345 sub handle_email_message{
346 my ($email,%options) = @_;
349 my $parser = new MIME::Parser;
350 # Because we are using memory, not tempfiles, there's no need to
351 # clean up here like in Debbugs::MIME
352 $parser->tmp_to_core(1);
353 $parser->output_to_core(1);
354 my $entity = $parser->parse_data( $email);
355 my @attachments = ();
356 display_entity($entity, $options{ref}, 1, $options{msg_number}, $output, @attachments);
363 push @log, handle_record($record,$ref,$msg_num);
365 Deals with a record in a bug log as returned by
366 L<Debbugs::Log::read_log_records>; returns the log information that
367 should be output to the browser.
372 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
375 local $_ = $record->{type};
377 my $class = $record->{text} =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/ ? 'infmessage':'msgreceived';
378 $output .= decode_rfc1522($record->{text});
379 # Link to forwarded http:// urls in the midst of the report
380 # (even though these links already exist at the top)
381 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
382 # Add links to the cloned bugs
383 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links($2).$3.bug_links($4,$5)}eo;
384 # Add links to merged bugs
385 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links($_)} (split /\s+/, $1))}eo;
386 # Add links to blocked bugs
387 $output =~ s{(?<=Blocking bugs)(?:(of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
388 {(defined $2?$1.bug_links($2):'').$3.
389 join(' ',map {bug_links($_)} (split /\,?\s+/, $4))}eo;
390 # Add links to reassigned packages
391 $output =~ s{(Bug reassigned from package \`)([^\']+)(' to \`)([^\']+)(')}
392 {$1.q(<a href=").pkgurl($2).qq(">$2</a>).$3.q(<a href=").pkgurl($4).qq(">$4</a>).$5}eo;
393 $output .= '<a href="' . bugurl($ref, 'msg='.($msg_number+1)) . '">Full text</a> and <a href="' .
394 bugurl($ref, 'msg='.($msg_number+1), 'mbox') . '">rfc822 format</a> available.';
396 $output = qq(<div class="$class"><hr>\n<a name="$msg_number">\n) . $output . "</div>\n";
399 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
400 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
403 elsif (defined $msg_id) {
404 $$seen_msg_ids{$msg_id} = 1;
406 $output .= qq(<hr><a name="$msg_number">\n);
407 $output .= 'View this message in <a href="' . bugurl($ref, "msg=$msg_number", "mbox") . '">rfc822 format</a>';
408 $output .= handle_email_message($record->{text},
410 msg_number => $msg_number,
413 elsif (/autocheck/) {
416 elsif (/incoming-recv/) {
417 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
418 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
421 elsif (defined $msg_id) {
422 $$seen_msg_ids{$msg_id} = 1;
424 # Incomming Mail Message
425 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
426 $output .= qq|<hr><p class="msgreceived"><a name="$msg_number"><a name="msg$msg_number">Message received</a> at |.
427 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";
428 $output .= handle_email_message($record->{text},
430 msg_number => $msg_number,
434 die "Unknown record type $_";
442 if (looks_like_number($msg) and ($msg-1) <= $#records) {
443 @records = ($records[$msg-1]);
448 my $date = strftime "%a %b %d %T %Y", localtime;
450 print qq(Content-Disposition: attachment; filename="bug_${ref}.mbox"\n);
451 print "Content-Type: text/plain\n\n";
455 print qq(Content-Disposition: attachment; filename="bug_${ref}_message_${msg_num}.mbox"\n);
456 print "Content-Type: message/rfc822\n\n";
458 if ($mbox_status_message and @records > 1) {
459 my $status_message='';
460 my @status_fields = (retitle => 'subject',
461 package => 'package',
462 submitter => 'originator',
463 severity => 'severity',
467 forward => 'forward',
470 while (($key,$value) = splice(@status_fields,0,2)) {
471 if (defined $status{$value} and length $status{$value}) {
472 $status_message .= qq($key $ref $status{$value}\n);
475 print STDOUT qq(From unknown $date\n),
476 create_mime_message([From => "$debbugs::gBug#$ref <$ref\@$debbugs::gEmailDomain>",
477 To => "$debbugs::gBug#$ref <$ref\@$debbugs::gEmailDomain>",
478 Subject => "Status: $status{subject}",
479 "Reply-To" => "$debbugs::gBug#$ref <$ref\@$debbugs::gEmailDomain>",
488 my $message_number=0;
489 my %seen_message_ids;
490 for my $record (@records) {
491 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
492 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
493 # we want to include control messages anyway
494 my $record_wanted_anyway = 0;
495 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
496 next if exists $seen_message_ids{$msg_id};
497 $seen_message_ids{$msg_id} = 1;
498 next if $msg_id =~/handler\..+\.ack(?:info)?\@/;
499 $record_wanted_anyway = 1 if $record->{text} =~ /^Received: \(at control\)/;
500 next if not $boring and $record->{type} ne $wanted_type and not $record_wanted_anyway and @records > 1;
501 my @lines = split( "\n", $record->{text}, -1 );
502 if ( $lines[ 1 ] =~ m/^From / ) {
503 my $tmp = $lines[ 0 ];
504 $lines[ 0 ] = $lines[ 1 ];
507 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
508 unshift @lines, "From unknown $date";
510 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
511 print join( "\n", @lines ) . "\n";
518 for my $record (@records) {
524 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
525 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
529 @log = reverse @log if $reverse;
530 $log = join("\n",@log);
533 print "Content-Type: text/html; charset=utf-8\n\n";
535 my $title = htmlsanit($status{subject});
537 my $dummy2 = $debbugs::gWebHostBugDir;
539 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
542 <TITLE>$short - $title - $debbugs::gProject $debbugs::gBug report logs</TITLE>
543 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
544 <link rel="stylesheet" href="$debbugs::gWebHostBugDir/css/bugs.css" type="text/css">
545 <script type="text/javascript">
547 function toggle_infmessages(){
548 var styles = document.styleSheets;
550 for (var i = 0; i < styles.length; i++) {
551 for (var j = 0; j < styles[i].cssRules.length; j++) {
552 if (styles[i].cssRules[j].cssText == ".infmessage { display: none; }") {
553 styles[i].deleteRule(j);
559 styles[0].insertRule(".infmessage { display: none; }",0);
567 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
568 "<BR>" . $title . "</H1>\n";
570 print "$descriptivehead\n";
571 print qq(<p><a href="mailto:$ref\@$debbugs::gEmailDomain">Reply</a> ),
572 qq(or <a href="mailto:$ref-subscribe\@$debbugs::gEmailDomain">subscribe</a> ),
573 qq(to this bug.</p>\n);
574 print qq(<p><a href="javascript:toggle_infmessages();">Show useless messages</a></p>);
575 printf qq(<div class="msgreceived"><p>View this report as an <a href="%s">mbox folder</a>, ).
576 qq(<a href="%s">status mbox</a>, <a href="%s">maintainer mbox</a></p></div>\n),
577 html_escape(bug_url($ref, mbox=>'yes')),
578 html_escape(bug_url($ref, mbox=>'yes',mboxstatus=>'yes')),
579 html_escape(bug_url($ref, mbox=>'yes',mboxmaint=>'yes'));
582 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";
585 print "</BODY></HTML>\n";