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