[PATCH 12/12] lib/message: add notmuch_message_set_header

Subject: [PATCH 12/12] lib/message: add notmuch_message_set_header

Date: Fri, 22 Jun 2018 22:42:47 -0300

To: notmuch@notmuchmail.org

Cc:

From: David Bremner


This saves a header value to the message document data area, where it
can be retrieved later by notmuch_message_get_header.
---
 lib/message.cc            | 24 +++++++++++++++
 lib/notmuch.h             |  6 +++-
 test/T720-message-data.sh | 64 +++++++++++++++++++++++++++++++++++++++
 util/string-map.c         |  4 +--
 4 files changed, 95 insertions(+), 3 deletions(-)
 create mode 100755 test/T720-message-data.sh

diff --git a/lib/message.cc b/lib/message.cc
index c3c71fd4..cfd51b2e 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -597,6 +597,30 @@ notmuch_message_get_header (notmuch_message_t *message, const char *header)
     return _notmuch_message_file_get_header (message->message_file, header);
 }
 
+notmuch_status_t
+notmuch_message_set_header (notmuch_message_t *message,
+			    const char *header,
+			    const char *value)
+{
+    /* We don't want to accept changes which will be silently lost on
+     * sync */
+    notmuch_status_t status = _notmuch_database_ensure_writable (message->notmuch);
+    if (status)
+	return status;
+
+    const char *key = talloc_asprintf (message->data_map, "header.%s", header);
+
+    _notmuch_message_ensure_data_map (message);
+
+    _notmuch_string_map_set (message->data_map, key, value);
+    message->modified = true;
+
+    if (! message->frozen)
+	_notmuch_message_sync (message);
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 /* Return the message ID from the In-Reply-To header of 'message'.
  *
  * Returns an empty string ("") if 'message' has no In-Reply-To
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 247f6ad7..ca809016 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -58,7 +58,7 @@ NOTMUCH_BEGIN_DECLS
  * version in Makefile.local.
  */
 #define LIBNOTMUCH_MAJOR_VERSION	5
-#define LIBNOTMUCH_MINOR_VERSION	2
+#define LIBNOTMUCH_MINOR_VERSION	3
 #define LIBNOTMUCH_MICRO_VERSION	0
 
 
@@ -1516,6 +1516,10 @@ notmuch_message_get_date  (notmuch_message_t *message);
 const char *
 notmuch_message_get_header (notmuch_message_t *message, const char *header);
 
+notmuch_status_t
+notmuch_message_set_header (notmuch_message_t *message,
+			    const char *header,
+			    const  char *value );
 /**
  * Get the tags for 'message', returning a notmuch_tags_t object which
  * can be used to iterate over all tags.
diff --git a/test/T720-message-data.sh b/test/T720-message-data.sh
new file mode 100755
index 00000000..3b74d0b8
--- /dev/null
+++ b/test/T720-message-data.sh
@@ -0,0 +1,64 @@
+#!/usr/bin/env bash
+test_description="message data API"
+
+. $(dirname "$0")/test-lib.sh || exit 1
+
+add_email_corpus
+
+cat <<EOF > c_head
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+#include <notmuch-test.h>
+
+int main (int argc, char** argv)
+{
+   notmuch_database_t *db;
+   char *val;
+   notmuch_status_t stat;
+
+   EXPECT0(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));
+
+EOF
+
+cat <<EOF > c_tail
+   EXPECT0(notmuch_database_destroy(db));
+}
+EOF
+
+add_message '[subject]=initial' '[id]=subject-test-id'
+
+test_begin_subtest "find initial subject"
+notmuch search id:subject-test-id | notmuch_search_sanitize > OUTPUT
+cat <<EOF >EXPECTED
+thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; initial (inbox unread)
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "modify subject"
+cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
+{
+   notmuch_message_t *message;
+   const char* subject;
+   EXPECT0 (notmuch_database_find_message (db, "subject-test-id", &message));
+   assert (message);
+   EXPECT0 (notmuch_message_set_header (message, "subject", "modified"));
+   subject = notmuch_message_get_header (message, "subject");
+   printf ("subject = %s\n", subject);
+}
+EOF
+cat <<'EOF' >EXPECTED
+== stdout ==
+subject = modified
+== stderr ==
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "modified subject persists"
+notmuch search id:subject-test-id | notmuch_search_sanitize > OUTPUT
+cat <<EOF >EXPECTED
+thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; modified (inbox unread)
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+test_done
diff --git a/util/string-map.c b/util/string-map.c
index 41a6881d..644803ac 100644
--- a/util/string-map.c
+++ b/util/string-map.c
@@ -157,8 +157,8 @@ _notmuch_string_map_set (notmuch_string_map_t *map,
     pair = bsearch_first (map->pairs, map->length, key, true);
     if (! pair)
 	_notmuch_string_map_append (map, key, value);
-
-    pair->value = talloc_strdup (map, value);
+    else
+	pair->value = talloc_strdup (map, value);
 
 }
 
-- 
2.17.1

_______________________________________________
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch

Thread: