--- /dev/null
+Return-Path: <igor.contato@gmail.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 arlo.cworth.org (Postfix) with ESMTP id 085AA6DE19A0\r
+ for <notmuch@notmuchmail.org>; Wed, 25 Nov 2015 18:16:56 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.801\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.801 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001,\r
+ SPF_PASS=-0.001] 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 4LZwQOqZ5con for <notmuch@notmuchmail.org>;\r
+ Wed, 25 Nov 2015 18:16:53 -0800 (PST)\r
+Received: from mail-qg0-f44.google.com (mail-qg0-f44.google.com\r
+ [209.85.192.44])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 5970C6DE1921\r
+ for <notmuch@notmuchmail.org>; Wed, 25 Nov 2015 18:16:53 -0800 (PST)\r
+Received: by qgec40 with SMTP id c40so45721171qge.2\r
+ for <notmuch@notmuchmail.org>; Wed, 25 Nov 2015 18:16:52 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references;\r
+ bh=KeAuTMX894k/I75lckoCwhjTUG7hX2vJcAcb88W3kdM=;\r
+ b=pu6DMvMSE67CKPO2oWj4E7VpH+6vAjVfqPO95sszrPKgz6qhmkgmSghCjnyYCyzAcO\r
+ hIBRqjA3MARaLi6TD5wUp0Hcf6wCGE8FljvTd4TZoVRqmMIrhMaYIPE3RNuv/fgxiS1y\r
+ aPjwCmrqJ1ewhS0W0toYsYG8XidUE7ys1D012WQLeQoWqhQPRVeJKVRhPlcUHizcPlCM\r
+ hoBmh93asiqUOv+yDJzgX+/PilFxADBHK3TyO6dCaIVDIn2SH1mZU1674lD8K/evo0Nt\r
+ DjhOEBp7TpgemV4V9iAPmd+qIw6lJrZBdNBFMOaHhgqcRs5gwfn722zPegS+M8djqKLk\r
+ Gd4A==\r
+X-Received: by 10.140.94.76 with SMTP id f70mr42901122qge.3.1448504212140;\r
+ Wed, 25 Nov 2015 18:16:52 -0800 (PST)\r
+Received: from arch-laptop.localdomain ([200.195.16.216])\r
+ by smtp.gmail.com with ESMTPSA id h206sm1566011qhc.43.2015.11.25.18.16.49\r
+ (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128);\r
+ Wed, 25 Nov 2015 18:16:50 -0800 (PST)\r
+From: Igor Almeida <igor.contato@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH/RFC 2/3] notmuch new: tag messages based on maildir custom\r
+ flags\r
+Date: Wed, 25 Nov 2015 23:16:30 -0300\r
+Message-Id: <1448504191-30974-3-git-send-email-igor.contato@gmail.com>\r
+X-Mailer: git-send-email 2.5.3\r
+In-Reply-To: <1448504191-30974-1-git-send-email-igor.contato@gmail.com>\r
+References: <1448504191-30974-1-git-send-email-igor.contato@gmail.com>\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: Thu, 26 Nov 2015 02:16:56 -0000\r
+\r
+Iterate through the flags in the message's filename, creating tags and\r
+tagging the message as they appear.\r
+This uses Bremner/Dovecot's convention of maildir_keyword_xxx, where xxx a char\r
+between 'a' and 'z'.\r
+\r
+Since more than one file may point to the same message in the database\r
+(think renames), the tags related to maildir custom flags are always\r
+removed and later re-added to maintain consistency.\r
+\r
+Signed-off-by: Igor Almeida <igor.contato@gmail.com>\r
+---\r
+ lib/message.cc | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 1 file changed, 88 insertions(+)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 26b5e76..8a89dee 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -1301,6 +1301,13 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)\r
+ unsigned i;\r
+ int seen_maildir_info = 0;\r
+ \r
++ /* For custom maildir flags */\r
++ char *c; /* to iterate over combined_flags */\r
++ const char *custom_flag_dbase_name;\r
++ int index;\r
++ notmuch_bool_t cancel_custom_flags = FALSE;\r
++ notmuch_bool_t must_free = FALSE;\r
++\r
+ for (filenames = notmuch_message_get_filenames (message);\r
+ notmuch_filenames_valid (filenames);\r
+ notmuch_filenames_move_to_next (filenames))\r
+@@ -1349,6 +1356,87 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)\r
+ if (status)\r
+ return status;\r
+ }\r
++\r
++ /* Now let's find custom maildir flags */\r
++\r
++ status = notmuch_message_freeze (message);\r
++ if (status)\r
++ goto finish;\r
++\r
++ cancel_custom_flags = FALSE;\r
++\r
++ /* First we remove the tags for custom maildir flags, then\r
++ * we add only the ones in combined_flags\r
++ */\r
++ char letter;\r
++ for (letter = 'a'; letter <= 'z'; letter++) {\r
++ index = letter - 'a';\r
++ status = notmuch_database_get_maildir_keyword (\r
++ message->notmuch, index, &custom_flag_dbase_name);\r
++ if (status) {\r
++ /* TODO probably OOM, what now? */\r
++ }\r
++\r
++ if (custom_flag_dbase_name == NULL) {\r
++ /* We don't have a custom flag for this letter, try the next\r
++ * one now\r
++ */\r
++ continue;\r
++ }\r
++\r
++ status = notmuch_message_remove_tag (message, custom_flag_dbase_name);\r
++ if (status) {\r
++ /* TODO tag too long? */\r
++ }\r
++ }\r
++\r
++ /* Go through combined_flags and tag the message accordingly */\r
++\r
++ for (c = combined_flags; *c; c++) {\r
++ if (*c >= 'a' && *c <= 'z') {\r
++ index = *c - 'a';\r
++\r
++ status = notmuch_database_get_maildir_keyword(\r
++ message->notmuch, index, &custom_flag_dbase_name);\r
++ if (status) {\r
++ cancel_custom_flags = TRUE;\r
++ break;\r
++ } else {\r
++ if (custom_flag_dbase_name == NULL) {\r
++ /* Custom flag does not yet exist */\r
++ custom_flag_dbase_name = talloc_asprintf (message,\r
++ "maildir_keyword_%c", 'a' + index);\r
++ /* Add to the database */\r
++ notmuch_database_set_maildir_keyword (\r
++ message->notmuch, index, custom_flag_dbase_name);\r
++\r
++ must_free = TRUE;\r
++ }\r
++\r
++ /* Tag the message */\r
++ status = notmuch_message_add_tag (message,\r
++ custom_flag_dbase_name);\r
++\r
++ if (must_free) {\r
++ talloc_free((void*)custom_flag_dbase_name);\r
++ must_free = FALSE;\r
++ }\r
++\r
++ if (status) {\r
++ cancel_custom_flags = TRUE;\r
++ break;\r
++ }\r
++ }\r
++ }\r
++ }\r
++\r
++ if (cancel_custom_flags) {\r
++ /* TODO rollback the add_tag's */\r
++ }\r
++\r
++ status = notmuch_message_thaw (message);\r
++\r
++finish:\r
+ status = notmuch_message_thaw (message);\r
+ \r
+ talloc_free (combined_flags);\r
+-- \r
+2.5.3\r
+\r