]> git.donarmstrong.com Git - debbugs.git/blob - cgi/bugreport.cgi
[project @ 2004-02-15 16:12:00 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
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 open L, "<$buglog" or &quitcgi("open log for $ref: $!");
243 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
244     $descriptivehead .= "\n<p>Bug is <strong>archived</strong>. No further changes may be made.</p>";
245 }
246
247 my $log='';
248
249 my $xmessage = 1;
250 my $suppressnext = 0;
251 my $found_msgid = 0;
252 my %seen_msgid = ();
253
254 my $thisheader = '';
255 my $this = '';
256
257 my $cmsg = 1;
258
259 my $normstate= 'kill-init';
260 my $linenum = 0;
261 my @mail = ();
262 my @mails = ();
263 while(my $line = <L>) {
264         $linenum++;
265         if ($line =~ m/^.$/ and 1 <= ord($line) && ord($line) <= 7) {
266                 # state transitions
267                 my $newstate;
268                 my $statenum = ord($line);
269
270                 $newstate = 'autocheck'     if ($statenum == 1);
271                 $newstate = 'recips'        if ($statenum == 2);
272                 $newstate = 'kill-end'      if ($statenum == 3);
273                 $newstate = 'go'            if ($statenum == 5);
274                 $newstate = 'html'          if ($statenum == 6);
275                 $newstate = 'incoming-recv' if ($statenum == 7);
276
277                 # disallowed transitions:
278                 $_ = "$normstate $newstate";
279                 unless (m/^(go|go-nox|html) kill-end$/
280                     || m/^(kill-init|kill-end) (incoming-recv|autocheck|recips|html)$/
281                     || m/^kill-body go$/)
282                 {
283                         &quitcgi("$ref: Transition from $normstate to $newstate at $linenum disallowed");
284                 }
285
286 #$this .= "\n<br>states: $normstate $newstate<br>\n";
287
288 #               if ($newstate eq 'go') {
289 #                       $this .= "<pre>\n";
290 #               }
291                 if ($newstate eq 'html') {
292                         $this = '';
293                 }
294
295                 if ($newstate eq 'kill-end') {
296
297                         my $show = 1;
298                         $show = $boring
299                                 if ($suppressnext && $normstate ne 'html');
300
301                         $show = ($xmessage == $msg) if ($msg);
302
303                         push @mails, join( '', @mail ) if ( $mbox && @mail );
304                         if ($show) {
305                                 if (not $mime and @mail) {
306                                         $this .= htmlsanit(join '', @mail);
307                                 } elsif (@mail) {
308                                         my $parser = new MIME::Parser;
309                                         $parser->tmp_to_core(1);
310                                         $parser->output_to_core(1);
311 #                                       $parser->output_under("/tmp");
312                                         my $entity = $parser->parse( new IO::Lines \@mail );
313                                         # TODO: make local subdir, clean it ourselves
314                                         # the following does NOT delete the msg dirs in /tmp
315                                         END { if ( $entity ) { $entity->purge; } if ( $parser ) { $parser->filer->purge; } }
316                                         my @attachments = ();
317                                         display_entity($entity, $ref, 1, $xmessage, $this, @attachments);
318                                 }
319 #                               if ($normstate eq 'go' || $normstate eq 'go-nox') {
320                                 if ($normstate ne 'html') {
321                                         $this = "<pre>\n$this</pre>\n";
322                                 }
323                                 if ($normstate eq 'html') {
324                                         $this = "<a name=\"msg$xmessage\">$this  <em><a href=\"" . bugurl($ref, "msg=$xmessage") . "\">Full text</a> available.</em></a>";
325                                 }
326                                 $this = "$thisheader$this" if $thisheader && !( $normstate eq 'html' );;
327                                 $thisheader = '';
328                                 my $delim = $terse ? "<p>" : "<hr>";
329                                 if ($reverse) {
330                                         $log = "$this\n$delim$log";
331                                 } else {
332                                         $log .= "$this\n$delim\n";
333                                 }
334                         }
335
336                         $xmessage++ if ($normstate ne 'html');
337
338                         $suppressnext = $normstate eq 'html';
339                         $found_msgid = 0;
340                 }
341                 
342                 $normstate = $newstate;
343                 @mail = ();
344                 next;
345         }
346
347         $_ = $line;
348         if ($normstate eq 'incoming-recv') {
349                 my $pl= $_;
350                 $pl =~ s/\n+$//;
351                 m/^Received: \(at (\S+)\) by (\S+)\;/
352                         || &quitcgi("bad line \`$pl' in state incoming-recv");
353                 $thisheader = "<h2><a name=\"msg$xmessage\">"
354                         . "Message received at ".htmlsanit("$1\@$2")
355                         . ":</a></h2>\n";
356                 $this = '';
357                 $normstate= 'go';
358                 push @mail, $_;
359         } elsif ($normstate eq 'html') {
360                 $this .= $_;
361         } elsif ($normstate eq 'go') {
362                 s/^\030//;
363                 if (!$suppressnext && !$found_msgid &&
364                     /^Message-ID: <(.*)>/i) {
365                         my $msgid = $1;
366                         $found_msgid = 1;
367                         if ($seen_msgid{$msgid}) {
368                                 $suppressnext = 1;
369                         } else {
370                                 $seen_msgid{$msgid} = 1;
371                         }
372                 }
373                 if (@mail) {
374                         push @mail, $_;
375                 } else {
376                         $this .= htmlsanit($_);
377                 }
378         } elsif ($normstate eq 'go-nox') {
379                 next if !s/^X//;
380                 if (!$suppressnext && !$found_msgid &&
381                     /^Message-ID: <(.*)>/i) {
382                         my $msgid = $1;
383                         $found_msgid = 1;
384                         if ($seen_msgid{$msgid}) {
385                                 $suppressnext = 1;
386                         } else {
387                                 $seen_msgid{$msgid} = 1;
388                         }
389                 }
390                 if (@mail) {
391                         push @mail, $_;
392                 } else {
393                         $this .= htmlsanit($_);
394                 }
395         } elsif ($normstate eq 'recips') {
396                 if (m/^-t$/) {
397                         $thisheader = "<h2>Message sent:</h2>\n";
398                 } else {
399                         s/\04/, /g; s/\n$//;
400                         $thisheader = "<h2>Message sent to ".htmlsanit($_).":</h2>\n";
401                 }
402                 $this = "";
403                 $normstate= 'kill-body';
404         } elsif ($normstate eq 'autocheck') {
405                 next if !m/^X-Debian-Bugs(-\w+)?: This is an autoforward from (\S+)/;
406                 $normstate= 'autowait';
407                 $thisheader = "<h2>Message received at $2:</h2>\n";
408                 $this = '';
409                 push @mail, $_;
410         } elsif ($normstate eq 'autowait') {
411                 next if !m/^$/;
412                 $normstate= 'go-nox';
413         } else {
414                 &quitcgi("$ref state $normstate line \`$_'");
415         }
416 }
417 &quitcgi("$ref state $normstate at end") unless $normstate eq 'kill-end';
418 close(L);
419
420 if ( $mbox ) {
421         print "Content-Type: text/plain\n\n";
422         foreach ( @mails ) {
423                 my @lines = split( "\n", $_, -1 );
424                 if ( $lines[ 1 ] =~ m/^From / ) {
425                         my $tmp = $lines[ 0 ];
426                         $lines[ 0 ] = $lines[ 1 ];
427                         $lines[ 1 ] = $tmp;
428                 }
429                 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
430                         my $date = strftime "%a %b %d %T %Y", localtime;
431                         unshift @lines, "From unknown $date";
432                 }
433                 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
434                 $_ = join( "\n", @lines ) . "\n";
435         }
436         print join("", @mails );
437         exit 0;
438 }
439 print "Content-Type: text/html\n\n";
440
441 my $title = htmlsanit($status{subject});
442
443 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
444 print "<HTML><HEAD>\n" . 
445     "<TITLE>$debbugs::gProject $debbugs::gBug report logs - $short - $title</TITLE>\n" .
446     "</HEAD>\n" .
447     '<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#800080">' .
448     "\n";
449 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
450       "<BR>" . $title . "</H1>\n";
451
452 print "$descriptivehead\n";
453 printf "<p>View this report as an <a href=\"%s\">mbox folder</a>.</p>\n", mboxurl($ref);
454 print "<HR>";
455 print "$log";
456 print $tail_html;
457
458 print "</BODY></HTML>\n";
459
460 exit 0;