unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Sebastian Spaeth <Sebastian@SSpaeth.de>
To: notmuch@notmuchmail.org
Subject: [PATCH] notmuch-new: Respect maildir flags when importing a new message
Date: Mon,  1 Mar 2010 14:28:56 +0100	[thread overview]
Message-ID: <1267450136-31749-1-git-send-email-Sebastian@SSpaeth.de> (raw)
In-Reply-To: <20100216022128.GH22402@flamingspork.com>

When importing a new mail do check for maildir tags and assign corresponding notmuch tags.

Based on a patch by Michiel Buddingh <michiel@michielbuddingh.net> and subsequently modified by Tim Stoakes, Stewart Smith, and Sebastian Spaeth (see mail thread around mail id:20100210031339.GH16686@mail.rocksoft.com)

Do note that this will only add tags when importing a really new message, and will not do anything when detecting a file rename (although someone should really make it honor file renames as well). Deleteing an existing message in another IMAP client will therefore not trigger tagging (as it counts as a file rename).

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
I cleaned up the patch by Stewart Smith a bit more, but it's basically his last version of the patch. The biggest caveat is really that file renames won't cause any change. So this approach is really only part of the solution to sync with e.g. thunderbird usage.
 notmuch-new.c |   64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 63 insertions(+), 1 deletions(-)

diff --git a/notmuch-new.c b/notmuch-new.c
index f25c71f..5a75950 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -39,6 +39,7 @@ typedef struct {
     int total_files;
     int processed_files;
     int added_messages;
+    notmuch_bool_t tag_maildir;
     struct timeval tv_start;
 
     _filename_list_t *removed_files;
@@ -169,6 +170,60 @@ _entries_resemble_maildir (struct dirent **entries, int count)
     return 0;
 }
 
+/* Tag new mail according to its Maildir attribute flags.
+ *
+ * Test if the mail file's filename contains any of the
+ * standard Maildir attributes, and translate these to
+ * the corresponding standard notmuch tags.
+ *
+ * If the message is not marked as 'seen', or if no
+ * flags are present, tag as 'inbox, unread'.
+ */
+static void
+derive_tags_from_maildir_flags (notmuch_message_t *message,
+                           const char * path)
+{
+    int seen = FALSE;
+    int end_of_flags = FALSE;
+    size_t l = strlen(path);
+
+    /* Non-experimental message flags start with this */
+    char * i = strstr(path, ":2,");
+    i = (i) ? i : strstr(path, "!2,"); /* This format is used on VFAT */
+    if (i != NULL) {
+   i += 3;
+   for (; i < (path + l) && !end_of_flags; i++) {
+       switch (*i) {
+       case 'F' :
+           notmuch_message_add_tag (message, "maildir::flagged");
+           break;
+       case 'R': /* replied */
+           notmuch_message_add_tag (message, "maildir::replied");
+           break;
+       case 'D':
+           notmuch_message_add_tag (message, "maildir::draft");
+           break;
+       case 'S': /* seen */
+           seen = TRUE;
+           break;
+       case 'T': /* trashed */
+           notmuch_message_add_tag (message, "maildir::trashed");
+           break;
+       case 'P': /* passed */
+           notmuch_message_add_tag (message, "maildir::forwarded");
+           break;
+       default:
+           end_of_flags = TRUE;
+           break;
+       }
+   }
+    }
+
+    if (i == NULL || !seen) {
+   tag_inbox_and_unread (message);
+    }
+}
+
 /* Examine 'path' recursively as follows:
  *
  *   o Ask the filesystem for the mtime of 'path' (fs_mtime)
@@ -299,6 +354,8 @@ add_files_recursive (notmuch_database_t *notmuch,
 	    strcmp (entry->d_name, ".notmuch") ==0)
 	{
 	    continue;
+	} else {
+	    state->tag_maildir = TRUE;
 	}
 
 	next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
@@ -412,7 +469,12 @@ add_files_recursive (notmuch_database_t *notmuch,
 	/* success */
 	case NOTMUCH_STATUS_SUCCESS:
 	    state->added_messages++;
-	    tag_inbox_and_unread (message);
+	    if (state->tag_maildir) {
+	      derive_tags_from_maildir_flags (message,
+					      entry->d_name);
+	    } else {
+	        tag_inbox_and_unread (message);
+	    }
 	    break;
 	/* Non-fatal issues (go on to next file) */
 	case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
-- 
1.6.3.3

  parent reply	other threads:[~2010-03-01 13:29 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-18 15:55 [PATCH] notmuch: Add Maildir directory name as tag name for messages Aneesh Kumar K.V
2009-11-21 18:39 ` Carl Worth
2009-11-21 20:28   ` Carl Worth
2009-11-21 22:12     ` Bart Trojanowski
     [not found]       ` <9cce5525b093b87fe74d427954ffad89@localhost>
2009-11-22  4:04         ` Carl Worth
2009-11-22  9:33           ` Michiel Buddingh'
2009-11-22 10:57             ` Dirk-Jan C. Binnema
2009-11-22 16:00               ` Michiel Buddingh'
2009-11-22 21:44                 ` Dirk-Jan C. Binnema
2009-11-22 12:19             ` Carl Worth
2009-11-22 15:57               ` Michiel Buddingh'
2009-11-25 17:52             ` Carl Worth
2009-11-26 21:12               ` Michiel Buddingh'
2009-11-26 21:53                 ` Ingmar Vanhassel
2009-11-27 12:01                   ` Jan Janak
2009-11-28  3:26                 ` Carl Worth
2009-12-06 19:55                   ` Michiel Buddingh'
2010-02-10  3:13                     ` [PATCH] notmuch: Respect maildir message flags Tim Stoakes
2010-02-15  8:13                       ` Stewart Smith
2010-02-16  1:58                         ` Stewart Smith
2010-02-16  2:12                           ` martin f krafft
2010-02-16  2:21                             ` Stewart Smith
2010-02-16  4:21                               ` martin f krafft
2010-02-16  9:35                               ` Michal Sojka
2010-03-01 13:28                               ` Sebastian Spaeth [this message]
2010-04-07 21:16                                 ` [PATCH] notmuch-new: Respect maildir flags when importing a new message Carl Worth
2010-04-08 12:57                                   ` Michal Sojka
2009-11-22 10:37           ` [PATCH] notmuch: Add Maildir directory name as tag name for messages Dirk-Jan C. Binnema

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1267450136-31749-1-git-send-email-Sebastian@SSpaeth.de \
    --to=sebastian@sspaeth.de \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).