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 use if defined $debbugs_dir, lib => $debbugs_dir;
41 use Debbugs::Config qw(:globals :text :config);
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);
51 use Debbugs::Status qw(splitpackages split_status_fields get_bug_status isstrongseverity);
54 use Scalar::Util qw(looks_like_number);
56 use Debbugs::Text qw(:templates);
57 use URI::Escape qw(uri_escape_utf8);
58 use List::AllUtils qw(max);
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);
69 my $q = new CGI::Simple;
70 # STDOUT should be using the utf8 io layer
71 binmode(STDOUT,':raw:encoding(UTF-8)');
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),
79 default => {# msg => '',
88 repeatmerged => 'yes',
92 # This is craptacular.
94 my $ref = $param{bug} or quitcgi("No bug number", '400 Bad Request');
95 $ref =~ /(\d+)/ or quitcgi("Invalid bug number", '400 Bad Request');
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';
107 my $trim_headers = ($param{trim} || ((defined $msg and $msg)?'no':'yes')) eq 'yes';
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;
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';
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);
130 print $q->header(-status => 404,
131 -content_type => "text/html",
133 -cache_control => 'public, max-age=600',
135 print fill_in_template(template=>'cgi/no_such_bug',
136 variables => {modify_time => strftime('%a, %e %b %Y %T UTC', gmtime),
143 ## calculate etag for this bugreport.cgi call
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.
149 my @dependent_files = ($buglog);
151 if (not (($mbox and not $mbox_status_message) or
152 (defined $att and defined $msg))) {
154 push @dependent_files,
156 defined $config{version_index} ? $config{version_index}:(),
157 defined $config{binary_source_map} ? $config{binary_source_map}:();
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);
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);
172 etag_does_not_match(cgi => $q,
173 additional_data => [grep {defined $_ ? $_ :()}
176 files => [@dependent_files,
180 print $q->header(-status => 304,
181 -cache_control => 'public, max-age=600',
184 -content_type => 'text/html',
186 print "304: Not modified\n";
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',
196 -content_type => 'text/html',
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);
206 if (defined $param{usertag}) {
207 for my $usertag (make_list($param{usertag})) {
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));
214 add_user($u,\%ut,\%bugusertags,\%seen_users);
215 push @{$param{tag}}, split /,/, $t;
219 my $bug = Debbugs::Bug->new(bug => $ref,
225 %status = %{split_status_fields(get_bug_status(bug=>$ref,
226 bugusertags => \%bugusertags,
233 @records = $bug->log_records();
236 quitcgi("Bad bug log for $gBug $ref. Unable to read records: $@");
242 if (defined($msg) and ($msg-1) <= $#records) {
243 @records = ($records[$msg-1]);
248 binmode(STDOUT,":raw");
249 my $date = strftime "%a %b %d %T %Y", localtime;
251 print $q->header(-type => "application/mbox",
252 -cache_control => 'public, max-age=600',
254 content_disposition => qq(attachment; filename="bug_${ref}.mbox"),
259 print $q->header(-type => "message/rfc822",
260 -cache_control => 'public, max-age=86400',
262 content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"),
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',
274 forward => 'forward',
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);
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>",
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);
311 if ($record->{inner_file}) {
312 push @lines, scalar $record->{fh}->getline;
313 push @lines, scalar $record->{fh}->getline;
317 @lines = split( "\n", $record->{text}, -1 );
319 if ( $lines[ 1 ] =~ m/^From / ) {
320 @lines = reverse @lines;
322 if ( !( $lines[ 0 ] =~ m/^From / ) ) {
323 unshift @lines, "From unknown $date";
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>);
336 if (defined $att and defined $msg and @records) {
337 binmode(STDOUT,":raw");
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],
348 trim_headers => $trim_headers,
353 for my $record (@records) {
359 $skip_next = 1 if $record->{type} eq 'html' and not $boring;
360 push @log, handle_record($record,$ref,$msg_num,
362 trim_headers => $trim_headers,
365 # if we're only looking at one record, allow
367 spam => (@records > 1)?$bug:undef,
372 @log = reverse @log if $reverse;
373 $log = join("\n",@log);
376 # All of the below should be turned into a template
382 no_such_bug($q,$ref);
385 my @packages = make_list($status{package});
388 print $q->header(-type => "text/html",
390 -cache_control => 'public, max-age=300',
394 print fill_in_template(template => 'cgi/bugreport',
395 variables => {bug => $bug,
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,
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,
415 # indent-tabs-mode: nil
416 # cperl-indent-level: 4