[PATCH v2] Add --emit-message-id flag to notmuch-insert.

Subject: [PATCH v2] Add --emit-message-id flag to notmuch-insert.

Date: Wed, 11 Sep 2024 11:00:45 -0400

To: notmuch@notmuchmail.org

Cc:

From: Brennan Vincent


---
 doc/man1/notmuch-insert.rst |  5 +++++
 notmuch-insert.c            | 26 +++++++++++++++++++++-----
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/doc/man1/notmuch-insert.rst b/doc/man1/notmuch-insert.rst
index e05bd0b5..3848b8f6 100644
--- a/doc/man1/notmuch-insert.rst
+++ b/doc/man1/notmuch-insert.rst
@@ -66,6 +66,11 @@ Supported options for **insert** include
    umask.  By default, delivered mail is only readable by the current
    user.
 
+.. option:: --emit-message-id
+
+   On successful exit (or with ``--keep``),
+   print the message ID of the newly indexed message.
+
 .. option:: --decrypt=(true|nostash|auto|false)
 
    If ``true`` and the message is encrypted, try to decrypt the
diff --git a/notmuch-insert.c b/notmuch-insert.c
index e44607ad..5554245b 100644
--- a/notmuch-insert.c
+++ b/notmuch-insert.c
@@ -399,9 +399,10 @@ maildir_write_new (const void *ctx, int fdin, const char *maildir, bool world_re
  * database results in error status regardless of keep.
  */
 static notmuch_status_t
-add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops,
+add_file (const void *ctx, notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops,
 	  bool synchronize_flags, bool keep,
-	  notmuch_indexopts_t *indexopts)
+	  notmuch_indexopts_t *indexopts,
+	  char **message_id_out)
 {
     notmuch_message_t *message;
     notmuch_status_t status;
@@ -441,8 +442,6 @@ add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops,
     }
 
   DONE:
-    notmuch_message_destroy (message);
-
     if (status) {
 	if (keep) {
 	    status = NOTMUCH_STATUS_SUCCESS;
@@ -458,6 +457,15 @@ add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops,
 	    }
 	}
     }
+    if (message_id_out) {
+	if (!status) {
+	    *message_id_out = talloc_strdup(ctx, notmuch_message_get_message_id(message));
+	} else {
+	    *message_id_out = NULL;
+	}
+    }
+    notmuch_message_destroy (message);
+
 
   FAIL:
     return status;
@@ -477,10 +485,12 @@ notmuch_insert_command (notmuch_database_t *notmuch, int argc, char *argv[])
     bool keep = false;
     bool hooks = true;
     bool world_readable = false;
+    bool emit_message_id = false;
     notmuch_bool_t synchronize_flags;
     char *maildir;
     char *newpath;
     int opt_index;
+    char *message_id;
     notmuch_indexopts_t *indexopts = notmuch_database_get_default_indexopts (notmuch);
 
     void *local = talloc_new (NULL);
@@ -491,6 +501,7 @@ notmuch_insert_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	{ .opt_bool = &keep, .name = "keep" },
 	{ .opt_bool = &hooks, .name = "hooks" },
 	{ .opt_bool = &world_readable, .name = "world-readable" },
+	{ .opt_bool = &emit_message_id, .name = "emit-message-id" },
 	{ .opt_inherit = notmuch_shared_indexing_options },
 	{ .opt_inherit = notmuch_shared_options },
 	{ }
@@ -580,7 +591,8 @@ notmuch_insert_command (notmuch_database_t *notmuch, int argc, char *argv[])
     }
 
     /* Index the message. */
-    status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep, indexopts);
+    status = add_file (local, notmuch, newpath, tag_ops, synchronize_flags, keep, indexopts,
+		       emit_message_id ? &message_id : NULL);
 
     /* Commit changes. */
     close_status = notmuch_database_close (notmuch);
@@ -606,6 +618,10 @@ notmuch_insert_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	}
     }
 
+    if (!status && emit_message_id && message_id) {
+	printf("%s\n", message_id);
+    }
+
     if (hooks && status == NOTMUCH_STATUS_SUCCESS) {
 	/* Ignore hook failures. */
 	notmuch_run_hook (notmuch, "post-insert");
-- 
2.45.2


_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: