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