unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/2] address: use comment as name if no phrase available
Date: Sat, 14 Dec 2019 01:02:55 +0000	[thread overview]
Message-ID: <20191214010256.19858-2-e@80x24.org> (raw)
In-Reply-To: <20191214010256.19858-1-e@80x24.org>

Some users will set their From: headers in the form of:
"<user@example.com> (A U Thor)", where their name is in
the parenthesized comment.  Use that instead of the
email address, if available.
---
 lib/PublicInbox/Address.pm | 30 ++++++++++++++++++++----------
 t/address.t                |  7 ++++---
 2 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/Address.pm b/lib/PublicInbox/Address.pm
index 46aa0da7..a58d1eff 100644
--- a/lib/PublicInbox/Address.pm
+++ b/lib/PublicInbox/Address.pm
@@ -13,16 +13,26 @@ sub emails {
 }
 
 sub names {
-	map {
-		tr/\r\n\t/ /;
-		s/\s*<([^<]+)\z//;
-		my $e = $1;
-		s/\A['"\s]*//;
-		s/['"\s]*\z//;
-		$e = $_ =~ /\S/ ? $_ : $e;
-		$e =~ s/\@\S+\z//;
-		$e;
-	} split(/\@+[\w\.\-]+>?\s*(?:\(.*?\))?(?:,\s*|\z)/, $_[0]);
+	my @p = split(/<?([^@<>]+)\@[\w\.\-]+>?\s*(\(.*?\))?(?:,\s*|\z)/,
+			$_[0]);
+	my @ret;
+	for (my $i = 0; $i <= $#p;) {
+		my $phrase = $p[$i++];
+		$phrase =~ tr/\r\n\t / /s;
+		$phrase =~ s/\A['"\s]*//;
+		$phrase =~ s/['"\s]*\z//;
+		my $user = $p[$i++] // '';
+		my $comment = $p[$i++] // '';
+		if ($phrase =~ /\S/) {
+			$phrase =~ s/\@\S+\z//;
+			push @ret, $phrase;
+		} elsif ($comment =~ /\A\((.*?)\)\z/) {
+			push @ret, $1;
+		} else {
+			push @ret, $user;
+		}
+	}
+	@ret;
 }
 
 1;
diff --git a/t/address.t b/t/address.t
index bea45daa..2a287102 100644
--- a/t/address.t
+++ b/t/address.t
@@ -14,8 +14,9 @@ is_deeply(['user@example.com'],
 	'comment after domain accepted before >');
 
 my @names = PublicInbox::Address::names(
-	'User <e@e>, e@e, "John A. Doe" <j@d>, <x@x>');
-is_deeply(['User', 'e', 'John A. Doe', 'x'], \@names,
+	'User <e@e>, e@e, "John A. Doe" <j@d>, <x@x>, <y@x> (xyz), '.
+	'U Ser <u@x> (do not use)');
+is_deeply(\@names, ['User', 'e', 'John A. Doe', 'x', 'xyz', 'U Ser'],
 	'name extraction works as expected');
 
 @names = PublicInbox::Address::names('"user@example.com" <user@example.com>');
@@ -25,7 +26,7 @@ is_deeply(['user'], \@names, 'address-as-name extraction works as expected');
 {
 	my $backwards = 'u@example.com (John Q. Public)';
 	@names = PublicInbox::Address::names($backwards);
-	is_deeply(\@names, ['u'], 'backwards name OK');
+	is_deeply(\@names, ['John Q. Public'], 'backwards name OK');
 	my @emails = PublicInbox::Address::emails($backwards);
 	is_deeply(\@emails, ['u@example.com'], 'backwards emails OK');
 }

  reply	other threads:[~2019-12-14  1:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-14  1:02 [PATCH 0/2] use Email::Address::XS if available Eric Wong
2019-12-14  1:02 ` Eric Wong [this message]
2019-12-14  1:02 ` [PATCH 2/2] address: " Eric Wong
2019-12-14  5:22 ` [PATCH 3/2] address: explicitly reject local-only addresses 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=20191214010256.19858-2-e@80x24.org \
    --to=e@80x24.org \
    --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.
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).