unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH] Add a new.filename_tags option
@ 2012-04-16 15:01 Neil Roberts
  2012-04-16 16:09 ` Austin Clements
  0 siblings, 1 reply; 2+ messages in thread
From: Neil Roberts @ 2012-04-16 15:01 UTC (permalink / raw)
  To: notmuch; +Cc: Neil Roberts

This option is similar to the existing new.tags option except that it
is instead used when a new filename is encountered for an existing
message.

This can be used to do post-processing based on the filenames that a
message has. For example, in my setup I use maildrop to filter the
messages in to maildirs and then I have an extra script that runs to
add the tags based on which folders maildrop put the message in. The
script only looks at messages that have the 'inbox' tag and then
removes the tag after processing. This works fine except sometimes I
will get a message twice for example if I am CC'd in a message from a
mailing list. In that case I want the message to be tagged twice, once
to indicate it was sent directly to me and once to indicate it was
sent to the mailing list. If one of these messages is delayed then I
can end up processing the message once and removing the inbox tag.
When the second message is finally received it would previously not
get processed again so I would lose the second tag. With this patch I
can configure it to re-add the inbox tag in this case to force it to
reconsider the tags.
---
 man/man1/notmuch-config.1 |    8 ++++++++
 notmuch-client.h          |    9 +++++++++
 notmuch-config.c          |   34 ++++++++++++++++++++++++++++++++++
 notmuch-new.c             |    7 +++++++
 4 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/man/man1/notmuch-config.1 b/man/man1/notmuch-config.1
index 395cb9c..caf20b4 100644
--- a/man/man1/notmuch-config.1
+++ b/man/man1/notmuch-config.1
@@ -74,6 +74,14 @@ A list of tags that will be added to all messages incorporated by
 
 .RS 4
 .TP 4
+.B new.filename_tags
+A list of tags that will be added to any message when a new filename
+is encountered for it during
+.BR "notmuch new".
+.RE
+
+.RS 4
+.TP 4
 .B new.ignore
 A list of file and directory names, without path, that will not be
 searched for messages by
diff --git a/notmuch-client.h b/notmuch-client.h
index 19b7f01..55bcef4 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -238,6 +238,15 @@ notmuch_config_set_new_tags (notmuch_config_t *config,
 			     size_t length);
 
 const char **
+notmuch_config_get_new_filename_tags (notmuch_config_t *config,
+				      size_t *length);
+
+void
+notmuch_config_set_new_filename_tags (notmuch_config_t *config,
+				      const char *new_tags[],
+				      size_t length);
+
+const char **
 notmuch_config_get_new_ignore (notmuch_config_t *config,
 			       size_t *length);
 
diff --git a/notmuch-config.c b/notmuch-config.c
index e9b2750..acbc08d 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -111,6 +111,8 @@ struct _notmuch_config {
     size_t user_other_email_length;
     const char **new_tags;
     size_t new_tags_length;
+    const char **new_filename_tags;
+    size_t new_filename_tags_length;
     const char **new_ignore;
     size_t new_ignore_length;
     notmuch_bool_t maildir_synchronize_flags;
@@ -272,6 +274,8 @@ notmuch_config_open (void *ctx,
     config->user_other_email_length = 0;
     config->new_tags = NULL;
     config->new_tags_length = 0;
+    config->new_filename_tags = NULL;
+    config->new_filename_tags_length = 0;
     config->new_ignore = NULL;
     config->new_ignore_length = 0;
     config->maildir_synchronize_flags = TRUE;
@@ -371,6 +375,10 @@ notmuch_config_open (void *ctx,
 	notmuch_config_set_new_tags (config, tags, 2);
     }
 
+    if (notmuch_config_get_new_filename_tags (config, &tmp) == NULL) {
+	notmuch_config_set_new_filename_tags (config, NULL, 0);
+    }
+
     if (notmuch_config_get_new_ignore (config, &tmp) == NULL) {
 	notmuch_config_set_new_ignore (config, NULL, 0);
     }
@@ -624,6 +632,16 @@ notmuch_config_get_new_tags (notmuch_config_t *config,   size_t *length)
 }
 
 const char **
+notmuch_config_get_new_filename_tags (notmuch_config_t *config,
+				      size_t *length)
+{
+    return _config_get_list (config, "new", "filename_tags",
+			     &(config->new_filename_tags),
+			     &(config->new_filename_tags_length),
+			     length);
+}
+
+const char **
 notmuch_config_get_new_ignore (notmuch_config_t *config, size_t *length)
 {
     return _config_get_list (config, "new", "ignore",
@@ -650,6 +668,15 @@ notmuch_config_set_new_tags (notmuch_config_t *config,
 }
 
 void
+notmuch_config_set_new_filename_tags (notmuch_config_t *config,
+				      const char *list[],
+				      size_t length)
+{
+    _config_set_list (config, "new", "filename_tags", list, length,
+		      &(config->new_filename_tags));
+}
+
+void
 notmuch_config_set_new_ignore (notmuch_config_t *config,
 			       const char *list[],
 			       size_t length)
@@ -731,6 +758,13 @@ notmuch_config_command_get (void *ctx, char *item)
 	tags = notmuch_config_get_new_tags (config, &length);
 	for (i = 0; i < length; i++)
 	    printf ("%s\n", tags[i]);
+    } else if (strcmp(item, "new.filename_tags") == 0) {
+	const char **tags;
+	size_t i, length;
+
+	tags = notmuch_config_get_new_filename_tags (config, &length);
+	for (i = 0; i < length; i++)
+	    printf ("%s\n", tags[i]);
     } else {
 	char **value;
 	size_t i, length;
diff --git a/notmuch-new.c b/notmuch-new.c
index 4f13535..4a128b7 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -39,6 +39,8 @@ typedef struct {
     int verbose;
     const char **new_tags;
     size_t new_tags_length;
+    const char **new_filename_tags;
+    size_t new_filename_tags_length;
     const char **new_ignore;
     size_t new_ignore_length;
 
@@ -494,8 +496,12 @@ add_files_recursive (notmuch_database_t *notmuch,
 	    break;
 	/* Non-fatal issues (go on to next file) */
 	case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
+	    notmuch_message_freeze (message);
+	    for (tag=state->new_filename_tags; *tag != NULL; tag++)
+	        notmuch_message_add_tag (message, *tag);
 	    if (state->synchronize_flags == TRUE)
 		notmuch_message_maildir_flags_to_tags (message);
+	    notmuch_message_thaw (message);
 	    break;
 	case NOTMUCH_STATUS_FILE_NOT_EMAIL:
 	    fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
@@ -861,6 +867,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 	return 1;
 
     add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);
+    add_files_state.new_filename_tags = notmuch_config_get_new_filename_tags (config, &add_files_state.new_filename_tags_length);
     add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length);
     add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
     db_path = notmuch_config_get_database_path (config);
-- 
1.7.3.16.g9464b

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Add a new.filename_tags option
  2012-04-16 15:01 [PATCH] Add a new.filename_tags option Neil Roberts
@ 2012-04-16 16:09 ` Austin Clements
  0 siblings, 0 replies; 2+ messages in thread
