--- /dev/null
+Return-Path: <bremner@tethera.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id E6EAE6DE0127\r
+ for <notmuch@notmuchmail.org>; Sun, 12 Jun 2016 18:06:25 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.011\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.011 tagged_above=-999 required=5\r
+ tests=[AWL=-0.000, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id BWps4Oxp_7aG for <notmuch@notmuchmail.org>;\r
+ Sun, 12 Jun 2016 18:06:17 -0700 (PDT)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 6FC626DE0231\r
+ for <notmuch@notmuchmail.org>; Sun, 12 Jun 2016 18:06:09 -0700 (PDT)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1bCGKB-0003wx-Ah; Sun, 12 Jun 2016 21:05:55 -0400\r
+Received: (nullmailer pid 5670 invoked by uid 1000);\r
+ Mon, 13 Jun 2016 01:06:04 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 3/8] lib: basic message-property API\r
+Date: Sun, 12 Jun 2016 22:05:50 -0300\r
+Message-Id: <1465779955-5539-4-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.8.1\r
+In-Reply-To: <1465779955-5539-1-git-send-email-david@tethera.net>\r
+References: <1465779955-5539-1-git-send-email-david@tethera.net>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 13 Jun 2016 01:06:26 -0000\r
+\r
+Initially, support get, set and removal of single key/value pair, as\r
+well as removing all properties.\r
+---\r
+ lib/Makefile.local | 1 +\r
+ lib/message-private.h | 16 +++++++\r
+ lib/message-property.cc | 101 ++++++++++++++++++++++++++++++++++++++++++\r
+ lib/message.cc | 52 +++++++++++++++++++++-\r
+ lib/notmuch.h | 69 +++++++++++++++++++++++++++++\r
+ test/T610-message-property.sh | 84 +++++++++++++++++++++++++++++++++++\r
+ 6 files changed, 321 insertions(+), 2 deletions(-)\r
+ create mode 100644 lib/message-private.h\r
+ create mode 100644 lib/message-property.cc\r
+ create mode 100755 test/T610-message-property.sh\r
+\r
+diff --git a/lib/Makefile.local b/lib/Makefile.local\r
+index 9280880..c012ed1 100644\r
+--- a/lib/Makefile.local\r
++++ b/lib/Makefile.local\r
+@@ -49,6 +49,7 @@ libnotmuch_cxx_srcs = \\r
+ $(dir)/directory.cc \\r
+ $(dir)/index.cc \\r
+ $(dir)/message.cc \\r
++ $(dir)/message-property.cc \\r
+ $(dir)/query.cc \\r
+ $(dir)/query-fp.cc \\r
+ $(dir)/config.cc \\r
+diff --git a/lib/message-private.h b/lib/message-private.h\r
+new file mode 100644\r
+index 0000000..7419925\r
+--- /dev/null\r
++++ b/lib/message-private.h\r
+@@ -0,0 +1,16 @@\r
++#ifndef MESSAGE_PRIVATE_H\r
++#define MESSAGE_PRIVATE_H\r
++\r
++notmuch_string_map_t *\r
++_notmuch_message_property_map (notmuch_message_t *message);\r
++\r
++notmuch_bool_t\r
++_notmuch_message_frozen (notmuch_message_t *message);\r
++\r
++void\r
++_notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix);\r
++\r
++void\r
++_notmuch_message_invalidate_metadata (notmuch_message_t *message, const char *prefix_name);\r
++\r
++#endif\r
+diff --git a/lib/message-property.cc b/lib/message-property.cc\r
+new file mode 100644\r
+index 0000000..ad2250f\r
+--- /dev/null\r
++++ b/lib/message-property.cc\r
+@@ -0,0 +1,101 @@\r
++/* message-property.cc - Properties are like tags, but (key,value) pairs.\r
++ * keys are allowed to repeat.\r
++ *\r
++ * This file is part of notmuch.\r
++ *\r
++ * Copyright © 2016 David Bremner\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 3 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program. If not, see http://www.gnu.org/licenses/ .\r
++ *\r
++ * Author: David Bremner <david@tethera.net>\r
++ */\r
++\r
++#include "notmuch-private.h"\r
++#include "database-private.h"\r
++#include "message-private.h"\r
++\r
++notmuch_status_t\r
++notmuch_message_get_property (notmuch_message_t *message, const char *key, const char **value)\r
++{\r
++ if (! value)\r
++ return NOTMUCH_STATUS_NULL_POINTER;\r
++\r
++ *value = _notmuch_string_map_get (_notmuch_message_property_map (message), key);\r
++\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
++\r
++static notmuch_status_t\r
++_notmuch_message_modify_property (notmuch_message_t *message, const char *key, const char *value,\r
++ notmuch_bool_t delete_it)\r
++{\r
++ notmuch_private_status_t private_status;\r
++ notmuch_status_t status;\r
++ char *term = NULL;\r
++\r
++ status = _notmuch_database_ensure_writable (_notmuch_message_database (message));\r
++ if (status)\r
++ return status;\r
++\r
++ if (key == NULL || value == NULL)\r
++ return NOTMUCH_STATUS_NULL_POINTER;\r
++\r
++ if (index (key, '='))\r
++ return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;\r
++\r
++ term = talloc_asprintf (message, "%s=%s", key, value);\r
++\r
++ if (delete_it)\r
++ private_status = _notmuch_message_remove_term (message, "property", term);\r
++ else\r
++ private_status = _notmuch_message_add_term (message, "property", term);\r
++\r
++ if (private_status)\r
++ return COERCE_STATUS (private_status,\r
++ "Unhandled error modifying message property");\r
++ if (! _notmuch_message_frozen (message))\r
++ _notmuch_message_sync (message);\r
++\r
++ if (term)\r
++ talloc_free (term);\r
++\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_message_add_property (notmuch_message_t *message, const char *key, const char *value)\r
++{\r
++ return _notmuch_message_modify_property (message, key, value, FALSE);\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_message_remove_property (notmuch_message_t *message, const char *key, const char *value)\r
++{\r
++ return _notmuch_message_modify_property (message, key, value, TRUE);\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_message_remove_all_properties (notmuch_message_t *message)\r
++{\r
++ notmuch_status_t status;\r
++ status = _notmuch_database_ensure_writable (_notmuch_message_database (message));\r
++ if (status)\r
++ return status;\r
++\r
++ _notmuch_message_invalidate_metadata (message, "property");\r
++ /* XXX better error reporting ? */\r
++ _notmuch_message_remove_terms (message, _find_prefix ("property"));\r
++\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 63a8da5..9d3e807 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -20,6 +20,7 @@\r
+ \r
+ #include "notmuch-private.h"\r
+ #include "database-private.h"\r
++#include "message-private.h"\r
+ \r
+ #include <stdint.h>\r
+ \r
+@@ -395,7 +396,7 @@ _notmuch_message_ensure_metadata (notmuch_message_t *message)\r
+ message->in_reply_to = talloc_strdup (message, "");\r
+ }\r
+ \r
+-static void\r
++void\r
+ _notmuch_message_invalidate_metadata (notmuch_message_t *message,\r
+ const char *prefix_name)\r
+ {\r
+@@ -552,7 +553,7 @@ notmuch_message_get_replies (notmuch_message_t *message)\r
+ return _notmuch_messages_create (message->replies);\r
+ }\r
+ \r
+-static void\r
++void\r
+ _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)\r
+ {\r
+ Xapian::TermIterator i;\r
+@@ -1799,3 +1800,50 @@ _notmuch_message_database (notmuch_message_t *message)\r
+ {\r
+ return message->notmuch;\r
+ }\r
++\r
++void\r
++_notmuch_message_ensure_property_map (notmuch_message_t *message)\r
++{\r
++ notmuch_string_node_t *node;\r
++\r
++ if (message->property_map)\r
++ return;\r
++\r
++ if (!message->property_term_list)\r
++ _notmuch_message_ensure_metadata (message);\r
++\r
++ message->property_map = _notmuch_string_map_create (message);\r
++\r
++ for (node = message->property_term_list->head; node; node = node->next) {\r
++ const char *key;\r
++ char *value;\r
++\r
++ value = index(node->string, '=');\r
++ if (!value)\r
++ INTERNAL_ERROR ("malformed property term");\r
++\r
++ *value = '\0';\r
++ value++;\r
++ key = node->string;\r
++\r
++ _notmuch_string_map_append (message->property_map, key, value);\r
++\r
++ }\r
++\r
++ talloc_free (message->property_term_list);\r
++ message->property_term_list = NULL;\r
++}\r
++\r
++notmuch_string_map_t *\r
++_notmuch_message_property_map (notmuch_message_t *message)\r
++{\r
++ _notmuch_message_ensure_property_map (message);\r
++\r
++ return message->property_map;\r
++}\r
++\r
++notmuch_bool_t\r
++_notmuch_message_frozen (notmuch_message_t *message)\r
++{\r
++ return message->frozen;\r
++}\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index d4a97cb..afc48c6 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -180,6 +180,11 @@ typedef enum _notmuch_status {\r
+ */\r
+ NOTMUCH_STATUS_PATH_ERROR,\r
+ /**\r
++ * One of the arguments violates the preconditions for the\r
++ * function, in a way not covered by a more specific argument.\r
++ */\r
++ NOTMUCH_STATUS_ILLEGAL_ARGUMENT,\r
++ /**\r
+ * Not an actual status value. Just a way to find out how many\r
+ * valid status values there are.\r
+ */\r
+@@ -1652,6 +1657,70 @@ void\r
+ notmuch_message_destroy (notmuch_message_t *message);\r
+ \r
+ /**\r
++ * @name Message Properties\r
++ *\r
++ * This interface provides the ability to attach arbitrary (key,value)\r
++ * string pairs to a message, to remove such pairs, and to iterate\r
++ * over them. The caller should take some care as to what keys they\r
++ * add or delete values for, as other subsystems or extensions may\r
++ * depend on these properties.\r
++ *\r
++ */\r
++/**@{*/\r
++/**\r
++ * Retrieve the value for a single property key\r
++ *\r
++ * *value* is set to a string owned by the message or NULL if there is\r
++ * no such key. In the case of multiple values for the given key, the\r
++ * first one is retrieved.\r
++ *\r
++ * @returns\r
++ * - NOTMUCH_STATUS_NULL_POINTER: *value* may not be NULL.\r
++ * - NOTMUCH_STATUS_SUCCESS: No error occured.\r
++\r
++ */\r
++notmuch_status_t\r
++notmuch_message_get_property (notmuch_message_t *message, const char *key, const char **value);\r
++\r
++/**\r
++ * Add a (key,value) pair to a message\r
++ *\r
++ * @returns\r
++ * - NOTMUCH_STATUS_ILLEGAL_ARGUMENT: *key* may not contain an '=' character.\r
++ * - NOTMUCH_STATUS_NULL_POINTER: Neither *key* nor *value* may be NULL.\r
++ * - NOTMUCH_STATUS_SUCCESS: No error occured.\r
++ */\r
++notmuch_status_t\r
++notmuch_message_add_property (notmuch_message_t *message, const char *key, const char *value);\r
++\r
++/**\r
++ * Remove a (key,value) pair from a message.\r
++ *\r
++ * It is not an error to remove a non-existant (key,value) pair\r
++ *\r
++ * @returns\r
++ * - NOTMUCH_STATUS_ILLEGAL_ARGUMENT: *key* may not contain an '=' character.\r
++ * - NOTMUCH_STATUS_NULL_POINTER: Neither *key* nor *value* may be NULL.\r
++ * - NOTMUCH_STATUS_SUCCESS: No error occured.\r
++ */\r
++notmuch_status_t\r
++notmuch_message_remove_property (notmuch_message_t *message, const char *key, const char *value);\r
++\r
++/**\r
++ * Remove all (key,value) pairs from the given message.\r
++ *\r
++ * @returns\r
++ * - NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in\r
++ * read-only mode so message cannot be modified.\r
++ * - NOTMUCH_STATUS_SUCCESS: No error occured.\r
++ *\r
++ */\r
++notmuch_status_t\r
++notmuch_message_remove_all_properties (notmuch_message_t *message);\r
++\r
++/**@}*/\r
++\r
++/**\r
+ * Is the given 'tags' iterator pointing at a valid tag.\r
+ *\r
+ * When this function returns TRUE, notmuch_tags_get will return a\r
+diff --git a/test/T610-message-property.sh b/test/T610-message-property.sh\r
+new file mode 100755\r
+index 0000000..0217950\r
+--- /dev/null\r
++++ b/test/T610-message-property.sh\r
+@@ -0,0 +1,84 @@\r
++#!/usr/bin/env bash\r
++test_description="message property API"\r
++\r
++. ./test-lib.sh || exit 1\r
++\r
++add_email_corpus\r
++\r
++cat <<EOF > c_head\r
++#include <stdio.h>\r
++#include <string.h>\r
++#include <stdlib.h>\r
++#include <notmuch-test.h>\r
++\r
++int main (int argc, char** argv)\r
++{\r
++ notmuch_database_t *db;\r
++ notmuch_message_t *message = NULL;\r
++ const char *val;\r
++ notmuch_status_t stat;\r
++\r
++ EXPECT0(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));\r
++ EXPECT0(notmuch_database_find_message(db, "4EFC743A.3060609@april.org", &message));\r
++ if (message == NULL) {\r
++ fprintf (stderr, "unable to find message");\r
++ exit (1);\r
++ }\r
++EOF\r
++\r
++cat <<EOF > c_tail\r
++ EXPECT0(notmuch_database_destroy(db));\r
++}\r
++EOF\r
++\r
++test_begin_subtest "notmuch_message_{add,get,remove}_property"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue1"));\r
++ EXPECT0(notmuch_message_get_property (message, "testkey1", &val));\r
++ printf("testkey1[1] = %s\n", val);\r
++ EXPECT0(notmuch_message_add_property (message, "testkey2", "this value has spaces and = sign"));\r
++ EXPECT0(notmuch_message_get_property (message, "testkey1", &val));\r
++ printf("testkey1[2] = %s\n", val);\r
++ EXPECT0(notmuch_message_get_property (message, "testkey1", &val));\r
++\r
++ EXPECT0(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* Add second value for key */\r
++ EXPECT0(notmuch_message_add_property (message, "testkey2", "zztestvalue3"));\r
++ EXPECT0(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* remove first value for key */\r
++ EXPECT0(notmuch_message_remove_property (message, "testkey2", "this value has spaces and = sign"));\r
++ EXPECT0(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* remove non-existant value for key */\r
++ EXPECT0(notmuch_message_remove_property (message, "testkey2", "this value has spaces and = sign"));\r
++ EXPECT0(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* remove only value for key */\r
++ EXPECT0(notmuch_message_remove_property (message, "testkey2", "zztestvalue3"));\r
++ EXPECT0(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val == NULL ? "NULL" : val);\r
++}\r
++EOF\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++testkey1[1] = testvalue1\r
++testkey1[2] = testvalue1\r
++testkey2 = this value has spaces and = sign\r
++testkey2 = this value has spaces and = sign\r
++testkey2 = zztestvalue3\r
++testkey2 = zztestvalue3\r
++testkey2 = NULL\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
++\r
++\r
++test_done\r
+-- \r
+2.8.1\r
+\r