[RFC] database: get and set mapping of dovecot compatible maildir keywords

David Bremner david at tethera.net
Sat Sep 27 01:17:15 PDT 2014


A future modification of notmuch_message_tags_to_maildir_flags and
notmuch_message_maildir_flags_to_tags could allow 26 more tags to be
synced via maildir.
---

I'm not sure if this is worth pursuing or not, but I thought I'd toss
it out there.  On IRC the other day the topic of syncing IMAP keywords
to notmuch tags came up again, in particular the dovecot variant that
maps 26 user defined keywords to characters a-z on the end of the
maildir info.  One roadblock I saw at the time was the need for
configuration of mapping of letters to tags.  This patch is the result
of my realizing that at least that part is not hard (unlike the rabbit
hole we seemed to get into for e.g. log configuration).

Some downsides of this approach are fairly obvious

    - only 26 tags. It turns out I don't have that many non-nmbug tags
      that I really care about. YMMV, of course.

    - nonstandard. this won't roundtrip via offlineimap (unless
      offlineimap is modified).  Directly syncing the maildir or using
      some dovecot specific syncer would work.

Some upsides are:

    - Provides an IMAP bridge solution; compatible with dovecot; my
      impression is this is the most common imap solution among
      notmuch users.  
    - relatively simple implementation, just need to
      update the maildir name synching routines. Of course no
      implementation is as simple as possible

 lib/database.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/notmuch.h   | 19 ++++++++++++++++++
 2 files changed, 80 insertions(+)

diff --git a/lib/database.cc b/lib/database.cc
index a3a7cd3..5427f7c 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -1169,6 +1169,67 @@ notmuch_database_get_version (notmuch_database_t *notmuch)
     return version;
 }
 
+notmuch_status_t
+notmuch_database_get_maildir_keyword (notmuch_database_t *notmuch,
+				      int index, const char **tag)
+{
+    string tag_string;
+    const char *key;
+    const char *str;
+
+    if (!notmuch || !tag)
+	return NOTMUCH_STATUS_NULL_POINTER;
+
+    if (index < 0 || index > ('z' - 'a'))
+	return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;
+
+    key = talloc_asprintf(notmuch, "maildir_keyword_%c", 'a' + index);
+    if (!key)
+	return NOTMUCH_STATUS_OUT_OF_MEMORY;
+
+    *tag = NULL;
+    tag_string = notmuch->xapian_db->get_metadata (key);
+    if (tag_string.empty ())
+	return NOTMUCH_STATUS_SUCCESS;
+
+    str = tag_string.c_str ();
+    if (str == NULL || *str == '\0')
+	return NOTMUCH_STATUS_SUCCESS;
+
+    *tag = str;
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
+notmuch_status_t
+notmuch_database_set_maildir_keyword (notmuch_database_t *notmuch,
+				      int index, const char *tag)
+{
+    string tag_string;
+    const char *key;
+    notmuch_status_t ret;
+    Xapian::WritableDatabase *db;
+
+    if (!notmuch || !tag)
+	return NOTMUCH_STATUS_NULL_POINTER;
+
+    ret = _notmuch_database_ensure_writable (notmuch);
+    if (ret)
+	return ret;
+
+    if (index < 0 || index > ('z' - 'a'))
+	return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;
+
+    key = talloc_asprintf(notmuch, "maildir_keyword_%c", 'a' + index);
+    if (!key)
+	return NOTMUCH_STATUS_OUT_OF_MEMORY;
+
+    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+    db->set_metadata (key, tag);
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 notmuch_bool_t
 notmuch_database_needs_upgrade (notmuch_database_t *notmuch)
 {
diff --git a/lib/notmuch.h b/lib/notmuch.h
index fe2340b..5fec8b3 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -614,6 +614,25 @@ notmuch_tags_t *
 notmuch_database_get_all_tags (notmuch_database_t *db);
 
 /**
+ * Return the tag corresponding to a maildir keyword.
+ *
+ */
+notmuch_status_t
+notmuch_database_get_maildir_keyword(notmuch_database_t *db,
+				     int index, char **tag);
+
+/**
+ * Set the tag corresponding to a maildir keyword.
+ *
+ * Note that no messages have their tags modified by this call.
+ */
+
+notmuch_status_t
+notmuch_database_set_maildir_keyword(notmuch_database_t *db,
+				     int index, const char *tag);
+
+
+/**
  * Create a new query for 'database'.
  *
  * Here, 'database' should be an open database, (see
-- 
2.1.0



More information about the notmuch mailing list