diff options
author | Eric Wong <e@80x24.org> | 2021-01-05 09:04:37 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2021-01-06 10:45:09 +0000 |
commit | 348e7a39627fd40d7cc0ca1be01412da51a71352 (patch) | |
tree | 00af1f86f9aa4cb935f4d6638217b2a8ea97b1ac /lib | |
parent | 27f8e2b161f4a8bb1ef030bd45e3f5ae7a9f0bb0 (diff) | |
download | public-inbox-348e7a39627fd40d7cc0ca1be01412da51a71352.tar.gz |
Per JMAP RFC 8621 sec 4.1.2.3, we should be able to denote the lack of a phrase/comment corresponding to an email address with a JSON "null" (or Perl `undef'). [ { "name": "James Smythe", "email": "james@example.com" }, { "name": null, "email": "jane@example.com" }, { "name": "John Smith", "email": "john@example.com" } ] The new "pairs" method just returns a 2 dimensional array and the consumer will fill in the field names if necessary (or not). lei(1) may use the two dimensional array as-is for JSON output.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/PublicInbox/Address.pm | 11 | ||||
-rw-r--r-- | lib/PublicInbox/AddressPP.pm | 21 |
2 files changed, 31 insertions, 1 deletions
diff --git a/lib/PublicInbox/Address.pm b/lib/PublicInbox/Address.pm index f5af4c23..a090fa43 100644 --- a/lib/PublicInbox/Address.pm +++ b/lib/PublicInbox/Address.pm @@ -2,7 +2,9 @@ # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> package PublicInbox::Address; use strict; -use warnings; +use v5.10.1; +use parent 'Exporter'; +our @EXPORT_OK = qw(pairs); sub xs_emails { grep { defined } map { $_->address() } parse_email_addresses($_[0]) @@ -17,11 +19,18 @@ sub xs_names { } parse_email_addresses($_[0]); } +sub xs_pairs { # for JMAP, RFC 8621 section 4.1.2.3 + [ map { # LHS (name) may be undef + [ $_->phrase // $_->comment, $_->address ] + } parse_email_addresses($_[0]) ]; +} + eval { require Email::Address::XS; Email::Address::XS->import(qw(parse_email_addresses)); *emails = \&xs_emails; *names = \&xs_names; + *pairs = \&xs_pairs; }; if ($@) { diff --git a/lib/PublicInbox/AddressPP.pm b/lib/PublicInbox/AddressPP.pm index c04de74b..6a3ae4fe 100644 --- a/lib/PublicInbox/AddressPP.pm +++ b/lib/PublicInbox/AddressPP.pm @@ -13,6 +13,7 @@ sub emails { } sub names { + # split by address and post-address comment my @p = split(/<?([^@<>]+)\@[\w\.\-]+>?\s*(\(.*?\))?(?:,\s*|\z)/, $_[0]); my @ret; @@ -35,4 +36,24 @@ sub names { @ret; } +sub pairs { # for JMAP, RFC 8621 section 4.1.2.3 + my ($s) = @_; + [ map { + my $addr = $_; + if ($s =~ s/\A\s*(.*?)\s*<\Q$addr\E>\s*(.*?)\s*(?:,|\z)// || + $s =~ s/\A\s*(.*?)\s*\Q$addr\E\s*(.*?)\s*(?:,|\z)//) { + my ($phrase, $comment) = ($1, $2); + $phrase =~ tr/\r\n\t / /s; + $phrase =~ s/\A['"\s]*//; + $phrase =~ s/['"\s]*\z//; + $phrase =~ s/\s*<*\s*\z//; + $phrase = undef if $phrase !~ /\S/; + $comment = ($comment =~ /\((.*?)\)/) ? $1 : undef; + [ $phrase // $comment, $addr ] + } else { + (); + } + } emails($s) ]; +} + 1; |