6 # Sanitize environent for taint
8 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
12 use POSIX qw(strftime);
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);
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/') {
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] !~ /^\//) {
39 if (defined $debbugs_dir) {
40 unshift @INC, $debbugs_dir.'/lib/';
44 use Debbugs::Config qw(:globals :text :config);
46 # for read_log_records
47 use Debbugs::Log qw(:read);
48 use Debbugs::Log::Spam;
49 use Debbugs::CGI qw(:url :html :util :cache :usertags);
50 use Debbugs::CGI::Bugreport qw(:all);
51 use Debbugs::Common qw(buglog getmaintainers make_list bug_status package_maintainer);
52 use Debbugs::Packages qw(binary_to_source);
54 use Debbugs::Status qw(splitpackages split_status_fields get_bug_status isstrongseverity);
57 use Scalar::Util qw(looks_like_number);
59 use Debbugs::Text qw(:templates);
60 use URI::Escape qw(uri_escape_utf8);
61 use List::AllUtils qw(max);
65 if (defined $config{database}) {
66 $s = Debbugs::DB->connect($config{database}) or
67 die "Unable to connect to DB";
68 @schema_arg = ('schema',$s);
72 my $q = CGI::Simple->new();
73 # STDOUT should be using the utf8 io layer
74 binmode(STDOUT,':raw:encoding(UTF-8)');
76 my %param = cgi_parameters(query => $q,
77 single => [qw(bug msg att boring terse),
78 qw(reverse mbox mime trim),
79 qw(mboxstat mboxmaint archive),
80 qw(repeatmerged avatars),
82 default => {# msg => '',
91 repeatmerged => 'yes',
95 # This is craptacular.
97 my $ref = $param{bug} or quitcgi("No bug number", '400 Bad Request');
98 $ref =~ /(\d+)/ or quitcgi("Invalid bug number", '400 Bad Request');
101 my ($msg) = $param{msg} =~ /^(\d+)$/ if exists $param{msg};
102 my ($att) = $param{att} =~ /^(\d+)$/ if exists $param{att};
103 my $boring = $param{'boring'} eq 'yes';
104 my $terse = $param{'terse'} eq 'yes';
105 my $reverse = $param{'reverse'} eq 'yes';
106 my $mbox = $param{'mbox'} eq 'yes';
107 my $mime = $param{'mime'} eq 'yes';
108 my $avatars = $param{avatars} eq 'yes';
110 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
112 my $mbox_status_message = $param{mboxstat} eq 'yes';
113 my $mbox_maint = $param{mboxmaint} eq 'yes';
114 $mbox = 1 if $mbox_status_message or $mbox_maint;
116 # Not used by this script directly, but fetch these so that pkgurl() and
117 # friends can propagate them correctly.
118 my $archive = $param{'archive'} eq 'yes';
119 my $repeatmerged = $param{'repeatmerged'} eq 'yes';
125 my $buglog = buglog($ref);
126 my $bug_status = bug_status($ref);
127 if (not defined $buglog or not defined $bug_status) {
128 no_such_bug($q,$ref);
133 print $q->header(-status => 404,
134 -content_type => "text/html",
136 -cache_control => 'public, max-age=600',
138 print fill_in_template(template=>'cgi/no_such_bug',
139 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
146 ## calculate etag for this bugreport.cgi call
148 ## identify the files that we need to look at; if someone just wants the mbox,
149 ## they don't need to see anything but the buglog; otherwise, track what is
150 ## necessary for the usertags and things to calculate status.
152 my @dependent_files = ($buglog);
154 if (not (($mbox and not $mbox_status_message) or
155 (defined $att and defined $msg))) {
157 push @dependent_files,
159 defined $config{version_index} ? $config{version_index}:(),
160 defined $config{binary_source_map} ? $config{binary_source_map}:();
163 ## Identify the users required
164 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
165 next unless length($user);
166 push @dependent_files,Debbugs::User::usertag_file_from_email($user);
168 if (defined $param{usertag}) {
169 for my $usertag (make_list($param{usertag})) {
170 my ($user, $tag) = split /:/, $usertag, 2;
171 push @dependent_files,Debbugs::User::usertag_file_from_email($user);
175 etag_does_not_match(cgi => $q,
176 additional_data => [grep {defined $_ ? $_ :()}
179 files => [@dependent_files,
183 print $q->header(-status => 304,
184 -cache_control => 'public, max-age=600',
187 -content_type => 'text/html',
189 print "304: Not modified\n";
193 ## if they're just asking for the head, stop here.
194 if ($q->request_method() eq 'HEAD' and not defined($att) and not $mbox) {
195 print $q->header(-status => 200,
196 -cache_control => 'public, max-age=600',
199 -content_type => 'text/html',
204 for my $user (map {split /[\s*,\s*]+/} make_list($param{users}||[])) {
205 next unless length($user);
206 add_user($user,\%ut,\%bugusertags,\%seen_users);
209 if (defined $param{usertag}) {
210 for my $usertag (make_list($param{usertag})) {
212 my ($u, $t) = split /:/, $usertag, 2;
213 Debbugs::User::read_usertags(\%select_ut, $u);
214 unless (defined $t && $t ne "") {
215 $t = join(",", keys(%select_ut));
217 add_user($u,\%ut,\%bugusertags,\%seen_users);
218 push @{$param{tag}}, split /,/, $t;
222 my $bug = Debbugs::Bug->new(bug => $ref,
228 %status = %{split_status_fields(get_bug_status(bug=>$ref,
229 bugusertags => \%bugusertags,
236 @records = $bug->log_records();
239 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
245 if (defined($msg) and ($msg-1) <= $#records) {
246 @records = ($records[$msg-1]);
251 binmode(STDOUT,":raw");
252 my $date = strftime "%a %b %d %T %Y", localtime;
254 print $q->header(-type => "application/mbox",
255 -cache_control => 'public, max-age=600',
257 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
262 print $q->header(-type => "message/rfc822",
263 -cache_control => 'public, max-age=86400',
265 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
268 if ($mbox_status_message and @records > 1) {
269 my $status_message='';
270 my @status_fields = (retitle => 'subject',
271 package => 'package',
272 submitter => 'originator',
273 severity => 'severity',
277 forward => 'forward',
280 while (($key,$value) = splice(@status_fields,0,2)) {
281 if (defined $status{$value} and length $status{$value}) {
282 $status_message .= qq($key $ref $status{$value}\n);
285 print STDOUT qq(From unknown $date\n),
286 create_mime_message([From => "$gBug#$ref <$ref\@$gEmailDomain>",
287 To => "$gBug#$ref <$ref\@$gEmailDomain>",
288 Subject => "Status: $status{subject}",
289 "Reply-To" => "$gBug#$ref <$ref\@$gEmailDomain>",
298 my $message_number=0;
299 my %seen_message_ids;
300 for my $record (@records) {
301 next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
302 my $wanted_type = $mbox_maint?'recips':'incoming-recv';
303 # we want to include control messages anyway
304 my $record_wanted_anyway = 0;
305 my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/im);
306 next if defined $msg_id and exists $seen_message_ids{$msg_id};
307 next if defined $msg_id and $msg_id =~/handler\..+\.ack(?:info|done)?\@/;
308 $record_wanted_anyway = 1 if record_regex($record,qr/^Received: \(at control\)/);
309 next if not $boring and not $record->{type} eq $wanted_type and not $record_wanted_anyway and @records > 1;
310 $seen_message_ids{$msg_id} = 1 if defined $msg_id;
311 # skip spam messages if we're outputting more than one message
312 next if @records > 1 and $bug->is_spam($msg_id);
314 if ($record->{inner_file}) {
315 push @lines, scalar $record->{fh}->getline;
316 push @lines, scalar $record->{fh}->getline;
320 @lines = split( "\n", $record->{text}, -1 );
322 if ( $lines[ 1 ] =~ m/^From / ) {
323 @lines = reverse @lines;
325 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
326 unshift @lines, "From unknown $date";
328 print $lines[0]."\n";
329 print map { s/^(>*From )/>$1/; $_."\n" } @lines[ 1 .. $#lines ];
330 if ($record->{inner_file}) {
331 my $fh = $record->{fh};
343 if (defined $att and defined $msg and @records) {
344 binmode(STDOUT,":raw");
346 ## allow this to be cached for a week
347 print "Status: 200 OK\n";
348 print "Cache-Control: public, max-age=604800\n";
349 print "Etag: $etag\n";
350 print handle_email_message($records[0],
355 trim_headers => $trim_headers,
360 for my $record (@records) {
366 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
367 push @log, handle_record($record,$ref,$msg_num,
369 trim_headers => $trim_headers,
372 # if we're only looking at one record, allow
374 spam => (@records > 1)?$bug:undef,
379 @log = reverse @log if $reverse;
380 $log = join("\n",@log);
383 # All of the below should be turned into a template
389 no_such_bug($q,$ref);
392 my @packages = make_list($status{package});
395 print $q->header(-type => "text/html",
397 -cache_control => 'public, max-age=300',
401 print fill_in_template(template => 'cgi/bugreport',
402 variables => {bug => $bug,
405 isstrongseverity => \&Debbugs::Status::isstrongseverity,
406 html_escape => \&Debbugs::CGI::html_escape,
407 uri_escape => \&URI::Escape::uri_escape_utf8,
408 looks_like_number => \&Scalar::Util::looks_like_number,
409 make_list => \&Debbugs::Common::make_list,
411 hole_var => {'&package_links' => \&Debbugs::CGI::package_links,
412 '&bug_links' => \&Debbugs::CGI::bug_links,
413 '&version_url' => \&Debbugs::CGI::version_url,
414 '&strftime' => \&POSIX::strftime,
415 '&maybelink' => \&Debbugs::CGI::maybelink,
422 # indent-tabs-mode: nil
423 # cperl-indent-level: 4