2 # $Id: process.in,v 1.109 2006/02/09 22:02:04 don Exp $
10 use POSIX qw(strftime);
17 use Debbugs::MIME qw(decode_rfc1522 create_mime_message getmailbody);
18 use Debbugs::Mail qw(send_mail_message encode_headers get_addresses);
19 use Debbugs::Packages qw(getpkgsrc binary_to_source);
20 use Debbugs::User qw(read_usertags write_usertags);
21 use Debbugs::Common qw(:lock get_hashname buglog package_maintainer overwritefile make_list);
22 use Debbugs::Status qw(writebug isstrongseverity lockreadbugmerge lockreadbug new_bug read_bug splitpackages :versions);
24 use Debbugs::CGI qw(html_escape bug_links);
26 use Debbugs::Log qw(:misc :write);
28 use Debbugs::Text qw(:templates);
30 use Debbugs::Config qw(:globals :config);
32 use Debbugs::Control qw(append_action_to_log);
33 use Debbugs::Control::Service qw(valid_control control_line);
34 use Debbugs::Recipients qw(determine_recipients);
35 use Encode qw(encode_utf8 decode);
39 process - Handle e-mails emails sent to bugs
46 --debug, -d debugging level (Default 0)
54 Debugging level (default 0)
62 my %options = (debug => 0,
68 'debug|d+','help|h|?','man|m');
70 pod2usage() if $options{help};
71 pod2usage({verbose=>2}) if $options{man};
74 $DEBUG=$options{debug};
75 my $debugfh = IO::File->new('/dev/null','w') or
76 die "Unable to open /dev/null for writing; $!";
81 # these are the valid bug addresses
82 my %baddress = (B => 'submit',
90 my $valid_codeletters = join('',keys %baddress);
93 chdir($config{spool_dir}) or die "Unable to chdir to spool ($config{spool_dir}): $!";
97 my $intdate = time or die "failed to get time: $!";
100 my ($codeletter,$tryref) =
101 $nn =~ m/^([$valid_codeletters])(\d*)\.\d+$/
102 or die "bad argument: $_";
103 $tryref = undef unless length ($tryref) and
106 if (!rename("incoming/G$nn","incoming/P$nn")) {
108 $error = '' if not defined $error;
109 # this is very fragile, but we should probably die here anyway
110 if ($error =~ m/no such file or directory/i) {
113 die "Unable to rename incoming/G$nn to lock: $error";
116 # die here to avoid continuously processing this mail
117 if (not exists $baddress{$codeletter}) {
118 die "bad codeletter $codeletter";
121 my $baddress = $baddress{$codeletter};
122 if ($baddress eq 'list') {
123 bug_list_forward($nn) if $codeletter eq 'L';
127 my $baddressroot= $baddress;
128 $baddress= "$tryref-$baddress" if defined $tryref;
134 my $log = IO::File->new("incoming/P$nn",'r') or
135 die "Unable to open 'incoming/P$nn' for reading; $!";
138 @msg = split /\n/, $msg;
143 my $tdate = strftime "%a, %d %h %Y %T +0000", gmtime;
144 my $fwd= "Received: via spool by $baddress\@$gEmailDomain id=$nn\n".
145 " (code $codeletter".(defined($tryref)?" ref $tryref":'')."); $tdate\n";
147 # header and decoded body respectively
148 my (@headerlines, @bodylines);
150 # whether maintainer addresses have been checked
151 our $maintainerschecked = 0;
152 #maintainer address for this message
154 # other src addresses
159 my $resentccexplain='';
161 # whether there's a new reference with this email
164 our $brokenness = '';
166 my $parser_output = Debbugs::MIME::parse($msg);
168 @headerlines = @{$parser_output->{header}};
169 @bodylines = @{$parser_output->{body}};
174 for my $hdr (@headerlines) {
175 $hdr = decode_rfc1522($hdr);
178 finish() if m/^x-loop: (\S+)$/i && $1 eq "$gMaintainerEmail";
179 my $ins = !m/^subject:/i && !m/^reply-to:/i && !m/^return-path:/i
180 && !m/^From / && !m/^X-Debbugs-/i;
181 $fwd .= $hdr."\n" if $ins;
182 # print {$debugfh} ">$_<\n";
183 if (s/^(\S+):\s*//) {
185 if ($v eq 'x-loop') {
186 push @common_headers, 'X-Loop',$_;
188 print {$debugfh} ">$v=$_<\n";
191 print {$debugfh} "!>$_<\n";
194 $header{'message-id'} = '' if not defined $header{'message-id'};
196 push @common_headers, 'X-Loop',$gMaintainerEmail;
199 shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
201 # Strip off RFC2440-style PGP clearsigning.
202 if (@bodylines and $bodylines[0] =~ /^-----BEGIN PGP SIGNED/) {
203 shift @bodylines while @bodylines and length $bodylines[0];
204 shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
205 for my $findsig (0 .. $#bodylines) {
206 if ($bodylines[$findsig] =~ /^-----BEGIN PGP SIGNATURE/) {
207 $#bodylines = $findsig - 1;
211 map { s/^- // } @bodylines;
217 # extract pseudo-headers
218 for my $phline (@bodylines)
220 # Remove BOM markers from UTF-8 strings
222 $phline =~ s/\xef\xbb\xbf//g;
223 last if $phline !~ m/^([\w-]+):\s*(\S.*)/;
224 my ($fn, $fv) = ($1, $2);
226 print {$debugfh} ">$fn|$fv|\n";
228 if ($fn =~ /^control$/) {
229 push @control_bits,$fv;
231 # Don't lc owner or forwarded
232 $fv = lc $fv unless $fn =~ /^(?:owner|forwarded|usertags|version|source-version)$/;
235 print {$debugfh} ">$fn~$fv<\n";
238 # Allow pseudo headers to set x-debbugs- stuff [#179340]
239 for my $key (grep /X-Debbugs-.*/i, keys %pheader) {
240 $header{$key} = $pheader{$key} if not exists $header{$key};
243 # set $i to beginning of encoded body data, so we can dump it out
246 ++$i while $msg[$i] =~ /./;
247 $fwd .= join("\n",@msg[$i..$#msg]);
249 print {$debugfh} "***\n$fwd\n***\n";
251 if (defined $header{'resent-from'} && !defined $header{'from'}) {
252 $header{'from'} = $header{'resent-from'};
254 defined($header{'from'}) || die "no From header";
256 my $replyto = $header{'reply-to'};
257 $replyto = '' unless defined $replyto;
260 unless (length $replyto) {
261 $replyto = $header{'from'};
264 my $subject = '(no subject)';
265 if (!defined($header{'subject'}))
267 $brokenness.= fill_template('mail/process_broken_subject');
270 $subject= $header{'subject'};
274 # remove Re: from the subject line
275 $subject =~ s/^Re:\s*//i;
276 # remove remaining mailing list name markers from the subject line
277 $subject =~ s/^\[.*\]\s*//i;
279 if (not defined $tryref and m/^Bug ?\#(\d+)\D/i) {
280 $tryref = $1 if $1 > 0;
284 if (defined $tryref) {
286 ($locks_recv, $data)= &lockreadbugmerge($tryref);
287 $locks += $locks_recv;
288 if ($locks_recv and not $data->{archived}) {
291 &sendmessage(create_mime_message(
292 [From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
294 Subject => "Unknown problem report $gBug#$tryref ($subject)",
295 'Message-ID' => "<handler.x.$nn.unknown\@$gEmailDomain>",
296 'In-Reply-To' => $header{'message-id'},
297 References => join(' ',grep {defined $_} $header{'message-id'},$data->{msgid}),
298 Precedence => 'bulk',
299 "X-$gProject-PR-Message" => 'error',
301 ],message_body_template('mail/process_unknown_bug_number',
302 {subject => $subject,
303 date => $header{date},
304 baddress => $baddress,
306 messageid => $header{'message-id'},
309 appendlog($ref,$msg);
313 &filelock('lock/-1');
316 # Attempt to determine which source package this is
317 my $source_pr_header = '';
318 my $source_package = '';
319 if (defined $pheader{source}) {
320 $source_package = $pheader{source};
322 elsif (defined $data->{package} or defined $pheader{package}) {
323 $source_package = binary_to_source(binary => $data->{package} // $pheader{package});
325 $source_pr_header = "X-$gProject-PR-Source: $source_package\n"
326 if defined $source_package and length $source_package;
328 # Done and Forwarded Bugs
329 if ($codeletter eq 'D' || $codeletter eq 'F')
331 if ($replyto =~ m/$gBounceFroms/o ||
332 $header{'from'} =~ m/$gBounceFroms/o)
334 print STDERR "bounce detected ! Mwaap! Mwaap!";
337 my $markedby= $header{'from'} eq $replyto ? $replyto :
338 "$header{'from'} (reply to $replyto)";
345 if ($codeletter eq 'F') { # Forwarded
346 (appendlog($ref,$msg),finish()) if defined $data->{forwarded} and length($data->{forwarded});
347 $receivedat= "forwarded\@$gEmailDomain";
348 $markaswhat= 'forwarded';
349 $set_forwarded= $header{'to'};
350 # Dissallow forwarded being set to this bug tracking system
351 if (defined $set_forwarded and $set_forwarded =~ /\Q$gEmailDomain\E/) {
352 undef $set_forwarded;
354 if ( length( $gListDomain ) > 0 && length( $gForwardList ) > 0 ) {
355 push @generalcc, "$gForwardList\@$gListDomain";
356 $generalcc= "$gForwardList\@$gListDomain";
361 if (defined $data->{done} and length($data->{done}) and
362 not defined $pheader{'source-version'} and
363 not defined $pheader{'version'}) {
364 appendlog($ref,$msg);
367 $receivedat= "done\@$gEmailDomain";
369 $set_done= $header{'from'};
370 if ( length( $gListDomain ) > 0 && length( $gDoneList ) > 0 ) {
371 $generalcc= "$gDoneList\@$gListDomain";
372 push @generalcc, "$gDoneList\@$gListDomain";
377 if (defined $gStrongList and isstrongseverity($data->{severity})) {
378 $generalcc = join ', ', $generalcc, "$gStrongList\@$gListDomain";
379 push @generalcc,"$gStrongList\@$gListDomain";
382 &sendmessage(create_mime_message(
383 [From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
385 Subject => "Message with no $gBug number ignored by $receivedat ($subject)",
386 'Message-ID' => "<handler.x.$nn.warnignore\@$gEmailDomain>",
387 'In-Reply-To' => $header{'message-id'},
388 References => join(' ',grep {defined $_} $header{'message-id'},$data->{msgid}),
389 Precedence => 'bulk',
390 "X-$gProject-PR-Message" => 'error',
392 ],message_body_template('mail/process_no_bug_number',
393 {subject => $subject,
394 date => $header{date},
395 markaswhat => $markaswhat,
396 receivedat => $receivedat,
397 messageid => $header{'message-id'},
400 appendlog($ref,$msg);
406 my @noticecc = grep($_ ne $replyto,@maintaddrs);
407 my $noticeccval.= join(', ', grep($_ ne $replyto,@maintaddrs));
408 $noticeccval =~ s/\s+\n\s+/ /g;
409 $noticeccval =~ s/^\s+/ /; $noticeccval =~ s/\s+$//;
411 my @process= ($ref,split(/ /,$data->{mergedwith}));
414 for $ref (@process) {
415 if ($ref != $orgref) {
418 $data = &lockreadbug($ref)
419 || die "huh ? $ref from $orgref out of ".join(' ',@process);
422 $data->{done}= $set_done if defined($set_done);
423 $data->{forwarded}= $set_forwarded if defined($set_forwarded);
424 if ($codeletter eq 'D') {
425 $data->{keywords} = join ' ', grep $_ ne 'pending',
426 split ' ', $data->{keywords};
427 if (defined $pheader{'source-version'}) {
428 if ($pheader{'source-version'} !~ m/^$config{package_version_re}$/) {
429 $brokenness .= fill_template('mail/invalid_version',
430 {version => $pheader{'source-version'}},
434 addfixedversions($data, $pheader{source}, $pheader{'source-version'}, '');
436 } elsif (defined $pheader{version}) {
437 if ($pheader{version} !~ m/^$config{package_version_re}$/) {
438 $brokenness .= fill_template('mail/invalid_version',
439 {version => $pheader{version}},
443 addfixedversions($data, $pheader{package}, $pheader{version}, '');
448 # Add bug mailing list to $generalbcc as appropriate
449 # This array is used to specify bcc in the cases where we're using create_mime_message.
450 my @generalbcc = (@generalcc,@addsrcaddrs,"bugs=$ref\@$gListDomain");
451 my $generalbcc = join(', ', $generalcc, @addsrcaddrs,"bugs=$ref\@$gListDomain");
452 $generalbcc =~ s/\s+\n\s+/ /g;
453 $generalbcc =~ s/^\s+/ /; $generalbcc =~ s/\s+$//;
454 if (length $generalbcc) {$generalbcc = "Bcc: $generalbcc\n"};
456 writebug($ref, $data);
458 my $hash = get_hashname($ref);
459 my $orig_report_fh = IO::File->new("db-h/$hash/$ref.report") or
460 die "Unable to read original report: $!";
462 { local $/; $orig_report = <$orig_report_fh>;}
463 close($orig_report_fh) or
464 die "Unable to close original report filehandle: $!";
465 if ($codeletter eq 'F') {
466 &htmllog("Reply","sent",$replyto,"You have marked $gBug as forwarded.");
467 &sendmessage(create_mime_message(
469 From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
471 Subject => "$gBug#$ref: marked as forwarded ($data->{subject})",
472 "Message-ID" => "<header.$ref.$nn.ackfwdd\@$gEmailDomain>",
473 "In-Reply-To" => $header{'message-id'},
474 References => join(' ',grep {defined $_} $header{'message-id'},$data->{msgid}),
475 Precedence => 'bulk',
476 "X-$gProject-PR-Message" => "forwarded $ref",
477 "X-$gProject-PR-Package" => $data->{package},
478 "X-$gProject-PR-Keywords" => $data->{keywords},
479 # Only have a X-$gProject-PR-Source when we know the source package
480 (defined($source_package) and length($source_package))?("X-$gProject-PR-Source" => $source_package):(),
481 ],message_body_template('mail/process_mark_as_forwarded',
482 {date => $header{date},
483 messageid => $header{'message-id'},
487 [join("\n",@msg)]),'',[$replyto,@generalbcc,@noticecc],1);
489 &htmllog("Reply","sent",$replyto,"You have taken responsibility.");
490 &sendmessage(create_mime_message(
492 From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
494 Subject => "$gBug#$ref: marked as done ($data->{subject})",
495 "Message-ID" => "<handler.$ref.$nn.ackdone\@$gEmailDomain>",
496 "In-Reply-To" => $header{'message-id'},
497 References => join(' ',grep {defined $_} $header{'message-id'},$data->{msgid}),
498 Precedence => 'bulk',
499 "X-$gProject-PR-Message" => "closed $ref",
500 "X-$gProject-PR-Package" => $data->{package},
501 "X-$gProject-PR-Keywords" => $data->{keywords},
502 # Only have a X-$gProject-PR-Source when we know the source package
503 (defined($source_package) and length($source_package))?("X-$gProject-PR-Source" => $source_package):(),
504 ],message_body_template('mail/process_mark_as_done',
505 {date => $header{date},
506 messageid => $header{'message-id'},
507 subject => $header{subject},
511 [$orig_report,join("\n",@msg)]),'',[$replyto,@generalbcc,@noticecc],1);
512 &htmllog("Notification","sent",$data->{originator},
513 "$gBug acknowledged by developer.");
514 &sendmessage(create_mime_message(
516 From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
517 To => "$data->{originator}",
518 Subject => "$gBug#$ref closed by $markedby ($header{'subject'})",
519 "Message-ID" => "<handler.$ref.$nn.notifdone\@$gEmailDomain>",
520 (defined $data->{msgid})?("In-Reply-To" => $data->{msgid}):(),
521 References => join(' ',grep {defined $_} ($header{'message-id'},$data->{msgid})),
522 "X-$gProject-PR-Message" => "they-closed $ref",
523 "X-$gProject-PR-Package" => "$data->{package}",
524 "X-$gProject-PR-Keywords" => "$data->{keywords}",
525 # Only have a X-$gProject-PR-Source when we know the source package
526 (defined($source_package) and length($source_package))?("X-$gProject-PR-Source" => $source_package):(),
527 "Reply-To" => "$ref\@$gEmailDomain",
528 "Content-Type" => 'text/plain; charset="utf-8"',
529 ],message_body_template('mail/process_your_bug_done',
531 markedby => $markedby,
532 messageid => $header{'message-id'},
533 subject => $header{subject},
536 [join("\n",@msg),$orig_report]),'',undef,1);
538 appendlog($ref,$msg);
543 if ($ref<0) { # new bug report
544 if ($codeletter eq 'U') { # -submitter
545 &sendmessage(create_mime_message(
547 From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
549 Subject => "Message with no $gBug number cannot be sent to submitter! ($subject)",
550 'Message-ID' => "<handler.x.$nn.nonumnosub\@$gEmailDomain>",
551 'In-Reply-To' => $header{'message-id'},
552 References => join(' ',grep {defined $_} $header{'message-id'},$data->{msgid}),
553 Precedence => 'bulk',
554 "X-$gProject-PR-Message" => 'error',
555 ],message_body_template('mail/process_no_bug_number',
556 {subject => $subject,
557 date => $header{date},
558 markaswhat => 'submitter',
559 receivedat => "$baddress\@$gEmailDomain",
560 messageid => $header{'message-id'},
563 appendlog($ref,$msg);
567 $data->{found_versions} = [];
568 $data->{fixed_versions} = [];
570 if (defined $pheader{source}) {
571 # source packages are identified by the src: prefix
572 $data->{package} = 'src:'.$pheader{source};
573 } elsif (defined $pheader{package}) {
574 $data->{package} = $pheader{package};
575 if ($data->{package} =~ /^src:(.+)/) {
576 $pheader{source} = $1;
578 } elsif (defined $config{default_package}) {
579 $data->{package} = $config{default_package},
582 my $body = message_body_template('mail/process_no_package',
584 &sendmessage(create_mime_message(
586 From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
588 Subject => "Message with no Package: tag cannot be processed! ($subject)",
589 "Message-ID" => "<handler.x.$nn.nonumnosub\@$gEmailDomain>",
590 "In-Reply-To" => $header{'message-id'},
591 References => join(' ',grep {defined $_} $header{'message-id'},$data->{msgid}),
592 Precedence => 'bulk',
593 "X-$gProject-PR-Message" => 'error'
595 message_body_template('mail/process_no_package',
596 {date => $header{date},
598 messageid => $header{'message-id'},
599 baddress => $baddress,
601 ),[join("\n", @msg)]), '',undef,1);
602 appendlog($ref,$msg);
606 if (defined $config{default_package}) {
608 # if there are no maintainers for this package, assign it to the default package
609 if (not @maintaddrs) {
610 $data->{package} = $config{default_package};
611 $brokenness.= fill_template('mail/process_default_package_selected',
612 {old_package => $pheader{source} || $pheader{package} || 'No package',
613 new_package => $data->{package},
616 # force the maintainers to be rechecked
617 $maintainerschecked = 0;
622 $data->{keywords}= '';
623 if (defined($pheader{'keywords'})) {
624 $data->{keywords}= $pheader{'keywords'};
625 } elsif (defined($pheader{'tags'})) {
626 $data->{keywords}= $pheader{'tags'};
628 if (length($data->{keywords})) {
630 my %gkws = map { ($_, 1) } @gTags;
631 foreach my $kw (sort split(/[,\s]+/, lc($data->{keywords}))) {
632 push @kws, $kw if (defined $gkws{$kw});
634 $data->{keywords} = join(" ", @kws);
636 $data->{severity}= '';
637 if (defined($pheader{'severity'}) || defined($pheader{'priority'})) {
638 $data->{severity}= $pheader{'severity'};
639 $data->{severity}= $pheader{'priority'} unless ($data->{severity});
640 $data->{severity} =~ s/^\s*(.+)\s*$/$1/;
642 if (!grep($_ eq $data->{severity}, @gSeverityList, "$gDefaultSeverity")) {
643 $brokenness.= fill_template('mail/invalid_severity',
644 {severity=>$data->{severity}}
646 $data->{severity}= '';
649 if (defined($pheader{owner})) {
650 $data->{owner}= $pheader{owner};
652 if (defined($pheader{forwarded})) {
653 $data->{forwarded} = $pheader{forwarded};
657 my $hash = get_hashname($ref);
658 $data->{originator} = $replyto;
659 $data->{date} = $intdate;
660 $data->{subject} = $subject;
661 $data->{msgid} = $header{'message-id'};
662 writebug($ref, $data);
664 if (exists $pheader{usertags}) {
666 $user = $pheader{user} if exists $pheader{user};
668 $user =~ s/^.*<(.*)>.*$/$1/;
669 $user =~ s/[(].*[)]//;
670 $user =~ s/^\s*(\S+)\s+.*$/$1/;
671 if ($user ne '' and Debbugs::User::is_valid_user($user)) {
672 $pheader{usertags} =~ s/(?:^\s+|\s+$)//g;
674 read_usertags(\%user_tags,$user);
675 for my $tag (split /[,\s]+/, $pheader{usertags}) {
676 if ($tag =~ /^[a-zA-Z0-9.+\@-]+/) {
678 @bugs_with_tag{@{$user_tags{$tag}||[]}} = (1) x @{$user_tags{$tag}||[]};
679 $bugs_with_tag{$ref} = 1;
680 $user_tags{$tag} = [keys %bugs_with_tag];
683 write_usertags(\%user_tags,$user);
686 $brokenness .= fill_template('mail/invalid_user',
691 overwritefile("db-h/$hash/$ref.report",
697 print {$debugfh} "maintainers >".join(' ',@maintaddrs)."<\n";
699 my $orgsender= defined($header{'sender'}) ? "Original-Sender: $header{'sender'}\n" : '';
700 my $newsubject= $subject; $newsubject =~ s/^$gBug#$ref:*\s*//;
702 my $xcchdr= $header{ 'x-debbugs-cc' } || '';
703 if ($xcchdr =~ m/\S/) {
704 push(@resentccs,get_addresses($xcchdr));
705 $resentccexplain.= fill_template('mail/xdebbugscc',
710 if (@maintaddrs && ($codeletter eq 'B' || $codeletter eq 'M')) {
711 push(@resentccs,@maintaddrs);
712 $resentccexplain.= fill_template('mail/maintainercc',
713 {maintaddrs => \@maintaddrs,
718 @bccs = @addsrcaddrs;
719 if (defined $gStrongList and isstrongseverity($data->{severity})) {
720 push @bccs, "$gStrongList\@$gListDomain";
723 # Send mail to the per bug list subscription too
724 push @bccs, "bugs=$ref\@$gListDomain";
726 if (defined $pheader{source}) {
727 # Prefix source versions with the name of the source package. They
728 # appear that way in version trees so that we can deal with binary
729 # packages moving from one source package to another.
730 if (defined $pheader{'source-version'}) {
731 if ($pheader{'source-version'} !~ m/^$config{package_version_re}$/) {
732 $brokenness .= fill_template('mail/invalid_version',
733 {version => $pheader{'source-version'}},
737 addfoundversions($data, $pheader{source}, $pheader{'source-version'}, '');
739 } elsif (defined $pheader{version}) {
740 if ($pheader{version} !~ m/^$config{package_version_re}$/) {
741 $brokenness .= fill_template('mail/invalid_version',
742 {version => $pheader{version}},
746 addfoundversions($data, $pheader{source}, $pheader{version}, '');
749 writebug($ref, $data);
750 } elsif (defined $pheader{package}) {
751 # TODO: could handle Source-Version: by looking up the source package?
752 if (defined $pheader{version}) {
753 if ($pheader{version} !~ m/^$config{package_version_re}$/) {
754 $brokenness .= fill_template('mail/invalid_version',
755 {version => $pheader{version}},
759 addfoundversions($data, $pheader{package}, $pheader{version}, 'binary');
762 writebug($ref, $data);
765 my $veryquiet= $codeletter eq 'Q';
766 if ($codeletter eq 'M' && !@maintaddrs) {
768 $brokenness.= fill_template('mail/invalid_maintainer',
773 my $resentccval.= join(', ',@resentccs);
774 $resentccval =~ s/\s+\n\s+/ /g; $resentccval =~ s/^\s+/ /; $resentccval =~ s/\s+$//;
776 if (length($resentccval)) {
777 $resentcc= "Resent-CC: $resentccval\n";
780 my $common_headers='';
782 my @tmp = @common_headers;
783 while (my ($key,$value) = splice(@tmp, 0,2)) {
784 $common_headers .= qq($key: $value\n);
787 if ($codeletter eq 'U') { # sent to -submitter
788 &htmllog("Message", "sent on", $data->{originator}, "$gBug#$ref.");
790 Subject: $gBug#$ref: $newsubject
791 Reply-To: $replyto, $ref-quiet\@$gEmailDomain
792 ${orgsender}Resent-To: $data->{originator}
793 ${resentcc}${common_headers}Resent-Date: $tdate
794 Resent-Message-ID: <handler.$ref.$nn\@$gEmailDomain>
795 Resent-Sender: $gMaintainerEmail
796 X-$gProject-PR-Message: report $ref
797 X-$gProject-PR-Package: $data->{package}
798 X-$gProject-PR-Keywords: $data->{keywords}
802 $enc_msg = encode_utf8($enc_msg).$fwd."\n";
803 &sendmessage($enc_msg,[$data->{originator},@resentccs],[@bccs]);
804 } elsif ($codeletter eq 'B') { # Sent to submit
805 my $report_followup = $newref ? 'report' : 'followup';
806 &htmllog($newref ? "Report" : "Information", "forwarded",
807 join(', ',"$gSubmitList\@$gListDomain",@resentccs),
808 "<code>$gBug#$ref</code>".
809 (length($data->{package})? "; Package <code>".html_escape($data->{package})."</code>" : '').
812 Subject: $gBug#$ref: $newsubject
813 Reply-To: $replyto, $ref\@$gEmailDomain
814 Resent-From: $header{'from'}
815 ${orgsender}Resent-To: $gSubmitList\@$gListDomain
816 ${resentcc}${common_headers}Resent-Date: $tdate
817 Resent-Message-ID: <handler.$ref.$nn\@$gEmailDomain>
818 Resent-Sender: $gMaintainerEmail
819 X-$gProject-PR-Message: $report_followup $ref
820 X-$gProject-PR-Package: $data->{package}
821 X-$gProject-PR-Keywords: $data->{keywords}
825 $enc_msg = encode_utf8($enc_msg).$fwd."\n";
826 &sendmessage($enc_msg,["$gSubmitList\@$gListDomain",@resentccs],[@bccs]);
827 } elsif (@resentccs or @bccs) { # Quiet or Maintainer
828 # D and F done far earlier; B just done - so this must be M or Q
829 # We preserve whichever it was in the Reply-To (possibly adding
831 my $report_followup = $newref ? 'report' : 'followup';
833 &htmllog($newref ? "Report" : "Information", "forwarded",
835 "<code>$gBug#$ref</code>".
836 (length($data->{package}) ? "; Package <code>".html_escape($data->{package})."</code>" : '').
839 &htmllog($newref ? "Report" : "Information", "stored",
841 "<code>$gBug#$ref</code>".
842 (length($data->{package}) ? "; Package <code>".html_escape($data->{package})."</code>" : '').
846 Subject: $gBug#$ref: $newsubject
847 Reply-To: $replyto, $ref-$baddressroot\@$gEmailDomain
848 Resent-From: $header{'from'}
849 ${orgsender}Resent-To: $resentccval
851 Resent-Message-ID: <handler.$ref.$nn\@$gEmailDomain>
852 Resent-Sender: $gMaintainerEmail
853 ${common_headers}X-$gProject-PR-Message: $report_followup $ref
854 X-$gProject-PR-Package: $data->{package}
855 X-$gProject-PR-Keywords: $data->{keywords}
859 $enc_msg = encode_utf8($enc_msg).$fwd."\n";
860 &sendmessage($enc_msg,[@resentccs],[@bccs]);
863 my $htmlbreak= length($brokenness) ? "<p>\n".html_escape($brokenness)."\n<p>\n" : '';
864 $htmlbreak =~ s/\n\n/\n<P>\n\n/g;
865 if (length($resentccval)) {
866 $htmlbreak = " Copy sent to <code>".html_escape($resentccval)."</code>.".
870 # Should we send an ack out?
871 if (not exists $header{'x-debbugs-no-ack'} and
873 ($codeletter ne 'U' and
874 (not defined $header{precedence} or
875 $header{'precedence'} !~ /\b(?:bulk|junk|list)\b/
881 # figure out forward explanation
882 my $forwardexplain = '';
885 # will contain info and -info in moreinfo messages
891 &htmllog("Acknowledgement","sent",$replyto,
893 "New $gBug report received and filed, but not forwarded." :
894 "New $gBug report received and forwarded."). $htmlbreak);
895 $thanks = fill_template('mail/process_ack_thanks_new');
898 &htmllog("Acknowledgement","sent",$replyto,
899 ($veryquiet ? "Extra info received and filed, but not forwarded." :
900 $codeletter eq 'M' ? "Extra info received and forwarded to maintainer." :
901 "Extra info received and forwarded to list."). $htmlbreak);
902 $thanks = fill_template('mail/process_ack_thanks_additional');
907 $forwardexplain = fill_template('mail/forward_veryquiet',
909 # these are the headers that quiet messages override
910 $t_h{messageid} = "<handler.$ref.$nn.ack${info}quiet\@$gEmailDomain>";
911 $t_h{pr_message} = "ack${infod}-quiet $ref";
912 $t_h{reply_to} = "$ref-quiet\@$gEmailDomain";
913 $extra_vars->{refreplyto} = "$ref-quiet\@$gEmailDomain";
914 $t_h{subject} = length($info)?
915 "$gBug#$ref: Info received and FILED only ($subject)":
916 "$gBug#$ref: Acknowledgement of QUIET report ($subject)";
918 elsif ($codeletter eq 'M') {
919 $forwardexplain = fill_template('mail/forward_maintonly',
921 # these are the headers that maintonly messages override
922 $t_h{messageid} = "<handler.$ref.$nn.ack{$info}maintonly\@$gEmailDomain>";
923 $t_h{pr_message} = "ack${infod}-maintonly $ref";
924 $t_h{reply_to} = "$ref-maintonly\@$gEmailDomain";
925 $extra_vars->{refreplyto} = "$ref-maintonly\@$gEmailDomain";
926 $t_h{subject} = length($info)?
927 "$gBug#$ref: Info received for maintainer only ($subject)":
928 "$gBug#$ref: Acknowledgement of maintainer-only report ($subject)";
931 $forwardexplain = fill_template('mail/forward_normal',
933 $t_h{messageid} = "<handler.$ref.$nn.ack${info}\@$gEmailDomain>";
934 $t_h{pr_message} = "ack${infod} $ref";
935 $t_h{reply_to} = "$ref\@$gEmailDomain";
936 $extra_vars->{refreplyto} = "$ref\@$gEmailDomain";
937 $t_h{subject} = (defined $info and length($info))?
938 "$gBug#$ref: Info received ($subject)" :
939 "$gBug#$ref: Acknowledgement ($subject)";
941 my $body = message_body_template('mail/process_ack',
942 {forwardexplain => $forwardexplain,
943 resentccexplain => $resentccexplain,
948 &sendmessage(create_mime_message(
950 From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
952 Subject => $t_h{subject},
953 "Message-ID" => $t_h{messageid},
954 "In-Reply-To" => $header{'message-id'},
955 References => $header{'message-id'},
956 Precedence => 'bulk',
957 "X-$gProject-PR-Message" => $t_h{pr_message} || "ack $ref",
958 "X-$gProject-PR-Package" => $data->{package},
959 "X-$gProject-PR-Keywords" => $data->{keywords},
960 # Only have a X-$gProject-PR-Source when we know the source package
961 (defined($source_package) and length($source_package))?("X-$gProject-PR-Source" => $source_package):(),
962 "Reply-To" => $t_h{reply_to} || "$ref\@$gEmailDomain",
963 ],$body,[]), '',undef,1);
966 appendlog($ref,$msg);
967 # unlock the locks we have received
968 while ($locks--) {unfilelock();}
970 ## handle control messages at this point, immediately before finishing
971 my %clonebugs = (-1 => $ref);
974 my $transcript_scalar = '';
975 open my $transcript, ">:scalar:utf8", \$transcript_scalar or
976 die "Unable to create transcript scalar: $!";
977 print {$transcript} "Processing control commands:\n\n";
978 my %affected_packages;
980 # this is the hashref which is passed to all control calls
985 my @common_control_options =
986 (transcript => $transcript,
987 requester => $header{from},
988 request_addr => $baddress.'@'.$config{email_domain},
989 request_msgid => $header{'message-id'},
990 request_subject => $header{subject},
992 request_replyto => $replyto,
994 affected_bugs => \%bug_affected,
995 affected_packages => \%affected_packages,
996 recipients => \%recipients,
999 if (@gExcludeFromControl and grep {$replyto =~ m/\Q$_\E/} @gExcludeFromControl) {
1000 print {$transcript} fill_template('mail/excluded_from_control');
1001 print {$transcript} "Stopping processing here.\n\n";
1003 for my $control_bit (@control_bits) {
1004 $control_bit =~ s/\xef\xbb\xbf//g;
1005 next unless $control_bit =~ m/\S/;
1007 my $temp = decode("utf8",$control_bit,Encode::FB_CROAK);
1008 $control_bit = $temp;
1010 print {$transcript} "> $control_bit\n";
1011 next if $control_bit =~ /^\s*\#/;
1014 if (defined valid_control($control_bit)) {
1015 my ($new_errors,$terminate_control) =
1016 control_line(line => $control_bit,
1017 clonebugs => \%clonebugs,
1019 common_control_options => \@common_control_options,
1021 transcript => $transcript,
1024 replyto => $replyto,
1026 if ($terminate_control) {
1032 if (++$unknowns >= 5) {
1033 print {$transcript} "Too many unknown commands, stopping here.\n\n";
1039 my $temp_transcript = $transcript_scalar;
1041 $temp_transcript = decode("utf8",$temp_transcript,Encode::FB_CROAK);
1043 my @maintccs = determine_recipients(recipients => \%recipients,
1047 my $error_text = $errors > 0 ? " (with $errors errors)":'';
1049 create_mime_message(['X-Loop' => $gMaintainerEmail,
1050 From => "$gMaintainerEmail ($gProject $gBug Tracking System)",
1052 @maintccs ? (Cc => join(', ',@maintccs)):(),
1053 Subject => "Processed${error_text}: $header{subject}",
1054 'Message-ID' => "<handler.s.$nn.transcript\@$gEmailDomain>",
1055 'In-Reply-To' => $header{'message-id'},
1056 References => join(' ',grep {defined $_} $header{'message-id'},$data->{msgid}),
1057 Precedence => 'bulk',
1058 keys %affected_packages ?("X-${gProject}-PR-Package" => join(' ',keys %affected_packages)):(),
1059 keys %affected_packages ?("X-${gProject}-PR-Source" =>
1061 map {defined $_ ?(ref($_)?@{$_}:$_):()}
1062 binary_to_source(binary => [keys %affected_packages],
1063 source_only => 1))):(),
1064 "X-$gProject-PR-Message" => 'transcript',
1067 fill_template('mail/message_body',
1068 {body => $temp_transcript},
1071 utime(time,time,"db-h");
1073 send_mail_message(message => $reply,
1074 recipients => [exists $header{'x-debbugs-no-ack'}?():$replyto,
1075 make_list(values %{{determine_recipients(recipients => \%recipients,
1088 my ($ref,$msg) = @_;
1089 my $log_location = buglog($ref);
1090 die "Unable to find .log for $ref"
1091 if not defined $log_location;
1092 my $logfh = IO::File->new(">>$log_location") or
1093 die "Unable to open $log_location for appending: $!";
1094 write_log_records(logfh => $logfh,
1095 records => [{type => 'incoming-recv',
1098 close ($logfh) or die "Unable to close $log_location: $!";
1104 utime(time,time,"db");
1105 # cleanups are run in an end block now.
1107 #while ($u= $cleanups[$#cleanups]) { &$u; }
1108 unlink("incoming/P$nn") || die "unlinking incoming/P$nn: $!";
1115 my ($whatobj,$whatverb,$where,$desc) = @_;
1116 append_action_to_log(bug => $ref,
1117 action => "$whatobj $whatverb",
1119 request_addr => $where,
1129 while ($msg =~ s/(.*\n)//) {
1136 # strip continuation lines too
1151 send_message($the_message,\@recipients,\@bcc,$do_not_encode)
1153 The first argument is the scalar message, the second argument is the
1154 arrayref of recipients, the third is the arrayref of Bcc:'ed
1157 The final argument turns off header encoding and the addition of the
1158 X-Loop header if true, defaults to false.
1164 my ($msg,$recips,$bcc,$no_encode) = @_;
1165 if (not defined $recips or (!ref($recips) && $recips eq '')
1169 # This is suboptimal. The right solution is to send headers
1170 # separately from the rest of the message and encode them rather
1172 $msg = "X-Loop: $gMaintainerEmail\n" . $msg unless $no_encode;
1173 # The original message received is written out in appendlog, so
1174 # before writing out the other messages we've sent out, we need to
1175 # RFC1522 encode the header.
1176 $msg = encode_headers($msg) unless $no_encode;
1178 my $hash = get_hashname($ref);
1179 #save email to the log
1180 my $logfh = IO::File->new(">>db-h/${hash}/${ref}.log") or
1181 die "opening db-h/$hash/${ref}.log: $!";
1182 write_log_records(logfh => $logfh,
1183 records => {text => stripbccs($msg),
1185 recips => [@{$recips}],
1189 shift @$recips if $recips->[0] eq '-t';
1190 push @$recips, @$bcc;
1193 send_mail_message(message => $msg,
1194 # Because we encode the headers above, we do not want to encode them here
1195 encode_headers => 0,
1196 recipients => $recips);
1199 =head2 message_body_template
1201 message_body_template('mail/ack',{ref=>'foo'});
1203 Creates a message body using a template
1207 sub message_body_template{
1208 my ($template,$extra_var) = @_;
1210 my $body = fill_template($template,$extra_var);
1211 return fill_template('mail/message_body',
1218 =head2 fill_template
1220 fill_template('mail/foo',{foo=>'bar'});
1222 Calls fill_in_template with a default set of variables and any extras
1228 my ($template,$extra_var) = @_;
1230 my $variables = {config => \%config,
1231 defined($ref)?(ref => $ref):(),
1232 defined($data)?(data => $data):(),
1233 refs => [map {exists $clonebugs{$_}?$clonebugs{$_}:$_} keys %bug_affected],
1236 my $hole_var = {'&bugurl' =>
1238 'http://'.$config{cgi_domain}.'/'.
1239 Debbugs::CGI::bug_links(bug=>$_[0],
1244 return fill_in_template(template => $template,
1245 variables => $variables,
1246 hole_var => $hole_var,
1251 # this shole routine is *bad*; will be changed to use
1252 # Debbugs::Recipients and stuff therin in short order.
1253 sub checkmaintainers {
1254 return if $maintainerschecked++;
1255 return if !length($data->{package});
1257 my $anymaintfound=0; my $anymaintnotfound=0;
1258 for my $p (splitpackages($data->{package})) {
1260 $p =~ /((?:src:)?[a-z0-9.+-]+)/;
1262 next unless defined $p;
1263 if (defined $gSubscriptionDomain) {
1264 my @source = binary_to_source(binary => $p,
1269 map {"$_\@$gSubscriptionDomain"} @source;
1271 push @addsrcaddrs, "$p\@$gSubscriptionDomain";
1274 # this is utter hackery until we switch to Debbugs::Recipients
1275 my @maints = package_maintainer(binary => $p);
1277 print {$debugfh} "maintainer add >$p|".join(',',@maints)."<\n";
1279 @temp{@maintaddrs} = @maintaddrs;
1281 grep {$_ ne $replyto and
1282 not exists $temp{$_}} @maints);
1285 print {$debugfh} "maintainer none >$p<\n";
1286 push(@maintaddrs,$gUnknownMaintainerEmail) unless $anymaintnotfound;
1287 $anymaintnotfound++;
1292 if (defined $data->{owner} and length $data->{owner}) {
1293 print {$debugfh} "owner add >$data->{package}|$data->{owner}<\n";
1294 my $addmaint = $data->{owner};
1295 push(@maintaddrs, $addmaint) unless
1296 $addmaint eq $replyto or grep($_ eq $addmaint, @maintaddrs);
1300 =head2 bug_list_forward
1302 bug_list_forward($spool_filename) if $codeletter eq 'L';
1305 Given the spool file, will forward a bug to the per bug mailing list
1306 subscription system.
1310 sub bug_list_forward{
1312 # Read the bug information and package information for passing to
1314 my $bug_fh = IO::File->new("incoming/P$bug_fn",'r') or
1315 die "Unable to open incoming/P$bug_fn $!";
1317 if (not defined $config{bug_subscription_domain} or not
1318 length $config{bug_subscription_domain}) {
1319 unlink("incoming/P$bug_fn") or
1320 die "unlinking incoming/P$bug_fn: $!";
1324 my ($bug_number) = $bug_fn =~ /^L(\d+)\./;
1325 my $data = read_bug(bug => $bug_number);
1328 my $bug_message = <$bug_fh>;
1329 my ($bug_address) = $bug_message =~ /^Received: \(at ([^\)]+)\) by/;
1330 my ($envelope_from) = $bug_message =~ s/\nFrom\s+([^\s]+)[^\n]+\n/\n/;
1331 if (not defined $envelope_from) {
1332 # Try to use the From: header or something to set it
1333 ($envelope_from) = $bug_message =~ /\nFrom:\s+(.+?)\n/;
1334 # Kludgy, and should really be using a full scale header
1335 # parser to do this.
1336 $envelope_from =~ s/^.+?<([^>]+)>.+$/$1/;
1338 my ($header,$body) = split /\n\n/, $bug_message, 2;
1339 # Add X-$gProject-PR-Message: list bug_number, package name, and bug title headers
1340 $header .= qq(\nX-$gProject-PR-Message: list $bug_number\n).
1341 qq(X-$gProject-PR-Package: $data->{package}\n).
1342 qq(X-$gProject-PR-Title: $data->{subject})
1344 print STDERR "Tried to loop me with $envelope_from\n"
1345 and exit 1 if $envelope_from =~ /\Q$gListDomain\E|\Q$gEmailDomain\E/;
1346 print {$debugfh} $envelope_from,qq(\n);
1347 # If we don't have a bug address, something has gone horribly wrong.
1348 print STDERR "Doesn't match: $bug_address\n" and exit 1 unless defined $bug_address;
1349 $bug_address =~ s/\@.+//;
1350 print {$debugfh} "Sending message to bugs=$bug_address\@$config{bug_subscription_domain}\n";
1351 print {$debugfh} $header.qq(\n\n).$body;
1352 send_mail_message(message => $header.qq(\n\n).$body,
1353 recipients => ["bugs=$bug_address\@$config{bug_subscription_domain}"],
1354 envelope_from => $envelope_from,
1355 encode_headers => 0,
1357 unlink("incoming/P$bug_fn") || die "unlinking incoming/P$bug_fn: $!";