[PATCH 2/2] lib: add 'safe' setting for flags
authorAntoine Beaupré <anarcat@koumbit.org>
Sun, 17 Jul 2011 03:56:13 +0000 (23:56 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:39:09 +0000 (09:39 -0800)
70/f2d199474b4e46bbdce1d262759c9be3e52e13 [new file with mode: 0644]

diff --git a/70/f2d199474b4e46bbdce1d262759c9be3e52e13 b/70/f2d199474b4e46bbdce1d262759c9be3e52e13
new file mode 100644 (file)
index 0000000..b569f21
--- /dev/null
@@ -0,0 +1,255 @@
+Return-Path: <anarcat@angela.anarcat.ath.cx>\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 82CD2429E2A\r
+       for <notmuch@notmuchmail.org>; Sat, 16 Jul 2011 20:56:27 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+       autolearn=disabled\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 3tMfZ-CsfKnU for <notmuch@notmuchmail.org>;\r
+       Sat, 16 Jul 2011 20:56:26 -0700 (PDT)\r
+Received: from bureau.koumbit.net (homere.koumbit.net [209.44.112.81])\r
+       (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 60CC9431FB6\r
+       for <notmuch@notmuchmail.org>; Sat, 16 Jul 2011 20:56:26 -0700 (PDT)\r
+Received: from localhost (H144.C72.B0.tor.eicat.ca [72.0.72.144])\r
+       (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by bureau.koumbit.net (Postfix) with ESMTPSA id 6614CE80EF7;\r
+       Sat, 16 Jul 2011 23:56:25 -0400 (EDT)\r
+Received: by localhost (Postfix, from userid 1000)\r
+       id AC3F36305; Sat, 16 Jul 2011 23:56:27 -0400 (EDT)\r
+From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= <anarcat@koumbit.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 2/2] lib: add 'safe' setting for flags\r
+Date: Sat, 16 Jul 2011 23:56:13 -0400\r
+Message-Id: <1310874973-28437-2-git-send-email-anarcat@koumbit.org>\r
+X-Mailer: git-send-email 1.7.2.5\r
+In-Reply-To: <1310874973-28437-1-git-send-email-anarcat@koumbit.org>\r
+References: <1310874973-28437-1-git-send-email-anarcat@koumbit.org>\r
+Cc: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= <anarcat@koumbit.org>\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: Sun, 17 Jul 2011 03:56:28 -0000\r
+\r
+the 'safe' setting needs to be 'true' for flags to be manipulated by\r
+notmuch new/tag/restore.\r
+\r
+for now, only the (T)rash tag is configurable and set to false (by\r
+default) but this could be extended to allow the user to configure which\r
+flags are allowed to be synchronized.\r
+\r
+the reason why only T is configurable is because (a) it's the the only\r
+one that is actually dangerous and (b) I couldn't figure out how to\r
+properly configure multiple settings like this.\r
+---\r
+ lib/message.cc    |   40 +++++++++++++++++++++++++---------------\r
+ lib/notmuch.h     |   20 ++++++++++++++++----\r
+ notmuch-new.c     |    1 +\r
+ notmuch-restore.c |    1 +\r
+ notmuch-tag.c     |    1 +\r
+ 5 files changed, 44 insertions(+), 19 deletions(-)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index f633887..f812648 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -50,16 +50,17 @@ struct maildir_flag_tag {\r
+     char flag;\r
+     const char *tag;\r
+     bool inverse;\r
++    bool safe;\r
+ };\r
\r
+ /* ASCII ordered table of Maildir flags and associated tags */\r
+ static struct maildir_flag_tag flag2tag[] = {\r
+-    { 'D', "draft",   false},\r
+-    { 'F', "flagged", false},\r
+-    { 'P', "passed",  false},\r
+-    { 'R', "replied", false},\r
+-    { 'S', "unread",  true },\r
+-    { 'T', "deleted", false},\r
++    { 'D', "draft",   false, true},\r
++    { 'F', "flagged", false, true},\r
++    { 'P', "passed",  false, true},\r
++    { 'R', "replied", false, true},\r
++    { 'S', "unread",  true , true},\r
++    { 'T', "deleted", false, false},\r
+ };\r
\r
+ /* We end up having to call the destructor explicitly because we had\r
+@@ -994,7 +995,6 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)\r
+     char *combined_flags = talloc_strdup (message, "");\r
+     unsigned i;\r
+     int seen_maildir_info = 0;\r
+-    notmuch_bool_t reckless_trash;\r
\r
+     for (filenames = notmuch_message_get_filenames (message);\r
+        notmuch_filenames_valid (filenames);\r
+@@ -1022,15 +1022,8 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)\r
+     if (status)\r
+       return status;\r
\r
+-    // TODO: this should probably be moved up in the stack to avoid\r
+-    // opening the config file on every message (!)\r
+-    config = notmuch_config_open (ctx, NULL, NULL);\r
+-    if (config == NULL)\r
+-      return 1;\r
+-    reckless_trash = notmuch_config_get_maildir_reckless_trash (config);\r
+-\r
+     for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {\r
+-      if (flag2tag[i].flag == 'T' && !reckless_trash) {\r
++      if (!flag2tag[i].safe) {\r
+           continue;\r
+       }\r
+       if ((strchr (combined_flags, flag2tag[i].flag) != NULL)\r
+@@ -1119,6 +1112,9 @@ _get_maildir_flag_actions (notmuch_message_t *message,\r
+       tag = notmuch_tags_get (tags);\r
\r
+       for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {\r
++          if (!flag2tag[i].safe) {\r
++              continue;\r
++          }\r
+           if (strcmp (tag, flag2tag[i].tag) == 0) {\r
+               if (flag2tag[i].inverse)\r
+                   to_clear = talloc_asprintf_append (to_clear,\r
+@@ -1134,6 +1130,9 @@ _get_maildir_flag_actions (notmuch_message_t *message,\r
\r
+     /* Then, find the flags for all tags not present. */\r
+     for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {\r
++      if (!flag2tag[i].safe) {\r
++          continue;\r
++      }\r
+       if (flag2tag[i].inverse) {\r
+           if (strchr (to_clear, flag2tag[i].flag) == NULL)\r
+               to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);\r
+@@ -1256,6 +1255,17 @@ _new_maildir_filename (void *ctx,\r
+     return filename_new;\r
+ }\r
\r
++void\r
++notmuch_message_set_flag_safety (char flag, notmuch_bool_t safe)\r
++{\r
++    unsigned i;\r
++    for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {\r
++        if (flag2tag[i].flag == flag) {\r
++            flag2tag[i].safe = safe;\r
++        }\r
++    }\r
++}\r
++\r
+ notmuch_status_t\r
+ notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)\r
+ {\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index f0c1b67..475e75a 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -922,8 +922,7 @@ notmuch_message_remove_all_tags (notmuch_message_t *message);\r
+  *    'P'     Adds the "passed" tag to the message\r
+  *    'R'     Adds the "replied" tag to the message\r
+  *    'S'     Removes the "unread" tag from the message\r
+- *    'T'     Adds the "deleted" tag to the message and\r
+- *            state->reckless_trash is TRUE.\r
++ *    'T'     Adds the "deleted" tag to the message\r
+  *\r
+  * For each flag that is not present, the opposite action (add/remove)\r
+  * is performed for the corresponding tags.\r
+@@ -941,6 +940,9 @@ notmuch_message_remove_all_tags (notmuch_message_t *message);\r
+  * notmuch_database_add_message. See also\r
+  * notmuch_message_tags_to_maildir_flags for synchronizing tag changes\r
+  * back to maildir flags.\r
++ *\r
++ * Actions are performed only if the tag is marked as "safe" in the\r
++ * configuration (only used by maildir_reckless_trash for now).\r
+  */\r
+ notmuch_status_t\r
+ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message);\r
+@@ -964,8 +966,7 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message);\r
+  *   'P' iff the message has the "passed" tag\r
+  *   'R' iff the message has the "replied" tag\r
+  *   'S' iff the message does not have the "unread" tag\r
+- *   'T' iff the message has the "trashed" tag and\r
+- *   state->reckless_trash is TRUE.\r
++ *   'T' iff the message has the "trashed" tag\r
+  *\r
+  * Any existing flags unmentioned in the list above will be preserved\r
+  * in the renaming.\r
+@@ -979,10 +980,21 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message);\r
+  * notmuch_message_remove_tag, or notmuch_message_freeze/\r
+  * notmuch_message_thaw). See also notmuch_message_maildir_flags_to_tags\r
+  * for synchronizing maildir flag changes back to tags.\r
++ *\r
++ * Actions are performed only if the tag is marked as "safe" in the\r
++ * configuration (only used by maildir_reckless_trash for now).\r
+  */\r
+ notmuch_status_t\r
+ notmuch_message_tags_to_maildir_flags (notmuch_message_t *message);\r
\r
++/* Set the 'safe' setting on the given flag\r
++ *\r
++ * The flag is the one-character IMAP flag, for example for Trashed\r
++ * messages, it's the char 'T'.\r
++ */\r
++void\r
++notmuch_message_set_flag_safety(char flag, notmuch_bool_t safe);\r
++\r
+ /* Freeze the current state of 'message' within the database.\r
+  *\r
+  * This means that changes to the message state, (via\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 7d17793..1502a70 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -801,6 +801,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
\r
+     add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);\r
+     add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
++    notmuch_message_set_flag_safety('T', notmuch_config_get_maildir_reckless_trash (config));\r
+     add_files_state.message_ids_to_sync = _filename_list_create (ctx);\r
+     db_path = notmuch_config_get_database_path (config);\r
\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index f095f64..1f3622e 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -43,6 +43,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+       return 1;\r
\r
+     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
++    notmuch_message_set_flag_safety('T', notmuch_config_get_maildir_reckless_trash (config));\r
\r
+     if (argc) {\r
+       input = fopen (argv[0], "r");\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 6204ae3..e2f7cb8 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -101,6 +101,7 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+       return 1;\r
\r
+     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
++    notmuch_message_set_flag_safety('T', notmuch_config_get_maildir_reckless_trash (config));\r
\r
+     query = notmuch_query_create (notmuch, query_string);\r
+     if (query == NULL) {\r
+-- \r
+1.7.2.5\r
+\r