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