]> git.donarmstrong.com Git - debbugs.git/blob - cgi/bugreport.cgi
[project @ 2004-03-28 06:02:45 by doogie]
[debbugs.git] / cgi / bugreport.cgi
1 #!/usr/bin/perl -wT
2
3 package debbugs;
4
5 use strict;
6 use POSIX qw(strftime tzset);
7 use MIME::Parser;
8 use MIME::Decoder;
9 use IO::Scalar;
10 use IO::Lines;
11
12 #require '/usr/lib/debbugs/errorlib';
13 require './common.pl';
14
15 require '/etc/debbugs/config';
16 require '/etc/debbugs/text';
17
18 use vars(qw($gEmailDomain $gHTMLTail $gSpoolDir $gWebDomain));
19
20 my %param = readparse();
21
22 my $tail_html;
23
24 my $ref = $param{'bug'} || quitcgi("No bug number");
25 $ref =~ /(\d+)/ or quitcgi("Invalid bug number");
26 $ref = $1;
27 my $short = "#$ref";
28 my $msg = $param{'msg'} || "";
29 my $att = $param{'att'};
30 my $boring = ($param{'boring'} || 'no') eq 'yes'; 
31 my $terse = ($param{'terse'} || 'no') eq 'yes';
32 my $reverse = ($param{'reverse'} || 'no') eq 'yes';
33 my $mbox = ($param{'mbox'} || 'no') eq 'yes'; 
34 my $mime = ($param{'mime'} || 'yes') eq 'yes';
35
36 # Not used by this script directly, but fetch these so that pkgurl() and
37 # friends can propagate them correctly.
38 my $archive = ($param{'archive'} || 'no') eq 'yes';
39 my $repeatmerged = ($param{'repeatmerged'} || 'yes') eq 'yes';
40 set_option('archive', $archive);
41 set_option('repeatmerged', $repeatmerged);
42
43 my $buglog = buglog($ref);
44
45 if ($ENV{REQUEST_METHOD} eq 'HEAD' and not defined($att) and not $mbox) {
46     print "Content-Type: text/html\n";
47     my @stat = stat $buglog;
48     if (@stat) {
49         my $mtime = strftime '%a, %d %b %Y %T GMT', gmtime($stat[9]);
50         print "Last-Modified: $mtime\n";
51     }
52     print "\n";
53     exit 0;
54 }
55
56 sub display_entity ($$$$\$\@);
57 sub display_entity ($$$$\$\@) {
58     my $entity = shift;
59     my $ref = shift;
60     my $top = shift;
61     my $xmessage = shift;
62     my $this = shift;
63     my $attachments = shift;
64
65     my $head = $entity->head;
66     my $disposition = $head->mime_attr('content-disposition');
67     $disposition = 'inline' if not defined $disposition or $disposition eq '';
68     my $type = $entity->effective_type;
69     my $filename = $entity->head->recommended_filename;
70     $filename = '' unless defined $filename;
71
72     if ($top) {
73         $$this .= htmlsanit($entity->stringify_header) unless ($terse);
74         $$this .= "\n";
75     }
76
77     unless (($top and $type =~ m[^text(?:/plain)?(?:;|$)]) or
78             ($type =~ m[^multipart/])) {
79         push @$attachments, $entity;
80         my @dlargs = ($ref, "msg=$xmessage", "att=$#$attachments");
81         push @dlargs, "filename=$filename" if $filename ne '';
82         my $printname = $filename;
83         $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
84         $$this .= '[<a href="' . dlurl(@dlargs) . qq{">$printname</a> } .
85                   "($type, $disposition)]\n\n";
86
87         if ($msg and defined($att) and $att eq $#$attachments) {
88             my $head = $entity->head;
89             chomp(my $type = $entity->effective_type);
90             my $body = $entity->stringify_body;
91             print "Content-Type: $type";
92             print "; name=$filename" if $filename ne '';
93             print "\n\n";
94             my $decoder = new MIME::Decoder($head->mime_encoding);
95             $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
96             exit(0);
97         }
98     }
99
100     return if not $top and $disposition eq 'attachment' and not defined($att);
101     return unless ($type =~ m[^text/?] and
102                    $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
103                   $type =~ m[^application/pgp(?:;|$)] or
104                   $entity->parts;
105
106     if ($entity->is_multipart) {
107         my @parts = $entity->parts;
108         foreach my $part (@parts) {
109             display_entity($part, $ref, 0, $xmessage,
110                            $$this, @$attachments);
111             $$this .= "\n";
112         }
113     } elsif ($entity->parts) {
114         # We must be dealing with a nested message.
115         $$this .= "<blockquote>\n";
116         my @parts = $entity->parts;
117         foreach my $part (@parts) {
118             display_entity($part, $ref, 1, $xmessage,
119                            $$this, @$attachments);
120             $$this .= "\n";
121         }
122         $$this .= "</blockquote>\n";
123     } else {
124         $$this .= htmlsanit($entity->bodyhandle->as_string) unless ($terse);
125     }
126 }
127
128 my %maintainer = %{getmaintainers()};
129 my %pkgsrc = %{getpkgsrc()};
130
131 my $indexentry;
132 my $descriptivehead;
133 my $showseverity;
134
135 my $tpack;
136 my $tmain;
137
138 $ENV{"TZ"} = 'UTC';
139 tzset();
140
141 my $dtime = strftime "%a, %e %b %Y %T UTC", localtime;
142 $tail_html = $debbugs::gHTMLTail;
143 $tail_html =~ s/SUBSTITUTE_DTIME/$dtime/;
144
145 my %status = %{getbugstatus($ref)};
146 unless (%status) {
147     print <<EOF;
148 Content-Type: text/html
149
150 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
151 <html>
152 <head><title>$debbugs::gProject $debbugs::gBug report logs - $short</title></head>
153 <body>
154 <h1>$debbugs::gProject $debbugs::gBug report logs - $short</h1>
155 <p>There is no record of $debbugs::gBug $short.
156 Try the <a href="http://$gWebDomain/">search page</a> instead.</p>
157 $tail_html</body></html>
158 EOF
159     exit 0;
160 }
161
162 $|=1;
163
164 $tpack = lc $status{'package'};
165 my @tpacks = splitpackages($tpack);
166
167 if  ($status{severity} eq 'normal') {
168         $showseverity = '';
169 #} elsif (isstrongseverity($status{severity})) {
170 #       $showseverity = "<strong>Severity: $status{severity}</strong>;\n";
171 } else {
172         $showseverity = "Severity: <em>$status{severity}</em>;\n";
173 }
174
175 $indexentry .= "<p>$showseverity";
176 $indexentry .= htmlpackagelinks($status{package}, 0);
177
178 $indexentry .= "Reported by: <a href=\"" . submitterurl($status{originator})
179               . "\">" . htmlsanit($status{originator}) . "</a>;\n";
180
181 $indexentry .= "Owned by: " . htmlsanit($status{owner}) . ";\n"
182               if length $status{owner};
183
184 my $dummy = strftime "%a, %e %b %Y %T UTC", localtime($status{date});
185 $indexentry .= "Date: ".$dummy.";\n<br>";
186
187 my @descstates;
188
189 push @descstates, "Tags: <strong>"
190                 . htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
191                 . "</strong>"
192                         if length($status{tags});
193
194 my @merged= split(/ /,$status{mergedwith});
195 if (@merged) {
196         my $descmerged = 'merged with ';
197         my $mseparator = '';
198         for my $m (@merged) {
199                 $descmerged .= $mseparator."<a href=\"" . bugurl($m) . "\">#$m</a>";
200                 $mseparator= ",\n";
201         }
202         push @descstates, $descmerged;
203 }
204
205 if (@{$status{found_versions}}) {
206     my $foundtext = 'Found in ';
207     $foundtext .= (@{$status{found_versions}} == 1) ? 'version ' : 'versions ';
208     $foundtext .= join ', ', map htmlsanit($_), @{$status{found_versions}};
209     push @descstates, $foundtext;
210 }
211
212 if (@{$status{fixed_versions}}) {
213     my $fixedtext = '<strong>Fixed</strong> in ';
214     $fixedtext .= (@{$status{fixed_versions}} == 1) ? 'version ' : 'versions ';
215     $fixedtext .= join ', ', map htmlsanit($_), @{$status{fixed_versions}};
216     if (length($status{done})) {
217         $fixedtext .= ' by ' . htmlsanit($status{done});
218     }
219     push @descstates, $fixedtext;
220 } elsif (length($status{done})) {
221         push @descstates, "<strong>Done:</strong> ".htmlsanit($status{done});
222 } elsif (length($status{forwarded})) {
223         push @descstates, "<strong>Forwarded</strong> to ".maybelink($status{forwarded});
224 }
225
226 $indexentry .= join(";\n", @descstates) . ";\n<br>" if @descstates;
227
228 $descriptivehead = $indexentry;
229 foreach my $pkg (@tpacks) {
230     my $tmaint = defined($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)';
231     my $tsrc = defined($pkgsrc{$pkg}) ? $pkgsrc{$pkg} : '(unknown)';
232
233     $descriptivehead .=
234             htmlmaintlinks(sub { $_[0] == 1 ? "Maintainer for $pkg is\n"
235                                             : "Maintainers for $pkg are\n" },
236                            $tmaint);
237     $descriptivehead .= ";\nSource for $pkg is\n".
238             '<a href="'.srcurl($tsrc)."\">$tsrc</a>" if ($tsrc ne "(unknown)");
239     $descriptivehead .= ".\n<br>";
240 }
241
242 if ($buglog =~ m/\.gz$/) {
243     my $oldpath = $ENV{'PATH'};
244     $ENV{'PATH'} = '/bin:/usr/bin';
245     open L, "zcat $buglog |" or &quitcgi("open log for $ref: $!");
246     $ENV{'PATH'} = $oldpath;
247 } else {
248     open L, "<$buglog" or &quitcgi("open log for $ref: $!");
249 }
250 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
251     $descriptivehead .= "\n<p>Bug is <strong>archived</strong>. No further changes may be made.</p>";
252 }
253
254 my $log='';
255
256 my $xmessage = 1;
257 my $suppressnext = 0;
258 my $found_msgid = 0;
259 my %seen_msgid = ();
260
261 my $thisheader = '';
262 my $this = '';
263
264 my $cmsg = 1;
265
266 my $normstate= 'kill-init';
267 my $linenum = 0;
268 my @mail = ();
269 my @mails = ();
270 while(my $line = <L>) {
271         $linenum++;
272         if ($line =~ m/^.$/ and 1 <= ord($line) && ord($line) <= 7) {
273                 # state transitions
274                 my $newstate;
275                 my $statenum = ord($line);
276
277                 $newstate = 'autocheck'     if ($statenum == 1);
278                 $newstate = 'recips'        if ($statenum == 2);
279                 $newstate = 'kill-end'      if ($statenum == 3);
280                 $newstate = 'go'            if ($statenum == 5);
281                 $newstate = 'html'          if ($statenum == 6);
282                 $newstate = 'incoming-recv' if ($statenum == 7);
283
284                 # disallowed transitions:
285                 $_ = "$normstate $newstate";
286                 unless (m/^(go|go-nox|html) kill-end$/
287                     || m/^(kill-init|kill-end) (incoming-recv|autocheck|recips|html)$/
288                     || m/^kill-body go$/)
289                 {
290                         &quitcgi("$ref: Transition from $normstate to $newstate at $linenum disallowed");
291                 }
292
293 #$this .= "\n<br>states: $normstate $newstate<br>\n";
294
295 #               if ($newstate eq 'go') {
296 #                       $this .= "<pre>\n";
297 #               }
298                 if ($newstate eq 'html') {
299                         $this = '';
300                 }
301
302                 if ($newstate eq 'kill-end') {
303
304                         my $show = 1;
305                         $show = $boring
306                                 if ($suppressnext && $normstate ne 'html');
307
308                         $show = ($xmessage == $msg) if ($msg);
309
310                         push @mails, join( '', @mail ) if ( $mbox && @mail );
311                         if ($show) {
312                                 if (not $mime and @mail) {
313                                         $this .= htmlsanit(join '', @mail);
314                                 } elsif (@mail) {
315                                         my $parser = new MIME::Parser;
316                                         $parser->tmp_to_core(1);
317                                         $parser->output_to_core(1);
318 #                                       $parser->output_under("/tmp");
319                                         my $entity = $parser->parse( new IO::Lines \@mail );
320                                         # TODO: make local subdir, clean it ourselves
321                                         # the following does NOT delete the msg dirs in /tmp
322                                         END { if ( $entity ) { $entity->purge; } if ( $parser ) { $parser->filer->purge; } }
323                                         my @attachments = ();
324                                         display_entity($entity, $ref, 1, $xmessage, $this, @attachments);
325                                 }
326 #                               if ($normstate eq 'go' || $normstate eq 'go-nox') {
327                                 if ($normstate ne 'html') {
328                                         $this = "<pre>\n$this</pre>\n";
329                                 }
330                                 if ($normstate eq 'html') {
331                                         $this = "<a name=\"msg$xmessage\">$this  <em><a href=\"" . bugurl($ref, "msg=$xmessage") . "\">Full text</a> available.</em></a>";
332                                 }
333                                 $this = "$thisheader$this" if $thisheader && !( $normstate eq 'html' );;
334                                 $thisheader = '';
335                                 my $delim = $terse ? "<p>" : "<hr>";
336                                 if ($reverse) {
337                                         $log = "$this\n$delim$log";
338                                 } else {
339                                         $log .= "$this\n$delim\n";
340                                 }
341                         }
342
343                         $xmessage++ if ($normstate ne 'html');
344
345                         $suppressnext = $normstate eq 'html';
346                         $found_msgid = 0;
347                 }
348                 
349                 $normstate = $newstate;
350                 @mail = ();
351                 next;
352         }
353
354         $_ = $line;
355         if ($normstate eq 'incoming-recv') {
356                 my $pl= $_;
357                 $pl =~ s/\n+$//;
358                 m/^Received: \(at (\S+)\) by (\S+)\;/
359                         || &quitcgi("bad line \`$pl' in state incoming-recv");
360                 $thisheader = "<h2><a name=\"msg$xmessage\">"
361                         . "Message received at ".htmlsanit("$1\@$2")
362                         . ":</a></h2>\n";
363                 $this = '';
364                 $normstate= 'go';
365                 push @mail, $_;
366         } elsif ($normstate eq 'html') {
367                 $this .= $_;
368         } elsif ($normstate eq 'go') {
369                 s/^\030//;
370                 if (!$suppressnext && !$found_msgid &&
371                     /^Message-ID: <(.*)>/i) {
372                         my $msgid = $1;
373                         $found_msgid = 1;
374                         if ($seen_msgid{$msgid}) {
375                                 $suppressnext = 1;
376                         } else {
377                                 $seen_msgid{$msgid} = 1;
378                         }
379                 }
380                 if (@mail) {
381                         push @mail, $_;
382                 } else {
383                         $this .= htmlsanit($_);
384                 }
385         } elsif ($normstate eq 'go-nox') {
386                 next if !s/^X//;
387                 if (!$suppressnext && !$found_msgid &&
388                     /^Message-ID: <(.*)>/i) {
389                         my $msgid = $1;
390                         $found_msgid = 1;
391                         if ($seen_msgid{$msgid}) {
392                                 $suppressnext = 1;
393                         } else {
394                                 $seen_msgid{$msgid} = 1;
395                         }
396                 }
397                 if (@mail) {
398                         push @mail, $_;
399                 } else {
400                         $this .= htmlsanit($_);
401                 }
402         } elsif ($normstate eq 'recips') {
403                 if (m/^-t$/) {
404                         $thisheader = "<h2>Message sent:</h2>\n";
405                 } else {
406                         s/\04/, /g; s/\n$//;
407                         $thisheader = "<h2>Message sent to ".htmlsanit($_).":</h2>\n";
408                 }
409                 $this = "";
410                 $normstate= 'kill-body';
411         } elsif ($normstate eq 'autocheck') {
412                 next if !m/^X-Debian-Bugs(-\w+)?: This is an autoforward from (\S+)/;
413                 $normstate= 'autowait';
414                 $thisheader = "<h2>Message received at $2:</h2>\n";
415                 $this = '';
416                 push @mail, $_;
417         } elsif ($normstate eq 'autowait') {
418                 next if !m/^$/;
419                 $normstate= 'go-nox';
420         } else {
421                 &quitcgi("$ref state $normstate line \`$_'");
422         }
423 }
424 &quitcgi("$ref state $normstate at end") unless $normstate eq 'kill-end';
425 close(L);
426
427 if ( $mbox ) {
428         print "Content-Type: text/plain\n\n";
429         foreach ( @mails ) {
430                 my @lines = split( "\n", $_, -1 );
431                 if ( $lines[ 1 ] =~ m/^From / ) {
432                         my $tmp = $lines[ 0 ];
433                         $lines[ 0 ] = $lines[ 1 ];
434                         $lines[ 1 ] = $tmp;
435                 }
436                 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
437                         my $date = strftime "%a %b %d %T %Y", localtime;
438                         unshift @lines, "From unknown $date";
439                 }
440                 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
441                 $_ = join( "\n", @lines ) . "\n";
442         }
443         print join("", @mails );
444         exit 0;
445 }
446 print "Content-Type: text/html\n\n";
447
448 my $title = htmlsanit($status{subject});
449
450 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
451 print "<HTML><HEAD>\n" . 
452     "<TITLE>$debbugs::gProject $debbugs::gBug report logs - $short - $title</TITLE>\n" .
453     "</HEAD>\n" .
454     '<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#800080">' .
455     "\n";
456 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
457       "<BR>" . $title . "</H1>\n";
458
459 print "$descriptivehead\n";
460 printf "<p>View this report as an <a href=\"%s\">mbox folder</a>.</p>\n", mboxurl($ref);
461 print "<HR>";
462 print "$log";
463 print $tail_html;
464
465 print "</BODY></HTML>\n";
466
467 exit 0;