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>";
294 @records = read_log_records($buglogfh);
297 quitcgi("Bad bug log for $debbugs::gBug $ref. Unable to read records: $@");
301 =head2 handle_email_message
303 handle_email_message($record->{text},
305 msg_number => $msg_number,
308 Returns a decoded e-mail message and displays entities/attachments as
314 sub handle_email_message{
315 my ($email,%options) = @_;
318 my $parser = new MIME::Parser;
319 $parser->tmp_to_core(1);
320 $parser->output_to_core(1);
321 # $parser->output_under("/tmp");
322 my $entity = $parser->parse_data( $email);
323 # TODO: make local subdir, clean it ourselves
324 # the following does NOT delete the msg dirs in /tmp
325 END { if ( $entity ) { $entity->purge; } if ( $parser ) { $parser->filer->purge; } }
326 my @attachments = ();
327 display_entity($entity, $options{ref}, 1, $options{msg_number}, $output, @attachments);
335 bug_links($starting_bug,$stoping_bugs,);
337 Creates a set of links to bugs, starting with bug number
338 $starting_bug, and finishing with $stoping_bug; if only one bug is
339 passed, makes a link to only a single bug.
341 The content of the link is the bug number.
346 my ($start,$stop,$query_arguments) = @_;
347 $stop = $stop || $start;
348 $query_arguments ||= '';
350 for my $bug ($start..$stop) {
351 push @output,'<a href="'.bugurl($bug,'').qq(">$bug</a>);
353 return join(', ',@output);
358 push @log, handle_record($record,$ref,$msg_num);
360 Deals with a record in a bug log as returned by
361 L<Debbugs::Log::read_log_records>; returns the log information that
362 should be output to the browser.
367 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
370 local $_ = $record->{type};
372 $output .= decode_rfc1522($record->{text});
373 # Link to forwarded http:// urls in the midst of the report
374 # (even though these links already exist at the top)
375 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
376 # Add links to the cloned bugs
377 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links($2).$3.bug_links($4,$5)}eo;
378 $output .= '<a href="' . bugurl($ref, 'msg='.($msg_number+1)) . '">Full text</a> and <a href="' .
379 bugurl($ref, 'msg='.($msg_number+1)) . '&mbox=yes">rfc822 format</a> available.</em>';
382 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
383 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
386 elsif (defined $msg_id) {
387 $$seen_msg_ids{$msg_id} = 1;
389 $output .= 'View this message in <a href="' . bugurl($ref, "msg=$msg_number") . '&mbox=yes">rfc822 format</a></em>';
390 $output .= '<pre class="message">' .
391 handle_email_message($record->{text},
393 msg_number => $msg_number,
396 elsif (/autocheck/) {
399 elsif (/incoming-recv/) {
400 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
401 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
404 elsif (defined $msg_id) {
405 $$seen_msg_ids{$msg_id} = 1;
407 # Incomming Mail Message
408 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
409 $output .= qq|<h2><a name="msg$msg_number">Message received at |.
410 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";
411 $output .= '<pre class="message">' .
412 handle_email_message($record->{text},
414 msg_number => $msg_number,
418 die "Unknown record type $_";
426 if (looks_like_number($msg) and ($msg-1) <= $#records) {
427 @records = ($records[$msg-1]);
433 print qq(Content-Disposition: attachment; filename="bug_${ref}.mbox"\n);
434 print "Content-Type: text/plain\n\n";
438 print qq(Content-Disposition: attachment; filename="bug_${ref}_message_${msg_num}.mbox"\n);
439 print "Content-Type: message/rfc822\n\n";
441 for my $record (@records) {
442 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
443 next if not $boring and $record->{type} eq 'recips' and @records > 1;
444 my @lines = split( "\n", $record->{text}, -1 );
445 if ( $lines[ 1 ] =~ m/^From / ) {
446 my $tmp = $lines[ 0 ];
447 $lines[ 0 ] = $lines[ 1 ];
450 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
451 my $date = strftime "%a %b %d %T %Y", localtime;
452 unshift @lines, "From unknown $date";
454 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
455 print join( "\n", @lines ) . "\n";
462 for my $record (@records) {
468 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
469 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
473 @log = reverse @log if $reverse;
474 $log = join('<hr>',@log);
477 print "Content-Type: text/html; charset=utf-8\n\n";
479 my $title = htmlsanit($status{subject});
481 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
482 print "<HTML><HEAD>\n" .
483 "<TITLE>$debbugs::gProject $debbugs::gBug report logs - $short - $title</TITLE>\n" .
484 '<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'.
485 # "<link rel=\"stylesheet\" href=\"$debbugs::gWebHostBugDir/bugs.css\" type=\"text/css\">" .
489 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
490 "<BR>" . $title . "</H1>\n";
492 print "$descriptivehead\n";
493 printf "<p>View this report as an <a href=\"%s\">mbox folder</a>.</p>\n", mboxurl($ref);
498 print "</BODY></HTML>\n";