[PATCH 1/2] Automatic tagging based on maildir
authorTaesoo Kim <taesoo@mit.edu>
Thu, 25 Oct 2012 23:33:58 +0000 (19:33 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:50:03 +0000 (09:50 -0800)
e8/5c1bccb3998846828c4388fe088b5d1627f98d [new file with mode: 0644]

diff --git a/e8/5c1bccb3998846828c4388fe088b5d1627f98d b/e8/5c1bccb3998846828c4388fe088b5d1627f98d
new file mode 100644 (file)
index 0000000..cadadd5
--- /dev/null
@@ -0,0 +1,260 @@
+Return-Path: <tsgatesv@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 olra.theworths.org (Postfix) with ESMTP id 5EE9D431FDE\r
+       for <notmuch@notmuchmail.org>; Thu, 25 Oct 2012 16:33:28 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.699\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.699 tagged_above=-999 required=5\r
+       tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001,\r
+       RCVD_IN_DNSWL_LOW=-0.7] 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 ibuNg8STPpy2 for <notmuch@notmuchmail.org>;\r
+       Thu, 25 Oct 2012 16:33:25 -0700 (PDT)\r
+Received: from mail-qc0-f181.google.com (mail-qc0-f181.google.com\r
+       [209.85.216.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 5FB81431FAE\r
+       for <notmuch@notmuchmail.org>; Thu, 25 Oct 2012 16:33:25 -0700 (PDT)\r
+Received: by mail-qc0-f181.google.com with SMTP id x40so1058185qcp.26\r
+       for <notmuch@notmuchmail.org>; Thu, 25 Oct 2012 16:33:25 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+       h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to\r
+       :references; bh=P7Gk2RXu8IxuwzfhcBZ2hlp/jpTkc4Pe8RllG0E+6HA=;\r
+       b=vm8yvmciJVXVUIkD2yYdIjbEC+cxBX6cZjQYlkd5if1X1/SpkMd1/pw+HdOVVuyVvq\r
+       1tQNM3aHMtYErNrmtn0X7IwkKolcc9G7tEo3T7lJM4a05b+DaFLXvZbaxK7KtpDI8Bru\r
+       O+kJ4wV3fS8dB2/rDaHaakqpeS6yEjmWs4PoF2p+V/9NYFyROvzmpRpRHZEXKvguZtVr\r
+       H3LFdqd9QPObsjq8Jir0rBSBYpYZEdJeJ4jgqJ+5LrXbimMCmzBUz+Ns8jghdcd3VBBl\r
+       +aZpxQRsvwhwx8du493PoWbeX4q27VQrYeJ7qd6fGQpPqNhZKC5ScY50Kx28yaDTgZbC\r
+       bCbA==\r
+Received: by 10.224.107.3 with SMTP id z3mr1880883qao.9.1351208005203;\r
+       Thu, 25 Oct 2012 16:33:25 -0700 (PDT)\r
+Received: from skynet (26-4-166.dynamic.csail.mit.edu. [18.26.4.166])\r
+       by mx.google.com with ESMTPS id gw9sm161680qab.4.2012.10.25.16.33.23\r
+       (version=SSLv3 cipher=OTHER); Thu, 25 Oct 2012 16:33:24 -0700 (PDT)\r
+Sender: Taesoo Kim <tsgatesv@gmail.com>\r
+Received: by skynet (sSMTP sendmail emulation); Thu, 25 Oct 2012 19:34:44\r
+ -0400\r
+From: Taesoo Kim <taesoo@mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 1/2] Automatic tagging based on maildir\r
+Date: Thu, 25 Oct 2012 19:33:58 -0400\r
+Message-Id: <1351208039-32293-2-git-send-email-taesoo@mit.edu>\r
+X-Mailer: git-send-email 1.8.0\r
+In-Reply-To: <1351208039-32293-1-git-send-email-taesoo@mit.edu>\r
+References: <1351208039-32293-1-git-send-email-taesoo@mit.edu>\r
+X-Mailman-Approved-At: Thu, 25 Oct 2012 22:45:51 -0700\r
+Cc: Taesoo Kim <taesoo@mit.edu>\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: Thu, 25 Oct 2012 23:33:28 -0000\r
+\r
+Since gmail will provide labels as imap sub/folders, notmuch also can\r
+take advantage of labels as tags, when maildir.add_as_tag_flags is\r
+specified.\r
+\r
+Signed-off-by: Taesoo Kim <taesoo@mit.edu>\r
+---\r
+ notmuch-client.h |  7 +++++++\r
+ notmuch-config.c | 42 ++++++++++++++++++++++++++++++++++++++----\r
+ notmuch-new.c    | 32 ++++++++++++++++++++++++++++++++\r
+ 3 files changed, 77 insertions(+), 4 deletions(-)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index ae9344b..ce05e52 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -265,6 +265,13 @@ void\r
+ notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,\r
+                                             notmuch_bool_t synchronize_flags);\r
+ \r
++notmuch_bool_t\r
++notmuch_config_get_maildir_add_as_tag_flags (notmuch_config_t *config);\r
++\r
++void\r
++notmuch_config_set_maildir_add_as_tag_flags (notmuch_config_t *config,\r
++                                           notmuch_bool_t add_as_tag_flags);\r
++\r
+ const char **\r
+ notmuch_config_get_search_exclude_tags (notmuch_config_t *config, size_t *length);\r
+ \r
+diff --git a/notmuch-config.c b/notmuch-config.c\r
+index 3e37a2d..c95f9dd 100644\r
+--- a/notmuch-config.c\r
++++ b/notmuch-config.c\r
+@@ -88,7 +88,16 @@ static const char maildir_config_comment[] =\r
+     "\n"\r
+     "\tThe \"notmuch new\" command will notice flag changes in filenames\n"\r
+     "\tand update tags, while the \"notmuch tag\" and \"notmuch restore\"\n"\r
+-    "\tcommands will notice tag changes and update flags in filenames\n";\r
++    "\tcommands will notice tag changes and update flags in filenames\n"\r
++    "\n"\r
++    "\tadd_as_tag_flags       Valid values are true and false.\n"\r
++    "\n"\r
++    "\nIf ture, then add the lower-cased name of maildirs"\r
++    "\n(also deliminated by .) as tags"\r
++    "\n"\r
++    "\n\te.g. Life              -> life"\r
++    "\n\te.g. MIT.CSAIL         -> mit, csail"\r
++    "\n\te.g. Mailing.OSS.Linux -> mailing, oss, linux";\r
+ \r
+ static const char search_config_comment[] =\r
+     " Search configuration\n"\r
+@@ -114,6 +123,7 @@ struct _notmuch_config {\r
+     const char **new_ignore;\r
+     size_t new_ignore_length;\r
+     notmuch_bool_t maildir_synchronize_flags;\r
++    notmuch_bool_t maildir_add_as_tag_flags;\r
+     const char **search_exclude_tags;\r
+     size_t search_exclude_tags_length;\r
+ };\r
+@@ -251,7 +261,7 @@ notmuch_config_open (void *ctx,\r
+       fprintf (stderr, "Out of memory.\n");\r
+       return NULL;\r
+     }\r
+-    \r
++\r
+     talloc_set_destructor (config, notmuch_config_destructor);\r
+ \r
+     if (filename) {\r
+@@ -393,6 +403,14 @@ notmuch_config_open (void *ctx,\r
+       g_error_free (error);\r
+     }\r
+ \r
++    config->maildir_add_as_tag_flags =\r
++      g_key_file_get_boolean (config->key_file,\r
++                              "maildir", "add_as_tag_flags", &error);\r
++    if (error) {\r
++      notmuch_config_set_maildir_add_as_tag_flags (config, FALSE);\r
++      g_error_free (error);\r
++    }\r
++\r
+     /* Whenever we know of configuration sections that don't appear in\r
+      * the configuration file, we add some comments to help the user\r
+      * understand what can be done. */\r
+@@ -438,7 +456,7 @@ notmuch_config_open (void *ctx,\r
+ }\r
+ \r
+ /* Close the given notmuch_config_t object, freeing all resources.\r
+- * \r
++ *\r
+  * Note: Any changes made to the configuration are *not* saved by this\r
+  * function. To save changes, call notmuch_config_save before\r
+  * notmuch_config_close.\r
+@@ -720,7 +738,7 @@ notmuch_config_command_get (void *ctx, char *item)\r
+     } else if (strcmp(item, "user.other_email") == 0) {\r
+       const char **other_email;\r
+       size_t i, length;\r
+-      \r
++\r
+       other_email = notmuch_config_get_user_other_email (config, &length);\r
+       for (i = 0; i < length; i++)\r
+           printf ("%s\n", other_email[i]);\r
+@@ -891,3 +909,19 @@ notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,\r
+                           "maildir", "synchronize_flags", synchronize_flags);\r
+     config->maildir_synchronize_flags = synchronize_flags;\r
+ }\r
++\r
++\r
++notmuch_bool_t\r
++notmuch_config_get_maildir_add_as_tag_flags (notmuch_config_t *config)\r
++{\r
++    return config->maildir_add_as_tag_flags;\r
++}\r
++\r
++void\r
++notmuch_config_set_maildir_add_as_tag_flags (notmuch_config_t *config,\r
++                                           notmuch_bool_t add_as_tag_flags)\r
++{\r
++    g_key_file_set_boolean (config->key_file,\r
++                          "maildir", "add_as_tag_flags", add_as_tag_flags);\r
++    config->maildir_add_as_tag_flags = add_as_tag_flags;\r
++}\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 56c4a6f..9ec983f 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -20,6 +20,7 @@\r
+ \r
+ #include "notmuch-client.h"\r
+ \r
++#include <ctype.h>\r
+ #include <unistd.h>\r
+ \r
+ typedef struct _filename_node {\r
+@@ -53,6 +54,7 @@ typedef struct {\r
+     _filename_list_t *directory_mtimes;\r
+ \r
+     notmuch_bool_t synchronize_flags;\r
++    notmuch_bool_t add_as_tag_flags;\r
+ } add_files_state_t;\r
+ \r
+ static volatile sig_atomic_t do_print_progress = 0;\r
+@@ -240,6 +242,32 @@ _entry_in_ignore_list (const char *entry, add_files_state_t *state)\r
+     return FALSE;\r
+ }\r
+ \r
++static void\r
++_add_maildir_as_tag(notmuch_database_t *notmuch,\r
++                  notmuch_message_t *msg, const char *path) \r
++{\r
++    char *tok = talloc_strdup (notmuch, path);\r
++    int len = strlen(tok);\r
++    \r
++    /* asserts path ends with /cur|/tmp|/new */\r
++    if (len > 4 && tok[len - 4] == '/') {\r
++      char *iter = tok + len - 4;\r
++      *iter = '\0';\r
++      while (-- iter && iter >= tok) {\r
++          char c = *iter;\r
++          if (c == '/' || c == '.') {\r
++              *iter = '\0';\r
++              notmuch_message_add_tag (msg, iter + 1);\r
++              if (c == '/') {\r
++                  break;\r
++              }\r
++          }\r
++          *iter = tolower(*iter);\r
++      }\r
++    }\r
++    talloc_free (tok);\r
++}\r
++\r
+ /* Examine 'path' recursively as follows:\r
+  *\r
+  *   o Ask the filesystem for the mtime of 'path' (fs_mtime)\r
+@@ -508,6 +536,9 @@ add_files (notmuch_database_t *notmuch,\r
+           notmuch_message_freeze (message);\r
+           for (tag=state->new_tags; *tag != NULL; tag++)\r
+               notmuch_message_add_tag (message, *tag);\r
++          if (state->add_as_tag_flags == TRUE) {\r
++              _add_maildir_as_tag(notmuch, message, path);\r
++          }\r
+           if (state->synchronize_flags == TRUE)\r
+               notmuch_message_maildir_flags_to_tags (message);\r
+           notmuch_message_thaw (message);\r
+@@ -878,6 +909,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+     add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);\r
+     add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length);\r
+     add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
++    add_files_state.add_as_tag_flags = notmuch_config_get_maildir_add_as_tag_flags (config);\r
+     db_path = notmuch_config_get_database_path (config);\r
+ \r
+     if (run_hooks) {\r
+-- \r
+1.8.0\r
+\r