]> git.donarmstrong.com Git - debbugs.git/blob - cgi/bugreport.cgi
* Merge in changes from mainline
[debbugs.git] / cgi / bugreport.cgi
1 #!/usr/bin/perl -wT
2
3 package debbugs;
4
5 use strict;
6
7 if (defined $ENV{REMOTE_ADDR} and $ENV{REMOTE_ADDR} =~ m/^(72\.17\.168\.57|60\.238\.143\.248|61\.224\.27\.141|146\.82\.138\.7|61\.214\.28\.119|84\.189\.46\.3|82\.124\.165\.25|220\.181\.26\.108|220\.95\.221\.\d+|82\.120\.77\.220|80\.68\.89\.71|66\.227\.249\.188|194\.46\.224\.153|66\.246\.72\.81|60\.42\.181\.163)$/) {
8     print "Content-Type: text/html\n\nGo away.";
9     sleep(5);
10     exit 0;
11 }
12
13 use POSIX qw(strftime tzset);
14 use MIME::Parser;
15 use MIME::Decoder;
16 use IO::Scalar;
17 use IO::File;
18
19 #require '/usr/lib/debbugs/errorlib';
20 require './common.pl';
21
22 require '/etc/debbugs/config';
23 require '/etc/debbugs/text';
24
25 use vars(qw($gEmailDomain $gHTMLTail $gSpoolDir $gWebDomain));
26
27 # for read_log_records
28 use Debbugs::Log;
29 use Debbugs::MIME qw(convert_to_utf8 decode_rfc1522);
30
31 use Scalar::Util qw(looks_like_number);
32
33 my %param = readparse();
34
35 my $tail_html;
36
37 my $ref = $param{'bug'} || quitcgi("No bug number");
38 $ref =~ /(\d+)/ or quitcgi("Invalid bug number");
39 $ref = $1;
40 my $short = "#$ref";
41 my $msg = $param{'msg'} || "";
42 my $att = $param{'att'};
43 my $boring = ($param{'boring'} || 'no') eq 'yes'; 
44 my $terse = ($param{'terse'} || 'no') eq 'yes';
45 my $reverse = ($param{'reverse'} || 'no') eq 'yes';
46 my $mbox = ($param{'mbox'} || 'no') eq 'yes'; 
47 my $mime = ($param{'mime'} || 'yes') eq 'yes';
48
49 my $trim_headers = ($param{trim} || ($msg?'no':'yes')) eq 'yes';
50
51 # Not used by this script directly, but fetch these so that pkgurl() and
52 # friends can propagate them correctly.
53 my $archive = ($param{'archive'} || 'no') eq 'yes';
54 my $repeatmerged = ($param{'repeatmerged'} || 'yes') eq 'yes';
55 set_option('archive', $archive);
56 set_option('repeatmerged', $repeatmerged);
57
58 my $buglog = buglog($ref);
59
60 if (defined $ENV{REQUEST_METHOD} and $ENV{REQUEST_METHOD} eq 'HEAD' and not defined($att) and not $mbox) {
61     print "Content-Type: text/html; charset=utf-8\n";
62     my @stat = stat $buglog;
63     if (@stat) {
64         my $mtime = strftime '%a, %d %b %Y %T GMT', gmtime($stat[9]);
65         print "Last-Modified: $mtime\n";
66     }
67     print "\n";
68     exit 0;
69 }
70
71 sub display_entity ($$$$\$\@);
72 sub display_entity ($$$$\$\@) {
73     my $entity = shift;
74     my $ref = shift;
75     my $top = shift;
76     my $xmessage = shift;
77     my $this = shift;
78     my $attachments = shift;
79
80     my $head = $entity->head;
81     my $disposition = $head->mime_attr('content-disposition');
82     $disposition = 'inline' if not defined $disposition or $disposition eq '';
83     my $type = $entity->effective_type;
84     my $filename = $entity->head->recommended_filename;
85     $filename = '' unless defined $filename;
86     $filename = decode_rfc1522($filename);
87
88     if ($top and not $terse) {
89          my $header = $entity->head;
90          $$this .= "<pre class=\"headers\">\n";
91          if ($trim_headers) {
92               my @headers;
93               foreach (qw(From To Cc Subject Date)) {
94                    my $head_field = $head->get($_);
95                    next unless defined $head_field and $head_field ne '';
96                    push @headers, qq(<b>$_:</b> ) . htmlsanit(decode_rfc1522($head_field));
97               }
98               $$this .= join(qq(), @headers) unless $terse;
99          } else {
100               $$this .= htmlsanit(decode_rfc1522($entity->head->stringify));
101          }
102          $$this .= "</pre>\n";
103     }
104
105     unless (($top and $type =~ m[^text(?:/plain)?(?:;|$)]) or
106             ($type =~ m[^multipart/])) {
107         push @$attachments, $entity;
108         my @dlargs = ($ref, "msg=$xmessage", "att=$#$attachments");
109         push @dlargs, "filename=$filename" if $filename ne '';
110         my $printname = $filename;
111         $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
112         $$this .= '<pre class="mime">[<a href="' . bugurl(@dlargs) . qq{">$printname</a> } .
113                   "($type, $disposition)]</pre>\n";
114
115         if ($msg and defined($att) and $att eq $#$attachments) {
116             my $head = $entity->head;
117             chomp(my $type = $entity->effective_type);
118             my $body = $entity->stringify_body;
119             print "Content-Type: $type";
120             my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
121             print qq(; charset="$charset") if defined $charset;
122             print "\n";
123             if ($filename ne '') {
124                 my $qf = $filename;
125                 $qf =~ s/"/\\"/g;
126                 $qf =~ s[.*/][];
127                 print qq{Content-Disposition: inline; filename="$qf"\n};
128             }
129             print "\n";
130             my $decoder = new MIME::Decoder($head->mime_encoding);
131             $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
132             exit(0);
133         }
134     }
135
136     return if not $top and $disposition eq 'attachment' and not defined($att);
137     return unless ($type =~ m[^text/?] and
138                    $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
139                   $type =~ m[^application/pgp(?:;|$)] or
140                   $entity->parts;
141
142     if ($entity->is_multipart) {
143         my @parts = $entity->parts;
144         foreach my $part (@parts) {
145             display_entity($part, $ref, 0, $xmessage,
146                            $$this, @$attachments);
147             $$this .= "\n";
148         }
149     } elsif ($entity->parts) {
150         # We must be dealing with a nested message.
151         $$this .= "<blockquote>\n";
152         my @parts = $entity->parts;
153         foreach my $part (@parts) {
154             display_entity($part, $ref, 1, $xmessage,
155                            $$this, @$attachments);
156             $$this .= "\n";
157         }
158         $$this .= "</blockquote>\n";
159     } else {
160          if (not $terse) {
161               my $content_type = $entity->head->get('Content-Type:') || "text/html";
162               my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
163               my $body = $entity->bodyhandle->as_string;
164               $body = convert_to_utf8($body,$charset) if defined $charset;
165               $body = htmlsanit($body);
166               # Add links to URLs
167               $body =~ s,((ftp|http|https)://[\S~-]+?/?)((\&gt\;)?[)]?[']?[:.\,]?(\s|$)),<a href=\"$1\">$1</a>$3,go;
168               # Add links to bug closures
169               $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)
170                         ][my $temp = $1; $temp =~ s{(\d+)}{qq(<a href=").bugurl($1).qq(">$1</a>)}ge; $temp;]gxie;
171               $$this .= qq(<pre class="message">$body</pre>\n);
172          }
173     }
174 }
175
176 my %maintainer = %{getmaintainers()};
177 my %pkgsrc = %{getpkgsrc()};
178
179 my $indexentry;
180 my $showseverity;
181
182 my $tpack;
183 my $tmain;
184
185 $ENV{"TZ"} = 'UTC';
186 tzset();
187
188 my $dtime = strftime "%a, %e %b %Y %T UTC", localtime;
189 $tail_html = $debbugs::gHTMLTail;
190 $tail_html =~ s/SUBSTITUTE_DTIME/$dtime/;
191
192 my %status = %{getbugstatus($ref)};
193 unless (%status) {
194     print <<EOF;
195 Content-Type: text/html; charset=utf-8
196
197 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
198 <html>
199 <head><title>$short - $debbugs::gProject $debbugs::gBug report logs</title></head>
200 <body>
201 <h1>$debbugs::gProject $debbugs::gBug report logs - $short</h1>
202 <p>There is no record of $debbugs::gBug $short.
203 Try the <a href="http://$gWebDomain/">search page</a> instead.</p>
204 $tail_html</body></html>
205 EOF
206     exit 0;
207 }
208
209 $|=1;
210
211 $tpack = lc $status{'package'};
212 my @tpacks = splitpackages($tpack);
213
214 if  ($status{severity} eq 'normal') {
215         $showseverity = '';
216 } elsif (isstrongseverity($status{severity})) {
217         $showseverity = "Severity: <em class=\"severity\">$status{severity}</em>;\n";
218 } else {
219         $showseverity = "Severity: $status{severity};\n";
220 }
221
222 $indexentry .= "<div class=\"msgreceived\">\n";
223 $indexentry .= htmlpackagelinks($status{package}, 0) . ";\n";
224
225 foreach my $pkg (@tpacks) {
226     my $tmaint = defined($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)';
227     my $tsrc = defined($pkgsrc{$pkg}) ? $pkgsrc{$pkg} : '(unknown)';
228
229     $indexentry .=
230             htmlmaintlinks(sub { $_[0] == 1 ? "Maintainer for $pkg is\n"
231                                             : "Maintainers for $pkg are\n" },
232                            $tmaint);
233     $indexentry .= ";\nSource for $pkg is\n".
234             '<a href="'.srcurl($tsrc)."\">$tsrc</a>" if ($tsrc ne "(unknown)");
235     $indexentry .= ".\n";
236 }
237
238 $indexentry .= "<br>";
239 $indexentry .= htmladdresslinks("Reported by: ", \&submitterurl,
240                                 $status{originator}) . ";\n";
241 $indexentry .= sprintf "Date: %s.\n",
242                 (strftime "%a, %e %b %Y %T UTC", localtime($status{date}));
243
244 $indexentry .= "<br>Owned by: " . htmlsanit($status{owner}) . ".\n"
245               if length $status{owner};
246
247 $indexentry .= "</div>\n";
248
249 my @descstates;
250
251 $indexentry .= "<h3>$showseverity";
252 $indexentry .= sprintf "Tags: %s;\n", 
253                 htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
254                         if length($status{tags});
255 $indexentry .= "<br>" if (length($showseverity) or length($status{tags}));
256
257 my @merged= split(/ /,$status{mergedwith});
258 if (@merged) {
259         my $descmerged = 'Merged with ';
260         my $mseparator = '';
261         for my $m (@merged) {
262                 $descmerged .= $mseparator."<a href=\"" . bugurl($m) . "\">#$m</a>";
263                 $mseparator= ",\n";
264         }
265         push @descstates, $descmerged;
266 }
267
268 if (@{$status{found_versions}}) {
269     my $foundtext = 'Found in ';
270     $foundtext .= (@{$status{found_versions}} == 1) ? 'version ' : 'versions ';
271     $foundtext .= join ', ', map htmlsanit($_), @{$status{found_versions}};
272     push @descstates, $foundtext;
273 }
274
275 if (@{$status{fixed_versions}}) {
276     my $fixedtext = '<strong>Fixed</strong> in ';
277     $fixedtext .= (@{$status{fixed_versions}} == 1) ? 'version ' : 'versions ';
278     $fixedtext .= join ', ', map htmlsanit($_), @{$status{fixed_versions}};
279     if (length($status{done})) {
280         $fixedtext .= ' by ' . htmlsanit(decode_rfc1522($status{done}));
281     }
282     push @descstates, $fixedtext;
283 } elsif (length($status{done})) {
284     push @descstates, "<strong>Done:</strong> ".htmlsanit(decode_rfc1522($status{done}));
285 } elsif (length($status{forwarded})) {
286     push @descstates, "<strong>Forwarded</strong> to ".maybelink($status{forwarded});
287 }
288
289
290 my @blockedby= split(/ /, $status{blockedby});
291 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
292     for my $b (@blockedby) {
293         my %s = %{getbugstatus($b)};
294         next if $s{"pending"} eq 'fixed' || length $s{done};
295         push @descstates, "Fix blocked by <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
296     }
297 }
298
299 my @blocks= split(/ /, $status{blocks});
300 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
301     for my $b (@blocks) {
302         my %s = %{getbugstatus($b)};
303         next if $s{"pending"} eq 'fixed' || length $s{done};
304         push @descstates, "Blocking fix for <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
305     }
306 }
307
308 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
309     push @descstates, "Bug is archived. No further changes may be made";
310 }
311
312 $indexentry .= join(";\n<br>", @descstates) . ".\n" if @descstates;
313 $indexentry .= "</h3>\n";
314
315 my $descriptivehead = $indexentry;
316
317 my $buglogfh;
318 if ($buglog =~ m/\.gz$/) {
319     my $oldpath = $ENV{'PATH'};
320     $ENV{'PATH'} = '/bin:/usr/bin';
321     $buglogfh = new IO::File "zcat $buglog |" or &quitcgi("open log for $ref: $!");
322     $ENV{'PATH'} = $oldpath;
323 } else {
324     $buglogfh = new IO::File "<$buglog" or &quitcgi("open log for $ref: $!");
325 }
326
327
328 my @records;
329 eval{
330      @records = read_log_records($buglogfh);
331 };
332 if ($@) {
333      quitcgi("Bad bug log for $debbugs::gBug $ref. Unable to read records: $@");
334 }
335 undef $buglogfh;
336
337 =head2 handle_email_message
338
339      handle_email_message($record->{text},
340                           ref        => $bug_number,
341                           msg_number => $msg_number,
342                          );
343
344 Returns a decoded e-mail message and displays entities/attachments as
345 appropriate.
346
347
348 =cut
349
350 sub handle_email_message{
351      my ($email,%options) = @_;
352
353      my $output = '';
354      my $parser = new MIME::Parser;
355      # Because we are using memory, not tempfiles, there's no need to
356      # clean up here like in Debbugs::MIME
357      $parser->tmp_to_core(1);
358      $parser->output_to_core(1);
359      my $entity = $parser->parse_data( $email);
360      my @attachments = ();
361      display_entity($entity, $options{ref}, 1, $options{msg_number}, $output, @attachments);
362      return $output;
363
364 }
365
366 =head2 bug_links
367
368      bug_links($one_bug);
369      bug_links($starting_bug,$stoping_bugs,);
370
371 Creates a set of links to bugs, starting with bug number
372 $starting_bug, and finishing with $stoping_bug; if only one bug is
373 passed, makes a link to only a single bug.
374
375 The content of the link is the bug number.
376
377 =cut
378
379 sub bug_links{
380      my ($start,$stop,$query_arguments) = @_;
381      $stop = $stop || $start;
382      $query_arguments ||= '';
383      my @output;
384      for my $bug ($start..$stop) {
385           push @output,'<a href="'.bugurl($bug,'').qq(">$bug</a>);
386      }
387      return join(', ',@output);
388 }
389
390 =head2 handle_record
391
392      push @log, handle_record($record,$ref,$msg_num);
393
394 Deals with a record in a bug log as returned by
395 L<Debbugs::Log::read_log_records>; returns the log information that
396 should be output to the browser.
397
398 =cut
399
400 sub handle_record{
401      my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
402
403      my $output = '';
404      local $_ = $record->{type};
405      if (/html/) {
406           $output .= decode_rfc1522($record->{text});
407           # Link to forwarded http:// urls in the midst of the report
408           # (even though these links already exist at the top)
409           $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
410           # Add links to the cloned bugs
411           $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links($2).$3.bug_links($4,$5)}eo;
412           # Add links to merged bugs
413           $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links($_)} (split /\s+/, $1))}eo;
414           # Add links to reassigned packages
415           $output =~ s{(Bug reassigned from package \`)([^\']+)(' to \`)([^\']+)(')}
416           {$1.q(<a href=").pkgurl($2).qq(">$2</a>).$3.q(<a href=").pkgurl($4).qq(">$4</a>).$5}eo;
417           $output .= '<a href="' . bugurl($ref, 'msg='.($msg_number+1)) . '">Full text</a> and <a href="' .
418                bugurl($ref, 'msg='.($msg_number+1), 'mbox') . '">rfc822 format</a> available.';
419
420           $output = "<div class=\"msgreceived\">\n" . $output . "</div>\n";
421      }
422      elsif (/recips/) {
423           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
424           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
425                return ();
426           }
427           elsif (defined $msg_id) {
428                $$seen_msg_ids{$msg_id} = 1;
429           }
430           $output .= 'View this message in <a href="' . bugurl($ref, "msg=$msg_number", "mbox") . '">rfc822 format</a>';
431           $output .= handle_email_message($record->{text},
432                                     ref        => $bug_number,
433                                     msg_number => $msg_number,
434                                    );
435      }
436      elsif (/autocheck/) {
437           # Do nothing
438      }
439      elsif (/incoming-recv/) {
440           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
441           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
442                return ();
443           }
444           elsif (defined $msg_id) {
445                $$seen_msg_ids{$msg_id} = 1;
446           }
447           # Incomming Mail Message
448           my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
449           $output .= qq|<p class="msgreceived"><a name="msg$msg_number">Message received</a> at |.
450                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";
451           $output .= handle_email_message($record->{text},
452                                     ref        => $bug_number,
453                                     msg_number => $msg_number,
454                                    );
455      }
456      else {
457           die "Unknown record type $_";
458      }
459      return $output;
460 }
461
462 my $log='';
463 my $msg_num = 0;
464 my $skip_next = 0;
465 if (looks_like_number($msg) and ($msg-1) <= $#records) {
466      @records = ($records[$msg-1]);
467      $msg_num = $msg - 1;
468 }
469 my @log;
470 if ( $mbox ) {
471      if (@records > 1) {
472           print qq(Content-Disposition: attachment; filename="bug_${ref}.mbox"\n);
473           print "Content-Type: text/plain\n\n";
474      }
475      else {
476           $msg_num++;
477           print qq(Content-Disposition: attachment; filename="bug_${ref}_message_${msg_num}.mbox"\n);
478           print "Content-Type: message/rfc822\n\n";
479      }
480      for my $record (@records) {
481           next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
482           next if not $boring and $record->{type} eq 'recips' and @records > 1;
483           my @lines = split( "\n", $record->{text}, -1 );
484           if ( $lines[ 1 ] =~ m/^From / ) {
485                my $tmp = $lines[ 0 ];
486                $lines[ 0 ] = $lines[ 1 ];
487                $lines[ 1 ] = $tmp;
488           }
489           if ( !( $lines[ 0 ] =~ m/^From / ) ) {
490                my $date = strftime "%a %b %d %T %Y", localtime;
491                unshift @lines, "From unknown $date";
492           }
493           map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
494           print join( "\n", @lines ) . "\n";
495      }
496      exit 0;
497 }
498
499 else {
500      my %seen_msg_ids;
501      for my $record (@records) {
502           $msg_num++;
503           if ($skip_next) {
504                $skip_next = 0;
505                next;
506           }
507           $skip_next = 1 if $record->{type} eq 'html' and not $boring;
508           push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
509      }
510 }
511
512 @log = reverse @log if $reverse;
513 $log = join('<hr>',@log);
514
515
516 print "Content-Type: text/html; charset=utf-8\n\n";
517
518 my $title = htmlsanit($status{subject});
519
520 my $dummy2 = $debbugs::gWebHostBugDir;
521
522 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
523 print "<HTML><HEAD>\n" . 
524     "<TITLE>$short - $title - $debbugs::gProject $debbugs::gBug report logs</TITLE>\n" .
525      '<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'.
526      "<link rel=\"stylesheet\" href=\"$debbugs::gWebHostBugDir/css/bugs.css\" type=\"text/css\">" .
527     "</HEAD>\n" .
528     '<BODY>' .
529     "\n";
530 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
531       "<BR>" . $title . "</H1>\n";
532
533 print "$descriptivehead\n";
534 printf "<div class=\"msgreceived\"><p>View this report as an <a href=\"%s\">mbox folder</a>.</p></div>\n", bugurl($ref, "mbox");
535 print "<HR>";
536 print "$log";
537 print "<HR>";
538 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";
539 print $tail_html;
540
541 print "</BODY></HTML>\n";
542
543 exit 0;