user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH 0/3] view: wrap long To:/Cc: lists
@ 2018-04-23  4:16 Eric Wong
  2018-04-23  4:16 ` [PATCH 1/3] view: untangle loop when showing message headers Eric Wong
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eric Wong @ 2018-04-23  4:16 UTC (permalink / raw)
  To: meta

While browsing my private LKML archives, I noticed
some Cc: lists were very long and difficult to read
(and no fault of the sending MUA or sender).

Unfortunately, Email::MIME/Email::Simple does not expose the raw
header (with original line breaks) through a public interface,
so wrapping needs to be performed ourselves.

I think this wrapping implementation is more clean than what my
MUA (mutt) does, even, as it won't split across names from
emails (unless the combination is too long).

A couple of cleanups along the way, too.

Eric Wong (3):
  view: untangle loop when showing message headers
  view: wrap To: and Cc: headers in HTML display
  view: drop redundant References: display code

 lib/PublicInbox/View.pm | 119 +++++++++++++++++++++++++---------------
 t/view.t                |  21 +++++++
 2 files changed, 96 insertions(+), 44 deletions(-)

-- 
EW

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

* [PATCH 1/3] view: untangle loop when showing message headers
  2018-04-23  4:16 [PATCH 0/3] view: wrap long To:/Cc: lists Eric Wong
@ 2018-04-23  4:16 ` Eric Wong
  2018-04-23  4:16 ` [PATCH 2/3] view: wrap To: and Cc: headers in HTML display Eric Wong
  2018-04-23  4:16 ` [PATCH 3/3] view: drop redundant References: display code Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2018-04-23  4:16 UTC (permalink / raw)
  To: meta

The old loop did not help with code clarity with the various
conditional statements.  It also hid a bug where we forgot to
(optionally) obfuscate email addresses in Subject: lines if
search was enabled.
---
 lib/PublicInbox/View.pm | 51 ++++++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 19 deletions(-)

diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index af287b9..add3dfc 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -591,29 +591,42 @@ sub _msg_html_prepare {
 		$ctx->{-upfx} = '../';
 	}
 	my @title;
