+notmuch_status_t
+notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
+{
+ const char *flags;
+ notmuch_status_t status;
+ notmuch_filenames_t *filenames;
+ const char *filename;
+ char *combined_flags = talloc_strdup (message, "");
+ unsigned i;
+
+ for (filenames = notmuch_message_get_filenames (message);
+ notmuch_filenames_valid (filenames);
+ notmuch_filenames_move_to_next (filenames))
+ {
+ filename = notmuch_filenames_get (filenames);
+
+ flags = strstr (filename, ":2,");
+ if (! flags)
+ continue;
+
+ flags += 3;
+
+ combined_flags = talloc_strdup_append (combined_flags, flags);
+ }
+
+ status = notmuch_message_freeze (message);
+ if (status)
+ return status;
+
+ for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
+ if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
+ ^
+ flag2tag[i].inverse)
+ {
+ status = notmuch_message_add_tag (message, flag2tag[i].tag);
+ } else {
+ status = notmuch_message_remove_tag (message, flag2tag[i].tag);
+ }
+ if (status)
+ return status;
+ }
+ status = notmuch_message_thaw (message);
+
+ talloc_free (combined_flags);
+
+ return status;
+}
+
+static void
+maildir_get_new_flags(notmuch_message_t *message, char *flags)
+{
+ notmuch_tags_t *tags;
+ const char *tag;
+ unsigned i;
+ char *p;
+
+ for (i = 0; i < ARRAY_SIZE(flag2tag); i++)
+ flags[i] = flag2tag[i].inverse ? flag2tag[i].flag : '\0';
+
+ for (tags = notmuch_message_get_tags (message);
+ notmuch_tags_valid (tags);
+ notmuch_tags_move_to_next (tags))
+ {
+ tag = notmuch_tags_get (tags);
+ for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
+ if (strcmp(tag, flag2tag[i].tag) == 0)
+ flags[i] = flag2tag[i].inverse ? '\0' : flag2tag[i].flag;
+ }
+ }
+
+ p = flags;
+ for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
+ if (flags[i])
+ *p++ = flags[i];
+ }
+ *p = '\0';
+}
+
+static char *
+maildir_get_subdir (char *filename)
+{
+ char *p, *subdir = NULL;
+
+ p = filename + strlen (filename) - 1;
+ while (p > filename + 3 && *p != '/')
+ p--;
+ if (*p == '/') {
+ subdir = p - 3;
+ if (subdir > filename && *(subdir - 1) != '/')
+ subdir = NULL;
+ }
+ return subdir;
+}
+
+/* XXX: Needs to iterate over all filenames in the message
+ *
+ * XXX: Needs to ensure that existing, unsupported flags in the
+ * filename are left unchanged (which also needs a test in the
+ * test suite).
+ */
+notmuch_status_t
+notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
+{
+ char flags[ARRAY_SIZE(flag2tag)+1];
+ const char *filename, *p;
+ char *filename_new, *subdir = NULL;
+ int ret;
+
+ maildir_get_new_flags (message, flags);
+
+ filename = notmuch_message_get_filename (message);
+ /* TODO: Iterate over all file names. */
+ p = strstr(filename, ":2,");
+ if ((p && strcmp (p+3, flags) == 0) ||
+ (!p && flags[0] == '\0')) {
+ // Return if flags are not to be changed - this suppresses
+ // moving the message from new/ to cur/ during initial
+ // tagging.
+ return NOTMUCH_STATUS_SUCCESS;
+ }
+ if (!p)
+ p = filename + strlen(filename);
+
+ filename_new = (char*)talloc_size(message, (p-filename) + 3 + sizeof(flags));
+ if (unlikely (filename_new == NULL))
+ return NOTMUCH_STATUS_OUT_OF_MEMORY;
+
+ memcpy(filename_new, filename, p-filename);
+ filename_new[p-filename] = '\0';
+
+ /* If message is in new/ move it under cur/. */
+ subdir = maildir_get_subdir (filename_new);
+ if (subdir && memcmp (subdir, "new/", 4) == 0)
+ memcpy (subdir, "cur/", 4);
+
+ strcpy (filename_new+(p-filename), ":2,");
+ strcpy (filename_new+(p-filename)+3, flags);
+
+ if (strcmp (filename, filename_new) != 0) {
+ notmuch_status_t status;
+
+ ret = rename (filename, filename_new);
+ if (ret == -1) {
+ perror (talloc_asprintf (message, "rename of %s to %s failed",
+ filename, filename_new));
+ exit (1);
+ }
+ status = _notmuch_message_rename (message, filename_new);
+
+ _notmuch_message_sync (message);
+
+ return status;
+ }
+ return NOTMUCH_STATUS_SUCCESS;
+}
+
+notmuch_status_t