6 use POSIX qw(strftime tzset);
13 #require '/usr/lib/debbugs/errorlib';
14 require './common.pl';
16 require '/etc/debbugs/config';
17 require '/etc/debbugs/text';
19 use vars(qw($gEmailDomain $gHTMLTail $gSpoolDir $gWebDomain));
21 # for read_log_records
23 use Debbugs::MIME qw(convert_to_utf8 decode_rfc1522);
25 use Scalar::Util qw(looks_like_number);
27 my %param = readparse();
31 my $ref = $param{'bug'} || quitcgi("No bug number");
32 $ref =~ /(\d+)/ or quitcgi("Invalid bug number");
35 my $msg = $param{'msg'} || "";
36 my $att = $param{'att'};
37 my $boring = ($param{'boring'} || 'no') eq 'yes';
38 my $terse = ($param{'terse'} || 'no') eq 'yes';
39 my $reverse = ($param{'reverse'} || 'no') eq 'yes';
40 my $mbox = ($param{'mbox'} || 'no') eq 'yes';
41 my $mime = ($param{'mime'} || 'yes') eq 'yes';
43 my $trim_headers = ($param{trim} || ($msg?'no':'yes')) eq 'yes';
45 # Not used by this script directly, but fetch these so that pkgurl() and
46 # friends can propagate them correctly.
47 my $archive = ($param{'archive'} || 'no') eq 'yes';
48 my $repeatmerged = ($param{'repeatmerged'} || 'yes') eq 'yes';
49 set_option('archive', $archive);
50 set_option('repeatmerged', $repeatmerged);
52 my $buglog = buglog($ref);
54 if ($ENV{REQUEST_METHOD} eq 'HEAD' and not defined($att) and not $mbox) {
55 print "Content-Type: text/html; charset=utf-8\n";
56 my @stat = stat $buglog;
58 my $mtime = strftime '%a, %d %b %Y %T GMT', gmtime($stat[9]);
59 print "Last-Modified: $mtime\n";
65 sub display_entity ($$$$\$\@);
66 sub display_entity ($$$$\$\@) {
72 my $attachments = shift;
74 my $head = $entity->head;
75 my $disposition = $head->mime_attr('content-disposition');
76 $disposition = 'inline' if not defined $disposition or $disposition eq '';
77 my $type = $entity->effective_type;
78 my $filename = $entity->head->recommended_filename;
79 $filename = '' unless defined $filename;
80 $filename = decode_rfc1522($filename);
83 my $header = $entity->head;
84 if ($trim_headers and not $terse) {
86 foreach (qw(From To Cc Subject Date)) {
87 my $head_field = $head->get($_);
88 next unless defined $head_field and $head_field ne '';
89 push @headers, qq($_: ) . htmlsanit(decode_rfc1522($head_field));
91 $$this .= join(qq(), @headers) unless $terse;
95 $$this .= htmlsanit(decode_rfc1522($entity->head->stringify));
100 unless (($top and $type =~ m[^text(?:/plain)?(?:;|$)]) or
101 ($type =~ m[^multipart/])) {
102 push @$attachments, $entity;
103 my @dlargs = ($ref, "msg=$xmessage", "att=$#$attachments");
104 push @dlargs, "filename=$filename" if $filename ne '';
105 my $printname = $filename;
106 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
107 $$this .= '[<a href="' . dlurl(@dlargs) . qq{">$printname</a> } .
108 "($type, $disposition)]\n\n";
110 if ($msg and defined($att) and $att eq $#$attachments) {
111 my $head = $entity->head;
112 chomp(my $type = $entity->effective_type);
113 my $body = $entity->stringify_body;
114 print "Content-Type: $type\n";
115 if ($filename ne '') {
119 print qq{Content-Disposition: attachment; filename="$qf"\n};
122 my $decoder = new MIME::Decoder($head->mime_encoding);
123 $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
128 return if not $top and $disposition eq 'attachment' and not defined($att);
129 return unless ($type =~ m[^text/?] and
130 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
131 $type =~ m[^application/pgp(?:;|$)] or
134 if ($entity->is_multipart) {
135 my @parts = $entity->parts;
136 foreach my $part (@parts) {
137 display_entity($part, $ref, 0, $xmessage,
138 $$this, @$attachments);
141 } elsif ($entity->parts) {
142 # We must be dealing with a nested message.
143 $$this .= "<blockquote>\n";
144 my @parts = $entity->parts;
145 foreach my $part (@parts) {
146 display_entity($part, $ref, 1, $xmessage,
147 $$this, @$attachments);
150 $$this .= "</blockquote>\n";
153 my $content_type = $entity->head->get('Content-Type:');
154 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
155 my $body = $entity->bodyhandle->as_string;
156 $body = convert_to_utf8($body,$charset) if defined $charset;
157 $body = htmlsanit($body);
158 $body =~ s,((ftp|http|https)://[\S~-]+?/?)((\>\;)?[)]?[']?[:.\,]?(\s|$)),<a href=\"$1\">$1</a>$3,go;
164 my %maintainer = %{getmaintainers()};
165 my %pkgsrc = %{getpkgsrc()};
177 my $dtime = strftime "%a, %e %b %Y %T UTC", localtime;
178 $tail_html = $debbugs::gHTMLTail;
179 $tail_html =~ s/SUBSTITUTE_DTIME/$dtime/;
181 my %status = %{getbugstatus($ref)};
184 Content-Type: text/html; charset=utf-8
186 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
188 <head><title>$debbugs::gProject $debbugs::gBug report logs - $short</title></head>
190 <h1>$debbugs::gProject $debbugs::gBug report logs - $short</h1>
191 <p>There is no record of $debbugs::gBug $short.
192 Try the <a href="http://$gWebDomain/">search page</a> instead.</p>
193 $tail_html</body></html>
200 $tpack = lc $status{'package'};
201 my @tpacks = splitpackages($tpack);
203 if ($status{severity} eq 'normal') {
205 #} elsif (isstrongseverity($status{severity})) {
206 # $showseverity = "<strong>Severity: $status{severity}</strong>;\n";
208 $showseverity = "Severity: <em>$status{severity}</em>;\n";
211 $indexentry .= "<p>$showseverity";
212 $indexentry .= htmlpackagelinks($status{package}, 0) . ";\n";
214 $indexentry .= htmladdresslinks("Reported by: ", \&submitterurl,
215 $status{originator}) . ";\n";
217 $indexentry .= "Owned by: " . htmlsanit($status{owner}) . ";\n"
218 if length $status{owner};
220 my $dummy = strftime "%a, %e %b %Y %T UTC", localtime($status{date});
221 $indexentry .= "Date: ".$dummy.";\n<br>";
225 push @descstates, "Tags: <strong>"
226 . htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
228 if length($status{tags});
230 my @merged= split(/ /,$status{mergedwith});
232 my $descmerged = 'merged with ';
234 for my $m (@merged) {
235 $descmerged .= $mseparator."<a href=\"" . bugurl($m) . "\">#$m</a>";
238 push @descstates, $descmerged;
241 if (@{$status{found_versions}}) {
242 my $foundtext = 'Found in ';
243 $foundtext .= (@{$status{found_versions}} == 1) ? 'version ' : 'versions ';
244 $foundtext .= join ', ', map htmlsanit($_), @{$status{found_versions}};
245 push @descstates, $foundtext;
248 if (@{$status{fixed_versions}}) {
249 my $fixedtext = '<strong>Fixed</strong> in ';
250 $fixedtext .= (@{$status{fixed_versions}} == 1) ? 'version ' : 'versions ';
251 $fixedtext .= join ', ', map htmlsanit($_), @{$status{fixed_versions}};
252 if (length($status{done})) {
253 $fixedtext .= ' by ' . htmlsanit(decode_rfc1522($status{done}));
255 push @descstates, $fixedtext;
256 } elsif (length($status{done})) {
257 push @descstates, "<strong>Done:</strong> ".htmlsanit(decode_rfc1522($status{done}));
258 } elsif (length($status{forwarded})) {
259 push @descstates, "<strong>Forwarded</strong> to ".maybelink($status{forwarded});
262 $indexentry .= join(";\n", @descstates) . ";\n<br>" if @descstates;
264 $descriptivehead = $indexentry;
265 foreach my $pkg (@tpacks) {
266 my $tmaint = defined($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)';
267 my $tsrc = defined($pkgsrc{$pkg}) ? $pkgsrc{$pkg} : '(unknown)';
270 htmlmaintlinks(sub { $_[0] == 1 ? "Maintainer for $pkg is\n"
271 : "Maintainers for $pkg are\n" },
273 $descriptivehead .= ";\nSource for $pkg is\n".
274 '<a href="'.srcurl($tsrc)."\">$tsrc</a>" if ($tsrc ne "(unknown)");
275 $descriptivehead .= ".\n<br>";
279 if ($buglog =~ m/\.gz$/) {
280 my $oldpath = $ENV{'PATH'};
281 $ENV{'PATH'} = '/bin:/usr/bin';
282 $buglogfh = new IO::File "zcat $buglog |" or &quitcgi("open log for $ref: $!");
283 $ENV{'PATH'} = $oldpath;
285 $buglogfh = new IO::File "<$buglog" or &quitcgi("open log for $ref: $!");
287 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
288 $descriptivehead .= "\n<p>Bug is <strong>archived</strong>. No further changes may be made.</p>";
292 my @records = read_log_records($buglogfh);
295 =head2 handle_email_message
297 handle_email_message($record->{text},
299 msg_number => $msg_number,
302 Returns a decoded e-mail message and displays entities/attachments as
308 sub handle_email_message{
309 my ($email,%options) = @_;
312 my $parser = new MIME::Parser;
313 $parser->tmp_to_core(1);
314 $parser->output_to_core(1);
315 # $parser->output_under("/tmp");
316 my $entity = $parser->parse_data( $email);
317 # TODO: make local subdir, clean it ourselves
318 # the following does NOT delete the msg dirs in /tmp
319 END { if ( $entity ) { $entity->purge; } if ( $parser ) { $parser->filer->purge; } }
320 my @attachments = ();
321 display_entity($entity, $options{ref}, 1, $options{msg_number}, $output, @attachments);
329 bug_links($starting_bug,$stoping_bugs,);
331 Creates a set of links to bugs, starting with bug number
332 $starting_bug, and finishing with $stoping_bug; if only one bug is
333 passed, makes a link to only a single bug.
335 The content of the link is the bug number.
340 my ($start,$stop,$query_arguments) = @_;
341 $stop = $stop || $start;
342 $query_arguments ||= '';
344 for my $bug ($start..$stop) {
345 push @output,'<a href="'.bugurl($bug,'').qq(">$bug</a>);
347 return join(', ',@output);
352 push @log, handle_record($record,$ref,$msg_num);
354 Deals with a record in a bug log as returned by
355 L<Debbugs::Log::read_log_records>; returns the log information that
356 should be output to the browser.
361 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
364 local $_ = $record->{type};
366 $output .= decode_rfc1522($record->{text});
367 # Link to forwarded http:// urls in the midst of the report
368 # (even though these links already exist at the top)
369 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
370 # Add links to the cloned bugs
371 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links($2).$3.bug_links($4,$5)}eo;
372 $output .= '<a href="' . bugurl($ref, 'msg='.($msg_number+1)) . '">Full text</a> and <a href="' .
373 bugurl($ref, 'msg='.($msg_number+1)) . '&mbox=yes">rfc822 format</a> available.</em>';
376 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
377 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
380 elsif (defined $msg_id) {
381 $$seen_msg_ids{$msg_id} = 1;
383 $output .= 'View this message in <a href="' . bugurl($ref, "msg=$msg_number") . '&mbox=yes">rfc822 format</a></em>';
384 $output .= '<pre class="message">' .
385 handle_email_message($record->{text},
387 msg_number => $msg_number,
390 elsif (/autocheck/) {
393 elsif (/incoming-recv/) {
394 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
395 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
398 elsif (defined $msg_id) {
399 $$seen_msg_ids{$msg_id} = 1;
401 # Incomming Mail Message
402 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
403 $output .= qq|<h2><a name="msg$msg_number">Message received at |.
404 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>)'.":</a></h2>\n";
405 $output .= '<pre class="message">' .
406 handle_email_message($record->{text},
408 msg_number => $msg_number,
412 die "Unknown record type $_";
420 if (looks_like_number($msg) and ($msg-1) <= $#records) {
421 @records = ($records[$msg-1]);
427 print qq(Content-Disposition: attachment; filename="bug_${ref}.mbox"\n);
428 print "Content-Type: text/plain\n\n";
431 print qq(Content-Disposition: attachment; filename="bug_${ref}_message_${msg_num}.mbox"\n);
432 print "Content-Type: message/rfc822\n\n";
434 for my $record (@records) {
435 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
436 next if not $boring and $record->{type} eq 'recips';
437 my @lines = split( "\n", $record->{text}, -1 );
438 if ( $lines[ 1 ] =~ m/^From / ) {
439 my $tmp = $lines[ 0 ];
440 $lines[ 0 ] = $lines[ 1 ];
443 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
444 my $date = strftime "%a %b %d %T %Y", localtime;
445 unshift @lines, "From unknown $date";
447 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
448 print join( "\n", @lines ) . "\n";
455 for my $record (@records) {
461 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
462 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
466 @log = reverse @log if $reverse;
467 $log = join('<hr>',@log);
470 print "Content-Type: text/html; charset=utf-8\n\n";
472 my $title = htmlsanit($status{subject});
474 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
475 print "<HTML><HEAD>\n" .
476 "<TITLE>$debbugs::gProject $debbugs::gBug report logs - $short - $title</TITLE>\n" .
477 # "<link rel=\"stylesheet\" href=\"$debbugs::gWebHostBugDir/bugs.css\" type=\"text/css\">" .
481 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
482 "<BR>" . $title . "</H1>\n";
484 print "$descriptivehead\n";
485 printf "<p>View this report as an <a href=\"%s\">mbox folder</a>.</p>\n", mboxurl($ref);
490 print "</BODY></HTML>\n";