emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] [debbugs] Add mu4e support.


From: Ricardo Wurmus
Subject: [PATCH] [debbugs] Add mu4e support.
Date: Sun, 23 Apr 2017 12:05:17 +0200
User-agent: mu4e 0.9.18; emacs 25.1.1

Hi,

this patch is for the debbugs.el EPLA package.  It adds the mail client
“mu4e” as a debbugs-gnu mail backend.

When “debbugs-gnu-mail-backend” is set to “'mu4e”, visiting a bug will
download the bug’s mbox file, turn it into a separate Maildir tree under
“mu4e-maildir” called “emacs-debbugs.1234” where “1234” is the bug id,
runs “mu index” (synchronously) to update the mail database, and then
shows the emails as usual with mu4e.

I’ve been using this patch for a while with satisfactory results.

--
Ricardo

GPG: BCA6 89B6 3655 3801 C3C6  2150 197A 5888 235F ACAC
https://elephly.net


Attachment: signature.asc
Description: PGP signature

>From c841c4ad0b77aad0b06a31915d46a7cefafc2058 Mon Sep 17 00:00:00 2001
From: Ricardo Wurmus <address@hidden>
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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]