Paste number 339361: mu4e patch for debbugs-gnu.el

Paste number 339361: mu4e patch for debbugs-gnu.el
Pasted by: rekado
When:3 years, 3 months ago
Share:Tweet this! | http://paste.lisp.org/+79UP
Channel:None
Paste contents:
Raw Source | XML | Display As
diff --git a/packages/debbugs/debbugs-gnu.el b/packages/debbugs/debbugs-gnu.el
index 42f2cb086..aaee49851 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")
 
@@ -292,11 +296,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,53 @@ 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-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"))
+         (maildir (concat tmpdir "/maildir-debbugs." bug-str)))
+    ;; 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))
+        (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)))
+        (setq start end)
+        (forward-char)))
+
+    ;; Clean up
+    (delete-file mbox)
+
+    ;; Index the mails in this maildir
+    (let ((mu4e-maildir maildir)
+          (mu4e-mu-home (concat maildir "/.mu")))
+      ;; We cannot use mu4e-update-index because it runs
+      ;; asynchronously.
+      (call-process-shell-command
+       (concat "mu index --muhome " mu4e-mu-home " -m " mu4e-maildir))
+      (with-current-buffer (window-buffer (selected-window))
+        (let ((msgid (substring (cdr (assq 'msgid status)) 1 -1)))
+          (mu4e-headers-search "" 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.
@@ -1360,6 +1412,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")))))

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.