]> git.notmuchmail.org Git - notmuch/blobdiff - lib/message.cc
maildir_flags_to_tags: Avoid interpreting "no info" as "no flags set".
[notmuch] / lib / message.cc
index 83e05191b2b2ad290c8f9bc4800785ab694644f5..b8128505bfb7c63176728bbe145b87113dd4c5a7 100644 (file)
@@ -859,53 +859,48 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
     return NOTMUCH_STATUS_SUCCESS;
 }
 
-/* XXX: Needs to iterate over all message filenames. */
 notmuch_status_t
 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
 {
-    const char *flags, *p;
-    char f;
-    bool valid, unread;
-    unsigned i;
+    const char *flags;
     notmuch_status_t status;
+    notmuch_filenames_t *filenames;
     const char *filename;
+    char *combined_flags = talloc_strdup (message, "");
+    unsigned i;
+    int seen_maildir_info = 0;
 
-    filename = notmuch_message_get_filename (message);
+    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)
-       return NOTMUCH_STATUS_FILE_NOT_EMAIL;
-    flags += 3;
-
-    /*  Check that the letters are valid Maildir flags */
-    f = 0;
-    valid = true;
-    for (p=flags; valid && *p; p++) {
-       switch (*p) {
-       case 'P':
-       case 'R':
-       case 'S':
-       case 'T':
-       case 'D':
-       case 'F':
-           if (*p > f) f=*p;
-           else valid = false;
-       break;
-       default:
-           valid = false;
-       }
-    }
-    if (!valid) {
-       fprintf (stderr, "Warning: Invalid maildir flags in filename %s\n", filename);
-       return NOTMUCH_STATUS_FILE_NOT_EMAIL;
+       flags = strstr (filename, ":2,");
+       if (! flags)
+           continue;
+
+       seen_maildir_info = 1;
+       flags += 3;
+
+       combined_flags = talloc_strdup_append (combined_flags, flags);
     }
 
+    /* If none of the filenames have any maildir info field (not even
+     * an empty info with no flags set) then there's no information to
+     * go on, so do nothing. */
+    if (! seen_maildir_info)
+       return NOTMUCH_STATUS_SUCCESS;
+
     status = notmuch_message_freeze (message);
     if (status)
        return status;
-    unread = true;
+
     for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
-       if ((strchr (flags, flag2tag[i].flag) != NULL) ^ flag2tag[i].inverse) {
+       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);
@@ -915,6 +910,8 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
     }
     status = notmuch_message_thaw (message);
 
+    talloc_free (combined_flags);
+
     return status;
 }
 
@@ -1009,14 +1006,19 @@ notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
     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);
        }
-       return _notmuch_message_rename (message, filename_new);
-       /* _notmuch_message_sync is our caller. Do not call it here. */
+       status = _notmuch_message_rename (message, filename_new);
+
+       _notmuch_message_sync (message);
+
+       return status;
     }
     return NOTMUCH_STATUS_SUCCESS;
 }