From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:267::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms12 with LMTPS id 2PtVF2MI8l9XMAAAsNZ9tg (envelope-from ); Sun, 03 Jan 2021 18:09:39 +0000 Received: from out0.migadu.com ([2001:41d0:2:267::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id +OsMI2II8l9WSwAA1q6Kng (envelope-from ); Sun, 03 Jan 2021 18:09:38 +0000 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kyleam.com; s=key1; t=1609697378; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E4sFQiAymC+moRTQuJh78g1SmuU5TwCL9E6XhpO0lJo=; b=KPuV+7KrSz8MBNYwKZhfTpTO4+fB0yreB1AtOztw3ne9njn5Z0bWL9PZP9peUC3zh42pOU q+8uzbfHOBKv2Rrjxv4QZku7KQjNxdhyfFywkcfkmxQNiO3GlBRYCxI+1s5kaag4sMBGGx W/UEUM3T9rhDRsXz7egb/vfOovu55BZW/eR323iV0XDQMEJM18G3+DEiHD5afiqJNPFtyf KwRQ3QVRQ3L9+x8dl5mvE5XG5nuOA5ShyAg2nR078+/lo+aSC7GVDJl6Ju/sBiEZnHS75c QfM27yAniBmNgv7LajoCv0KQ6WlHxiv3/J/RJ10hpMx9Wu+LZZ/rHg9jVT74Ew== From: Kyle Meyer To: piem@inbox.kyleam.com Subject: [PATCH 2/3] piem-notmuch-am-ready-mbox: Improve handling of attachments Message-Id: <20210103180912.17404-3-kyle@kyleam.com> In-Reply-To: <20210103180912.17404-1-kyle@kyleam.com> References: <20210103180912.17404-1-kyle@kyleam.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: kyle@kyleam.com Date: Sun, 03 Jan 2021 18:09:38 GMT X-TUID: Ahu1zH0HvEt5 piem-notmuch-am-ready-mbox tries to get attached patches from the plist returned by notmuch-show-get-message-properties. This works okay for simple cases, but it doesn't find a patch if the content is encoded or if the MIME tree isn't structured as expected. Instead use mm-decode functions to get a list of handles and display their content. This new approach, as well as the previous one, probably isn't compatible with `format-patch --attach' patches, but I'm not going to worry about that until I actually see such a patch in the wild. --- piem-notmuch.el | 55 ++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/piem-notmuch.el b/piem-notmuch.el index ff8654c..94b1fa0 100644 --- a/piem-notmuch.el +++ b/piem-notmuch.el @@ -28,6 +28,7 @@ ;;; Code: +(require 'mm-decode) (require 'notmuch) (require 'piem) (require 'subr-x) @@ -78,29 +79,37 @@ (defun piem-notmuch-am-ready-mbox () those parts' contents (in order) as the mbox. Otherwise, use the message itself if it looks like a patch." (when (derived-mode-p 'notmuch-show-mode) - (let ((body (car (plist-get (notmuch-show-get-message-properties) - :body)))) - (pcase (plist-get body :content-type) - ((and "text/plain" - (guard (string-match-p piem-patch-subject-re - (notmuch-show-get-subject)))) - (let ((id (notmuch-show-get-message-id))) - (lambda () - (call-process notmuch-command nil t nil - "show" "--format=mbox" id)))) - ("multipart/mixed" - (when-let ((patches - (delq nil - (mapcar (lambda (part) - (and (piem-am-patch-attachment-p - (plist-get part :content-type) - (plist-get part :filename)) - (plist-get part :content))) - (plist-get body :content))))) - (cons (lambda () - (dolist (patch patches) - (insert patch))) - "mbox"))))))) + (if (string-prefix-p + "multipart/" + (plist-get + (car (plist-get (notmuch-show-get-message-properties) + :body)) + :content-type)) + (let (patches) + (piem-notmuch--with-current-message + (notmuch-foreach-mime-part + (lambda (p) + (and (piem-am-patch-attachment-p + (mm-handle-media-type p) + (mm-handle-filename p)) + (with-temp-buffer + (mm-display-inline p) + (push (buffer-substring-no-properties + (point-min) (point-max)) + patches)))) + (mm-dissect-buffer))) + (when patches + (setq patches (nreverse patches)) + (cons (lambda () + (dolist (patch patches) + (insert patch))) + "mbox"))) + (when (string-match-p piem-patch-subject-re + (notmuch-show-get-subject)) + (let ((id (notmuch-show-get-message-id))) + (lambda () + (call-process notmuch-command nil t nil + "show" "--format=mbox" id))))))) ;;;###autoload (define-minor-mode piem-notmuch-mode -- 2.29.2