-	foreach my $h (qw(From To Cc Subject Date)) {
-		my $v = $hdr->header($h);
-		defined($v) && ($v ne '') or next;
+	my $v;
+	if (defined($v = $hdr->header('From'))) {
 		$v = PublicInbox::Hval->new($v);
-
-		if ($h eq 'From') {
-			my @n = PublicInbox::Address::names($v->raw);
-			$title[1] = ascii_html(join(', ', @n));
-			obfuscate_addrs($obfs_ibx, $title[1]) if $obfs_ibx;
-		} elsif ($h eq 'Subject') {
-			$title[0] = $v->as_html;
-			if ($srch) {
-				$rv .= qq($h: <a\nhref="#r"\nid=t>);
-				$rv .= $v->as_html . "</a>\n";
-				next;
-			}
-		}
+		my @n = PublicInbox::Address::names($v->raw);
+		$title[1] = ascii_html(join(', ', @n));
 		$v = $v->as_html;
+		if ($obfs_ibx) {
+			obfuscate_addrs($obfs_ibx, $v);
+			obfuscate_addrs($obfs_ibx, $title[1]);
+		}
+		$rv .= "From: $v\n" if $v ne '';
+	}
+	foreach my $h (qw(To Cc)) {
+		defined($v = $hdr->header($h)) or next;
+		$v = ascii_html($v);
 		obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
-		$rv .= "$h: $v\n";
-
+		$rv .= "$h: $v\n" if $v ne '';
+	}
+	if (defined($v = $hdr->header('Subject')) && ($v ne '')) {
+		$v = ascii_html($v);
+		obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
+		if ($srch) {
+			$rv .= qq(Subject: <a\nhref="#r"\nid=t>$v</a>\n);
+		} else {
+			$rv .= "Subject: $v\n";
+		}
+		$title[0] = $v;
+	} else { # dummy anchor for thread skeleton at bottom of page
+		$rv .= qq(<a\nhref="#r"\nid=t></a>) if $srch;
+		$title[0] = '(no subject)';
+	}
+	if (defined($v = $hdr->header('Date'))) {
+		$v = ascii_html($v);
+		obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
+		$rv .= "Date: $v\n";
 	}
-	$title[0] ||= '(no subject)';
 	$ctx->{-title_html} = join(' - ', @title);
 	foreach (@$mids) {
 		my $mid = PublicInbox::Hval->new_msgid($_) ;
-- 
EW


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

* [PATCH 2/3] view: wrap To: and Cc: headers in HTML display
  2018-04-23  4:16 [PATCH 0/3] view: wrap long To:/Cc: lists Eric Wong
  2018-04-23  4:16 ` [PATCH 1/3] view: untangle loop when showing message headers Eric Wong
@ 2018-04-23  4:16 ` Eric Wong
  2018-04-23  4:16 ` [PATCH 3/3] view: drop redundant References: display code Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2018-04-23  4:16 UTC (permalink / raw)
  To: meta

It is common to have large amounts of addresses Cc:-ed in large
mailing lists like LKML.  Make them more readable by wrapping
after addresses.  Unfortunately, line breaks inserted by the
MUA get lost when using the public Email::MIME API.

Subject and body lines remain unwrapped, as it's the author's
fault to have such long lines :P
---
 lib/PublicInbox/View.pm | 46 ++++++++++++++++++++++++++++++++++-------
 t/view.t                | 21 +++++++++++++++++++
 2 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index add3dfc..7339467 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -17,6 +17,7 @@ use PublicInbox::Reply;
 require POSIX;
 use Time::Local qw(timegm);
 
+use constant COLS => 72;
 use constant INDENT => '  ';
 use constant TCHILD => '` ';
 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
@@ -164,6 +165,24 @@ sub in_reply_to {
 	$refs->[-1];
 }
 
+sub fold_addresses ($) {
+	return $_[0] if length($_[0]) <= COLS;
+	# try to fold on commas after non-word chars before $lim chars,
+	# Try to get the "," preceeded by ">" or ")", but avoid folding
+	# on the comma where somebody uses "Lastname, Firstname".
+	# We also try to keep the last and penultimate addresses in
+	# the list on the same line if possible, hence the extra \z
+	# Fall back to folding on spaces at $lim + 1 chars
+	my $lim = COLS - 8; # 8 = "\t" display width
+	my $too_long = $lim + 1;
+	$_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
+	$_[0] = join("\n\t",
+		($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
+				.{1,$lim}(?:,|\z)|
+				.{1,$lim}|
+				.{$too_long,}?)(?:\s|\z)/xgo));
+}
+
 sub _hdr_names_html ($$) {
 	my ($hdr, $field) = @_;
 	my $val = $hdr->header($field) or return '';
@@ -198,13 +217,6 @@ sub index_entry {
 	my @tocc;
 	my $mime = $smsg->{mime};
 	my $hdr = $mime->header_obj;
-	foreach my $f (qw(To Cc)) {
-		my $dst = _hdr_names_html($hdr, $f);
-		if ($dst ne '') {
-			obfuscate_addrs($obfs_ibx, $dst) if $obfs_ibx;
-			push @tocc, "$f: $dst";
-		}
-	}
 	my $from = _hdr_names_html($hdr, 'From');
 	obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
 	$rv .= "From: $from @ ".fmt_ts($smsg->ds)." UTC";
@@ -212,7 +224,24 @@ sub index_entry {
 	my $mhref = $upfx . mid_escape($mid_raw) . '/';
 	$rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
 	$rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
-	$rv .= '  '.join('; +', @tocc) . "\n" if @tocc;
+	my $to = fold_addresses(_hdr_names_html($hdr, 'To'));
+	my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc'));
+	my ($tlen, $clen) = (length($to), length($cc));
+	my $to_cc = '';
+	if (($tlen + $clen) > COLS) {
+		$to_cc .= '  To: '.$to."\n" if $tlen;
+		$to_cc .= '  Cc: '.$cc."\n" if $clen;
+	} else {
+		if ($tlen) {
+			$to_cc .= '  To: '.$to;
+			$to_cc .= '; <b>+Cc:</b> '.$cc if $clen;
+		} else {
+			$to_cc .= '  Cc: '.$cc if $clen;
+		}
+		$to_cc .= "\n";
+	}
+	obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx;
+	$rv .= $to_cc;
 
 	my $mapping = $ctx->{mapping};
 	if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
@@ -605,6 +634,7 @@ sub _msg_html_prepare {
 	}
 	foreach my $h (qw(To Cc)) {
 		defined($v = $hdr->header($h)) or next;
+		fold_addresses($v);
 		$v = ascii_html($v);
 		obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
 		$rv .= "$h: $v\n" if $v ne '';
diff --git a/t/view.t b/t/view.t
index 8ae4225..b829ecf 100644
--- a/t/view.t
+++ b/t/view.t
@@ -170,4 +170,25 @@ EOF
 		'regular ID not compressed');
 }
 
+{
+	my $cols = PublicInbox::View::COLS();
+	my @addr;
+	until (length(join(', ', @addr)) > ($cols * 2)) {
+		push @addr, '"l, f" <a@a>';
+		my $n = int(rand(20)) + 1;
+		push @addr, ('x'x$n).'@x';
+	}
+	my $orig = join(', ', @addr);
+	my $res = PublicInbox::View::fold_addresses($orig.'');
+	isnt($res, $orig, 'folded result');
+	unlike($res, qr/l,\n\tf/s, '"last, first" no broken');
+	my @nospc = ($res, $orig);
+	s/\s+//g for @nospc;
+	is($nospc[0], $nospc[1], 'no addresses lost in translation');
+	my $tws = PublicInbox::View::fold_addresses($orig.' ');
+	# (Email::Simple drops leading whitespace, but not trailing)
+	$tws =~ s/ \z//;
+	is($tws, $res, 'not thrown off by trailing whitespace');
+}
+
 done_testing();
-- 
EW


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

* [PATCH 3/3] view: drop redundant References: display code
  2018-04-23  4:16 [PATCH 0/3] view: wrap long To:/Cc: lists Eric Wong
  2018-04-23  4:16 ` [PATCH 1/3] view: untangle loop when showing message headers Eric Wong
  2018-04-23  4:16 ` [PATCH 2/3] view: wrap To: and Cc: headers in HTML display Eric Wong
@ 2018-04-23  4:16 ` Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2018-04-23  4:16 UTC (permalink / raw)
  To: meta

We no longer need to parse and dedupe References:
ourselves, PublicInbox::MID::references does it for us.
---
 lib/PublicInbox/View.pm | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)

diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index 7339467..58851ed 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -723,7 +723,8 @@ sub _parent_headers {
 	my ($hdr, $srch) = @_;
 	my $rv = '';
 
-	my $irt = in_reply_to($hdr);
+	my $refs = references($hdr);
+	my $irt = pop @$refs;
 	if (defined $irt) {
 		my $v = PublicInbox::Hval->new_msgid($irt);
 		my $html = $v->as_html;
@@ -736,22 +737,9 @@ sub _parent_headers {
 	# we show the thread skeleton at the bottom, instead.
 	return $rv if $srch;
 
-	my $refs = $hdr->header_raw('References');
-	if ($refs) {
-		# avoid redundant URLs wasting bandwidth
-		my %seen;
-		$seen{$irt} = 1 if defined $irt;
-		my @refs;
-		my @raw_refs = ($refs =~ /<([^>]+)>/g);
-		foreach my $ref (@raw_refs) {
-			next if $seen{$ref};
-			$seen{$ref} = 1;
-			push @refs, linkify_ref_nosrch($ref);
-		}
-
-		if (@refs) {
-			$rv .= 'References: '. join("\n\t", @refs) . "\n";
-		}
+	if (@$refs) {
+		@$refs = map { linkify_ref_nosrch($_) } @$refs;
+		$rv .= 'References: '. join("\n\t", @$refs) . "\n";
 	}
 	$rv;
 }
-- 
EW


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

end of thread, other threads:[~2018-04-23  4:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-23  4:16 [PATCH 0/3] view: wrap long To:/Cc: lists Eric Wong
2018-04-23  4:16 ` [PATCH 1/3] view: untangle loop when showing message headers Eric Wong
2018-04-23  4:16 ` [PATCH 2/3] view: wrap To: and Cc: headers in HTML display Eric Wong
2018-04-23  4:16 ` [PATCH 3/3] view: drop redundant References: display code Eric Wong

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