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