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