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