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