From c841c4ad0b77aad0b06a31915d46a7cefafc2058 Mon Sep 17 00:00:00 2001 From: Ricardo Wurmus Date: Mon, 10 Apr 2017 09:33:51 +0200 Subject: [PATCH] [debbugs] Add mu4e support. * packages/debbugs/debbugs-gnu.el (debbugs-gnu-mail-backend, debbugs-gnu-select-report): Handle "mu4e" option. (debbugs--extract-mbox-to-maildir, debbugs-read-emacs-bug-with-mu4e): New procedures. --- packages/debbugs/debbugs-gnu.el | 65 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/packages/debbugs/debbugs-gnu.el b/packages/debbugs/debbugs-gnu.el index ebfa738a4..aea0ddd86 100644 --- a/packages/debbugs/debbugs-gnu.el +++ b/packages/debbugs/debbugs-gnu.el @@ -170,9 +170,13 @@ (autoload 'mail-header-subject "nnheader") (autoload 'message-goto-body "message") (autoload 'message-make-from "message") +(autoload 'message-fetch-field "message") +(autoload 'nndoc-mbox-article-begin "nndoc") +(autoload 'nnmail-write-region "nnmail") (autoload 'rmail-get-new-mail "rmail") (autoload 'rmail-show-message "rmail") (autoload 'rmail-summary "rmailsum") +(autoload 'mu4e-headers-search "mu4e-headers") (autoload 'vc-dir-hide-up-to-date "vc-dir") (autoload 'vc-dir-mark "vc-dir") @@ -293,11 +297,12 @@ suppressed bugs is toggled by `debbugs-gnu-toggle-suppress'." (defcustom debbugs-gnu-mail-backend 'gnus "The email backend to use for reading bug report email exchange. -If this is `gnus', the default, use Gnus. -If this is `rmail', use Rmail instead." +If this is `gnus', the default, use Gnus. If this is `rmail', +use Rmail. If this is `mu4e', use Mu4e instead." :group 'debbugs-gnu :type '(radio (function-item :tag "Use Gnus" gnus) - (function-item :tag "Use Rmail" rmail)) + (function-item :tag "Use Rmail" rmail) + (function-item :tag "Use Mu4e" mu4e)) :version "25.1") (defface debbugs-gnu-archived '((t (:inverse-video t))) @@ -1329,6 +1334,58 @@ MERGED is the list of bugs merged with this one." (define-key rmail-mode-map "C" 'debbugs-gnu-send-control-message) (rmail-show-message 1))) +(defun debbugs--extract-mbox-to-maildir (bug-str maildir start end) + (let* ((msgid (save-restriction + (narrow-to-region start end) + (message-fetch-field "Message-ID"))) + (file (concat bug-str "-" (secure-hash 'md5 msgid) ":2,")) + (path (concat maildir "/cur/" file))) + (when (not (file-exists-p path)) + (nnmail-write-region start end path)))) + +(defun debbugs-read-emacs-bug-with-mu4e (id status merged) + "Read email exchange for debbugs bug ID. +STATUS is the bug's status list. +MERGED is the list of bugs merged with this one." + (let* ((bug-str (number-to-string id)) + (tmpdir "/tmp") + (mbox (concat tmpdir "/bug-" bug-str ".mbox")) + (subdir (concat "/emacs-debbugs." bug-str)) + (maildir (concat mu4e-maildir subdir))) + ;; Create temporary maildir structure + (dolist (dir '("cur" "new" "tmp")) + (make-directory (concat maildir "/" dir) t)) + + ;; Download mbox for the given bug + (debbugs-get-mbox id 'mboxmaint mbox) + + ;; Split mbox into individual mail files + (with-temp-buffer + (insert-file-contents mbox) + (goto-char (point-min)) + (setq start (point)) + (forward-char) + (while (nndoc-mbox-article-begin) + (setq end (point)) + (debbugs--extract-mbox-to-maildir bug-str maildir start end) + (setq start end) + (forward-char)) + ;; Save last email + (debbugs--extract-mbox-to-maildir bug-str maildir start (point-max))) + + ;; Clean up + (delete-file mbox) + + ;; We cannot use mu4e-update-index because it runs + ;; asynchronously. + (call-process-shell-command + (concat "mu index -m " mu4e-maildir)) + (with-current-buffer (window-buffer (selected-window)) + (let ((msgid (substring (cdr (assq 'msgid status)) 1 -1))) + (message msgid) + (mu4e-headers-search (concat "maildir:" subdir) + nil nil nil msgid nil))))) + (defun debbugs-read-emacs-bug-with-gnus (id status merged) "Read email exchange for debbugs bug ID. STATUS is the bug's status list. @@ -1361,6 +1418,8 @@ MERGED is the list of bugs merged with this one." (message "No bug report on the current line")) ((eq debbugs-gnu-mail-backend 'rmail) (debbugs-read-emacs-bug-with-rmail id status merged)) + ((eq debbugs-gnu-mail-backend 'mu4e) + (debbugs-read-emacs-bug-with-mu4e id status merged)) ((eq debbugs-gnu-mail-backend 'gnus) (debbugs-read-emacs-bug-with-gnus id status merged)) (t (error "No valid mail backend specified"))))) -- 2.12.2