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