]> git.donarmstrong.com Git - debbugs.git/blobdiff - scripts/process.in
* Explain how to close bugs in the ack message
[debbugs.git] / scripts / process.in
index 21bf8af3c2883ff36b79ba9ed2aa7b9556468615..2f9ccc04c20984f32be7acc5f3b1d0d745aa1631 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl
-# $Id: process.in,v 1.98 2005/07/26 04:30:53 don Exp $
+# $Id: process.in,v 1.109 2006/02/09 22:02:04 don Exp $
 #
 # Usage: process nn
 # Temps:  incoming/Pnn
@@ -8,12 +8,17 @@ use POSIX qw(strftime tzset);
 $ENV{"TZ"} = 'UTC';
 tzset();
 
-use IO::File;
 use MIME::Parser;
-use Debbugs::MIME qw(decode_rfc1522);
+use Debbugs::MIME qw(decode_rfc1522 create_mime_message);
+use Debbugs::Mail qw(send_mail_message encode_headers);
+use Debbugs::Packages qw(getpkgsrc);
 
-$config_path = '/etc/debbugs';
-$lib_path = '/usr/lib/debbugs';
+my $config_path = '/etc/debbugs';
+my $lib_path = '/usr/lib/debbugs';
+
+# TODO DLA; needs config reworking and errorlib reworking
+# use warnings;
+# use strict;
 
 require "$config_path/config";
 require "$lib_path/errorlib";
@@ -25,13 +30,13 @@ chdir( "$gSpoolDir" ) || die "chdir spool: $!\n";
 umask(002);
 open DEBUG, ">/dev/null";
 
-defined( $intdate= time ) || &quit( "failed to get time: $!" );
+my $intdate = time or quit("failed to get time: $!");
 
 $_=shift;
-m/^([BMQFDUL])(\d*)\.\d+$/ || &quit("bad argument");
-$codeletter= $1;
-$tryref= length($2) ? $2+0 : -1;
-$nn= $_;
+m/^([BMQFDUL])(\d*)\.\d+$/ or quit("bad argument: $_");
+my $codeletter= $1;
+my $tryref= length($2) ? $2 : -1;
+my $nn= $_;
 
 if (!rename("incoming/G$nn","incoming/P$nn")) 
 {
@@ -39,7 +44,7 @@ if (!rename("incoming/G$nn","incoming/P$nn"))
     &quit("renaming to lock: $!");
 }
 
-$baddress= 'submit' if $codeletter eq 'B';
+my $baddress= 'submit' if $codeletter eq 'B';
 $baddress= 'maintonly' if $codeletter eq 'M';
 $baddress= 'quiet' if $codeletter eq 'Q';
 $baddress= 'forwarded' if $codeletter eq 'F';
@@ -47,20 +52,20 @@ $baddress= 'done' if $codeletter eq 'D';
 $baddress= 'submitter' if $codeletter eq 'U';
 bug_list_forward($nn) if $codeletter eq 'L';
 $baddress || &quit("bad codeletter $codeletter");
-$baddressroot= $baddress;
+my $baddressroot= $baddress;
 $baddress= "$tryref-$baddress" if $tryref>=0;
 
 open(M,"incoming/P$nn");
-@log=<M>;
+my @log=<M>;
 close(M);
 
-@msg=@log;
+my @msg = @log;
 chomp @msg;
 
 print DEBUG "###\n",join("##\n",@msg),"\n###\n";
 
-$tdate = strftime "%a, %d %h %Y %T UTC", localtime;
-$fwd= <<END;
+my $tdate = strftime "%a, %d %h %Y %T +0000", gmtime;
+my $fwd= <<END;
 Received: via spool by $baddress\@$gEmailDomain id=$nn
           (code $codeletter ref $tryref); $tdate
 END
@@ -73,6 +78,7 @@ mkdir "$gSpoolDir/mime.tmp", 0777;
 $parser->output_under("$gSpoolDir/mime.tmp");
 my $entity = eval { $parser->parse_data(join('',@log)) };
 
