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