diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2021-08-19 16:48:59 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2021-08-19 16:48:59 -0700 |
commit | 07fcbb558d797272b9f43547da60beda485873a3 (patch) | |
tree | 77d5da14e9f9d9d8b1d877c70c01296fd3893796 /lisp/mail/mail-parse.el | |
parent | c9bdeff3e45a7ac84a74a81bb048046f82dddc91 (diff) | |
parent | fb81c8c3adf8633f2f617c82f6019aef630860c7 (diff) | |
download | emacs-07fcbb558d797272b9f43547da60beda485873a3.tar.gz |
Merge remote-tracking branch 'origin/master' into athena/unstable
Diffstat (limited to 'lisp/mail/mail-parse.el')
-rw-r--r-- | lisp/mail/mail-parse.el | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lisp/mail/mail-parse.el b/lisp/mail/mail-parse.el index e72ed828494..212fadf3823 100644 --- a/lisp/mail/mail-parse.el +++ b/lisp/mail/mail-parse.el @@ -71,6 +71,45 @@ (defalias 'mail-decode-encoded-address-region 'rfc2047-decode-address-region) (defalias 'mail-decode-encoded-address-string 'rfc2047-decode-address-string) +(defun mail-header-parse-addresses-lax (string) + "Parse STRING as a comma-separated list of mail addresses. +The return value is a list with mail/name pairs." + (delq nil + (mapcar (lambda (elem) + (or (mail-header-parse-address elem) + (mail-header-parse-address-lax elem))) + (mail-header-parse-addresses string t)))) + +(defun mail-header-parse-address-lax (string) + "Parse STRING as a mail address. +Returns a mail/name pair. + +This function will first try to parse STRING as a +standards-compliant address string, and if that fails, try to use +heuristics to determine the email address and the name in the +string." + (with-temp-buffer + (insert (string-clean-whitespace string)) + ;; Find the bit with the @ and guess that that's the mail. + (goto-char (point-max)) + (when (search-backward "@" nil t) + (if (re-search-backward " " nil t) + (forward-char 1) + (goto-char (point-min))) + (let* ((start (point)) + (mail (buffer-substring + start (or (re-search-forward " " nil t) + (goto-char (point-max)))))) + (delete-region start (point)) + ;; We've now removed the email bit, so the rest of the stuff + ;; has to be the name. + (cons (string-trim mail "[<]+" "[>]+") + (let ((name (string-trim (buffer-string) + "[ \t\n\r(]+" "[ \t\n\r)]+"))) + (if (length= name 0) + nil + name))))))) + (provide 'mail-parse) ;;; mail-parse.el ends here |