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