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