discussion and development of piem
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: piem@inbox.kyleam.com
Subject: [PATCH 2/3] inject: Support skipping messages
Date: Sat, 22 Aug 2020 15:01:29 -0400	[thread overview]
Message-ID: <20200822190130.20397-3-kyle@kyleam.com> (raw)
In-Reply-To: <20200822190130.20397-1-kyle@kyleam.com>

piem-inject-thread-into-maildir allows pulling a thread from a
public-inbox URL, but repeating the process to get new messages in the
thread leads to duplicates in the local store.  Add a predicate that
the caller can configure to avoid this.

Note that the predicate has the entire message available to make its
decision, but it's called with the message ID for convenience because
that's likely the only information needed to determine if the message
is already in the local store.
 piem.el | 63 ++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 45 insertions(+), 18 deletions(-)

diff --git a/piem.el b/piem.el
index 5fc7f04..aa0ec0c 100644
--- a/piem.el
+++ b/piem.el
@@ -145,6 +145,16 @@ (defcustom piem-maildir-directory nil
 If non-nil, this must be an existing Maildir directory."
   :type 'string)
+(defcustom piem-mail-injection-skipif-predicate nil
+  "Predicate to decide whether to skip injecting a message.
+The function is called with the message ID (no surrounding
+brackets) within a buffer that is narrowed to the message.  The
+function does not need to worry about saving point.  A non-nil
+return value signals that `piem-inject-thread-into-maildir'
+should skip the message."
+  :type 'function)
 (defcustom piem-after-mail-injection-functions nil
   "Functions called after writing messages to `piem-maildir-directory'.
 Functions should accept one argument, the message ID given to
@@ -406,7 +416,8 @@ (defun piem-download-and-decompress (url)
     (url-retrieve url #'piem--decompress-callback)))
 (defun piem--write-mbox-to-maildir ()
-  (let ((n-messages 0))
+  (let ((n-added 0)
+        (n-skipped 0))
     (while (and (not (eobp))
                 (search-forward "From mboxrd@z" nil t))
       (let* ((beg (line-beginning-position 2))
@@ -417,21 +428,31 @@ (defun piem--write-mbox-to-maildir ()
              (basename (piem-maildir-make-uniq-maildir-id))
              (tmpfile (concat piem-maildir-directory "/tmp/" basename)))
         (goto-char beg)
-        ;; TODO: Consider supporting a caller-specified predicate that
-        ;; could be used, for example, to skip messages already in
-        ;; their local mail.
-        (let ((case-fold-search nil))
-          (while (re-search-forward
-                  (rx line-start ">" (group (zero-or-more ">") "From "))
-                  end t)
-            (replace-match "\\1" t)))
-        (write-region beg end tmpfile nil nil nil 'excl)
-        (piem-maildir-move-tmp-to-new piem-maildir-directory
-                                      basename)
-        (delete-file tmpfile)
-        (cl-incf n-messages)
+        (if (and (functionp piem-mail-injection-skipif-predicate)
+                 (save-excursion
+                   (save-restriction
+                     (narrow-to-region beg end)
+                     (funcall piem-mail-injection-skipif-predicate
+                              (replace-regexp-in-string
+                               "\\`<\\(.*\\)>\\'" "\\1"
+                               (or (save-restriction
+                                     (save-excursion
+                                       (message-narrow-to-head-1)
+                                       (message-fetch-field "message-id" t)))
+                                   (error "Message lacks message-ID")))))))
+            (cl-incf n-skipped)
+          (let ((case-fold-search nil))
+            (while (re-search-forward
+                    (rx line-start ">" (group (zero-or-more ">") "From "))
+                    end t)
+              (replace-match "\\1" t)))
+          (write-region beg end tmpfile nil nil nil 'excl)
+          (piem-maildir-move-tmp-to-new piem-maildir-directory
+                                        basename)
+          (delete-file tmpfile)
+          (cl-incf n-added))
         (goto-char end)))
-    n-messages))
+    (cons n-added n-skipped)))
 (defun piem--inject-thread-callback (status mid message-only)
   (let ((buffer (current-buffer)))
@@ -442,9 +463,15 @@ (defun piem--inject-thread-callback (status mid message-only)
             (unless message-only
-            (let ((message-count (piem--write-mbox-to-maildir)))
-              (message "%d message(s) for %s moved to %s"
-                       message-count mid
+            (pcase-let ((`(,added-count . ,skipped-count)
+                         (piem--write-mbox-to-maildir)))
+              (message "Added %d message%s%s for %s to %s"
+                       added-count
+                       (if (= added-count 1) "" "s")
+                       (if (> skipped-count 0)
+                           (format " (skipping %d)" skipped-count)
+                         "")
+                       mid
                        (abbreviate-file-name piem-maildir-directory)))
             (run-hook-with-args 'piem-after-mail-injection-functions mid)))
       (and (buffer-live-p buffer)

  parent reply	other threads:[~2020-08-22 19:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-22 19:01 [PATCH 0/3] inject: Add support for skipping messages Kyle Meyer
2020-08-22 19:01 ` [PATCH 1/3] inject: Abbreviate piem-maildir-directory in message Kyle Meyer
2020-08-22 19:01 ` Kyle Meyer [this message]
2020-08-22 19:01 ` [PATCH 3/3] notmuch: Extract "known message?" logic to function Kyle Meyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://git.kyleam.com/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200822190130.20397-3-kyle@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=piem@inbox.kyleam.com \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).