+my $i;
 if ($entity and $entity->head->tags) {
     @headerlines = @{$entity->head->header};
     chomp @headerlines;
@@ -100,7 +106,10 @@ if ($entity and $entity->head->tags) {
     @bodylines = @msg[$i..$#msg];
 }
 
+my %header;
+
 for my $hdr (@headerlines) {
+    $hdr = decode_rfc1522($hdr);
     $_ = $hdr;
     s/\n\s/ /g;
     &finish if m/^x-loop: (\S+)$/i && $1 eq "$gMaintainerEmail";
@@ -111,7 +120,7 @@ for my $hdr (@headerlines) {
     if (s/^(\S+):\s*//) {
        my $v = lc $1;
        print DEBUG ">$v=$_<\n";
-       $header{$v} = decode_rfc1522($_);
+       $header{$v} = $_;
     } else {
        print DEBUG "!>$_<\n";
     }
@@ -141,11 +150,16 @@ for my $phline (@bodylines)
     $fv =~ s/\s*$//;
     print DEBUG ">$fn|$fv|\n";
     $fn = lc $fn;
-    $fv = lc $fv;
+    # Don't lc owner or forwarded
+    $fv = lc $fv unless $fh =~ /^(?:owner|forwarded)$/;
     $pheader{$fn} = $fv;
     print DEBUG ">$fn~$fv<\n";
 }
 
+# Allow pseudo headers to set x-debbugs- stuff [#179340]
+for my $key (grep /X-Debbugs-.*/i, keys %pheader) {
+     $header{$key} = $pheader{$key} if not exists $header{$key};
+}
 
 $fwd .= join("\n",@msg[$i..$#msg]);
 
@@ -156,7 +170,7 @@ if (defined $header{'resent-from'} && !defined $header{'from'}) {
 }
 defined($header{'from'}) || &quit("no From header");
 
-$replyto = $header{'reply-to'};
+my $replyto = $header{'reply-to'};
 $replyto = '' unless defined $replyto;
 $replyto =~ s/^ +//;
 $replyto =~ s/ +$//;
@@ -164,11 +178,7 @@ unless (length $replyto) {
     $replyto = $header{'from'};
 }
 
-$_= $replyto;
-$_= "$2 <$1>" if m/^([^\<\> \t\n\(\)]+) \(([^\(\)\<\>]+)\)$/;
-$replytocompare= $_;
-print DEBUG "replytocompare >$replytocompare<\n";
-    
+my $subject = '(no subject)';
 if (!defined($header{'subject'})) 
 {
        $brokenness.= <<END;
@@ -178,14 +188,11 @@ useful because the title of a $gBug is determined using this field.
 Please remember to include a Subject field in your messages in future.
 END
 
-# RFC822 actually lists it as an `optional-field'.
-
-    $subject= '(no subject)';
 } else { 
     $subject= $header{'subject'}; 
 }
 
-$ref=-1;
+my $ref=-1;
 $subject =~ s/^Re:\s*//i; $_= $subject."\n";
 if ($tryref < 0 && m/^Bug ?\#(\d+)\D/i) {
     $tryref= $1+0; 
@@ -246,6 +253,20 @@ END
     &filelock('lock/-1'); 
 }
 
+# Attempt to determine which source package this is
+my $source_pr_header = '';
+my $source_package = '';
+if (defined $pheader{source}) {
+     $source_package = $pheader{source};
+}
+elsif (defined $data->{package} or defined $pheader{package}) {
+     my $pkg_src = getpkgsrc();
+     $source_package = $pkg_src->{defined $data->{package}?$data->{package}:$pheader{package}};
+}
+$source_pr_header = "X-$gProject-PR-Source: $source_package\n"
+     if defined $source_package and length $source_package;
+
+# Done and Forwarded Bugs
 if ($codeletter eq 'D' || $codeletter eq 'F') 
 {
     if ($replyto =~ m/$gBounceFroms/o ||
@@ -255,17 +276,19 @@ if ($codeletter eq 'D' || $codeletter eq 'F')
     }
     $markedby= $header{'from'} eq $replyto ? $replyto :
                "$header{'from'} (reply to $replyto)";
-    if ($codeletter eq 'F') {
+    my @generalcc;
+    if ($codeletter eq 'F') { # Forwarded
         (&appendlog,&finish) if length($data->{forwarded});
         $receivedat= "forwarded\@$gEmailDomain";
         $markaswhat= 'forwarded';
         $set_forwarded= $header{'to'};
        if ( length( $gListDomain ) > 0 && length( $gForwardList ) > 0 ) {
+           push @generalcc, "$gForwardList\@$gListDomain";
            $generalcc= "$gForwardList\@$gListDomain";
        } else { 
            $generalcc=''; 
         }
-    } else {
+    } else { # Done
         if (length($data->{done}) and
                 not defined $pheader{'source-version'} and
                 not defined $pheader{'version'}) {
@@ -277,12 +300,14 @@ if ($codeletter eq 'D' || $codeletter eq 'F')
         $set_done= $header{'from'};
        if ( length( $gListDomain ) > 0 && length( $gDoneList ) > 0 ) {
             $generalcc= "$gDoneList\@$gListDomain";
+           push @generalcc, "$gDoneList\@$gListDomain";
        } else { 
            $generalcc=''; 
        }
     }
     if (defined $gStrongList and isstrongseverity($data->{severity})) {
         $generalcc = join ', ', $generalcc, "$gStrongList\@$gListDomain";
+       push @generalcc,"$gStrongList\@$gListDomain";
     }
     if ($ref<0) {
        &htmllog("Warning","sent",$replyto,"Message ignored.");
@@ -331,21 +356,11 @@ END
 
     &checkmaintainers;
 
-    # Add bug mailing lists as appropriate
-    my @bug_mailing_lists;
-    push @bug_mailing_lists, map {"bugs=$_\@$gListDomain"} ($ref, split (/ /, $data->{mergedwith}));
-
+    my @noticecc = grep($_ ne $replyto,@maintaddrs);
     $noticeccval.= join(', ', grep($_ ne $replyto,@maintaddrs));
     $noticeccval =~ s/\s+\n\s+/ /g; 
     $noticeccval =~ s/^\s+/ /; $noticeccval =~ s/\s+$//;
 
-    $generalcc = join(', ', $generalcc, @addsrcaddrs, @bug_mailing_lists);
-    $generalcc =~ s/\s+\n\s+/ /g;
-    $generalcc =~ s/^\s+/ /; $generalcc =~ s/\s+$//;
-
-    if (length($noticeccval)) { $noticecc= "Cc: $noticeccval\n"; }
-    if (length($generalcc)) { $noticecc.= "Bcc: $generalcc\n"; }
-
     @process= ($ref,split(/ /,$data->{mergedwith}));
     $orgref= $ref;
 
@@ -367,6 +382,14 @@ END
             }
         }
 
+       # Add bug mailing list to $generalbcc as appropriate
+       # This array is used to specify bcc in the cases where we're using create_mime_message.
+       my @generalbcc = (@generalcc,@addsrcaddrs,"bugs=$ref\@$gListDomain");
+       my $generalbcc = join(', ', $generalcc, @addsrcaddrs,"bugs=$ref\@$gListDomain");
+       $generalbcc =~ s/\s+\n\s+/ /g;
+       $generalbcc =~ s/^\s+/ /; $generalbcc =~ s/\s+$//;
+       if (length $generalbcc) {$generalbcc = "Bcc: $generalbcc\n"};
+
        writebug($ref, $data);
 
        my $hash = get_hashname($ref);
@@ -374,18 +397,21 @@ END
         $x= join('',<O>); close(O);
         if ($codeletter eq 'F') {
            &htmllog("Reply","sent",$replyto,"You have marked $gBug as forwarded.");
-            &sendmessage(<<END."---------------------------------------\n".join( "\n", @msg ), '');
-From: $gMaintainerEmail ($gProject $gBug Tracking System)
-To: $replyto
-${noticecc}Subject: $gBug#$ref: marked as forwarded ($data->{subject})
-Message-ID: <header.$ref.$nn.ackfwdd\@$gEmailDomain>
-In-Reply-To: $header{'message-id'}
-References: $header{'message-id'} $data->{msgid}
-Precedence: bulk
-X-$gProject-PR-Message: forwarded $ref
-X-$gProject-PR-Package: $data->{package}
-X-$gProject-PR-Keywords: $data->{keywords}
-
+            &sendmessage(create_mime_message(
+            ["X-Loop"      => "$gMaintainerEmail",
+             From          => "$gMaintainerEmail ($gProject $gBug Tracking System)",
+              To            => "$replyto",
+              Subject       => "$gBug#$ref: marked as forwarded ($data->{subject})",
+              "Message-ID"  => "<header.$ref.$nn.ackfwdd\@$gEmailDomain>",
+              "In-Reply-To" => $header{'message-id'},
+              References    => "$header{'message-id'} $data->{msgid}",
+              Precedence    => 'bulk',
+              "X-$gProject-PR-Message"  => "forwarded $ref",
+              "X-$gProject-PR-Package"  => $data->{package},
+              "X-$gProject-PR-Keywords" => $data->{keywords},
+             # Only have a X-$gProject-PR-Source when we know the source package
+             length($source_package)?("X-$gProject-PR-Source" => $source_package):(),
+             ],<<END ,[join("\n",@msg)]),'',[$replyto,@generalbcc,@noticecc],1);
 Your message dated $header{'date'}
 with message-id $header{'message-id'}
 has caused the $gProject $gBug report #$ref,
@@ -404,18 +430,21 @@ END
 
         } else {
            &htmllog("Reply","sent",$replyto,"You have taken responsibility.");
-            &sendmessage(<<END."--------------------------------------\n".$x."---------------------------------------\n".join( "\n", @msg ), '');
-From: $gMaintainerEmail ($gProject $gBug Tracking System)
-To: $replyto
-${noticecc}Subject: $gBug#$ref: marked as done ($data->{subject})
-Message-ID: <handler.$ref.$nn.ackdone\@$gEmailDomain>
-In-Reply-To: $header{'message-id'}
-References: $header{'message-id'} $data->{msgid}
-Precedence: bulk
-X-$gProject-PR-Message: closed $ref
-X-$gProject-PR-Package: $data->{package}
-X-$gProject-PR-Keywords: $data->{keywords}
-
+            &sendmessage(create_mime_message(
+            ["X-Loop"      => "$gMaintainerEmail",
+             From          => "$gMaintainerEmail ($gProject $gBug Tracking System)",
+              To            => $replyto,
+              Subject       => "$gBug#$ref: marked as done ($data->{subject})",
+              "Message-ID"  => "<handler.$ref.$nn.ackdone\@$gEmailDomain>",
+              "In-Reply-To" => $header{'message-id'},
+              References    => "$header{'message-id'} $data->{msgid}",
+              Precedence    => 'bulk',
+              "X-$gProject-PR-Message"  => "closed $ref",
+              "X-$gProject-PR-Package"  => $data->{package},
+              "X-$gProject-PR-Keywords" => $data->{keywords},
+             # Only have a X-$gProject-PR-Source when we know the source package
+             length($source_package)?("X-$gProject-PR-Source" => $source_package):(),
+             ],<<END ,[$x,join("\n",@msg)]),'',[$replyto,@generalbcc,@noticecc],1);
 Your message dated $header{'date'}
 with message-id $header{'message-id'}
 and subject line $subject
@@ -435,29 +464,32 @@ $gMaintainer
 END
             &htmllog("Notification","sent",$data->{originator}, 
                "$gBug acknowledged by developer.");
-            &sendmessage(<<END.join("\n",@msg),'');
-From: $gMaintainerEmail ($gProject $gBug Tracking System)
-To: $data->{originator}
-Subject: $gBug#$ref acknowledged by developer
-         ($header{'subject'})
-Message-ID: <handler.$ref.$nn.notifdone\@$gEmailDomain>
-In-Reply-To: $data->{msgid}
-References: $header{'message-id'} $data->{msgid}
-X-$gProject-PR-Message: they-closed $ref
-X-$gProject-PR-Package: $data->{package}
-X-$gProject-PR-Keywords: $data->{keywords}
-Reply-To: $ref\@$gEmailDomain
-
+            &sendmessage(create_mime_message(
+            ["X-Loop"      => "$gMaintainerEmail",
+             From          => "$gMaintainerEmail ($gProject $gBug Tracking System)",
+              To            => "$data->{originator}",
+              Subject       => "$gBug#$ref closed by $markedby ($header{'subject'})",
+              "Message-ID"  => "<handler.$ref.$nn.notifdone\@$gEmailDomain>",
+              "In-Reply-To" => "$data->{msgid}",
+              References    => "$header{'message-id'} $data->{msgid}",
+              "X-$gProject-PR-Message"  => "they-closed $ref",
+              "X-$gProject-PR-Package"  => "$data->{package}",
+              "X-$gProject-PR-Keywords" => "$data->{keywords}",
+             # Only have a X-$gProject-PR-Source when we know the source package
+             length($source_package)?("X-$gProject-PR-Source" => $source_package):(),
+              "Reply-To"                => "$ref\@$gEmailDomain",
+              "Content-Type"            => 'text/plain; charset="utf-8"',
+             ],<<END ,[join("\n",@msg)]),'',undef,1);
 This is an automatic notification regarding your $gBug report
 #$ref: $data->{subject},
 which was filed against the $data->{package} package.
 
-It has been closed by one of the developers, namely
-$markedby.
+It has been closed by $markedby.
 
 Their explanation is attached below.  If this explanation is
 unsatisfactory and you have not received a better one in a separate
-message then please contact the developer, by replying to this email.
+message then please contact $markedby by replying
+to this email.
 
 $gMaintainer
 (administrator, $gProject $gBugs database)
@@ -525,16 +557,17 @@ END
         $data->{package} = $pheader{package};
     } else {
        &htmllog("Warning","sent",$replyto,"Message not forwarded.");
-        &sendmessage(<<END."---------------------------------------------------------------------------\n".join("\n", @msg), '');
-From: $gMaintainerEmail ($gProject $gBug Tracking System)
-To: $replyto
-Subject: Message with no Package: tag cannot be processed!
-         ($subject)
-Message-ID: <handler.x.$nn.nonumnosub\@$gEmailDomain>
-In-Reply-To: $header{'message-id'}
-References: $header{'message-id'} $data->{msgid}
-Precedence: bulk
-X-$gProject-PR-Message: error
+        &sendmessage(create_mime_message(
+                       ["X-Loop"      => "$gMaintainerEmail",
+                       From          => "$gMaintainerEmail ($gProject $gBug Tracking System)",
+                        To            => $replyto,
+                        Subject       => "Message with no Package: tag cannot be processed! ($subject)",
+                        "Message-ID"  => "<handler.x.$nn.nonumnosub\@$gEmailDomain>",
+                        "In-Reply-To" => $header{'message-id'},
+                        References    => "$header{'message-id'} $data->{msgid}",
+                        Precedence    => 'bulk',
+                        "X-$gProject-PR-Message" => 'error'
+                      ],<<END,[join("\n", @msg)]), '',undef,1);
 
 Your message didn't have a Package: line at the start (in the
 pseudo-header following the real mail header), or didn't have a
@@ -598,6 +631,9 @@ END
     if (defined($pheader{owner})) {
         $data->{owner}= $pheader{owner};
     }
+    if (defined($pheader{forwarded})) {
+       $data->{'forwarded-to'} = $pheader{forwarded};
+    }
     &filelock("nextnumber.lock");
     open(N,"nextnumber") || &quit("nextnumber: read: $!");
     $v=<N>; $v =~ s/\n$// || &quit("nextnumber bad format");
@@ -699,9 +735,9 @@ Resent-Sender: $gMaintainerEmail
 X-$gProject-PR-Message: report $ref
 X-$gProject-PR-Package: $data->{package}
 X-$gProject-PR-Keywords: $data->{keywords}
-$fwd
+${source_pr_header}$fwd
 END
-} elsif ($codeletter eq 'B') {
+} elsif ($codeletter eq 'B') { # Sent to submit
     &htmllog($newref ? "Report" : "Information", "forwarded",
              join(', ',"$gSubmitList\@$gListDomain",@resentccs),
              "<code>$gBug#$ref</code>".
@@ -718,9 +754,9 @@ Resent-Sender: $gMaintainerEmail
 X-$gProject-PR-Message: report $ref
 X-$gProject-PR-Package: $data->{package}
 X-$gProject-PR-Keywords: $data->{keywords}
-$fwd
+${source_pr_header}$fwd
 END
-} elsif (@resentccs or @bccs) {
+} elsif (@resentccs or @bccs) { # Quiet or Maintainer
     # D and F done far earlier; B just done - so this must be M or Q
     # We preserve whichever it was in the Reply-To (possibly adding
     # the $gBug#).
@@ -748,7 +784,7 @@ Resent-Sender: $gMaintainerEmail
 X-$gProject-PR-Message: report $ref
 X-$gProject-PR-Package: $data->{package}
 X-$gProject-PR-Keywords: $data->{keywords}
-$fwd
+${source_pr_header}$fwd
 END
 }
 
@@ -764,20 +800,23 @@ unless (exists $header{'x-debbugs-no-ack'}) {
                  ($veryquiet ?
                   "New $gBug report received and filed, but not forwarded." :
                   "New $gBug report received and forwarded."). $htmlbreak);
-        &sendmessage($veryquiet ? <<END : $codeletter eq 'M' ? <<END : <<END,'');
-From: $gMaintainerEmail ($gProject $gBug Tracking System)
-To: $replyto
-Subject: $gBug#$ref: Acknowledgement of QUIET report
-         ($subject)
-Message-ID: <handler.$ref.$nn.ackquiet\@$gEmailDomain>
-In-Reply-To: $header{'message-id'}
-References: $header{'message-id'}
-Precedence: bulk
-X-$gProject-PR-Message: ack-quiet $ref
-X-$gProject-PR-Package: $data->{package}
-X-$gProject-PR-Keywords: $data->{keywords}
-Reply-To: $ref-quiet\@$gEmailDomain
-
+       if ($veryquiet) {
+            &sendmessage(create_mime_message(
+                      ["X-Loop"      => "$gMaintainerEmail",
+                       From          => "$gMaintainerEmail ($gProject $gBug Tracking System)",
+                       To            => $replyto,
+                        Subject       => "$gBug#$ref: Acknowledgement of QUIET report ($subject)",
+                        "Message-ID"  => "<handler.$ref.$nn.ackquiet\@$gEmailDomain>",
+                        "In-Reply-To" => $header{'message-id'},
+                        References    => $header{'message-id'},
+                        Precedence    => 'bulk',
+                       "X-$gProject-PR-Message"  => "ack-quiet $ref",
+                       "X-$gProject-PR-Package"  => $data->{package},
+                       "X-$gProject-PR-Keywords" => $data->{keywords},
+                       # Only have a X-$gProject-PR-Source when we know the source package
+                       length($source_package)?("X-$gProject-PR-Source" => $source_package):(),
+                       "Reply-To"                => "$ref-quiet\@$gEmailDomain",
+                      ],<<END,[join("\n", @msg)]), '',undef,1);
 Thank you for the problem report you have sent regarding $gProject.
 This is an automatically generated reply, to let you know your message
 has been received.  It has not been forwarded to the package maintainers
@@ -789,25 +828,34 @@ If you wish to submit further information on your problem, please send it
 to $ref-$baddressroot\@$gEmailDomain (and *not*
 to $baddress\@$gEmailDomain).
 
+If you have filed this report in error and wish to close it, please
+send mail to $ref-done\@$gEmailDomain with an explanation
+why the bug report should be closed.
+
 Please do not reply to the address at the top of this message,
 unless you wish to report a problem with the $gBug-tracking system.
 $brokenness
 $gMaintainer
 (administrator, $gProject $gBugs database)
 END
-From: $gMaintainerEmail ($gProject $gBug Tracking System)
-To: $replyto
-Subject: $gBug#$ref: Acknowledgement of maintainer-only report
-         ($subject)
-Message-ID: <handler.$ref.$nn.ackmaint\@$gEmailDomain>
-In-Reply-To: $header{'message-id'}
-References: $header{'message-id'}
-Precedence: bulk
-X-$gProject-PR-Message: ack-maintonly $ref
-X-$gProject-PR-Package: $data->{package}
-X-$gProject-PR-Keywords: $data->{keywords}
-Reply-To: $ref-maintonly\@$gEmailDomain
-
+       }
+       elsif ($codeletter eq 'M') { # Maintonly
+            &sendmessage(create_mime_message(
+                      ["X-Loop"      => "$gMaintainerEmail",
+                       From          => "$gMaintainerEmail ($gProject $gBug Tracking System)",
+                       To            => $replyto,
+                        Subject       => "$gBug#$ref: Acknowledgement of maintainer-only report ($subject)",
+                        "Message-ID"  => "<handler.$ref.$nn.ackmaint\@$gEmailDomain>",
+                        "In-Reply-To" => $header{'message-id'},
+                        References    => $header{'message-id'},
+                        Precedence    => 'bulk',
+                       "X-$gProject-PR-Message" => "ack-maintonly $ref",
+                       "X-$gProject-PR-Package"  => $data->{package},
+                       "X-$gProject-PR-Keywords" => $data->{keywords},
+                       # Only have a X-$gProject-PR-Source when we know the source package
+                       length($source_package)?("X-$gProject-PR-Source" => $source_package):(),
+                       "Reply-To"               => "$ref-maintonly\@$gEmailDomain",
+                      ],<<END,[]), '',undef,1);
 Thank you for the problem report you have sent regarding $gProject.
 This is an automatically generated reply, to let you know your message has
 been received.  It is being forwarded to the package maintainers (but not
@@ -818,24 +866,34 @@ If you wish to submit further information on your problem, please send
 it to $ref-$baddressroot\@$gEmailDomain (and *not*
 to $baddress\@$gEmailDomain).
 
+If you have filed this report in error and wish to close it, please
+send mail to $ref-done\@$gEmailDomain with an explanation
+why the bug report should be closed.
+
 Please do not reply to the address at the top of this message,
 unless you wish to report a problem with the $gBug-tracking system.
 $brokenness
 $gMaintainer
 (administrator, $gProject $gBugs database)
 END
-From: $gMaintainerEmail ($gProject $gBug Tracking System)
-To: $replyto
-Subject: $gBug#$ref: Acknowledgement ($subject)
-Message-ID: <handler.$ref.$nn.ack\@$gEmailDomain>
-In-Reply-To: $header{'message-id'}
-References: $header{'message-id'}
-Precedence: bulk
-X-$gProject-PR-Message: ack $ref
-X-$gProject-PR-Package: $data->{package}
-X-$gProject-PR-Keywords: $data->{keywords}
-Reply-To: $ref\@$gEmailDomain
-
+                  }
+       else {
+            &sendmessage(create_mime_message(
+                      ["X-Loop"      => "$gMaintainerEmail",
+                       From          => "$gMaintainerEmail ($gProject $gBug Tracking System)",
+                       To            => $replyto,
+                        Subject       => "$gBug#$ref: Acknowledgement ($subject)",
+                        "Message-ID"  => "<handler.$ref.$nn.ack\@$gEmailDomain>",
+                        "In-Reply-To" => $header{'message-id'},
+                        References    => $header{'message-id'},
+                        Precedence    => 'bulk',
+                       "X-$gProject-PR-Message"  => "ack $ref",
+                       "X-$gProject-PR-Package"  => $data->{package},
+                       "X-$gProject-PR-Keywords" => $data->{keywords},
+                       # Only have a X-$gProject-PR-Source when we know the source package
+                       length($source_package)?("X-$gProject-PR-Source" => $source_package):(),
+                       "Reply-To"                => "$ref\@$gEmailDomain",
+                      ],<<END,[]), '',undef,1);
 Thank you for the problem report you have sent regarding $gProject.
 This is an automatically generated reply, to let you know your message has
 been received.  It is being forwarded to the package maintainers and other
@@ -845,32 +903,40 @@ If you wish to submit further information on your problem, please send
 it to $ref\@$gEmailDomain (and *not* to
 $baddress\@$gEmailDomain).
 
+If you have filed this report in error and wish to close it, please
+send mail to $ref-done\@$gEmailDomain with an explanation
+why the bug report should be closed.
+
 Please do not reply to the address at the top of this message,
 unless you wish to report a problem with the $gBug-tracking system.
 $brokenness
 $gMaintainer
 (administrator, $gProject $gBugs database)
 END
+                  }
     } elsif ($codeletter ne 'U' and
              $header{'precedence'} !~ /\b(?:bulk|junk|list)\b/) {
         &htmllog("Acknowledgement","sent",$replyto,
                  ($veryquiet ? "Extra info received and filed, but not forwarded." :
                   $codeletter eq 'M' ? "Extra info received and forwarded to maintainer." :
                   "Extra info received and forwarded to list."). $htmlbreak);
-        &sendmessage($veryquiet ? <<END : $codeletter eq 'M' ? <<END : <<END,'');
-From: $gMaintainerEmail ($gProject $gBug Tracking System)
-To: $replyto
-Subject: $gBug#$ref: Info received and FILED only
-         (was $subject)
-Message-ID: <handler.$ref.$nn.ackinfoquiet\@$gEmailDomain>
-In-Reply-To: $header{'message-id'}
-References: $header{'message-id'}
-Precedence: bulk
-X-$gProject-PR-Message: ack-info-quiet $ref
-X-$gProject-PR-Package: $data->{package}
-X-$gProject-PR-Keywords: $data->{keywords}
-Reply-To: $ref-quiet\@$gEmailDomain
-
+       if ($veryquiet) {
+            &sendmessage(create_mime_message(
+                      ["X-Loop"      => "$gMaintainerEmail",
+                       From          => "$gMaintainerEmail ($gProject $gBug Tracking System)",
+                       To            => $replyto,
+                        Subject       => "$gBug#$ref: Info received and FILED only (was $subject)",
+                        "Message-ID"  => "<handler.$ref.$nn.ackinfoquiet\@$gEmailDomain>",
+                        "In-Reply-To" => $header{'message-id'},
+                        References    => $header{'message-id'},
+                        Precedence    => 'bulk',
+                       "X-$gProject-PR-Message" => "ack-info-quiet $ref",
+                       "X-$gProject-PR-Package"  => $data->{package},
+                       "X-$gProject-PR-Keywords" => $data->{keywords},
+                       # Only have a X-$gProject-PR-Source when we know the source package
+                       length($source_package)?("X-$gProject-PR-Source" => $source_package):(),
+                       "Reply-To"               => "$ref-maintonly\@$gEmailDomain",
+                      ],<<END,[]), '',undef,1);
 Thank you for the additional information you have supplied regarding
 this problem report.  It has NOT been forwarded to the package
 maintainers, but will accompany the original report in the $gBug
@@ -886,19 +952,22 @@ $brokenness
 $gMaintainer
 (administrator, $gProject $gBugs database)
 END
-From: $gMaintainerEmail ($gProject $gBug Tracking System)
-To: $replyto
-Subject: $gBug#$ref: Info received for maintainer only
-         (was $subject)
-Message-ID: <handler.$ref.$nn.ackinfomaint\@$gEmailDomain>
-In-Reply-To: $header{'message-id'}
-References: $header{'message-id'}
-Precedence: bulk
-X-$gProject-PR-Message: ack-info-maintonly $ref
-X-$gProject-PR-Package: $data->{package}
-X-$gProject-PR-Keywords: $data->{keywords}
-Reply-To: $ref-maintonly\@$gEmailDomain
-
+                }
+       elsif ($codeletter eq 'M') {
+            &sendmessage(create_mime_message(
+                      ["X-Loop"      => "$gMaintainerEmail",
+                       From          => "$gMaintainerEmail ($gProject $gBug Tracking System)",
+                       To            => $replyto,
+                        Subject       => "$gBug#$ref: Info received for maintainer only (was $subject)",
+                        "Message-ID"  => "<handler.$ref.$nn.ackinfomaint\@$gEmailDomain>",
+                        "In-Reply-To" => $header{'message-id'},
+                        References    => "$header{'message-id'} $data->{msgid}",
+                        Precedence    => 'bulk',
+                       "X-$gProject-PR-Message" => "ack-info-maintonly $ref",
+                       "X-$gProject-PR-Package"  => $data->{package},
+                       "X-$gProject-PR-Keywords" => $data->{keywords},
+                       "Reply-To"               => "$ref-maintonly\@$gEmailDomain",
+                      ],<<END,[]), '',undef,1);
 Thank you for the additional information you have supplied regarding
 this problem report.  It has been forwarded to the package maintainer(s)
 (but not to other interested parties) to accompany the original report.
@@ -912,17 +981,24 @@ $brokenness
 $gMaintainer
 (administrator, $gProject $gBugs database)
 END
-From: $gMaintainerEmail ($gProject $gBug Tracking System)
-To: $replyto
-Subject: $gBug#$ref: Info received (was $subject)
-Message-ID: <handler.$ref.$nn.ackinfo\@$gEmailDomain>
-In-Reply-To: $header{'message-id'}
-References: $header{'message-id'}
-Precedence: bulk
-X-$gProject-PR-Message: ack-info $ref
-X-$gProject-PR-Package: $data->{package}
-X-$gProject-PR-Keywords: $data->{keywords}
-
+                  }
+       else {
+            &sendmessage(create_mime_message(
+                      ["X-Loop"      => "$gMaintainerEmail",
+                       From          => "$gMaintainerEmail ($gProject $gBug Tracking System)",
+                       To            => $replyto,
+                        Subject       => "$gBug#$ref: Info received ($subject)",
+                        "Message-ID"  => "<handler.$ref.$nn.ackinfo\@$gEmailDomain>",
+                        "In-Reply-To" => $header{'message-id'},
+                        References    => "$header{'message-id'} $data->{msgid}",
+                        Precedence    => 'bulk',
+                       "X-$gProject-PR-Message"  => "ack-info $ref",
+                       "X-$gProject-PR-Package"  => $data->{package},
+                       "X-$gProject-PR-Keywords" => $data->{keywords},
+                       # Only have a X-$gProject-PR-Source when we know the source package
+                       length($source_package)?("X-$gProject-PR-Source" => $source_package):(),
+                       "Reply-To"                => "$ref\@$gEmailDomain",
+                      ],<<END,[]), '',undef,1);
 Thank you for the additional information you have supplied regarding
 this problem report.  It has been forwarded to the package maintainer(s)
 and to other interested parties to accompany the original report.
@@ -936,10 +1012,9 @@ $brokenness
 $gMaintainer
 (administrator, $gProject $gBugs database)
 END
-# Reply-To: in previous ack disabled by doogie due to mail loops.
-# Are these still a concern?
-# Reply-To: $ref\@$gEmailDomain
-    }
+
+                  }
+   }
 }
 
 &appendlog;
@@ -974,8 +1049,6 @@ sub finish {
 
 &quit("wot no exit");
 
-sub chldhandle { $chldexit = 'yes'; }
-
 sub htmllog {
     local ($whatobj,$whatverb,$where,$desc) = @_;
     my $hash = get_hashname($ref);
@@ -1013,12 +1086,34 @@ sub stripbccs {
     return $ret . $msg;
 }
 
+=head2 send_message
+
+     send_message($the_message,\@recipients,\@bcc,$do_not_encode)
+
+The first argument is the scalar message, the second argument is the
+arrayref of recipients, the third is the arrayref of Bcc:'ed
+recipients.
+
+The final argument turns off header encoding and the addition of the
+X-Loop header if true, defaults to false.
+
+=cut
+
+
 sub sendmessage {
-    local ($msg,$recips,$bcc) = @_;
-    if ((!ref($recips) && $recips eq '') || @$recips == 0) {
-        $recips = ['-t'];
+    my ($msg,$recips,$bcc,$no_encode) = @_;
+    if (not defined $recips or (!ref($recips) && $recips eq '')
+       or @$recips == 0) {
+       $recips = ['-t'];
     }
-    $msg = "X-Loop: $gMaintainerEmail\n" . $msg;
+    # This is suboptimal. The right solution is to send headers
+    # separately from the rest of the message and encode them rather
+    # than doing this.
+    $msg = "X-Loop: $gMaintainerEmail\n" . $msg unless $no_encode;
+    # The original message received is written out in appendlog, so
+    # before writing out the other messages we've sent out, we need to
+    # RFC1522 encode the header.
+    $msg = encode_headers($msg) unless $no_encode;
 
     my $hash = get_hashname($ref);
     #save email to the log
@@ -1033,9 +1128,13 @@ sub sendmessage {
         push @$recips, @$bcc;
     }
 
-    send_mail_message($msg,$recips);
+    send_mail_message(message        => $msg,
+                     # Because we encode the headers above, we do not want to encode them here
+                     encode_headers => 0,
+                     recipients     => $recips);
 }
 
+my $maintainerschecked = 0;
 sub checkmaintainers {
     return if $maintainerschecked++;
     return if !length($data->{package});
@@ -1045,7 +1144,8 @@ sub checkmaintainers {
        m/^\s*$/ && next;
         m/^(\S+)\s+(\S.*\S)\s*\n$/ || &quit("maintainers bogus \`$_'");
         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
-        $maintainerof{$1}= $2;
+       # use the package which is normalized to lower case; we do this because we lc the pseudo headers.
+        $maintainerof{$a}= $2;
     }
     close(MAINT);
     open(MAINT,"$gMaintainerFileOverride") || die &quit("maintainers.override open: $!");
@@ -1054,7 +1154,8 @@ sub checkmaintainers {
        m/^\s*$/ && next;
         m/^(\S+)\s+(\S.*\S)\s*\n$/ || &quit("maintainers.override bogus \`$_'");
         $a= $1; $b= $2; $a =~ y/A-Z/a-z/;
-        $maintainerof{$1}= $2;
+       # use the package which is normalized to lower case; we do this because we lc the pseudo headers.
+        $maintainerof{$a}= $2;
     }
     close(MAINT);
     open(SOURCES,"$gPackageSource") || &quit("pkgsrc open: $!");
@@ -1082,8 +1183,7 @@ sub checkmaintainers {
            print DEBUG "maintainer add >$p|$maintainerof{$p}<\n";
             $addmaint= $maintainerof{$p};
             push(@maintaddrs,$addmaint) unless
-                ($addmaint eq $replyto and $codeletter ne 'M') || 
-                    grep($_ eq $addmaint, @maintaddrs);
+                $addmaint eq $replyto || grep($_ eq $addmaint, @maintaddrs);
             $anymaintfound++;
         } else {
            print DEBUG "maintainer none >$p<\n";
@@ -1101,80 +1201,6 @@ sub checkmaintainers {
     }
 }
 
-=head2 send_mail_message
-
-     send_mail_message($message,[@recipients],$envelope_from)
-
-Sends a mail message out to a set of recepients with envelope sender
-$envelope_from; if $envelope_from is not set, defaults to
-$gMaintainerEmail.
-
-=cut
-
-sub send_mail_message{
-     my ($message,$recipients,$envelope_from) = @_;
-
-     # Default to $gMaintainerEmail
-     $envelope_from ||= $gMaintainerEmail;
-
-     print DEBUG "sending mail to ".join(', ',@$recipients)." with -f $envelope_from";
-     local $_ = '';
-     $SIG{'CHLD'}='chldhandle';
-     #print DEBUG "mailing sigchild set up<\n";
-     our $chldexit = 'no';
-     our $c= open(U,"-|");
-     #print DEBUG "mailing opened pipe fork<\n";
-     defined($c) || die $!;
-     #print DEBUG "mailing opened pipe fork ok $c<\n";
-     if (!$c) { # ie, we are in the child process
-         #print DEBUG "mailing child<\n";
-         unless (open(STDERR,">&STDOUT")) {
-              #print DEBUG "mailing child opened stderr<\n";
-              print STDOUT "redirect stderr: $!\n";
-              #print DEBUG "mailing child opened stderr fail<\n";
-              exit 1;
-              #print DEBUG "mailing child opened stderr fail exit !?<\n";
-         }
-         #print DEBUG "mailing child opened stderr ok<\n";
-         $c= open(D,"|-");
-         #print DEBUG "mailing child forked again<\n";
-         defined($c) || die $!;
-         #print DEBUG "mailing child forked again ok $c<\n";
-         if (!$c) { # ie, we are the child process
-              #print DEBUG "mailing grandchild<\n";
-              exec '/usr/lib/sendmail', (defined $envelope_from?'-f'.$envelope_from:''),'-odq','-oem','-oi',
-                   @{$recipients};
-              #print DEBUG "mailing grandchild exec failed<\n";
-              die $!;
-              #print DEBUG "mailing grandchild died !?<\n";
-         }
-         #print DEBUG "mailing child not grandchild<\n";
-         print(D $message) || die $!;
-         #print DEBUG "mailing child printed msg<\n";
-         close(D);
-         #print DEBUG "mailing child closed pipe<\n";
-         die "\n*** command returned exit status $?\n" if $?;
-         #print DEBUG "mailing child exit status ok<\n";
-         exit 0;
-         #print DEBUG "mailing child exited ?!<\n";
-     }
-     #print DEBUG "mailing parent<\n";
-     $results='';
-     #print DEBUG "mailing parent results emptied<\n";
-     while( $chldexit eq 'no' ) { $results.= $_; }
-     #print DEBUG "mailing parent results read >$results<\n";
-     close(U);
-     #print DEBUG "mailing parent results closed<\n";
-     $results.= "\n*** child returned exit status $?\n" if $?;
-     #print DEBUG "mailing parent exit status ok<\n";
-     $SIG{'CHLD'}='DEFAULT';
-     #print DEBUG "mailing parent sigchild default<\n";
-     if (length($results)) { &quit("running sendmail: $results"); }
-     #print DEBUG "mailing parent results ok<\n";
-
-
-}
-
 =head2 bug_list_forward
 
      bug_list_forward($spool_filename) if $codeletter eq 'L';
@@ -1218,9 +1244,10 @@ sub bug_list_forward{
      $bug_address =~ s/\@.+//;
      print DEBUG "Sending message to bugs=$bug_address\@$gListDomain\n";
      print DEBUG $header.qq(\n\n).$body;
-     send_mail_message($header.qq(\n\n).$body,
-                      ["bugs=$bug_address\@$gListDomain"],
-                      $envelope_from,
+     send_mail_message(message        => $header.qq(\n\n).$body,
+                      recipients     => ["bugs=$bug_address\@$gListDomain"],
+                      envelope_from  => $envelope_from,
+                      encode_headers => 0,
                      );
      unlink("incoming/P$bug_fn") || &quit("unlinking incoming/P$bug_fn: $!");
      exit 0;