6 use POSIX qw(strftime tzset);
12 #require '/usr/lib/debbugs/errorlib';
13 require './common.pl';
15 require '/etc/debbugs/config';
16 require '/etc/debbugs/text';
18 use vars(qw($gEmailDomain $gHTMLTail $gSpoolDir $gWebDomain));
20 # for read_log_records
22 use Debbugs::MIME qw(convert_to_utf8 decode_rfc1522);
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 # Not used by this script directly, but fetch these so that pkgurl() and
45 # friends can propagate them correctly.
46 my $archive = ($param{'archive'} || 'no') eq 'yes';
47 my $repeatmerged = ($param{'repeatmerged'} || 'yes') eq 'yes';
48 set_option('archive', $archive);
49 set_option('repeatmerged', $repeatmerged);
51 my $buglog = buglog($ref);
53 if (defined $ENV{REQUEST_METHOD} and $ENV{REQUEST_METHOD} eq 'HEAD' and not defined($att) and not $mbox) {
54 print "Content-Type: text/html; charset=utf-8\n";
55 my @stat = stat $buglog;
57 my $mtime = strftime '%a, %d %b %Y %T GMT', gmtime($stat[9]);
58 print "Last-Modified: $mtime\n";
64 sub display_entity ($$$$\$\@);
65 sub display_entity ($$$$\$\@) {
71 my $attachments = shift;
73 my $head = $entity->head;
74 my $disposition = $head->mime_attr('content-disposition');
75 $disposition = 'inline' if not defined $disposition or $disposition eq '';
76 my $type = $entity->effective_type;
77 my $filename = $entity->head->recommended_filename;
78 $filename = '' unless defined $filename;
79 $filename = decode_rfc1522($filename);
81 if ($top and not $terse) {
82 my $header = $entity->head;
83 $$this .= "<pre class=\"headers\">\n";
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(<b>$_:</b> ) . htmlsanit(decode_rfc1522($head_field));
91 $$this .= join(qq(), @headers) unless $terse;
93 $$this .= htmlsanit(decode_rfc1522($entity->head->stringify));
98 unless (($top and $type =~ m[^text(?:/plain)?(?:;|$)]) or
99 ($type =~ m[^multipart/])) {
100 push @$attachments, $entity;
101 my @dlargs = ($ref, "msg=$xmessage", "att=$#$attachments");
102 push @dlargs, "filename=$filename" if $filename ne '';
103 my $printname = $filename;
104 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
105 $$this .= '<pre class="mime">[<a href="' . bugurl(@dlargs) . qq{">$printname</a> } .
106 "($type, $disposition)]</pre>\n";
108 if ($msg and defined($att) and $att eq $#$attachments) {
109 my $head = $entity->head;
110 chomp(my $type = $entity->effective_type);
111 my $body = $entity->stringify_body;
112 print "Content-Type: $type";
113 my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
114 print qq(; charset="$charset") if defined $charset;
116 if ($filename ne '') {
120 print qq{Content-Disposition: inline; filename="$qf"\n};
123 my $decoder = new MIME::Decoder($head->mime_encoding);
124 $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
129 return if not $top and $disposition eq 'attachment' and not defined($att);
130 return unless ($type =~ m[^text/?] and
131 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
132 $type =~ m[^application/pgp(?:;|$)] or
135 if ($entity->is_multipart) {
136 my @parts = $entity->parts;
137 foreach my $part (@parts) {
138 display_entity($part, $ref, 0, $xmessage,
139 $$this, @$attachments);
142 } elsif ($entity->parts) {
143 # We must be dealing with a nested message.
144 $$this .= "<blockquote>\n";
145 my @parts = $entity->parts;
146 foreach my $part (@parts) {
147 display_entity($part, $ref, 1, $xmessage,
148 $$this, @$attachments);
151 $$this .= "</blockquote>\n";
154 my $content_type = $entity->head->get('Content-Type:') || "text/html";
155 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
156 my $body = $entity->bodyhandle->as_string;
157 $body = convert_to_utf8($body,$charset) if defined $charset;
158 $body = htmlsanit($body);
159 $body =~ s,((ftp|http|https)://[\S~-]+?/?)((\>\;)?[)]?[']?[:.\,]?(\s|$)),<a href=\"$1\">$1</a>$3,go;
160 $$this .= "<pre class=\"message\">";
162 $$this .= "</pre>\n";
167 my %maintainer = %{getmaintainers()};
168 my %pkgsrc = %{getpkgsrc()};
179 my $dtime = strftime "%a, %e %b %Y %T UTC", localtime;
180 $tail_html = $debbugs::gHTMLTail;
181 $tail_html =~ s/SUBSTITUTE_DTIME/$dtime/;
183 my %status = %{getbugstatus($ref)};
186 Content-Type: text/html; charset=utf-8
188 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
190 <head><title>$short - $debbugs::gProject $debbugs::gBug report logs</title></head>
192 <h1>$debbugs::gProject $debbugs::gBug report logs - $short</h1>
193 <p>There is no record of $debbugs::gBug $short.
194 Try the <a href="http://$gWebDomain/">search page</a> instead.</p>
195 $tail_html</body></html>
202 $tpack = lc $status{'package'};
203 my @tpacks = splitpackages($tpack);
205 if ($status{severity} eq 'normal') {
207 } elsif (isstrongseverity($status{severity})) {
208 $showseverity = "Severity: <em class=\"severity\">$status{severity}</em>;\n";
210 $showseverity = "Severity: $status{severity};\n";
213 $indexentry .= "<div class=\"msgreceived\">\n";
214 $indexentry .= htmlpackagelinks($status{package}, 0) . ";\n";
216 foreach my $pkg (@tpacks) {
217 my $tmaint = defined($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)';
218 my $tsrc = defined($pkgsrc{$pkg}) ? $pkgsrc{$pkg} : '(unknown)';
221 htmlmaintlinks(sub { $_[0] == 1 ? "Maintainer for $pkg is\n"
222 : "Maintainers for $pkg are\n" },
224 $indexentry .= ";\nSource for $pkg is\n".
225 '<a href="'.srcurl($tsrc)."\">$tsrc</a>" if ($tsrc ne "(unknown)");
226 $indexentry .= ".\n";
229 $indexentry .= "<br>";
230 $indexentry .= htmladdresslinks("Reported by: ", \&submitterurl,
231 $status{originator}) . ";\n";
232 $indexentry .= sprintf "Date: %s.\n",
233 (strftime "%a, %e %b %Y %T UTC", localtime($status{date}));
235 $indexentry .= "<br>Owned by: " . htmlsanit($status{owner}) . ".\n"
236 if length $status{owner};
238 $indexentry .= "</div>\n";
242 $indexentry .= "<h3>$showseverity";
243 $indexentry .= sprintf "Tags: %s;\n",
244 htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
245 if length($status{tags});
246 $indexentry .= "<br>" if (length($showseverity) or length($status{tags}));
248 my @merged= split(/ /,$status{mergedwith});
250 my $descmerged = 'Merged with ';
252 for my $m (@merged) {
253 $descmerged .= $mseparator."<a href=\"" . bugurl($m) . "\">#$m</a>";
256 push @descstates, $descmerged;
259 if (@{$status{found_versions}}) {
260 my $foundtext = 'Found in ';
261 $foundtext .= (@{$status{found_versions}} == 1) ? 'version ' : 'versions ';
262 $foundtext .= join ', ', map htmlsanit($_), @{$status{found_versions}};
263 push @descstates, $foundtext;
266 if (@{$status{fixed_versions}}) {
267 my $fixedtext = '<strong>Fixed</strong> in ';
268 $fixedtext .= (@{$status{fixed_versions}} == 1) ? 'version ' : 'versions ';
269 $fixedtext .= join ', ', map htmlsanit($_), @{$status{fixed_versions}};
270 if (length($status{done})) {
271 $fixedtext .= ' by ' . htmlsanit(decode_rfc1522($status{done}));
273 push @descstates, $fixedtext;
274 } elsif (length($status{done})) {
275 push @descstates, "<strong>Done:</strong> ".htmlsanit(decode_rfc1522($status{done}));
276 } elsif (length($status{forwarded})) {
277 push @descstates, "<strong>Forwarded</strong> to ".maybelink($status{forwarded});
281 my @blockedby= split(/ /, $status{blockedby});
282 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
283 for my $b (@blockedby) {
284 my %s = %{getbugstatus($b)};
285 next if $s{"pending"} eq 'fixed' || length $s{done};
286 push @descstates, "Fix blocked by <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
290 my @blocks= split(/ /, $status{blocks});
291 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
292 for my $b (@blocks) {
293 my %s = %{getbugstatus($b)};
294 next if $s{"pending"} eq 'fixed' || length $s{done};
295 push @descstates, "Blocking fix for <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
299 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
300 push @descstates, "Bug is archived. No further changes may be made";
303 $indexentry .= join(";\n<br>", @descstates) . ".\n" if @descstates;
304 $indexentry .= "</h3>\n";
306 my $descriptivehead = $indexentry;
309 if ($buglog =~ m/\.gz$/) {
310 my $oldpath = $ENV{'PATH'};
311 $ENV{'PATH'} = '/bin:/usr/bin';
312 $buglogfh = new IO::File "zcat $buglog |" or &quitcgi("open log for $ref: $!");
313 $ENV{'PATH'} = $oldpath;
315 $buglogfh = new IO::File "<$buglog" or &quitcgi("open log for $ref: $!");
321 @records = read_log_records($buglogfh);
324 quitcgi("Bad bug log for $debbugs::gBug $ref. Unable to read records: $@");
328 =head2 handle_email_message
330 handle_email_message($record->{text},
332 msg_number => $msg_number,
335 Returns a decoded e-mail message and displays entities/attachments as
341 sub handle_email_message{
342 my ($email,%options) = @_;
345 my $parser = new MIME::Parser;
346 $parser->tmp_to_core(1);
347 $parser->output_to_core(1);
348 # $parser->output_under("/tmp");
349 my $entity = $parser->parse_data( $email);
350 # TODO: make local subdir, clean it ourselves
351 # the following does NOT delete the msg dirs in /tmp
352 END { if ( $entity ) { $entity->purge; } if ( $parser ) { $parser->filer->purge; } }
353 my @attachments = ();
354 display_entity($entity, $options{ref}, 1, $options{msg_number}, $output, @attachments);
362 bug_links($starting_bug,$stoping_bugs,);
364 Creates a set of links to bugs, starting with bug number
365 $starting_bug, and finishing with $stoping_bug; if only one bug is
366 passed, makes a link to only a single bug.
368 The content of the link is the bug number.
373 my ($start,$stop,$query_arguments) = @_;
374 $stop = $stop || $start;
375 $query_arguments ||= '';
377 for my $bug ($start..$stop) {
378 push @output,'<a href="'.bugurl($bug,'').qq(">$bug</a>);
380 return join(', ',@output);
385 push @log, handle_record($record,$ref,$msg_num);
387 Deals with a record in a bug log as returned by
388 L<Debbugs::Log::read_log_records>; returns the log information that
389 should be output to the browser.
394 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
397 local $_ = $record->{type};
399 $output .= decode_rfc1522($record->{text});
400 # Link to forwarded http:// urls in the midst of the report
401 # (even though these links already exist at the top)
402 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
403 # Add links to the cloned bugs
404 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links($2).$3.bug_links($4,$5)}eo;
405 # Add links to merged bugs
406 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links($_)} (split /\s+/, $1))}eo;
407 $output .= '<a href="' . bugurl($ref, 'msg='.($msg_number+1)) . '">Full text</a> and <a href="' .
408 bugurl($ref, 'msg='.($msg_number+1), 'mbox') . '">rfc822 format</a> available.';
410 $output = "<div class=\"msgreceived\">\n" . $output . "</div>\n";
413 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
414 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
417 elsif (defined $msg_id) {
418 $$seen_msg_ids{$msg_id} = 1;
420 $output .= 'View this message in <a href="' . bugurl($ref, "msg=$msg_number", "mbox") . '">rfc822 format</a>';
421 $output .= handle_email_message($record->{text},
423 msg_number => $msg_number,
426 elsif (/autocheck/) {
429 elsif (/incoming-recv/) {
430 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
431 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
434 elsif (defined $msg_id) {
435 $$seen_msg_ids{$msg_id} = 1;
437 # Incomming Mail Message
438 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
439 $output .= qq|<p class="msgreceived"><a name="msg$msg_number">Message received</a> at |.
440 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";
441 $output .= handle_email_message($record->{text},
443 msg_number => $msg_number,
447 die "Unknown record type $_";
455 if (looks_like_number($msg) and ($msg-1) <= $#records) {
456 @records = ($records[$msg-1]);
462 print qq(Content-Disposition: attachment; filename="bug_${ref}.mbox"\n);
463 print "Content-Type: text/plain\n\n";
467 print qq(Content-Disposition: attachment; filename="bug_${ref}_message_${msg_num}.mbox"\n);
468 print "Content-Type: message/rfc822\n\n";
470 for my $record (@records) {
471 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
472 next if not $boring and $record->{type} eq 'recips' and @records > 1;
473 my @lines = split( "\n", $record->{text}, -1 );
474 if ( $lines[ 1 ] =~ m/^From / ) {
475 my $tmp = $lines[ 0 ];
476 $lines[ 0 ] = $lines[ 1 ];
479 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
480 my $date = strftime "%a %b %d %T %Y", localtime;
481 unshift @lines, "From unknown $date";
483 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
484 print join( "\n", @lines ) . "\n";
491 for my $record (@records) {
497 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
498 push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
502 @log = reverse @log if $reverse;
503 $log = join('<hr>',@log);
506 print "Content-Type: text/html; charset=utf-8\n\n";
508 my $title = htmlsanit($status{subject});
510 my $dummy2 = $debbugs::gWebHostBugDir;
512 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
513 print "<HTML><HEAD>\n" .
514 "<TITLE>$short - $title - $debbugs::gProject $debbugs::gBug report logs</TITLE>\n" .
515 '<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'.
516 "<link rel=\"stylesheet\" href=\"$debbugs::gWebHostBugDir/css/bugs.css\" type=\"text/css\">" .
520 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
521 "<BR>" . $title . "</H1>\n";
523 print "$descriptivehead\n";
524 printf "<div class=\"msgreceived\"><p>View this report as an <a href=\"%s\">mbox folder</a>.</p></div>\n", bugurl($ref, "mbox");
528 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";
531 print "</BODY></HTML>\n";