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