Subject: 29.0.50; mail-extract-address-components poorly handles " via " addresses

From: Sam Steingold <[email-masked]>
Date: Wed, 06 Jul 2022 11:22:35 -0400

Often "From" email addresses in mailing lists look like this:

--8<---------------cut here---------------start------------->8---
From: Po Lu via "Emacs development discussions." <[email-masked]>
From: carlmarcos--- via Users list for the GNU Emacs text editor <[email-masked]>
From: Stefan Monnier via Users list for the GNU Emacs text editor <[email-masked]>
--8<---------------cut here---------------end--------------->8---

`mail-extract-address-components' handles them poorly:

--8<---------------cut here---------------start------------->8---
(mail-extract-address-components "Po Lu via \"Emacs development discussions.\" <[email-masked]>")
=> ("Po Lu via" "[email-masked]")
(mail-extract-address-components "carlmarcos--- via Users list for the GNU Emacs text editor <[email-masked]>")
=> ("carlmarcos" "[email-masked]")
(mail-extract-address-components "Stefan Monnier via Users list for the GNU Emacs text editor <[email-masked]>")
=> ("Stefan Monnier via Users list for the" "[email-masked]")
--8<---------------cut here---------------end--------------->8---

The correct handling would be

--8<---------------cut here---------------start------------->8---
(mail-extract-address-components "Po Lu via \"Emacs development discussions.\" <[email-masked]>")
=> ("Emacs development discussions." "[email-masked]")
(mail-extract-address-components "carlmarcos--- via Users list for the GNU Emacs text editor <[email-masked]>")
=> ("Users list for the GNU Emacs text editor" "[email-masked]")
(mail-extract-address-components "Stefan Monnier via Users list for the GNU Emacs text editor <[email-masked]>")
=> ("Users list for the GNU Emacs text editor" "[email-masked]")
--8<---------------cut here---------------end--------------->8---

or, at least,

--8<---------------cut here---------------start------------->8---
(mail-extract-address-components "Po Lu via \"Emacs development discussions.\" <[email-masked]>")
=> ("Po Lu via \"Emacs development discussions.\"" "[email-masked]")
(mail-extract-address-components "carlmarcos--- via Users list for the GNU Emacs text editor <[email-masked]>")
=> ("carlmarcos--- via Users list for the GNU Emacs text editor" "[email-masked]")
(mail-extract-address-components "Stefan Monnier via Users list for the GNU Emacs text editor <[email-masked]>")
=> ("Stefan Monnier via Users list for the GNU Emacs text editor" "[email-masked]")
--8<---------------cut here---------------end--------------->8---

Please see the relevant discussion on the BBDB user list:
https://lists.nongnu.org/archive/html/bbdb-user/2022-06/msg00000.html
https://lists.nongnu.org/archive/html/bbdb-user/2022-07/msg00000.html

In https://lists.nongnu.org/archive/html/bbdb-user/2022-07/msg00006.html
I propose a workaround for a _single_ address (i.e., when the second
argument to `mail-extract-address-components' is nil):
--8<---------------cut here---------------start------------->8---
(defun mail-extract-handle-via (args)
  "Handle `via` in email address"
  (let ((address (car args))
        (all (cadr args)))
    (if (and (null all)
             (string-match " via \\(.*\\)$" address))
        (list (match-string 1 address) nil)
      (list address all))))
(advice-add 'mail-extract-address-components :filter-args 'mail-extract-handle-via)
--8<---------------cut here---------------end--------------->8---

this is clearly suboptimal, especially when the "name" part of the
address contains many words:

--8<---------------cut here---------------start------------->8---
(mail-extract-address-components "Stefan Monnier via Users list for the GNU Emacs text editor <[email-masked]>")
("Users list for the" "[email-masked]")
--8<---------------cut here---------------end--------------->8---

It appears that `mail-header-parse-address' is a better choice (as per
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=10406) but you might still
consider addressing this issue...

Thank you.

In GNU Emacs 29.0.50 (build 2, x86_64-apple-darwin21.5.0, NS appkit-2113.50 Version 12.4 (Build 21F79))
 of 2022-07-05 built on 3c22fb11fdab.ant.amazon.com
Repository revision: 59276ff81d1ab391f4e3cd91f3070a12c51a3507
Repository branch: master
Windowing system distributor 'Apple', version 10.3.2113
System Description:  macOS 12.4

-- 
Sam Steingold (http://sds.podval.org/) on darwin Ns 10.3.2113
http://childpsy.net http://calmchildstories.com http://steingoldpsychology.com
https://www.peaceandtolerance.org/ https://honestreporting.com
The past is gone, the present is ephemeral, the future is a guess.