[PATCH 1/2] Automatic tagging based on maildir
Taesoo Kim
taesoo at mit.edu
Thu Oct 25 16:33:58 PDT 2012
Since gmail will provide labels as imap sub/folders, notmuch also can
take advantage of labels as tags, when maildir.add_as_tag_flags is
specified.
Signed-off-by: Taesoo Kim <taesoo at mit.edu>
---
notmuch-client.h | 7 +++++++
notmuch-config.c | 42 ++++++++++++++++++++++++++++++++++++++----
notmuch-new.c | 32 ++++++++++++++++++++++++++++++++
3 files changed, 77 insertions(+), 4 deletions(-)
diff --git a/notmuch-client.h b/notmuch-client.h
index ae9344b..ce05e52 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -265,6 +265,13 @@ void
notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
notmuch_bool_t synchronize_flags);
+notmuch_bool_t
+notmuch_config_get_maildir_add_as_tag_flags (notmuch_config_t *config);
+
+void
+notmuch_config_set_maildir_add_as_tag_flags (notmuch_config_t *config,
+ notmuch_bool_t add_as_tag_flags);
+
const char **
notmuch_config_get_search_exclude_tags (notmuch_config_t *config, size_t *length);
diff --git a/notmuch-config.c b/notmuch-config.c
index 3e37a2d..c95f9dd 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -88,7 +88,16 @@ static const char maildir_config_comment[] =
"\n"
"\tThe \"notmuch new\" command will notice flag changes in filenames\n"
"\tand update tags, while the \"notmuch tag\" and \"notmuch restore\"\n"
- "\tcommands will notice tag changes and update flags in filenames\n";
+ "\tcommands will notice tag changes and update flags in filenames\n"
+ "\n"
+ "\tadd_as_tag_flags Valid values are true and false.\n"
+ "\n"
+ "\nIf ture, then add the lower-cased name of maildirs"
+ "\n(also deliminated by .) as tags"
+ "\n"
+ "\n\te.g. Life -> life"
+ "\n\te.g. MIT.CSAIL -> mit, csail"
+ "\n\te.g. Mailing.OSS.Linux -> mailing, oss, linux";
static const char search_config_comment[] =
" Search configuration\n"
@@ -114,6 +123,7 @@ struct _notmuch_config {
const char **new_ignore;
size_t new_ignore_length;
notmuch_bool_t maildir_synchronize_flags;
+ notmuch_bool_t maildir_add_as_tag_flags;
const char **search_exclude_tags;
size_t search_exclude_tags_length;
};
@@ -251,7 +261,7 @@ notmuch_config_open (void *ctx,
fprintf (stderr, "Out of memory.\n");
return NULL;
}
-
+
talloc_set_destructor (config, notmuch_config_destructor);
if (filename) {
@@ -393,6 +403,14 @@ notmuch_config_open (void *ctx,
g_error_free (error);
}
+ config->maildir_add_as_tag_flags =
+ g_key_file_get_boolean (config->key_file,
+ "maildir", "add_as_tag_flags", &error);
+ if (error) {
+ notmuch_config_set_maildir_add_as_tag_flags (config, FALSE);
+ g_error_free (error);
+ }
+
/* Whenever we know of configuration sections that don't appear in
* the configuration file, we add some comments to help the user
* understand what can be done. */
@@ -438,7 +456,7 @@ notmuch_config_open (void *ctx,
}
/* Close the given notmuch_config_t object, freeing all resources.
- *
+ *
* Note: Any changes made to the configuration are *not* saved by this
* function. To save changes, call notmuch_config_save before
* notmuch_config_close.
@@ -720,7 +738,7 @@ notmuch_config_command_get (void *ctx, char *item)
} else if (strcmp(item, "user.other_email") == 0) {
const char **other_email;
size_t i, length;
-
+
other_email = notmuch_config_get_user_other_email (config, &length);
for (i = 0; i < length; i++)
printf ("%s\n", other_email[i]);
@@ -891,3 +909,19 @@ notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
"maildir", "synchronize_flags", synchronize_flags);
config->maildir_synchronize_flags = synchronize_flags;
}
+
+
+notmuch_bool_t
+notmuch_config_get_maildir_add_as_tag_flags (notmuch_config_t *config)
+{
+ return config->maildir_add_as_tag_flags;
+}
+
+void
+notmuch_config_set_maildir_add_as_tag_flags (notmuch_config_t *config,
+ notmuch_bool_t add_as_tag_flags)
+{
+ g_key_file_set_boolean (config->key_file,
+ "maildir", "add_as_tag_flags", add_as_tag_flags);
+ config->maildir_add_as_tag_flags = add_as_tag_flags;
+}
diff --git a/notmuch-new.c b/notmuch-new.c
index 56c4a6f..9ec983f 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -20,6 +20,7 @@
#include "notmuch-client.h"
+#include <ctype.h>
#include <unistd.h>
typedef struct _filename_node {
@@ -53,6 +54,7 @@ typedef struct {
_filename_list_t *directory_mtimes;
notmuch_bool_t synchronize_flags;
+ notmuch_bool_t add_as_tag_flags;
} add_files_state_t;
static volatile sig_atomic_t do_print_progress = 0;
@@ -240,6 +242,32 @@ _entry_in_ignore_list (const char *entry, add_files_state_t *state)
return FALSE;
}
+static void
+_add_maildir_as_tag(notmuch_database_t *notmuch,
+ notmuch_message_t *msg, const char *path)
+{
+ char *tok = talloc_strdup (notmuch, path);
+ int len = strlen(tok);
+
+ /* asserts path ends with /cur|/tmp|/new */
+ if (len > 4 && tok[len - 4] == '/') {
+ char *iter = tok + len - 4;
+ *iter = '\0';
+ while (-- iter && iter >= tok) {
+ char c = *iter;
+ if (c == '/' || c == '.') {
+ *iter = '\0';
+ notmuch_message_add_tag (msg, iter + 1);
+ if (c == '/') {
+ break;
+ }
+ }
+ *iter = tolower(*iter);
+ }
+ }
+ talloc_free (tok);
+}
+
/* Examine 'path' recursively as follows:
*
* o Ask the filesystem for the mtime of 'path' (fs_mtime)
@@ -508,6 +536,9 @@ add_files (notmuch_database_t *notmuch,
notmuch_message_freeze (message);
for (tag=state->new_tags; *tag != NULL; tag++)
notmuch_message_add_tag (message, *tag);
+ if (state->add_as_tag_flags == TRUE) {
+ _add_maildir_as_tag(notmuch, message, path);
+ }
if (state->synchronize_flags == TRUE)
notmuch_message_maildir_flags_to_tags (message);
notmuch_message_thaw (message);
@@ -878,6 +909,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_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);
+ add_files_state.add_as_tag_flags = notmuch_config_get_maildir_add_as_tag_flags (config);
db_path = notmuch_config_get_database_path (config);
if (run_hooks) {
--
1.8.0
More information about the notmuch
mailing list