--- /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 394A06DE0926\r
+ for <notmuch@notmuchmail.org>; Mon, 30 May 2016 04:50:36 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.012\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.012 tagged_above=-999 required=5\r
+ tests=[AWL=-0.001, 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 mMopsA6eUKDo for <notmuch@notmuchmail.org>;\r
+ Mon, 30 May 2016 04:50:27 -0700 (PDT)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id E60AE6DE02A6\r
+ for <notmuch@notmuchmail.org>; Mon, 30 May 2016 04:50:12 -0700 (PDT)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1b7Lhq-0000Q4-20; Mon, 30 May 2016 07:50:02 -0400\r
+Received: (nullmailer pid 14852 invoked by uid 1000);\r
+ Mon, 30 May 2016 11:50:06 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC2 Patch 3/5] lib: basic message-property API\r
+Date: Mon, 30 May 2016 08:49:57 -0300\r
+Message-Id: <1464608999-14774-4-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.8.1\r
+In-Reply-To: <1464608999-14774-1-git-send-email-david@tethera.net>\r
+References: <1463927339-5441-1-git-send-email-david@tethera.net>\r
+ <1464608999-14774-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, 30 May 2016 11:50:36 -0000\r
+\r
+Initially, support get, set and remove of single key/value pair\r
+---\r
+ lib/Makefile.local | 1 +\r
+ lib/message-private.h | 10 +++++\r
+ lib/message-property.cc | 86 +++++++++++++++++++++++++++++++++++++++\r
+ lib/message.cc | 48 ++++++++++++++++++++++\r
+ lib/notmuch.h | 14 +++++++\r
+ test/T610-message-property.sh | 94 +++++++++++++++++++++++++++++++++++++++++++\r
+ 6 files changed, 253 insertions(+)\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..61e5bac\r
+--- /dev/null\r
++++ b/lib/message-private.h\r
+@@ -0,0 +1,10 @@\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
++#endif\r
+diff --git a/lib/message-property.cc b/lib/message-property.cc\r
+new file mode 100644\r
+index 0000000..21348a3\r
+--- /dev/null\r
++++ b/lib/message-property.cc\r
+@@ -0,0 +1,86 @@\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, '=') || index (value, '='))\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
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 47946a3..0eb7e2c 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
+@@ -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 bd977c3..c9e654e 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
+@@ -1651,6 +1656,15 @@ notmuch_message_thaw (notmuch_message_t *message);\r
+ void\r
+ notmuch_message_destroy (notmuch_message_t *message);\r
+ \r
++notmuch_status_t\r
++notmuch_message_get_property (notmuch_message_t *message, const char *key, const char **value);\r
++\r
++notmuch_status_t\r
++notmuch_message_add_property (notmuch_message_t *message, const char *key, const char *value);\r
++\r
++notmuch_status_t\r
++notmuch_message_remove_property (notmuch_message_t *message, const char *key, const char *value);\r
++\r
+ /**\r
+ * Is the given 'tags' iterator pointing at a valid tag.\r
+ *\r
+diff --git a/test/T610-message-property.sh b/test/T610-message-property.sh\r
+new file mode 100755\r
+index 0000000..45ed66b\r
+--- /dev/null\r
++++ b/test/T610-message-property.sh\r
+@@ -0,0 +1,94 @@\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.h>\r
++\r
++void run(int line, notmuch_status_t ret)\r
++{\r
++ if (ret) {\r
++ fprintf (stderr, "line %d: %s\n", line, ret);\r
++ exit (1);\r
++ }\r
++}\r
++\r
++#define RUN(v) run(__LINE__, v);\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
++ RUN(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));\r
++ RUN(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
++ RUN(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
++ RUN(notmuch_message_add_property (message, "testkey1", "testvalue1"));\r
++ RUN(notmuch_message_get_property (message, "testkey1", &val));\r
++ printf("testkey1[1] = %s\n", val);\r
++ RUN(notmuch_message_add_property (message, "testkey2", "testvalue2"));\r
++ RUN(notmuch_message_get_property (message, "testkey1", &val));\r
++ printf("testkey1[2] = %s\n", val);\r
++ RUN(notmuch_message_get_property (message, "testkey1", &val));\r
++\r
++ RUN(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* Add second value for key */\r
++ RUN(notmuch_message_add_property (message, "testkey2", "testvalue3"));\r
++ RUN(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* remove first value for key */\r
++ RUN(notmuch_message_remove_property (message, "testkey2", "testvalue2"));\r
++ RUN(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* remove non-existant value for key */\r
++ RUN(notmuch_message_remove_property (message, "testkey2", "testvalue2"));\r
++ RUN(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* remove only value for key */\r
++ RUN(notmuch_message_remove_property (message, "testkey2", "testvalue3"));\r
++ RUN(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 = testvalue2\r
++testkey2 = testvalue2\r
++testkey2 = testvalue3\r
++testkey2 = testvalue3\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