]> git.donarmstrong.com Git - debbugs.git/blobdiff - Debbugs/Mail.pm
switch to compatibility level 12
[debbugs.git] / Debbugs / Mail.pm
index a00b41c4b72fcf796d6bee1eb5e2ab212fca50ed..e4c8bf7da825e53ce64b3f0849079237e07745fa 100644 (file)
@@ -39,7 +39,7 @@ END
 use warnings;
 use strict;
 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
-use base qw(Exporter);
+use Exporter qw(import);
 
 use IPC::Open3;
 use POSIX qw(:sys_wait_h strftime);
@@ -49,7 +49,7 @@ use Debbugs::MIME qw(encode_rfc1522);
 use Debbugs::Config qw(:config);
 use Params::Validate qw(:types validate_with);
 use Encode qw(encode is_utf8);
-use Debbugs::UTF8 qw(encode_utf8_safely);
+use Debbugs::UTF8 qw(encode_utf8_safely convert_to_utf8);
 
 use Debbugs::Packages;
 
@@ -334,7 +334,7 @@ sub send_mail_message{
                                         message              => {type => SCALAR,
                                                                 },
                                         envelope_from        => {type => SCALAR,
-                                                                 optional => 1,
+                                                                 default => $config{envelope_from},
                                                                 },
                                         recipients           => {type => ARRAYREF|UNDEF,
                                                                  optional => 1,
@@ -342,7 +342,10 @@ sub send_mail_message{
                                        },
                              );
      my @sendmail_arguments = @{$param{sendmail_arguments}};
-     push @sendmail_arguments, '-f', $param{envelope_from} if exists $param{envelope_from};
+     push @sendmail_arguments, '-f', $param{envelope_from} if
+        exists $param{envelope_from} and
+        defined $param{envelope_from} and
+        length $param{envelope_from};
 
      my @recipients;
      @recipients = @{$param{recipients}} if defined $param{recipients} and
@@ -435,8 +438,10 @@ sub reply_headers{
     $r_l{subject} = 'Re: '. $r_l{subject} unless $r_l{subject} =~ /(?:^|\s)Re:\s+/;
     $r_l{subject} =~ s/(?:^\s*|\s*$)//g;
     $r_l{'In-Reply-To'} = $head->get('Message-Id');
+    $r_l{'In-Reply-To'} =~ s/(?:^\s*|\s*$)//g if defined $r_l{'In-Reply-To'};
     delete $r_l{'In-Reply-To'} unless defined $r_l{'In-Reply-To'};
     $r_l{References} = ($head->get('References')//''). ' '.($head->get('Message-Id')//'');
+    $r_l{References} =~ s/(?:^\s*|\s*$)//g;
     my $date = $head->get('Date') // 'some date';
     $date =~ s/(?:^\s*|\s*$)//g;
     my $who = $head->get('From') // $head->get('Reply-To') // 'someone';
@@ -444,14 +449,47 @@ sub reply_headers{
 
     my $body = "On $date $who wrote:\n";
     my $i = 60;
-    my $b_h = $entity->bodyhandle;
-    my $IO = $b_h->open("r");
-    while (defined($_ = $IO->getline)) {
-        $i--;
-        last if $i < 0;
-        $body .= '> '. $_;
+    my $b_h;
+    # Default to UTF-8.
+    my $charset="utf-8";
+    ## find the first part which has a defined body handle and appears
+    ## to be text
+    if (defined $entity->bodyhandle) {
+       my $this_charset =
+           $entity->head->mime_attr("content-type.charset");
+       $charset = $this_charset if
+           defined $this_charset and
+           length $this_charset;
+        $b_h = $entity->bodyhandle;
+    } elsif ($entity->parts) {
+        my @parts = $entity->parts;
+        while (defined(my $part = shift @parts)) {
+            if ($part->parts) {
+                push @parts,$part->parts;
+            }
+            if (defined $part->bodyhandle and
+                $part->effective_type =~ /text/) {
+               my $this_charset =
+                   $part->head->mime_attr("content-type.charset");
+               $charset =  $this_charset if
+                   defined $this_charset and
+                   length $this_charset;
+                $b_h = $part->bodyhandle;
+                last;
+            }
+        }
+    }
+    if (defined $b_h) {
+        eval {
+            my $IO = $b_h->open("r");
+            while (defined($_ = $IO->getline)) {
+                $i--;
+                last if $i < 0;
+                $body .= '> '. convert_to_utf8($_,$charset);
+            }
+            $IO->close();
+        };
     }
-    $IO->close();
     $r_l{body} = $body;
     return \%r_l;
 }