]> git.donarmstrong.com Git - debbugs.git/blobdiff - Debbugs/Mail.pm
Prefer "use Exporter qw(import)" to inheriting from it
[debbugs.git] / Debbugs / Mail.pm
index ad2df8c66e493ad36c6e7a3946a865090fa5d30b..01ae327798fd41e7f8c2e7125615b3c6ce456338 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);
@@ -48,6 +48,8 @@ use Mail::Address ();
 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::Packages;
 
@@ -59,6 +61,7 @@ BEGIN{
      %EXPORT_TAGS = (addresses => [qw(get_addresses)],
                     misc      => [qw(rfc822_date)],
                     mail      => [qw(send_mail_message encode_headers default_headers)],
+                     reply     => [qw(reply_headers)],
                    );
      @EXPORT_OK = ();
      Exporter::export_ok_tags(keys %EXPORT_TAGS);
@@ -162,20 +165,19 @@ passed through.
 
 sub default_headers {
     my %param = validate_with(params => \@_,
-                             spec   => {queue_file => {type => SCALAR,
+                             spec   => {queue_file => {type => SCALAR|UNDEF,
                                                        optional => 1,
                                                       },
                                         data       => {type => HASHREF,
                                                        optional => 1,
                                                       },
-                                        msgid      => {type => SCALAR,
+                                        msgid      => {type => SCALAR|UNDEF,
                                                        optional => 1,
                                                       },
-                                        msgtype    => {type => SCALAR,
+                                        msgtype    => {type => SCALAR|UNDEF,
                                                        default => 'misc',
-                                                       optional => 1,
                                                       },
-                                        pr_msg     => {type => SCALAR,
+                                        pr_msg     => {type => SCALAR|UNDEF,
                                                        default => 'misc',
                                                       },
                                         headers    => {type => ARRAYREF,
@@ -185,6 +187,17 @@ sub default_headers {
                             );
     my @header_order = (qw(X-Loop From To subject),
                        qw(Message-ID In-Reply-To References));
+    # handle various things being undefined
+    if (not exists $param{queue_file} or
+       not defined $param{queue_file}) {
+       $param{queue_file} = join('',gettimeofday())
+    }
+    for (qw(msgtype pr_msg)) {
+       if (not exists $param{$_} or
+           not defined $param{$_}) {
+           $param{$_} = 'misc';
+       }
+    }
     my %header_order;
     @header_order{map {lc $_} @header_order} = 0..$#header_order;
     my %set_headers;
@@ -204,7 +217,7 @@ sub default_headers {
 
     # calculate our headers
     my $bug_num = exists $param{data} ? $param{data}{bug_num} : 'x';
-    my $nn = exists $param{queue_file} ? $param{queue_file} : join('',gettimeofday());
+    my $nn = $param{queue_file};
     # handle the user giving the actual queue filename instead of nn
     $nn =~ s/^[a-zA-Z]([a-zA-Z])/$1/;
     $nn = lc($nn);
@@ -387,7 +400,7 @@ sub encode_headers{
 
      my ($header,$body) = split /\n\n/, $message, 2;
      $header = encode_rfc1522($header);
-     return $header . qq(\n\n). $body;
+     return $header . qq(\n\n). encode_utf8_safely($body);
 }
 
 =head2 rfc822_date
@@ -402,6 +415,70 @@ sub rfc822_date{
      return scalar strftime "%a, %d %h %Y %T +0000", gmtime;
 }
 
+=head2 reply_headers
+
+     reply_headers(MIME::Parser->new()->parse_data(\$data));
+
+Generates suggested headers and a body for replies. Primarily useful
+for use in RFC2368 mailto: entries.
+
+=cut
+
+sub reply_headers{
+    my ($entity) = @_;
+
+    my $head = $entity->head;
+    # build reply link
+    my %r_l;
+    $r_l{subject} = $head->get('Subject');
+    $r_l{subject} //= 'Your mail';
+    $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';
+    $who =~ s/(?:^\s*|\s*$)//g;
+
+    my $body = "On $date $who wrote:\n";
+    my $i = 60;
+    my $b_h;
+    ## find the first part which has a defined body handle and appears
+    ## to be text
+    if (defined $entity->bodyhandle) {
+        $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/) {
+                $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 .= '> '. $_;
+            }
+            $IO->close();
+        };
+    }
+    $r_l{body} = $body;
+    return \%r_l;
+}
+
 =head1 PRIVATE FUNCTIONS
 
 =head2 _send_message