unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
* [PATCH 1/2] filter: preserve QP when collapsing multipart
@ 2014-08-05  0:45 Eric Wong
  2014-08-05  0:45 ` [PATCH 2/2] t/view: ensure HTML shows QP text Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2014-08-05  0:45 UTC (permalink / raw)
  To: meta; +Cc: Eric Wong

HTML clients also tend to send quoted-printable crap in
their plain-text parts, preserve that so it's displayed
correctly for all QP-capable handlers.
---
 lib/PublicInbox/Filter.pm |  4 ++++
 t/filter.t                | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/lib/PublicInbox/Filter.pm b/lib/PublicInbox/Filter.pm
index a158768..e784cde 100644
--- a/lib/PublicInbox/Filter.pm
+++ b/lib/PublicInbox/Filter.pm
@@ -201,6 +201,10 @@ sub collapse {
 	my ($mime, $part) = @_;
 	$mime->header_set('Content-Type', $part->content_type);
 	$mime->body_set($part->body_raw);
+	my $cte = $part->header('Content-Transfer-Encoding');
+	if (defined($cte) && length($cte)) {
+		$mime->header_set('Content-Transfer-Encoding', $cte);
+	}
 	mark_changed($mime);
 	return 1;
 }
diff --git a/t/filter.t b/t/filter.t
index c3cd39f..e4f6a2b 100644
--- a/t/filter.t
+++ b/t/filter.t
@@ -15,6 +15,53 @@ sub count_body_parts {
 	$bodies->{$body}++;
 }
 
+# multipart/alternative: HTML and quoted-printable, keep the plain-text
+{
+	my $html_body = "<html><body>hi</body></html>";
+	my $parts = [
+		Email::MIME->create(
+			attributes => {
+				content_type => 'text/html; charset=UTF-8',
+				encoding => 'base64',
+			},
+			body => $html_body,
+		),
+		Email::MIME->create(
+			attributes => {
+				content_type => 'text/plain',
+				encoding => 'quoted-printable',
+			},
+			body => 'hi = "bye"',
+		)
+	];
+	my $email = Email::MIME->create(
+		header_str => [
+		  From => 'a@example.com',
+		  Subject => 'blah',
+		  'Content-Type' => 'multipart/alternative'
+		],
+		parts => $parts,
+	);
+	is(1, PublicInbox::Filter->run($email), "run was a success");
+	my $parsed = Email::MIME->new($email->as_string);
+	is("text/plain", $parsed->header("Content-Type"));
+	is(scalar $parsed->parts, 1, "HTML part removed");
+	my %bodies;
+	$parsed->walk_parts(sub {
+		my ($part) = @_;
+		return if $part->subparts; # walk_parts already recurses
+		count_body_parts(\%bodies, $part);
+	});
+	is(scalar keys %bodies, 1, "one bodies");
+	is($bodies{"hi =3D \"bye\"="}, 1, "QP text part unchanged");
+	$parsed->walk_parts(sub {
+		my ($part) = @_;
+		my $b = $part->body;
+		$b =~ s/\s*\z//;
+		is($b, "hi = \"bye\"", "decoded body matches");
+	});
+}
+
 # plain-text email is passed through unchanged
 {
 	my $s = Email::MIME->create(
-- 
2.0.1.427.gc47372d


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] t/view: ensure HTML shows QP text
  2014-08-05  0:45 [PATCH 1/2] filter: preserve QP when collapsing multipart Eric Wong
@ 2014-08-05  0:45 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2014-08-05  0:45 UTC (permalink / raw)
  To: meta; +Cc: Eric Wong

We need to ensure the HTML output is not mangled, either.
---
 t/view.t | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/t/view.t b/t/view.t
index 10a182b..bc6fbed 100644
--- a/t/view.t
+++ b/t/view.t
@@ -110,4 +110,29 @@ EOF
 		"parts split with filename");
 }
 
+# multipart collapsed to single quoted-printable text/plain
+{
+	my $parts = [
+		Email::MIME->create(
+			attributes => {
+				content_type => 'text/plain',
+				encoding => 'quoted-printable',
+			},
+			body => 'hi = bye',
+		)
+	];
+	my $mime = Email::MIME->create(
+		header_str => [
+			From => 'qp@example.com',
+			Subject => 'QP',
+			'Message-ID' => '<qp@example.com>',
+			],
+		parts => $parts,
+	);
+
+	my $html = PublicInbox::View->msg_html($mime);
+	like($mime->body_raw, qr/hi =3D bye=/, "our test used QP correctly");
+	like($html, qr/\bhi = bye\b/, "HTML output decoded QP");
+}
+
 done_testing();
-- 
2.0.1.427.gc47372d


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-08-05  0:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-05  0:45 [PATCH 1/2] filter: preserve QP when collapsing multipart Eric Wong
2014-08-05  0:45 ` [PATCH 2/2] t/view: ensure HTML shows QP text Eric Wong

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).