]> git.donarmstrong.com Git - debbugs.git/blob - cgi/bugreport.cgi
[project @ 2003-05-03 20:42:15 by doogie]
[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
152 my $thisheader = '';
153 my $this = '';
154
155 my $cmsg = 1;
156
157 my $normstate= 'kill-init';
158 my $linenum = 0;
159 my @mail = ();
160 my @mails = ();
161 while(my $line = <L>) {
162         $linenum++;
163         if ($line =~ m/^.$/ and 1 <= ord($line) && ord($line) <= 7) {
164                 # state transitions
165                 my $newstate;
166                 my $statenum = ord($line);
167
168                 $newstate = 'autocheck'     if ($statenum == 1);
169                 $newstate = 'recips'        if ($statenum == 2);
170                 $newstate = 'kill-end'      if ($statenum == 3);
171                 $newstate = 'go'            if ($statenum == 5);
172                 $newstate = 'html'          if ($statenum == 6);
173                 $newstate = 'incoming-recv' if ($statenum == 7);
174
175                 # disallowed transitions:
176                 $_ = "$normstate $newstate";
177                 unless (m/^(go|go-nox|html) kill-end$/
178                     || m/^(kill-init|kill-end) (incoming-recv|autocheck|recips|html)$/
179                     || m/^kill-body go$/)
180                 {
181                         &quitcgi("$ref: Transition from $normstate to $newstate at $linenum disallowed");
182                 }
183
184 #$this .= "\n<br>states: $normstate $newstate<br>\n";
185
186 #               if ($newstate eq 'go') {
187 #                       $this .= "<pre>\n";
188 #               }
189                 if ($newstate eq 'html') {
190                         $this = '';
191                 }
192
193                 if ($newstate eq 'kill-end') {
194
195                         my $show = 1;
196                         $show = $boring
197                                 if ($suppressnext && $normstate ne 'html');
198
199                         $show = ($xmessage == $msg) if ($msg);
200
201                         push @mails, join( '', @mail ) if ( $mbox && @mail );
202                         if ($show) {
203                                 my $downloadHtml = '';
204                                 if (@mail) {
205                                         my $parser = new MIME::Parser;
206                                         $parser->tmp_to_core(1);
207                                         $parser->output_to_core(1);
208 #                                       $parser->output_under("/tmp");
209                                         my $entity = $parser->parse( new IO::Lines \@mail );
210                                         # TODO: make local subdir, clean it outselves
211                                         # the following does NOT delete the msg dirs in /tmp
212                                         END { if ( $entity ) { $entity->purge; } if ( $parser ) { $parser->filer->purge; } }
213                                         my @attachments = ();
214                                         if ( $entity->is_multipart ) {
215                                                 my @parts = $entity->parts_DFS;
216 #                                               $this .= htmlsanit($entity->head->stringify);
217                                                 my @keep = ();
218                                                 foreach ( @parts ) {
219                                                         my $head = $_->head;
220 #                                                       $head->mime_attr("content-transfer-encoding" => "8bit")
221 #                                                               if !$head->mime_attr("content-transfer-encoding");
222                                                         my ($disposition,$type) = (
223                                                                 $head->mime_attr("content-disposition"),
224                                                                 lc $head->mime_attr("content-type")
225                                                                 );
226                                                         
227 #print STDERR "'$type' '$disposition'\n";
228                                                         if ($disposition && ( $disposition eq "attachment" || $disposition eq "inline" ) && $_->head->recommended_filename ) {
229                                                                 push @attachments, $_;
230                                                                 my $file = $_->head->recommended_filename;
231                                                                 $downloadHtml .= "View Attachment: <a href=\"".dlurl($ref,"msg=$xmessage","att=$#attachments","filename=$file")."\">$file</a>\n";
232                                                                 if ($msg && defined($att) && $att eq $#attachments) {
233                                                                         my $head = $_->head;
234                                                                         my $type;
235                                                                         chomp($type = $head->mime_attr("content-type"));
236                                                                         my $body = $_->stringify_body;
237                                                                         print "Content-Type: $type; name=$file\n\n";
238                                                                         my $decoder = new MIME::Decoder($head->mime_encoding);
239                                                                         $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
240                                                                         exit(0);
241                                                                 }
242                                                                 if ($type eq 'text/plain') {
243 #                                                                       push @keep, $_;
244                                                                 }
245 #                                                               $this .= htmlsanit($_->head->stringify);
246                                                         } else {
247 #                                                               $this .= htmlsanit($_->head->stringify);
248 #                                                               push @keep, $_;
249                                                         }
250 #                                                       $this .= "\n" . htmlsanit($_->stringify_body);
251                                                 }
252 #                                               $entity->parts(\@keep) if (!$msg);
253                                         }
254                                         $this .= htmlsanit($entity->stringify);
255                                 }
256                                 $this = "$downloadHtml\n$this$downloadHtml" if $downloadHtml;
257 #                               if ($normstate eq 'go' || $normstate eq 'go-nox') {
258                                 if ($normstate ne 'html') {
259                                         $this = "<pre>\n$this</pre>\n";
260                                 }
261                                 if ($normstate eq 'html') {
262                                         $this .= "  <em><a href=\"" . bugurl($ref, "msg=$xmessage") . "\">Full text</a> available.</em>";
263                                 }
264                                 $this = "$thisheader$this" if $thisheader && !( $normstate eq 'html' );;
265                                 $this = "$downloadHtml" if ($terse && $normstate ne 'html');
266                                 $downloadHtml = '';
267                                 $thisheader = '';
268                                 my $delim = $terse ? "<p>" : "<hr>";
269                                 if ($reverse) {
270                                         $log = "$this\n$delim$log";
271                                 } else {
272                                         $log .= "$this\n$delim\n";
273                                 }
274                         }
275
276                         $xmessage++ if ($normstate ne 'html');
277
278                         $suppressnext = $normstate eq 'html';
279                 }
280                 
281                 $normstate = $newstate;
282                 @mail = ();
283                 next;
284         }
285
286         $_ = $line;
287         if ($normstate eq 'incoming-recv') {
288                 my $pl= $_;
289                 $pl =~ s/\n+$//;
290                 m/^Received: \(at (\S+)\) by (\S+)\;/
291                         || &quitcgi("bad line \`$pl' in state incoming-recv");
292                 $thisheader = "<h2>Message received at ".htmlsanit("$1\@$2")
293                         . ":</h2>\n";
294                 $this = '';
295                 $normstate= 'go';
296                 push @mail, $_;
297         } elsif ($normstate eq 'html') {
298                 $this .= $_;
299         } elsif ($normstate eq 'go') {
300                 s/^\030//;
301                 if (@mail) {
302                         push @mail, $_;
303                 } else {
304                         $this .= htmlsanit($_);
305                 }
306         } elsif ($normstate eq 'go-nox') {
307                 next if !s/^X//;
308                 if (@mail) {
309                         push @mail, $_;
310                 } else {
311                         $this .= htmlsanit($_);
312                 }
313         } elsif ($normstate eq 'recips') {
314                 if (m/^-t$/) {
315                         $thisheader = "<h2>Message sent:</h2>\n";
316                 } else {
317                         s/\04/, /g; s/\n$//;
318                         $thisheader = "<h2>Message sent to ".htmlsanit($_).":</h2>\n";
319                 }
320                 $this = "";
321                 $normstate= 'kill-body';
322         } elsif ($normstate eq 'autocheck') {
323                 next if !m/^X-Debian-Bugs(-\w+)?: This is an autoforward from (\S+)/;
324                 $normstate= 'autowait';
325                 $thisheader = "<h2>Message received at $2:</h2>\n";
326                 $this = '';
327                 push @mail, $_;
328         } elsif ($normstate eq 'autowait') {
329                 next if !m/^$/;
330                 $normstate= 'go-nox';
331         } else {
332                 &quitcgi("$ref state $normstate line \`$_'");
333         }
334 }
335 &quitcgi("$ref state $normstate at end") unless $normstate eq 'kill-end';
336 close(L);
337
338 if ( $mbox ) {
339         print "Content-Type: text/plain\n\n";
340         foreach ( @mails ) {
341                 my @lines = split( "\n", $_, -1 );
342                 if ( $lines[ 1 ] =~ m/^From / ) {
343                         my $tmp = $lines[ 0 ];
344                         $lines[ 0 ] = $lines[ 1 ];
345                         $lines[ 1 ] = $tmp;
346                 }
347                 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
348                         my $date = strftime "%a %b %d %T %Y", localtime;
349                         unshift @lines, "From unknown $date";
350                 }
351                 map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
352                 $_ = join( "\n", @lines ) . "\n";
353         }
354         print join("", @mails );
355         exit 0;
356 }
357 print "Content-Type: text/html\n\n";
358
359 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
360 print "<HTML><HEAD>\n" . 
361     "<TITLE>$debbugs::gProject $debbugs::gBug report logs - $short</TITLE>\n" .
362     "</HEAD>\n" .
363     '<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#800080">' .
364     "\n";
365 print "<H1>" .  "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
366       "<BR>" . htmlsanit($status{subject}) . "</H1>\n";
367
368 print "$descriptivehead\n";
369 printf "<p>View this report as an <a href=\"%s\">mbox folder</a>.</p>\n", mboxurl($ref);
370 print "<HR>";
371 print "$log";
372 print $tail_html;
373
374 print "</BODY></HTML>\n";
375
376 exit 0;