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