]> git.donarmstrong.com Git - debbugs.git/blob - cgi/bugreport.cgi
[project @ 2002-09-07 20:47:50 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
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 $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'} || "0";
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
105 my $log='';
106
107 my $xmessage = 1;
108 my $suppressnext = 0;
109
110 my $thisheader = '';
111 my $this = '';
112
113 my $cmsg = 1;
114
115 my $normstate= 'kill-init';
116 my $linenum = 0;
117 my $mail = '';
118 my @mails = ();
119 while(my $line = <L>) {
120         $linenum++;
121         if ($line =~ m/^.$/ and 1 <= ord($line) && ord($line) <= 7) {
122                 # state transitions
123                 my $newstate;
124                 my $statenum = ord($line);
125
126                 $newstate = 'autocheck'     if ($statenum == 1);
127                 $newstate = 'recips'        if ($statenum == 2);
128                 $newstate = 'kill-end'      if ($statenum == 3);
129                 $newstate = 'go'            if ($statenum == 5);
130                 $newstate = 'html'          if ($statenum == 6);
131                 $newstate = 'incoming-recv' if ($statenum == 7);
132
133                 # disallowed transitions:
134                 $_ = "$normstate $newstate";
135                 unless (m/^(go|go-nox|html) kill-end$/
136                     || m/^(kill-init|kill-end) (incoming-recv|autocheck|recips|html)$/
137                     || m/^kill-body go$/)
138                 {
139                         &quit("$ref: Transition from $normstate to $newstate at $linenum disallowed");
140                 }
141
142                 if ($newstate eq 'go') {
143                         $this .= "<pre>\n";
144                 }
145
146                 if ($newstate eq 'html') {
147                         $this = '';
148                 }
149
150                 if ($newstate eq 'kill-end') {
151
152                         my $show = 1;
153                         $show = $boring
154                                 if ($suppressnext && $normstate ne 'html');
155
156                         $show = ($xmessage == $msg) if ($msg);
157
158                         push @mails, $mail if ( $mbox && $mail );
159                         if ($show) {
160                                 my $downloadHtml = '';
161                                 if ($mail) {
162                                         my $parser = new MIME::Parser;
163                                         $parser->tmp_to_core(1);
164                                         $parser->output_to_core(1);
165 #                                       $parser->output_under("/tmp");
166                                         my $entity = $parser->parse_data($mail);
167                                         # TODO: make local subdir, clean it outselves
168                                         # the following does NOT delete the msg dirs in /tmp
169                                         END { $entity->purge; $parser->filer->purge; }
170                                         my @attachments = ();
171                                         if ( $entity->is_multipart ) {
172                                                 my @parts = $entity->parts_DFS;
173 #                                               $this .= htmlsanit($entity->head->stringify);
174                                                 my @keep = ();
175                                                 foreach ( @parts ) {
176                                                         my $head = $_->head;
177 #                                                       $head->mime_attr("content-transfer-encoding" => "8bit")
178 #                                                               if !$head->mime_attr("content-transfer-encoding");
179                                                         my ($disposition,$type) = (
180                                                                 $head->mime_attr("content-disposition"),
181                                                                 lc $head->mime_attr("content-type")
182                                                                 );
183                                                         
184 #print STDERR "'$type' '$disposition'\n";
185                                                         if ($disposition && ( $disposition eq "attachment" || $disposition eq "inline" ) && $_->head->recommended_filename ) {
186                                                                 push @attachments, $_;
187                                                                 my $file = $_->head->recommended_filename;
188                                                                 $downloadHtml .= "View Attachment: <a href=\"".dlurl($ref,"msg=$xmessage","att=$#attachments","filename=$file")."\">$file</a>\n";
189                                                                 if ($msg && $att eq $#attachments) {
190                                                                         my $head = $_->head;
191                                                                         my $type;
192                                                                         chomp($type = $head->mime_attr("content-type"));
193                                                                         my $body = $_->stringify_body;
194                                                                         print "Content-Type: $type; name=$file\n\n";
195                                                                         my $decoder = new MIME::Decoder($head->mime_encoding);
196                                                                         $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
197                                                                         exit(0);
198                                                                 }
199                                                                 if ($type eq 'text/plain') {
200 #                                                                       push @keep, $_;
201                                                                 }
202 #                                                               $this .= htmlsanit($_->head->stringify);
203                                                         } else {
204 #                                                               $this .= htmlsanit($_->head->stringify);
205 #                                                               push @keep, $_;
206                                                         }
207 #                                                       $this .= "\n" . htmlsanit($_->stringify_body);
208                                                 }
209 #                                               $entity->parts(\@keep) if (!$msg);
210                                         }
211                                         $this .= htmlsanit($entity->stringify);
212                                 }
213                                 $this = "$downloadHtml\n$this$downloadHtml" if $downloadHtml;
214                                 $downloadHtml = '';
215                                 $this = "<pre>\n$this</pre>\n"
216                                         if $normstate eq 'go' || $normstate eq 'go-nox';
217                                 $this = "$thisheader$this" if $thisheader && !( $normstate eq 'html' );;
218                                 $thisheader = '';
219                                 if ($normstate eq 'html') {
220                                         $this .= "  <em><A href=\"" . bugurl($ref, "msg=$xmessage") . "\">Full text</A> available.</em>";
221                                 }
222                                 if ($reverse) {
223                                         $log = "$this\n<hr>$log";
224                                 } else {
225                                         $log .= "$this\n<hr>\n";
226                                 }
227                         }
228
229                         $xmessage++ if ($normstate ne 'html');
230
231                         $suppressnext = $normstate eq 'html';
232                 }
233                 
234                 $normstate = $newstate;
235                 $mail = '';
236                 next;
237         }
238
239         $_ = $line;
240         if ($normstate eq 'incoming-recv') {
241                 my $pl= $_;
242                 $pl =~ s/\n+$//;
243                 m/^Received: \(at (\S+)\) by (\S+)\;/
244                         || &quit("bad line \`$pl' in state incoming-recv");
245                 $thisheader = "<h2>Message received at ".htmlsanit("$1\@$2")
246                         . ":</h2><br>\n";
247                 $this = '';
248                 $normstate= 'go';
249                 $mail .= $_;
250         } elsif ($normstate eq 'html') {
251                 $this .= $_;
252         } elsif ($normstate eq 'go') {
253                 if ($mail) {
254                         $mail .= $_;
255                 } else {
256                         $this .= htmlsanit($_);
257                 }
258         } elsif ($normstate eq 'go-nox') {
259                 next if !s/^X//;
260                 if ($mail) {
261                         $mail .= $_;
262                 } else {
263                         $this .= htmlsanit($_);
264                 }
265         } elsif ($normstate eq 'recips') {
266                 if (m/^-t$/) {
267                         $this = "<h2>Message sent:</h2><br>\n";
268                 } else {
269                         s/\04/, /g; s/\n$//;
270                         $this = "<h2>Message sent to ".htmlsanit($_).":</h2><br>\n";
271                 }
272                 $normstate= 'kill-body';
273         } elsif ($normstate eq 'autocheck') {
274                 next if !m/^X-Debian-Bugs(-\w+)?: This is an autoforward from (\S+)/;
275                 $normstate= 'autowait';
276                 $this = "<h2>Message received at $2:</h2><br>\n";
277         } elsif ($normstate eq 'autowait') {
278                 next if !m/^$/;
279                 $normstate= 'go-nox';
280                 $this .= "<pre>\n";
281         } else {
282                 &quit("$ref state $normstate line \`$_'");
283         }
284 }
285 &quit("$ref state $normstate at end") unless $normstate eq 'kill-end';
286 close(L);
287
288 if ( $mbox ) {
289         print "Content-Type: text/plain\n\n";
290         foreach ( @mails ) {
291                 my @lines = split( "\n", $_, -1 );
292                 if ( $lines[ 1 ] =~ m/^From / ) {
293                         my $tmp = $lines[ 0 ];
294                         $lines[ 0 ] = $lines[ 1 ];
295                         $lines[ 1 ] = $tmp;
296                         $_ = join( "\n", @lines ) . "\n";
297                 }
298                 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
299                         $ENV{ PATH } = "/bin:/usr/bin:/usr/local/bin";
300                         my $date = `date "+%a %b %d %T %Y"`;
301                         chomp $date;
302                         $_ = "From unknown $date\n" . $_;
303                 }
304         }
305         print join("", @mails );
306         exit 0;
307 }
308 print "Content-Type: text/html\n\n";
309
310 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
311 print "<HTML><HEAD>\n" . 
312     "<TITLE>$debbugs::gProject $debbugs::gBug report logs - $short</TITLE>\n" .
313     "</HEAD>\n" .
314     '<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#800080">' .
315     "\n";
316 print "<H1>" .  "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@bugs.debian.org\">$short</A>" .
317       "<BR>" . htmlsanit($status{subject}) . "</H1>\n";
318
319 print "$descriptivehead\n";
320 printf "<p><a href=\"%s\">View</a> this report as an mbox folder.</p>", mboxurl($ref);
321 print "<HR>";
322 print "$log";
323 print $tail_html;
324
325 print "</BODY></HTML>\n";
326
327 exit 0;