user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH 10/13] eml: remove dependency on Email::MIME::Encodings
Date: Thu,  7 May 2020 21:05:53 +0000	[thread overview]
Message-ID: <20200507210556.22995-11-e@yhbt.net> (raw)
In-Reply-To: <20200507210556.22995-1-e@yhbt.net>

Since Email::MIME usage is going away, Email::MIME::Encodings
might as well go away, too.  We can also use fewer branches
and just rely on hash lookups, unlike E::M::E.
---
 lib/PublicInbox/Eml.pm | 47 ++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/Eml.pm b/lib/PublicInbox/Eml.pm
index 1988bdb3..1adaff04 100644
--- a/lib/PublicInbox/Eml.pm
+++ b/lib/PublicInbox/Eml.pm
@@ -30,18 +30,24 @@ use v5.10.1;
 use Carp qw(croak);
 use Encode qw(find_encoding decode encode); # stdlib
 use Text::Wrap qw(wrap); # stdlib, we need Perl 5.6+ for $huge
+use MIME::Base64 3.05; # Perl 5.10.0 / 5.9.2
+use MIME::QuotedPrint 3.05; # ditto
 
 my $MIME_Header = find_encoding('MIME-Header');
 
 use PublicInbox::EmlContentFoo qw(parse_content_type parse_content_disposition);
-use Email::MIME::Encodings;
 $PublicInbox::EmlContentFoo::STRICT_PARAMS = 0;
 
 our $MAXPARTS = 1000; # same as SpamAssassin
 our $MAXDEPTH = 20; # seems enough, Perl sucks, here
 our $MAXBOUNDLEN = 2048; # same as postfix
 
-my $NO_ENCODE_RE = qr/\A(?:7bit|8bit|binary)[ \t]*(?:;|$)?/i;
+my %MIME_ENC = (qp => \&enc_qp, base64 => \&encode_base64);
+my %MIME_DEC = (qp => \&dec_qp, base64 => \&decode_base64);
+$MIME_ENC{quotedprint} = $MIME_ENC{'quoted-printable'} = $MIME_ENC{qp};
+$MIME_DEC{quotedprint} = $MIME_DEC{'quoted-printable'} = $MIME_DEC{qp};
+$MIME_ENC{$_} = \&identity_codec for qw(7bit 8bit binary);
+
 my %DECODE_ADDRESS = map { $_ => 1 } qw(From To Cc Sender Reply-To);
 my %DECODE_FULL = (
 	Subject => 1,
@@ -111,13 +117,6 @@ sub ct ($) {
 	$_[0]->{ct} //= parse_content_type(header($_[0], 'Content-Type'));
 }
 
-sub body_decode ($$) {
-	my $cte = header_raw($_[0], 'Content-Transfer-Encoding');
-	($cte) = ($cte =~ /([a-zA-Z0-9\-]+)/) if $cte; # For S/MIME, etc
-	(!$cte || $cte =~ $NO_ENCODE_RE) ?
-		$_[1] : Email::MIME::Encodings::decode($cte, $_[1], '7bit');
-}
-
 # returns a queue of sub-parts iff it's worth descending into
 # TODO: descend into message/rfc822 parts (Email::MIME didn't)
 sub mp_descend ($$) {
@@ -197,6 +196,22 @@ sub each_part {
 	}
 }
 
+sub enc_qp {
+	# prevent MIME::QuotedPrint from encoding CR as =0D since it's
+	# against RFCs and breaks MUAs
+	$_[0] =~ s/\r\n/\n/sg;
+	encode_qp($_[0], "\r\n");
+}
+
+sub dec_qp {
+	# RFC 2822 requires all lines to end in CRLF, though... :<
+	$_[0] = decode_qp($_[0]);
+	$_[0] =~ s/\n/\r\n/sg;
+	$_[0]
+}
+
+sub identity_codec { $_[0] }
+
 ########### compatibility section for existing Email::MIME uses #########
 
 sub header_obj {
@@ -240,9 +255,9 @@ EOF
 sub body_set {
 	my ($self, $body) = @_;
 	my $bdy = $self->{bdy} = ref($body) ? $body : \$body;
-	my $cte = header_raw($self, 'Content-Transfer-Encoding');
-	if ($cte && $cte !~ $NO_ENCODE_RE) {
-		$$bdy = Email::MIME::Encodings::encode($cte, $$bdy)
+	if (my $cte = header_raw($self, 'Content-Transfer-Encoding')) {
+		my $enc = $MIME_ENC{lc($cte)} or croak("can't encode `$cte'");
+		$$bdy = $enc->($$bdy); # in-place
 	}
 	undef;
 }
@@ -351,7 +366,13 @@ sub header_str {
 
 sub body_raw { ${$_[0]->{bdy} // \''}; }
 
-sub body { body_decode($_[0], body_raw($_[0])) }
+sub body {
+	my $raw = body_raw($_[0]);
+	my $cte = header_raw($_[0], 'Content-Transfer-Encoding') or return $raw;
+	($cte) = ($cte =~ /([a-zA-Z0-9\-]+)/) or return $raw; # For S/MIME, etc
+	my $dec = $MIME_DEC{lc($cte)} or return $raw;
+	$dec->($raw);
+}
 
 sub body_str {
 	my ($self) = @_;

  parent reply	other threads:[~2020-05-07 21:05 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-07 21:05 [PATCH 00/13] eml: pure-Perl replacement for Email::MIME Eric Wong
2020-05-07 21:05 ` [PATCH 01/13] msg_iter: make ->each_part method for PublicInbox::MIME Eric Wong
2020-05-07 21:05 ` [PATCH 02/13] msg_iter: pass $idx as a scalar, not array Eric Wong
2020-05-07 21:05 ` [PATCH 03/13] filter/rubylang: avoid recursing subparts to strip trailers Eric Wong
2020-05-07 21:05 ` [PATCH 04/13] smsg: use capitalization for header retrieval Eric Wong
2020-05-07 21:05 ` [PATCH 05/13] eml: pure-Perl replacement for Email::MIME Eric Wong
2020-05-07 21:05 ` [PATCH 06/13] switch read-only Email::Simple users to Eml Eric Wong
2020-05-07 21:05 ` [PATCH 07/13] replace most uses of PublicInbox::MIME with Eml Eric Wong
2020-05-07 21:05 ` [PATCH 08/13] EmlContentFoo: Email::MIME::ContentType replacement Eric Wong
2020-05-07 21:05 ` [PATCH 09/13] EmlContentFoo: relax Encode version requirement Eric Wong
2020-05-07 21:05 ` Eric Wong [this message]
2020-05-07 21:05 ` [PATCH 11/13] xt: eml comparison tests Eric Wong
2020-05-08  4:47   ` Eric Wong
2020-05-07 21:05 ` [PATCH 12/13] remove most internal Email::MIME usage Eric Wong
2020-05-07 21:05 ` [PATCH 13/13] eml: drop trailing blank line on missing epilogue Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200507210556.22995-11-e@yhbt.net \
    --to=e@yhbt.net \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).