From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms13.migadu.com with LMTPS id MGt2OVnD9Wb+owAAe85BDQ:P1 (envelope-from ) for ; Thu, 26 Sep 2024 20:26:02 +0000 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id MGt2OVnD9Wb+owAAe85BDQ (envelope-from ) for ; Thu, 26 Sep 2024 22:26:02 +0200 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1727382361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-owner:list-unsubscribe:list-subscribe:list-post; bh=0Mno93Th0F93PaGQf3FscIXt2Uo+VeObQ/nYjOvtaHg=; b=TWZTLgin/RnsXU7foSA0C4NfKBLttFsyyPvN5WFGn6Prr5yuXJUzYipJ/duuC5usqlJL+g oAtcSZvahjUKMtuGTbb28F0i+y2iDp6FTApkuWwJUMtluIJZuRUtaz/3/liC4xLRRad3cw B5MC8LMbwdyrOqqJcQI6ZrPQgyMKqL6vbN2CMohITquLYOEofcb5rIicgtA4Z/jW5t9hUs QduAfNq9K1+ceQ2M5oQZvJE0Hn00bTCcc0lznQqgE93zx0QD27b+ACeAIv7T/G1ktIOSdR vdtLbKo3/aSFkmOyDHwuIwhS5XW3n1wRkG+wkJNRT2pT2tjLRypNrx0Y6LeJZQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1727382361; a=rsa-sha256; cv=none; b=Yn1gfup94o+WBfrQ0zEwrkcr8JZrx2HTD5+o+NO8Lwg595cKZkVLm2YkuJzz4urfUzqJw8 /HBZgFooQHhtwoaSsso3Z/chJFYYmWY+OU9KENOoPZ8b2C8flaToL1Bgh7Be06Jtn/lTJC 1NP2F4CuCnfLYFWoBjqLozLGXUTRSywOAbXDuR9iAML3d6Ldls9l0QEUhU7abVzjF6Jyb8 Qqsn4JOTUR18b/VYYgVWDwsgvPqCETQfjJ9kY+pakB6DukWnKCaYVnwqgrwpZ6HwlleAtJ XGTlqnP03YMata6MzX4FA6Q0RAv0m80crksQJDTsxvfxQKEcEE9eSWOggHxW6Q== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org Received: from mail.notmuchmail.org (yantan.tethera.net [IPv6:2a01:4f9:c011:7a79::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 9460A74C1B for ; Thu, 26 Sep 2024 22:26:00 +0200 (CEST) Received: from yantan.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 922055F71C; Thu, 26 Sep 2024 20:25:57 +0000 (UTC) Received: from smtp.umanwizard.com (smtp.umanwizard.com [54.203.248.109]) by mail.notmuchmail.org (Postfix) with ESMTP id A55DF5E016 for ; Thu, 26 Sep 2024 20:25:54 +0000 (UTC) Received: from localhost ([98.167.244.13]) by smtp.umanwizard.com ; 26 Sep 2024 20:25:52 +0000 X-Fes-Received-For: notmuch@notmuchmail.org X-Fes-Received-From: From: Brennan Vincent To: notmuch@notmuchmail.org Subject: [PATCH v3] Add --emit-message-id flag to notmuch-insert. In-Reply-To: <87plpbtfj2.fsf@taipei.mail-host-address-is-not-set> References: <87plpbtfj2.fsf@taipei.mail-host-address-is-not-set> X-Envelope-From: 358dec4b477935503156f4d6133e3d262cf236de Mon Sep 17 00:00:00 2001 Date: Thu, 26 Sep 2024 16:25:51 -0400 Message-ID: <87a5fukvqo.fsf@denver.mail-host-address-is-not-set> MIME-Version: 1.0 X-Fes-Encrypted: true X-Fes-Ehlo-Domain: localhost Message-ID-Hash: V4TEVGY7DRC3VLCGW7RTL4IPMDPYNSYZ X-Message-ID-Hash: V4TEVGY7DRC3VLCGW7RTL4IPMDPYNSYZ X-MailFrom: brennan@umanwizard.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Country: DE X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -4.21 X-Spam-Score: -4.21 X-Migadu-Scanner: mx13.migadu.com X-Migadu-Queue-Id: 9460A74C1B X-TUID: wyID9t0cTPpp It may be the case that a user (or script) wants to take some action on a message immediately after inserting it. This can be done if one knows the Message-ID with which the message was inserted. In order to facilitate this use case, we add a flag "--emit-message-id" to notmuch insert. One example of a possible usecase (which is part of an upcoming patch) is an emacs command to begin editing the current buffer as a draft. --- doc/man1/notmuch-insert.rst | 5 +++++ notmuch-insert.c | 24 ++++++++++++++++++++---- test/T070-insert.sh | 31 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/doc/man1/notmuch-insert.rst b/doc/man1/notmuch-insert.rst index e05bd0b5..141e5cb9 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 + + If the message was successfully inserted into the database, + 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..05945a99 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -401,7 +401,8 @@ maildir_write_new (const void *ctx, int fdin, const char *maildir, bool world_re static notmuch_status_t add_file (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 == NOTMUCH_STATUS_SUCCESS) { + *message_id_out = talloc_strdup(notmuch, 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 (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 (emit_message_id && message_id && status == NOTMUCH_STATUS_SUCCESS) { + printf("%s\n", message_id); + } + if (hooks && status == NOTMUCH_STATUS_SUCCESS) { /* Ignore hook failures. */ notmuch_run_hook (notmuch, "post-insert"); diff --git a/test/T070-insert.sh b/test/T070-insert.sh index 73953272..6ae6abd0 100755 --- a/test/T070-insert.sh +++ b/test/T070-insert.sh @@ -257,6 +257,33 @@ test_expect_code 1 "notmuch insert < $gen_msg_filename 2>&1" notmuch config set new.tags $OLDCONFIG +gen_insert_msg +test_begin_subtest "--emit-message-id works" +output=$(notmuch insert --emit-message-id < $gen_msg_filename) +expected_msg_id=$(< $gen_msg_filename grep Message-Id | sed -E 's/^Message-Id: <(.*)>$/\1/') +test_expect_equal "$output" "$expected_msg_id" + +test_begin_subtest "--emit-message-id works even when the message already exists" +output=$(notmuch insert --emit-message-id < $gen_msg_filename) +expected_msg_id=$(< $gen_msg_filename grep Message-Id | sed -E 's/^Message-Id: <(.*)>$/\1/') +test_expect_equal "$output" "$expected_msg_id" + +gen_insert_msg +test_begin_subtest "--keep --emit-message-id works when maildir flag sync fails" +make_shim shim-failed-sync < +notmuch_status_t +notmuch_message_tags_to_maildir_flags (notmuch_message_t *message) +{ + return NOTMUCH_STATUS_OUT_OF_MEMORY; +} +EOF +notmuch config set maildir.synchronize_flags true +output=$(notmuch_with_shim shim-failed-sync insert --keep --emit-message-id < $gen_msg_filename) +expected_msg_id=$(< $gen_msg_filename grep Message-Id | sed -E 's/^Message-Id: <(.*)>$/\1/') +test_expect_equal "$output" "$expected_msg_id" +notmuch config set maildir.synchronize_flags false + # DUPLICATE_MESSAGE_ID is not tested here, because it should actually pass. # pregenerate all of the test shims for code in FILE_NOT_EMAIL READ_ONLY_DATABASE UPGRADE_REQUIRED PATH_ERROR OUT_OF_MEMORY XAPIAN_EXCEPTION; do @@ -282,6 +309,10 @@ for code in FILE_NOT_EMAIL READ_ONLY_DATABASE UPGRADE_REQUIRED PATH_ERROR; do test_begin_subtest "success exit with --keep when index_file returns $code" test_expect_code 0 "notmuch_with_shim shim-$code insert --keep < \"$gen_msg_filename\"" + + test_begin_subtest "nothing printed even with --keep --emit-message-id when index_file returns $code" + output=$(notmuch_with_shim shim-$code insert --keep --emit-message-id < $gen_msg_filename) + test_expect_equal "$output" "" done for code in OUT_OF_MEMORY XAPIAN_EXCEPTION ; do -- 2.46.0