From 1da0cae55f1c04609f605a0502a4492ca1d833fc Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Sat, 18 May 2019 16:18:10 +0200 Subject: [PATCH] message: Clean up bogus GitHub recipients --- .emacs.d/lisp/init-message.el | 47 ++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/.emacs.d/lisp/init-message.el b/.emacs.d/lisp/init-message.el index 2a9b5d6c..40df0245 100644 --- a/.emacs.d/lisp/init-message.el +++ b/.emacs.d/lisp/init-message.el @@ -66,15 +66,44 @@ systematically send encrypted emails when possible." (add-hook 'message-send-hook #'ambrevar/message-sign-encrypt-if-all-keys-trusted) -;; Fix replying to GitHub. TODO: Does not work. -(defun ambrevar/message-alter-bogus-github-recipients (addrcell) - "Discard @reply.github.com mail addresses. -ADDRCELL is a cons cell where the car is the mail address and the -cdr is the complete address (full name and mail address)." - (unless (string-match (rx "@noreply.github.com" string-end) - (cdr addrcell)) - addrcell)) -(setq message-alter-recipients-function 'ambrevar/message-alter-bogus-github-recipients) +;; Fix replying to GitHub. +;; We could leverage `message-alter-recipients-function' but it does not seem to +;; be called with Notmuch for instance. +(defun ambrevar/cleanup-github-recipients () + "When replying to a github message, clean up all bogus recipients. +Also remove signature. +This function is useful in `message-setup-hook'." + (interactive) + (let ((to (message-fetch-field "To"))) + (when (and to + (string-match (rx "@reply.github.com" string-end) + (cadr (mail-extract-address-components to)))) + (dolist (hdr '("To" "Cc" "Bcc")) + (let ((header-value (message-fetch-field hdr))) + (when header-value + (message-replace-header + hdr + (mapconcat (lambda (addrcell) + (format "\"%s\" <%s>" (car addrcell) (cadr addrcell))) + (cl-delete-if + (lambda (addrcell) + (string-match (rx "@noreply.github.com" string-end) + (cadr addrcell))) + (mail-extract-address-components header-value t)) + ", "))))) + ;; Delete signature if any. + (delete-region (save-excursion + (message-goto-signature) + (unless (eobp) + (forward-line -1)) + (point)) + (point-max)) + ;; Deleting trailing blank lines. + (save-excursion + (goto-char (point-max)) + (delete-blank-lines) + (delete-blank-lines))))) +(add-hook 'message-setup-hook 'ambrevar/cleanup-github-recipients) (defvar ambrevar/message-compose-fortune-p nil