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