Mu4e: More robust Gmail account checking

Emails sent to an @gmail.com account may have been forwarded to a non-gmail mailbox.
Thus the current approach is over-eager.

Only checking @gmail on send, and then matching a user's maildir (not email) with
received messages is a more robust approach.

To accommodate this we switch `+mu4e-gmail-addresses' out for `+mu4e-gmail-accounts'.

Along the way `mu4e-index-cleanup' and `mu4e-index-lazy-check' customisations were
moved into the readme as a recommendation for Gmail-only users, to avoid causing
adverse effects for users who have non-gmail accounts too.
This commit is contained in:
TEC 2021-01-17 13:49:10 -06:00
parent 611e532d38
commit 2307ae5644
No known key found for this signature in database
GPG key ID: 779591AFDB81F06C
2 changed files with 39 additions and 32 deletions

View file

@ -387,45 +387,32 @@ Must be set before org-msg is loaded to take effect.")
(when (featurep! +gmail)
(after! mu4e
(defvar +mu4e-gmail-addresses nil
"A list of email addresses which, despite not:
- having '@gmail.com' in them, or
- being in a maildir where the name includes 'gmail'
(defvar +mu4e-gmail-accounts nil
"Gmail accounts that do not contain \"gmail\" in address and maildir.
Should be treated as a gmail address.")
An alist of Gmail addresses of the format \((\"username@domain.com\" . \"account-maildir\"))
to which Gmail integrations (behind the `+gmail' flag of the `mu4e' module) should be applied.
See `+mu4e-msg-gmail-p' and `mu4e-sent-messages-behavior'.")
;; don't save message to Sent Messages, Gmail/IMAP takes care of this
(setq mu4e-sent-messages-behavior
(lambda () ;; TODO make use +mu4e-msg-gmail-p
(if (or (string-match-p "@gmail.com\\'" (message-sendmail-envelope-from))
(member (message-sendmail-envelope-from) +mu4e-gmail-addresses))
'delete 'sent))
;; don't need to run cleanup after indexing for gmail
mu4e-index-cleanup nil
;; because gmail uses labels as folders we can use lazy check since
;; messages don't really "move"
mu4e-index-lazy-check t)
(member (message-sendmail-envelope-from)
(mapcar #'car +mu4e-gmail-accounts)))
'delete 'sent)))
(defun +mu4e-msg-gmail-p (msg)
(or
(string-match-p "@gmail.com"
(cond
((member (mu4e-message-field msg :to)
(append (mu4e-personal-addresses)
+mu4e-gmail-addresses))
(mu4e-message-field msg :to))
((member (mu4e-message-field msg :from)
(append (mu4e-personal-addresses)
+mu4e-gmail-addresses))
(mu4e-message-field msg :from))
(t "")))
(string-match-p "gmail" (mu4e-message-field msg :maildir))))
(let ((root-maildir
(replace-regexp-in-string "/.*" ""
(substring (mu4e-message-field msg :maildir) 1))))
(or (string-match-p "gmail" root-maildir)
(member root-maildir (mapcar #'cdr +mu4e-gmail-accounts)))))
;; In my workflow, emails won't be moved at all. Only their flags/labels are
;; changed. Se we redefine the trash and refile marks not to do any moving.
;; However, the real magic happens in `+mu4e|gmail-fix-flags'.
;; However, the real magic happens in `+mu4e-gmail-fix-flags-h'.
;;
;; Gmail will handle the rest.
(defun +mu4e--mark-seen (docid _msg target)