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