djcb / mu

maildir indexer/searcher + emacs mail client + guile bindings
http://www.djcbsoftware.nl/code/mu
GNU General Public License v3.0
1.61k stars 387 forks source link

[mu4e rfe] Make it easier to access MIME part of given type #2647

Open stefanv opened 6 months ago

stefanv commented 6 months ago

I have an action that parses GitHub notifications to find a pull request link and open it in the browser. To do that, I need to extract the plain text version of the email.

Before, I used the :body-txt field, but I guess GitHub changed to using MIME attachments. As far as I can tell, extracting the first plain-text MIME attachment is not entirely trivial:

(let*
  (parts (mu4e-view-mime-parts))
  (text-parts (seq-filter
               (lambda (el) (equal (plist-get el :mime-type) "text/plain"))
               parts))
  (txt (mm-get-part (plist-get (car text-parts) :handle)))

Would it make sense to add a utility to either (a) grab the first MIME part of a certain type or (b) provide some logic to extract the plain text version of the email?

Here is the full action, in case it's useful:

  (defun stefan/mu4e-action-view-pull-request (msg)
    "Locate a GitHub pull request URL and open in the browser.
  The browser used is specified in `browse-url-generic-program'."
    (let* ((parts (mu4e-view-mime-parts))
           (text-parts (seq-filter
                        (lambda (el) (equal (plist-get el :mime-type) "text/plain"))
                        parts))
           (txt (mm-get-part (plist-get (car text-parts) :handle)))
           (pr-re (rx line-start "https://github.com"
                      "/" (one-or-more (not "/"))
                      "/" (one-or-more (not "/"))
                      "/" (or "pull" "issues")
                      "/" (one-or-more digit)
                      (optional "#" (one-or-more not-newline)))))
      (unless txt
        (mu4e-error "No text part for this message"))

      (unless (string-match pr-re txt)
        (mu4e-error "No pull request URL found"))

      (browse-url (match-string 0 txt))))
djcb commented 6 months ago

Sounds useful. Something for the next development series :)