--- /dev/null
+Return-Path: <stewart@flamingspork.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 6E2D5431FBC\r
+ for <notmuch@notmuchmail.org>; Mon, 15 Feb 2010 17:59:02 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.778\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.778 tagged_above=-999 required=5\r
+ tests=[AWL=-0.593, BAYES_40=-0.185] autolearn=ham\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 6uRP7vDbRfwh for <notmuch@notmuchmail.org>;\r
+ Mon, 15 Feb 2010 17:59:01 -0800 (PST)\r
+Received: from kaylee.flamingspork.com (kaylee.flamingspork.com\r
+ [74.207.245.61])\r
+ by olra.theworths.org (Postfix) with ESMTP id 8F4D4431FAE\r
+ for <notmuch@notmuchmail.org>; Mon, 15 Feb 2010 17:59:01 -0800 (PST)\r
+Received: from willster (localhost [127.0.0.1])\r
+ by kaylee.flamingspork.com (Postfix) with ESMTPS id 0244C6393;\r
+ Tue, 16 Feb 2010 01:56:00 +0000 (UTC)\r
+Received: from flamingspork.com (localhost.localdomain [127.0.0.1])\r
+ by willster (Postfix) with ESMTPS id A039B10260A3;\r
+ Tue, 16 Feb 2010 12:58:58 +1100 (EST)\r
+Date: Tue, 16 Feb 2010 12:58:56 +1100\r
+From: Stewart Smith <stewart@flamingspork.com>\r
+To: Tim Stoakes <notmuch@stoakes.net>\r
+Message-ID: <20100216015856.GG22402@flamingspork.com>\r
+References: <20091121221207.GB17268@jukie.net>\r
+ <9cce5525b093b87fe74d427954ffad89@localhost>\r
+ <87d43b2oif.fsf@yoom.home.cworth.org>\r
+ <9bfdedddeab9c58cd45d8d448323d0fc@localhost>\r
+ <87skc23327.fsf@yoom.home.cworth.org>\r
+ <4b0eef22.JwxdgTGElffx149F%michiel@michielbuddingh.net>\r
+ <87ws1bjpmm.fsf@yoom.home.cworth.org> <878wdfkhcl.fsf@aegir.org.uk>\r
+ <20100210031339.GH16686@mail.rocksoft.com>\r
+ <20100215081331.GD22402@flamingspork.com>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <20100215081331.GD22402@flamingspork.com>\r
+User-Agent: Mutt/1.5.20 (2009-06-14)\r
+Cc: notmuch@notmuchmail.org\r
+Subject: [notmuch] [PATCH] notmuch: Respect maildir message flags\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://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: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 16 Feb 2010 01:59:02 -0000\r
+\r
+New patch that does it. Pretty much same as the old one, just with\r
+that one bug I mentioned fixed. This is what I've currently used to\r
+import my Maildir. I'm now happy :)\r
+\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index f25c71f..43371a3 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -39,6 +39,7 @@ typedef struct {\r
+ int total_files;\r
+ int processed_files;\r
+ int added_messages;\r
++ int tag_maildir;\r
+ struct timeval tv_start;\r
+ \r
+ _filename_list_t *removed_files;\r
+@@ -169,6 +170,60 @@ _entries_resemble_maildir (struct dirent **entries, int count)\r
+ return 0;\r
+ }\r
+ \r
++/* Tag new mail according to its Maildir attribute flags.\r
++ *\r
++ * Test if the mail file's filename contains any of the\r
++ * standard Maildir attributes, and translate these to\r
++ * the corresponding standard notmuch tags.\r
++ *\r
++ * If the message is not marked as 'seen', or if no\r
++ * flags are present, tag as 'inbox, unread'.\r
++ */\r
++static void\r
++derive_tags_from_maildir_flags (notmuch_message_t *message,\r
++ const char * path)\r
++{\r
++ int seen = FALSE;\r
++ int end_of_flags = FALSE;\r
++ size_t l = strlen(path);\r
++\r
++ /* Non-experimental message flags start with this */\r
++ char * i = strstr(path, ":2,");\r
++ i = (i) ? i : strstr(path, "!2,"); /* This format is used on VFAT */\r
++ if (i != NULL) {\r
++ i += 3;\r
++ for (; i < (path + l) && !end_of_flags; i++) {\r
++ switch (*i) {\r
++ case 'F' :\r
++ notmuch_message_add_tag (message, "flagged");\r
++ break;\r
++ case 'R': /* replied */\r
++ notmuch_message_add_tag (message, "answered");\r
++ break;\r
++ case 'D':\r
++ notmuch_message_add_tag (message, "draft");\r
++ break;\r
++ case 'S': /* seen */\r
++ seen = TRUE;\r
++ break;\r
++ case 'T': /* trashed */\r
++ notmuch_message_add_tag (message, "deleted");\r
++ break;\r
++ case 'P': /* passed */\r
++ notmuch_message_add_tag (message, "forwarded");\r
++ break;\r
++ default:\r
++ end_of_flags = TRUE;\r
++ break;\r
++ }\r
++ }\r
++ }\r
++\r
++ if (i == NULL || !seen) {\r
++ tag_inbox_and_unread (message);\r
++ }\r
++}\r
++\r
+ /* Examine 'path' recursively as follows:\r
+ *\r
+ * o Ask the filesystem for the mtime of 'path' (fs_mtime)\r
+@@ -222,6 +277,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ notmuch_filenames_t *db_subdirs = NULL;\r
+ struct stat st;\r
+ notmuch_bool_t is_maildir, new_directory;\r
++ int maildir_detected = -1;\r
+ \r
+ if (stat (path, &st)) {\r
+ fprintf (stderr, "Error reading directory %s: %s\n",\r
+@@ -301,6 +357,28 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ continue;\r
+ }\r
+ \r
++ /* If this directory is a Maildir folder, we need to\r
++ * ignore any subdirectories marked tmp/, and scan for\r
++ * Maildir attributes on messages contained in the sub-\r
++ * directories 'new' and 'cur'. */\r
++ if (maildir_detected != 0 &&\r
++ (entry->d_type == DT_DIR || entry->d_type == DT_UNKNOWN) &&\r
++ ((strcmp (entry->d_name, "tmp") == 0) ||\r
++ (strcmp (entry->d_name, "new") == 0) ||\r
++ (strcmp (entry->d_name, "cur") == 0))) {\r
++\r
++ if (maildir_detected == -1) {\r
++ maildir_detected = _entries_resemble_maildir(fs_entries, num_fs_entries);\r
++ }\r
++ if (maildir_detected == 1) {\r
++ if (strcmp (entry->d_name, "tmp") == 0) {\r
++ continue;\r
++ } else {\r
++ state->tag_maildir = TRUE;\r
++ }\r
++ }\r
++ }\r
++\r
+ next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
+ status = add_files_recursive (notmuch, next, state);\r
+ if (status && ret == NOTMUCH_STATUS_SUCCESS)\r
+@@ -412,7 +490,12 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ /* success */\r
+ case NOTMUCH_STATUS_SUCCESS:\r
+ state->added_messages++;\r
+- tag_inbox_and_unread (message);\r
++ if (state->tag_maildir) {\r
++ derive_tags_from_maildir_flags (message,\r
++ entry->d_name);\r
++ } else {\r
++ tag_inbox_and_unread (message);\r
++ }\r
+ break;\r
+ /* Non-fatal issues (go on to next file) */\r
+ case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
+\r
+\r
+-- \r
+Stewart Smith\r