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