]> git.donarmstrong.com Git - debbugs.git/blob - cgi/bugreport.cgi
Merge branch 'database'
[debbugs.git] / cgi / bugreport.cgi
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 # Sanitize environent for taint
7 BEGIN{
8     delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
9 }
10
11
12 use POSIX qw(strftime);
13 use MIME::Parser;
14 use MIME::Decoder;
15 use IO::Scalar;
16 use IO::File;
17
18 use Debbugs::Config qw(:globals :text :config);
19
20 # for read_log_records
21 use Debbugs::Log qw(:read);
22 use Debbugs::Log::Spam;
23 use Debbugs::CGI qw(:url :html :util :cache :usertags);
24 use Debbugs::CGI::Bugreport qw(:all);
25 use Debbugs::Common qw(buglog getmaintainers make_list bug_status);
26 use Debbugs::Packages qw(getpkgsrc);
27 use Debbugs::DB;
28 use Debbugs::Status qw(splitpackages split_status_fields get_bug_status isstrongseverity);
29
30 use Scalar::Util qw(looks_like_number);
31
32 use Debbugs::Text qw(:templates);
33 use URI::Escape qw(uri_escape_utf8);
34 use List::AllUtils qw(max);
35
36 my $s;
37 if (defined $config{database}) {
38     $s = Debbugs::DB->connect($config{database}) or
39         die "Unable to connect to database";
40 }
41
42 use CGI::Simple;
43 my $q = new CGI::Simple;
44 # STDOUT should be using the utf8 io layer
45 binmode(STDOUT,':raw:encoding(UTF-8)');
46
47 my %param = cgi_parameters(query => $q,
48                            single => [qw(bug msg att boring terse),
49                                       qw(reverse mbox mime trim),
50                                       qw(mboxstat mboxmaint archive),
51                                       qw(repeatmerged avatars),
52                                      ],
53                            default => {# msg       => '',
54                                        boring    => 'no',
55                                        terse     => 'no',
56                                        reverse   => 'no',
57                                        mbox      => 'no',
58                                        mime      => 'no',
59                                        mboxstat  => 'no',
60                                        mboxmaint => 'no',
61                                        archive   => 'no',
62                                        repeatmerged => 'yes',
63                                        avatars   => 'yes',
64                                       },
65                           );
66 # This is craptacular.
67
68 my $ref = $param{bug} or quitcgi("No bug number", '400 Bad Request');
69 $ref =~ /(\d+)/ or quitcgi("Invalid bug number", '400 Bad Request');
70 $ref = $1;
71 my $short = "#$ref";
72 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
73 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
74 my $boring = $param{'boring'} eq 'yes';
75 my $terse = $param{'terse'} eq 'yes';
76 my $reverse = $param{'reverse'} eq 'yes';
77 my $mbox = $param{'mbox'} eq 'yes';
78 my $mime = $param{'mime'} eq 'yes';
79 my $avatars = $param{avatars} eq 'yes';
80
81 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
82
83 my $mbox_status_message = $param{mboxstat} eq 'yes';
84 my $mbox_maint = $param{mboxmaint} eq 'yes';
85 $mbox = 1 if $mbox_status_message or $mbox_maint;
86
87 # Not used by this script directly, but fetch these so that pkgurl() and
88 # friends can propagate them correctly.
89 my $archive = $param{'archive'} eq 'yes';
90 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
91
92 my %bugusertags;
93 my %ut;
94 my %seen_users;
95
96 my $buglog = buglog($ref);
97 my $bug_status = bug_status($ref);
98 if (not defined $buglog or not defined $bug_status) {
99     no_such_bug($q,$ref);
100 }
101
102 sub no_such_bug {
103     my ($q,$ref) = @_;
104     print $q->header(-status => 404,
105                      -content_type => "text/html",
106                      -charset => 'utf-8',
107                      -cache_control => 'public, max-age=600',
108                     );
109     print fill_in_template(template=>'cgi/no_such_bug',
110                            variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
111                                          bug_num     => $ref,
112                                         },
113                           );
114     exit 0;
115 }
116
117 ## calculate etag for this bugreport.cgi call
118 my $etag;
119 ## identify the files that we need to look at; if someone just wants the mbox,
120 ## they don't need to see anything but the buglog; otherwise, track what is
121 ## necessary for the usertags and things to calculate status.
122
123 my @dependent_files = ($buglog);
124 my $need_status = 0;
125 if (not (($mbox and not $mbox_status_message) or
126          (defined $att and defined $msg))) {
127     $need_status = 1;
128     push @dependent_files,
129         $bug_status,
130         defined $config{version_index} ? $config{version_index}:(),
131         defined $config{binary_source_map} ? $config{binary_source_map}:();
132 }
133
134 ## Identify the users required
135 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
136     next unless length($user);
137     push @dependent_files,Debbugs::User::usertag_file_from_email($user);
138 }
139 if (defined $param{usertag}) {
140     for my $usertag (make_list($param{usertag})) {
141         my ($user, $tag) = split /:/, $usertag, 2;
142         push @dependent_files,Debbugs::User::usertag_file_from_email($user);
143     }
144 }
145 $etag =
146     etag_does_not_match(cgi => $q,
147                         additional_data => [grep {defined $_ ? $_ :()}
148                                             values %param
149                                            ],
150                         files => [@dependent_files,
151                                  ],
152                        );
153 if (not $etag) {
154     print $q->header(-status => 304,
155                      -cache_control => 'public, max-age=600',
156                      -etag => $etag,
157                      -charset => 'utf-8',
158                      -content_type => 'text/html',
159                     );
160     print "304: Not modified\n";
161     exit 0;
162 }
163
164 ## if they're just asking for the head, stop here.
165 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
166     print $q->header(-status => 200,
167                      -cache_control => 'public, max-age=600',
168                      -etag => $etag,
169                      -charset => 'utf-8',
170                      -content_type => 'text/html',
171                     );
172      exit 0;
173 }
174
175 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
176     next unless length($user);
177     add_user($user,\%ut,\%bugusertags,\%seen_users);
178 }
179
180 if (defined $param{usertag}) {
181      for my $usertag (make_list($param{usertag})) {
182           my %select_ut = ();
183           my ($u, $t) = split /:/, $usertag, 2;
184           Debbugs::User::read_usertags(\%select_ut, $u);
185           unless (defined $t && $t ne "") {
186                $t = join(",", keys(%select_ut));
187           }
188           add_user($u,\%ut,\%bugusertags,\%seen_users);
189           push @{$param{tag}}, split /,/, $t;
190      }
191 }
192
193 my %status;
194 if ($need_status) {
195     %status = %{split_status_fields(get_bug_status(bug=>$ref,
196                                                    bugusertags => \%bugusertags,
197                                                    defined $s?(schema => $s):(),
198                                                   ))}
199 }
200
201 my @records;
202 my $spam;
203 eval{
204     @records = read_log_records(bug_num => $ref,inner_file => 1);
205     $spam = Debbugs::Log::Spam->new(bug_num => $ref);
206 };
207 if ($@) {
208      quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
209 }
210
211 my $log='';
212 my $msg_num = 0;
213 my $skip_next = 0;
214 if (defined($msg) and ($msg-1) <= $#records) {
215      @records = ($records[$msg-1]);
216      $msg_num = $msg - 1;
217 }
218 my @log;
219 if ( $mbox ) {
220      binmode(STDOUT,":raw");
221      my $date = strftime "%a %b %d %T %Y", localtime;
222      if (@records > 1) {
223          print $q->header(-type => "application/mbox",
224                           -cache_control => 'public, max-age=600',
225                           -etag => $etag,
226                           content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
227                          );
228      }
229      else {
230           $msg_num++;
231           print $q->header(-type => "message/rfc822",
232                            -cache_control => 'public, max-age=86400',
233                            -etag => $etag,
234                            content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
235                           );
236      }
237      if ($mbox_status_message and @records > 1) {
238           my $status_message='';
239           my @status_fields = (retitle   => 'subject',
240                                package   => 'package',
241                                submitter => 'originator',
242                                severity  => 'severity',
243                                tag       => 'tags',
244                                owner     => 'owner',
245                                blocks    => 'blocks',
246                                forward   => 'forward',
247                               );
248           my ($key,$value);
249           while (($key,$value) = splice(@status_fields,0,2)) {
250                if (defined $status{$value} and length $status{$value}) {
251                     $status_message .= qq($key $ref $status{$value}\n);
252                }
253           }
254           print STDOUT qq(From unknown $date\n),
255                create_mime_message([From       => "$gBug#$ref <$ref\@$gEmailDomain>",
256                                     To         => "$gBug#$ref <$ref\@$gEmailDomain>",
257                                     Subject    => "Status: $status{subject}",
258                                     "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
259                                    ],
260                                    <<END,);
261 $status_message
262 thanks
263
264
265 END
266      }
267      my $message_number=0;
268      my %seen_message_ids;
269      for my $record (@records) {
270           next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
271           my $wanted_type = $mbox_maint?'recips':'incoming-recv';
272           # we want to include control messages anyway
273           my $record_wanted_anyway = 0;
274           my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/im);
275           next if defined $msg_id and exists $seen_message_ids{$msg_id};
276           next if defined $msg_id and $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
277           $record_wanted_anyway = 1 if record_regex($record,qr/^Received: \(at control\)/);
278           next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
279           $seen_message_ids{$msg_id} = 1 if defined $msg_id;
280           # skip spam messages if we're outputting more than one message
281           next if @records > 1 and $spam->is_spam($msg_id);
282       my @lines;
283       if ($record->{inner_file}) {
284           push @lines, scalar $record->{fh}->getline;
285           push @lines, scalar $record->{fh}->getline;
286           chomp $lines[0];
287           chomp $lines[1];
288       } else {
289           @lines = split( "\n", $record->{text}, -1 );
290       }
291           if ( $lines[ 1 ] =~ m/^From / ) {
292           @lines = reverse @lines;
293           }
294           if ( !( $lines[ 0 ] =~ m/^From / ) ) {
295                unshift @lines, "From unknown $date";
296        }
297       print $lines[0]."\n";
298           print map { s/^(>*From )/>$1/; $_."\n" } @lines[ 1 .. $#lines ];
299       if ($record->{inner_file}) {
300           my $fh = $record->{fh};
301           print $_ while (<$fh>);
302       }
303      }
304      exit 0;
305 }
306
307 else {
308      if (defined $att and defined $msg and @records) {
309          binmode(STDOUT,":raw");
310          $msg_num++;
311          ## allow this to be cached for a week
312          print "Status: 200 OK\n";
313          print "Cache-Control: public, max-age=604800\n";
314          print "Etag: $etag\n";
315           print handle_email_message($records[0],
316                                      ref => $ref,
317                                      msg_num => $msg_num,
318                                      att => $att,
319                                      msg => $msg,
320                                      trim_headers => $trim_headers,
321                                     );
322           exit 0;
323      }
324      my %seen_msg_ids;
325      for my $record (@records) {
326           $msg_num++;
327           if ($skip_next) {
328                $skip_next = 0;
329                next;
330           }
331           $skip_next = 1 if $record->{type} eq 'html' and not $boring;
332           push @log, handle_record($record,$ref,$msg_num,
333                                    \%seen_msg_ids,
334                                    trim_headers => $trim_headers,
335                                    avatars => $avatars,
336                                    terse => $terse,
337                                    # if we're only looking at one record, allow
338                                    # spam to be output
339                                    spam  => (@records > 1)?$spam:undef,
340                                   );
341      }
342 }
343
344 @log = reverse @log if $reverse;
345 $log = join("\n",@log);
346
347
348 # All of the below should be turned into a template
349
350 my %maintainer = %{getmaintainers()};
351 my %pkgsrc = %{getpkgsrc()};
352
353 my $indexentry;
354 my $showseverity;
355
356 my $tpack;
357 my $tmain;
358
359 my $dtime = strftime "%a, %e %b %Y %T UTC", gmtime;
360
361 unless (%status) {
362     no_such_bug($q,$ref);
363 }
364
365 #$|=1;
366
367
368 my @packages = make_list($status{package});
369
370
371 my %packages_affects;
372 for my $p_a (qw(package affects)) {
373     foreach my $pkg (make_list($status{$p_a})) {
374         if ($pkg =~ /^src\:/) {
375             my ($srcpkg) = $pkg =~ /^src:(.*)/;
376             $packages_affects{$p_a}{$pkg} =
377                {maintainer => exists($maintainer{$srcpkg}) ? $maintainer{$srcpkg} : '(unknown)',
378                 source     => $srcpkg,
379                 package    => $pkg,
380                 is_source  => 1,
381                };
382         }
383         else {
384             $packages_affects{$p_a}{$pkg} =
385                {maintainer => exists($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)',
386                 exists($pkgsrc{$pkg}) ? (source => $pkgsrc{$pkg}) : (),
387                 package    => $pkg,
388                };
389         }
390     }
391 }
392
393 # fixup various bits of the status
394 $status{tags_array} = [sort(make_list($status{tags}))];
395 $status{date_text} = strftime('%a, %e %b %Y %T UTC', gmtime($status{date}));
396 $status{mergedwith_array} = [make_list($status{mergedwith})];
397
398
399 my $version_graph = '';
400 if (@{$status{found_versions}} or @{$status{fixed_versions}}) {
401      $version_graph = q(<a href=").
402           html_escape(version_url(package => $status{package},
403                                   found => $status{found_versions},
404                                   fixed => $status{fixed_versions},
405                                  )
406                      ).
407           q("><img alt="version graph" src=").
408           html_escape(version_url(package => $status{package},
409                                   found => $status{found_versions},
410                                   fixed => $status{fixed_versions},
411                                   width => 2,
412                                   height => 2,
413                                  )
414                      ).
415           qq{"></a>};
416 }
417
418
419
420 my @blockedby= make_list($status{blockedby});
421 $status{blockedby_array} = [];
422 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
423     for my $b (@blockedby) {
424         my %s = %{get_bug_status($b)};
425         next if (defined $s{pending} and
426                  $s{"pending"} eq 'fixed') or
427                      length $s{done};
428         push @{$status{blockedby_array}},{bug_num => $b, subject => $s{subject}, status => \%s};
429    }
430 }
431
432 my @blocks= make_list($status{blocks});
433 $status{blocks_array} = [];
434 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
435     for my $b (@blocks) {
436         my %s = %{get_bug_status($b)};
437         next if $s{"pending"} eq 'fixed' || length $s{done};
438         push @{$status{blocks_array}}, {bug_num => $b, subject => $s{subject}, status => \%s};
439     }
440 }
441
442 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
443      $status{archived} = 1;
444 }
445
446 my $descriptivehead = $indexentry;
447
448 print $q->header(-type => "text/html",
449                  -charset => 'utf-8',
450                  -cache_control => 'public, max-age=300',
451                  -etag => $etag,
452                 );
453
454 print fill_in_template(template => 'cgi/bugreport',
455                        variables => {status => \%status,
456                                      package => $packages_affects{'package'},
457                                      affects => $packages_affects{'affects'},
458                                      log           => $log,
459                                      bug_num       => $ref,
460                                      version_graph => $version_graph,
461                                      msg           => $msg,
462                                      isstrongseverity => \&Debbugs::Status::isstrongseverity,
463                                      html_escape   => \&Debbugs::CGI::html_escape,
464                                      uri_escape    => \&URI::Escape::uri_escape_utf8,
465                                      looks_like_number => \&Scalar::Util::looks_like_number,
466                                      make_list        => \&Debbugs::Common::make_list,
467                                     },
468                        hole_var  => {'&package_links' => \&Debbugs::CGI::package_links,
469                                      '&bug_links'     => \&Debbugs::CGI::bug_links,
470                                      '&version_url'   => \&Debbugs::CGI::version_url,
471                                      '&strftime'      => \&POSIX::strftime,
472                                      '&maybelink'     => \&Debbugs::CGI::maybelink,
473                                     },
474                       );
475
476 __END__
477
478 # Local Variables:
479 # indent-tabs-mode: nil
480 # cperl-indent-level: 4
481 # End: