[notmuch] [PATCH] notmuch new: Support for conversion of spool subdirectories into...
authorJan Janak <jan@ryngle.com>
Wed, 18 Nov 2009 04:57:03 +0000 (05:57 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:35:36 +0000 (09:35 -0800)
1c/7bef3ce110086573357c2a3a96702e5ffc8d31 [new file with mode: 0644]

diff --git a/1c/7bef3ce110086573357c2a3a96702e5ffc8d31 b/1c/7bef3ce110086573357c2a3a96702e5ffc8d31
new file mode 100644 (file)
index 0000000..59f0277
--- /dev/null
@@ -0,0 +1,203 @@
+Return-Path: <jan@iptel.org>\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 A77AB431FBC\r
+       for <notmuch@notmuchmail.org>; Tue, 17 Nov 2009 20:57:08 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\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 5GfykVyL0XQe for <notmuch@notmuchmail.org>;\r
+       Tue, 17 Nov 2009 20:57:08 -0800 (PST)\r
+X-Greylist: delayed 23897 seconds by postgrey-1.32 at olra;\r
+       Tue, 17 Nov 2009 20:57:07 PST\r
+Received: from mail.iptel.org (smtp.iptel.org [213.192.59.67])\r
+       by olra.theworths.org (Postfix) with ESMTP id AFF89431FAE\r
+       for <notmuch@notmuchmail.org>; Tue, 17 Nov 2009 20:57:07 -0800 (PST)\r
+Received: by mail.iptel.org (Postfix, from userid 103)\r
+       id 6A03E370823; Wed, 18 Nov 2009 05:57:06 +0100 (CET)\r
+Received: from x61s.janakj (r2c34.net.upc.cz [62.245.66.34])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (Client did not present a certificate)\r
+       by mail.iptel.org (Postfix) with ESMTPSA id 9A42937082E\r
+       for <notmuch@notmuchmail.org>; Wed, 18 Nov 2009 05:57:05 +0100 (CET)\r
+Received: by x61s.janakj (Postfix, from userid 1000)\r
+       id CA6AC440655; Wed, 18 Nov 2009 05:57:03 +0100 (CET)\r
+From: Jan Janak <jan@ryngle.com>\r
+To: notmuch@notmuchmail.org\r
+Date: Wed, 18 Nov 2009 05:57:03 +0100\r
+Message-Id: <1258520223-15328-1-git-send-email-jan@ryngle.com>\r
+X-Mailer: git-send-email 1.6.3.3\r
+Subject: [notmuch] [PATCH] notmuch new: Support for conversion of spool\r
+       subdirectories into tags\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.12\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: Wed, 18 Nov 2009 04:57:08 -0000\r
+\r
+This patch makes it possible to convert subdirectory names inside the\r
+spool directory into message tags. Messages stored in subdirectory\r
+"foo" will be marked with tag "foo". Message duplicates found in several\r
+subdirectories will be given one tag per subdirectory.\r
+\r
+This feature can be used to synchronize notmuch's tags with with gmail\r
+tags, for example. Gmail IMAP servers convert tags to IMAP\r
+subdirectories and those subdirectories can be converted back to tags\r
+in notmuch.\r
+\r
+The patch modifies notmuch_database_add_message function to return a\r
+pointer to the message even if a message duplicate was found in the\r
+database. This is needed if we want to add a tag for each subdirectory\r
+in which a message duplicate was found.\r
+\r
+In addition to that, it makes the pointer to notmuch_config_t global\r
+(previously it was a local variable in notmuch_new_command). The\r
+configuration data structure is used by the function which converts\r
+subdirectory names to tags.\r
+\r
+Finally, there is a new function called subdir_to_tag. The function\r
+extracts the name of the subdirectory inside the spool from the full\r
+path of the message (also removing Maildir's cur,dir,and tmp\r
+subdirectories) and adds it as a new tag to the message.\r
+\r
+Signed-off-by: Jan Janak <jan@ryngle.com>\r
+---\r
+ lib/database.cc |    3 +-\r
+ notmuch-new.c   |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++-\r
+ 2 files changed, 74 insertions(+), 3 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 3c8d626..f7799d2 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -949,7 +949,8 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
\r
+   DONE:\r
+     if (message) {\r
+-      if (ret == NOTMUCH_STATUS_SUCCESS && message_ret)\r
++              if ((ret == NOTMUCH_STATUS_SUCCESS ||\r
++                       ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) && message_ret)\r
+           *message_ret = message;\r
+       else\r
+           notmuch_message_destroy (message);\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 83a05ba..d94ce16 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -19,6 +19,9 @@\r
+  */\r
\r
+ #include "notmuch-client.h"\r
++#include <libgen.h>\r
++\r
++static notmuch_config_t *config = 0;\r
\r
+ static volatile sig_atomic_t do_add_files_print_progress = 0;\r
\r
+@@ -45,6 +48,69 @@ tag_inbox_and_unread (notmuch_message_t *message)\r
+     notmuch_message_add_tag (message, "unread");\r
+ }\r
\r
++/*\r
++ * Extracts the sub-directory from the filename and adds it as a new tag to\r
++ * the message. The filename must begin with the database directory configured\r
++ * in the configuration file. This prefix is then removed. If the remaining\r
++ * sub-directory ends with one of the Maildir special directories (/tmp, /new,\r
++ * /cur) then they are removed as well. If there is anything left then the\r
++ * function adds it as a new tag to the message.\r
++ *\r
++ * The function does nothing if it cannot extract a sub-directory from\r
++ * filename.\r
++ */\r
++static void\r
++subdir_to_tag (char* filename, notmuch_message_t *message)\r
++{\r
++      const char* db_path;\r
++      char *msg_dir, *tmp;\r
++      int db_path_len, msg_dir_len;\r
++\r
++      if (config == NULL) return;\r
++    db_path = notmuch_config_get_database_path (config);\r
++      if (db_path == NULL) return;\r
++      db_path_len = strlen(db_path);\r
++\r
++      /* Make a copy of the string as dirname may need to modify it. */\r
++      tmp = talloc_strdup(message, filename);\r
++      msg_dir = dirname(tmp);\r
++      msg_dir_len = strlen(msg_dir);\r
++\r
++      /* If msg_dir starts with db_path, remove it, including the / which delimits\r
++       * it from the rest of the directory name. */\r
++      if (db_path_len < msg_dir_len &&\r
++              !strncmp(db_path, msg_dir, db_path_len)) {\r
++              msg_dir += db_path_len + 1;\r
++              msg_dir_len -= db_path_len + 1;\r
++      } else {\r
++              /* If we get here, either the message filename is not inside the\r
++               * database directory configured in the configuration file, or it is a\r
++               * file in the root directory of the database. Either way we just skip\r
++               * it because we do not know how to convert it to a meaningful\r
++               * subdirectory string that we could add as tag. */\r
++              goto out;\r
++      }\r
++\r
++      /* Special conditioning for Maildirs. If the remainder of the directory\r
++       * name ends with /new, /cur, or /tmp then remove it. */\r
++      if ((msg_dir_len >= 4) &&\r
++              (!strncmp(msg_dir + msg_dir_len - 4, "/new", 4) ||\r
++               !strncmp(msg_dir + msg_dir_len - 4, "/cur", 4) ||\r
++               !strncmp(msg_dir + msg_dir_len - 4, "/tmp", 4))) {\r
++              msg_dir[msg_dir_len - 4] = '\0';\r
++      }\r
++\r
++      /* If, after all the modifications, we still have a subdirectory, add it\r
++       * as tag. */\r
++      if (strlen(msg_dir)) {\r
++              notmuch_message_add_tag (message, msg_dir);\r
++      }\r
++\r
++out:\r
++      talloc_free(tmp);\r
++}\r
++\r
++\r
+ static void\r
+ add_files_print_progress (add_files_state_t *state)\r
+ {\r
+@@ -186,10 +252,15 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+                   case NOTMUCH_STATUS_SUCCESS:\r
+                       state->added_messages++;\r
+                       tag_inbox_and_unread (message);\r
++                      subdir_to_tag(next, message);\r
+                       break;\r
+                   /* Non-fatal issues (go on to next file) */\r
+                   case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
+-                      /* Stay silent on this one. */\r
++                      /* Stay silent on this one. The message already exists in the\r
++                               * database, that means we may have found a duplicate in\r
++                               * another directory. If that's the case then we add another\r
++                               * tag to the message with the sub-directory. */\r
++                              subdir_to_tag(next, message);\r
+                       break;\r
+                   case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
+                       fprintf (stderr, "Note: Ignoring non-mail file: %s\n",\r
+@@ -386,7 +457,6 @@ int\r
+ notmuch_new_command (void *ctx,\r
+                    unused (int argc), unused (char *argv[]))\r
+ {\r
+-    notmuch_config_t *config;\r
+     notmuch_database_t *notmuch;\r
+     add_files_state_t add_files_state;\r
+     double elapsed;\r
+-- \r
+1.6.3.3\r
+\r