From: Austin Clements @ 2012-04-16 16:09 UTC (permalink / raw)
  To: Neil Roberts; +Cc: notmuch

Quoth Neil Roberts on Apr 16 at  4:01 pm:
> This option is similar to the existing new.tags option except that it
> is instead used when a new filename is encountered for an existing
> message.
> 
> This can be used to do post-processing based on the filenames that a
> message has. For example, in my setup I use maildrop to filter the
> messages in to maildirs and then I have an extra script that runs to
> add the tags based on which folders maildrop put the message in. The
> script only looks at messages that have the 'inbox' tag and then
> removes the tag after processing. This works fine except sometimes I
> will get a message twice for example if I am CC'd in a message from a
> mailing list. In that case I want the message to be tagged twice, once
> to indicate it was sent directly to me and once to indicate it was
> sent to the mailing list. If one of these messages is delayed then I
> can end up processing the message once and removing the inbox tag.
> When the second message is finally received it would previously not
> get processed again so I would lose the second tag. With this patch I
> can configure it to re-add the inbox tag in this case to force it to
> reconsider the tags.

This is an interesting idea.  Unfortunately, the duplicate message-ID
code path you've modified is also used for rename detection.  Hence,
if the user modifies the maildir from another MUA, new.filename_tags
will be applied if the message gets moved to another folder, or even
if they simply change the maildir flags (e.g., marking the message
read).

I'm not sure exactly how your mail flow works, but would it be
possible to use folder-based tagging in your post-new hook to
accomplish this?  E.g.,
  notmuch tag +debian folder:debian
Since this isn't filtered by any delivery tags, it will apply to any
message that winds up in folder:debian, regardless of when it gets
there.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-04-16 16:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-16 15:01 [PATCH] Add a new.filename_tags option Neil Roberts
2012-04-16 16:09 ` Austin Clements

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).