]> git.donarmstrong.com Git - debbugs.git/blob - cgi/bugreport.cgi
[project @ 2003-05-21 20:59:02 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
35 # Not used by this script directly, but fetch these so that pkgurl() and
36 # friends can propagate them correctly.
37 my $archive = ($param{'archive'} || 'no') eq 'yes';
38 my $repeatmerged = ($param{'repeatmerged'} || 'yes') eq 'yes';
39 set_option('archive', $archive);
40 set_option('repeatmerged', $repeatmerged);
41
42 my $buglog = buglog($ref);
43
44 if ($ENV{REQUEST_METHOD} eq 'HEAD' and not defined($att) and not $mbox) {
45     print "Content-Type: text/html\n";
46     my @stat = stat $buglog;
47     if (@stat) {
48         my $mtime = strftime '%a, %d %b %Y %T GMT', gmtime($stat[9]);
49         print "Last-Modified: $mtime\n";
50     }
51     print "\n";
52     exit 0;
53 }
54
55 my %maintainer = %{getmaintainers()};
56 my %pkgsrc = %{getpkgsrc()};
57
58 my $indexentry;
59 my $descriptivehead;
60 my $showseverity;
61
62 my $tpack;
63 my $tmain;
64
65 $ENV{"TZ"} = 'UTC';
66 tzset();
67
68 my $dtime = strftime "%a, %e %b %Y %T UTC", localtime;
69 $tail_html = $debbugs::gHTMLTail;
70 $tail_html =~ s/SUBSTITUTE_DTIME/$dtime/;
71
72 my %status = %{getbugstatus($ref)};
73 unless (%status) {
74     print <<EOF;
75 Content-Type: text/html
76
77 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
78 <html>
79 <head><title>$debbugs::gProject $debbugs::gBug report logs - $short</title></head>
80 <body>
81 <h1>$debbugs::gProject $debbugs::gBug report logs - $short</h1>
82 <p>There is no record of $debbugs::gBug $short.
83 Try the <a href="http://$gWebDomain/">search page</a> instead.</p>
84 $tail_html</body></html>
85 EOF
86     exit 0;
87 }
88
89 $|=1;
90
91 $tpack = lc $status{'package'};
92 my @tpacks = splitpackages($tpack);
93
94 if  ($status{severity} eq 'normal') {
95         $showseverity = '';
96 #} elsif (grep($status{severity} eq $_, @strongseverities)) {
97 #       $showseverity = "<strong>Severity: $status{severity}</strong>;\n";
98 } else {
99         $showseverity = "Severity: <em>$status{severity}</em>;\n";
100 }
101
102 $indexentry .= "<p>$showseverity";
103 $indexentry .= htmlpackagelinks($status{package}, 0);
104
105 $indexentry .= "Reported by: <a href=\"" . submitterurl($status{originator})
106               . "\">" . htmlsanit($status{originator}) . "</a>;\n";
107
108 my $dummy = strftime "%a, %e %b %Y %T UTC", localtime($status{date});
109 $indexentry .= "Date: ".$dummy.";\n<br>";
110
111 my @descstates;
112
113 push @descstates, "Tags: <strong>"
114                 . htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
115                 . "</strong>"
116                         if length($status{tags});
117
118 my @merged= split(/ /,$status{mergedwith});
119 if (@merged) {
120         my $descmerged = 'merged with ';
121         my $mseparator = '';
122         for my $m (@merged) {
123                 $descmerged .= $mseparator."<a href=\"" . bugurl($m) . "\">#$m</a>";
124                 $mseparator= ",\n";
125         }
126         push @descstates, $descmerged;
127 }
128
129 if (length($status{done})) {
130         push @descstates, "<strong>Done:</strong> ".htmlsanit($status{done});
131 } elsif (length($status{forwarded})) {
132         push @descstates, "<strong>Forwarded</strong> to ".maybelink($status{forwarded});
133 }
134
135 $indexentry .= join(";\n", @descstates) . ";\n<br>" if @descstates;
136
137 $descriptivehead = $indexentry;
138 foreach my $pkg (@tpacks) {
139     my $tmaint = defined($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)';
140     my $tsrc = defined($pkgsrc{$pkg}) ? $pkgsrc{$pkg} : '(unknown)';
141
142     $descriptivehead .= "Maintainer for $pkg is\n".
143             '<a href="'.mainturl($tmaint).'">'.htmlsanit($tmaint).'</a>';
144     $descriptivehead .= ";\nSource for $pkg is\n".
145             '<a href="'.srcurl($tsrc)."\">$tsrc</a>" if ($tsrc ne "(unknown)");
146     $descriptivehead .= ".\n<br>";
147 }
148
149 open L, "<$buglog" or &quitcgi("open log for $ref: $!");
150 if ($buglog !~ m#^\Q$gSpoolDir/db-h/#) {
151     $descriptivehead .= "\n<p>Bug is <strong>archived</strong>. No further changes may be made.</p>";
152 }
153
154 my $log='';
155
156 my $xmessage = 1;
157 my $suppressnext = 0;
158 my $found_msgid = 0;
159 my %seen_msgid = ();
160
161 my $thisheader = '';
162 my $this = '';
163
164 my $cmsg = 1;
165
166 my $normstate= 'kill-init';
167 my $linenum = 0;
168 my @mail = ();
169 my @mails = ();
170 while(my $line = <L>) {
171         $linenum++;
172         if ($line =~ m/^.$/ and 1 <= ord($line) && ord($line) <= 7) {
173                 # state transitions
174                 my $newstate;
175                 my $statenum = ord($line);
176
177                 $newstate = 'autocheck'     if ($statenum == 1);
178                 $newstate = 'recips'        if ($statenum == 2);
179                 $newstate = 'kill-end'      if ($statenum == 3);
180                 $newstate = 'go'            if ($statenum == 5);
181                 $newstate = 'html'          if ($statenum == 6);
182                 $newstate = 'incoming-recv' if ($statenum == 7);
183
184                 # disallowed transitions:
185                 $_ = "$normstate $newstate";
186                 unless (m/^(go|go-nox|html) kill-end$/
187                     || m/^(kill-init|kill-end) (incoming-recv|autocheck|recips|html)$/
188                     || m/^kill-body go$/)
189                 {
190                         &quitcgi("$ref: Transition from $normstate to $newstate at $linenum disallowed");
191                 }
192
193 #$this .= "\n<br>states: $normstate $newstate<br>\n";
194
195 #               if ($newstate eq 'go') {
196 #                       $this .= "<pre>\n";
197 #               }
198                 if ($newstate eq 'html') {
199                         $this = '';
200                 }
201
202                 if ($newstate eq 'kill-end') {
203
204                         my $show = 1;
205                         $show = $boring
206                                 if ($suppressnext && $normstate ne 'html');
207
208                         $show = ($xmessage == $msg) if ($msg);
209
210                         push @mails, join( '', @mail ) if ( $mbox && @mail );
211                         if ($show) {
212                                 my $downloadHtml = '';
213                                 if (@mail) {
214                                         my $parser = new MIME::Parser;
215                                         $parser->tmp_to_core(1);
216                                         $parser->output_to_core(1);
217 #                                       $parser->output_under("/tmp");
218                                         my $entity = $parser->parse( new IO::Lines \@mail );
219                                         # TODO: make local subdir, clean it outselves
220                                         # the following does NOT delete the msg dirs in /tmp
221                                         END { if ( $entity ) { $entity->purge; } if ( $parser ) { $parser->filer->purge; } }
222                                         my @attachments = ();
223                                         if ( $entity->is_multipart ) {
224                                                 my @parts = $entity->parts_DFS;
225 #                                               $this .= htmlsanit($entity->head->stringify);
226                                                 my @keep = ();
227                                                 foreach ( @parts ) {
228                                                         my $head = $_->head;
229 #                                                       $head->mime_attr("content-transfer-encoding" => "8bit")
230 #                                                               if !$head->mime_attr("content-transfer-encoding");
231                                                         my ($disposition,$type) = (
232                                                                 $head->mime_attr("content-disposition"),
233                                                                 lc $head->mime_attr("content-type")
234                                                                 );
235                                                         
236 #print STDERR "'$type' '$disposition'\n";
237                                                         if ($disposition && ( $disposition eq "attachment" || $disposition eq "inline" ) && $_->head->recommended_filename ) {
238                                                                 push @attachments, $_;
239                                                                 my $file = $_->head->recommended_filename;
240                                                                 $downloadHtml .= "View Attachment: <a href=\"".dlurl($ref,"msg=$xmessage","att=$#attachments","filename=$file")."\">$file</a>\n";
241                                                                 if ($msg && defined($att) && $att eq $#attachments) {
242                                                                         my $head = $_->head;
243                                                                         my $type;
244                                                                         chomp($type = $head->mime_attr("content-type"));
245                                                                         my $body = $_->stringify_body;
246                                                                         print "Content-Type: $type; name=$file\n\n";
247                                                                         my $decoder = new MIME::Decoder($head->mime_encoding);
248                                                                         $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
249                                                                         exit(0);
250                                                                 }
251                                                                 if ($type eq 'text/plain') {
252 #                                                                       push @keep, $_;
253                                                                 }
254 #                                                               $this .= htmlsanit($_->head->stringify);
255                                                         } else {
256 #                                                               $this .= htmlsanit($_->head->stringify);
257 #                                                               push @keep, $_;
258                                                         }
259 #                                                       $this .= "\n" . htmlsanit($_->stringify_body);
260                                                 }
261 #                                               $entity->parts(\@keep) if (!$msg);
262                                         }
263                                         $this .= htmlsanit($entity->stringify);
264                                 }
265                                 $this = "$downloadHtml\n$this$downloadHtml" if $downloadHtml;
266 #                               if ($normstate eq 'go' || $normstate eq 'go-nox') {
267                                 if ($normstate ne 'html') {
268                                         $this = "<pre>\n$this</pre>\n";
269                                 }
270                                 if ($normstate eq 'html') {
271                                         $this .= "  <em><a href=\"" . bugurl($ref, "msg=$xmessage") . "\">Full text</a> available.</em>";
272                                 }
273                                 $this = "$thisheader$this" if $thisheader && !( $normstate eq 'html' );;
274                                 $this = "$downloadHtml" if ($terse && $normstate ne 'html');
275                                 $downloadHtml = '';
276                                 $thisheader = '';
277                                 my $delim = $terse ? "<p>" : "<hr>";
278                                 if ($reverse) {
279                                         $log = "$this\n$delim$log";
280                                 } else {
281                                         $log .= "$this\n$delim\n";
282                                 }
283                         }
284
285                         $xmessage++ if ($normstate ne 'html');
286
287                         $suppressnext = $normstate eq 'html';
288                         $found_msgid = 0;
289                 }
290                 
291                 $normstate = $newstate;
292                 @mail = ();
293                 next;
294         }
295
296         $_ = $line;
297         if ($normstate eq 'incoming-recv') {
298                 my $pl= $_;
299                 $pl =~ s/\n+$//;
300                 m/^Received: \(at (\S+)\) by (\S+)\;/
301                         || &quitcgi("bad line \`$pl' in state incoming-recv");
302                 $thisheader = "<h2>Message received at ".htmlsanit("$1\@$2")
303                         . ":</h2>\n";
304                 $this = '';
305                 $normstate= 'go';
306                 push @mail, $_;
307         } elsif ($normstate eq 'html') {
308                 $this .= $_;
309         } elsif ($normstate eq 'go') {
310                 s/^\030//;
311                 if (!$suppressnext && !$found_msgid &&
312                     /^Message-ID: <(.*)>/i) {
313                         my $msgid = $1;
314                         $found_msgid = 1;
315                         if ($seen_msgid{$msgid}) {
316                                 $suppressnext = 1;
317                         } else {
318                                 $seen_msgid{$msgid} = 1;
319                         }
320                 }
321                 if (@mail) {
322                         push @mail, $_;
323                 } else {
324                         $this .= htmlsanit($_);
325                 }
326         } elsif ($normstate eq 'go-nox') {
327                 next if !s/^X//;
328                 if (!$suppressnext && !$found_msgid &&
329                     /^Message-ID: <(.*)>/i) {
330                         my $msgid = $1;
331                         $found_msgid = 1;
332                         if ($seen_msgid{$msgid}) {
333                                 $suppressnext = 1;
334                         } else {
335                                 $seen_msgid{$msgid} = 1;
336                         }
337                 }
338                 if (@mail) {
339                         push @mail, $_;
340                 } else {
341                         $this .= htmlsanit($_);
342                 }
343         } elsif ($normstate eq 'recips') {
344                 if (m/^-t$/) {
345                         $thisheader = "<h2>Message sent:</h2>\n";
346                 } else {
347                         s/\04/, /g; s/\n$//;
348                         $thisheader = "<h2>Message sent to ".htmlsanit($_).":</h2>\n";
349                 }
350                 $this = "";
351                 $normstate= 'kill-body';
352         } elsif ($normstate eq 'autocheck') {
353                 next if !m/^X-Debian-Bugs(-\w+)?: This is an autoforward from (\S+)/;
354                 $normstate= 'autowait';
355                 $thisheader = "<h2>Message received at $2:</h2>\n";
356                 $this = '';
357                 push @mail, $_;
358         } elsif ($normstate eq 'autowait') {
359                 next if !m/^$/;
360                 $normstate= 'go-nox';
361         } else {
362                 &quitcgi("$ref state $normstate line \`$_'");
363         }
364 }
365 &quitcgi("$ref state $normstate at end") unless $normstate eq 'kill-end';
366 close(L);
367
368 if ( $mbox ) {
369         print "Content-Type: text/plain\n\n";
370         foreach ( @mails ) {
371                 my @lines = split( "\n", $_, -1 );
372                 if ( $lines[ 1 ] =~ m/^From / ) {
373                         my $tmp = $lines[ 0 ];
374                         $lines[ 0 ] = $lines[ 1 ];
375                         $lines[ 1 ] = $tmp;
376                 }
377                 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
378                         my $date = strftime "%a %b %d %T %Y", localtime;
379                         unshift @lines, "From unknown $date";
380                 }
381                 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
382                 $_ = join( "\n", @lines ) . "\n";
383         }
384         print join("", @mails );
385         exit 0;
386 }
387 print "Content-Type: text/html\n\n";
388
389 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
390 print "<HTML><HEAD>\n" . 
391     "<TITLE>$debbugs::gProject $debbugs::gBug report logs - $short</TITLE>\n" .
392     "</HEAD>\n" .
393     '<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#800080">' .
394     "\n";
395 print "<H1>" .  "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
396       "<BR>" . htmlsanit($status{subject}) . "</H1>\n";
397
398 print "$descriptivehead\n";
399 printf "<p>View this report as an <a href=\"%s\">mbox folder</a>.</p>\n", mboxurl($ref);
400 print "<HR>";
401 print "$log";
402 print $tail_html;
403
404 print "</BODY></HTML>\n";
405
406 exit 0;