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