X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch.c;h=b58af15663d66b0c8ce1dc6f80372a518bf06cf1;hp=e2347524506192b892bfd578faa2e5ed55d575b1;hb=5eaec1e31679d2a1a9ea3b32fb306ad67ee1a936;hpb=46ba33b1159fb48b8360ddae72492cc3162dc999 diff --git a/notmuch.c b/notmuch.c index e2347524..b58af156 100644 --- a/notmuch.c +++ b/notmuch.c @@ -41,8 +41,6 @@ #include -#include /* g_strdup_printf */ - #define unused(x) x __attribute__ ((unused)) /* There's no point in continuing when we've detected that we've done @@ -69,6 +67,8 @@ typedef struct command { const char *usage; } command_t; +typedef void (*add_files_callback_t) (notmuch_message_t *message); + typedef struct { int ignore_read_only_directories; int saw_read_only_directory; @@ -77,6 +77,8 @@ typedef struct { int processed_files; int added_messages; struct timeval tv_start; + + add_files_callback_t callback; } add_files_state_t; static void @@ -178,6 +180,7 @@ add_files_recursive (notmuch_database_t *notmuch, char *next = NULL; time_t path_mtime, path_dbtime; notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS; + notmuch_message_t *message = NULL, **closure; /* If we're told to, we bail out on encountering a read-only * directory, (with this being a clear clue from the user to @@ -236,7 +239,7 @@ add_files_recursive (notmuch_database_t *notmuch, continue; } - next = g_strdup_printf ("%s/%s", path, entry->d_name); + next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name); if (stat (next, st)) { fprintf (stderr, "Error reading %s: %s\n", @@ -251,11 +254,18 @@ add_files_recursive (notmuch_database_t *notmuch, if (st->st_mtime > path_dbtime) { state->processed_files++; - status = notmuch_database_add_message (notmuch, next); + if (state->callback) + closure = &message; + else + closure = NULL; + + status = notmuch_database_add_message (notmuch, next, closure); switch (status) { /* success */ case NOTMUCH_STATUS_SUCCESS: state->added_messages++; + if (state->callback) + (state->callback) (message); break; /* Non-fatal issues (go on to next file) */ case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: @@ -276,10 +286,17 @@ add_files_recursive (notmuch_database_t *notmuch, case NOTMUCH_STATUS_FILE_ERROR: case NOTMUCH_STATUS_NULL_POINTER: case NOTMUCH_STATUS_TAG_TOO_LONG: + case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: case NOTMUCH_STATUS_LAST_STATUS: INTERNAL_ERROR ("add_message returned unexpected value: %d", status); goto DONE; } + + if (message) { + notmuch_message_destroy (message); + message = NULL; + } + if (state->processed_files % 1000 == 0) add_files_print_progress (state); } @@ -289,7 +306,7 @@ add_files_recursive (notmuch_database_t *notmuch, ret = status; } - free (next); + talloc_free (next); next = NULL; } @@ -299,7 +316,7 @@ add_files_recursive (notmuch_database_t *notmuch, DONE: if (next) - free (next); + talloc_free (next); if (entry) free (entry); if (dir) @@ -342,7 +359,7 @@ static void count_files (const char *path, int *count) { DIR *dir; - struct dirent *entry, *e; + struct dirent *e, *entry = NULL; int entry_length; int err; char *next; @@ -353,7 +370,7 @@ count_files (const char *path, int *count) if (dir == NULL) { fprintf (stderr, "Warning: failed to open directory %s: %s\n", path, strerror (errno)); - return; + goto DONE; } entry_length = offsetof (struct dirent, d_name) + @@ -366,7 +383,7 @@ count_files (const char *path, int *count) fprintf (stderr, "Error reading directory: %s\n", strerror (errno)); free (entry); - return; + goto DONE; } if (e == NULL) @@ -384,7 +401,12 @@ count_files (const char *path, int *count) continue; } - next = g_strdup_printf ("%s/%s", path, entry->d_name); + if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) { + next = NULL; + fprintf (stderr, "Error descending from %s to %s: Out of memory\n", + path, entry->d_name); + continue; + } stat (next, &st); @@ -401,7 +423,9 @@ count_files (const char *path, int *count) free (next); } - free (entry); + DONE: + if (entry) + free (entry); closedir (dir); } @@ -490,6 +514,7 @@ setup_command (unused (int argc), unused (char *argv[])) add_files_state.total_files = count; add_files_state.processed_files = 0; add_files_state.added_messages = 0; + add_files_state.callback = NULL; gettimeofday (&add_files_state.tv_start, NULL); ret = add_files (notmuch, mail_directory, &add_files_state); @@ -532,6 +557,13 @@ setup_command (unused (int argc), unused (char *argv[])) return ret; } +static void +tag_inbox_and_unread (notmuch_message_t *message) +{ + notmuch_message_add_tag (message, "inbox"); + notmuch_message_add_tag (message, "unread"); +} + static int new_command (unused (int argc), unused (char *argv[])) { @@ -555,6 +587,7 @@ new_command (unused (int argc), unused (char *argv[])) add_files_state.total_files = 0; add_files_state.processed_files = 0; add_files_state.added_messages = 0; + add_files_state.callback = tag_inbox_and_unread; gettimeofday (&add_files_state.tv_start, NULL); ret = add_files (notmuch, mail_directory, &add_files_state); @@ -602,6 +635,40 @@ new_command (unused (int argc), unused (char *argv[])) return ret; } +/* Construct a single query string from the passed arguments, using + * 'ctx' as the talloc owner for all allocations. + * + * Currently, the arguments are just connected with space characters, + * but we might do more processing in the future, (such as inserting + * any AND operators needed to work around Xapian QueryParser bugs). + * + * This function returns NULL in case of insufficient memory. + */ +static char * +query_string_from_args (void *ctx, int argc, char *argv[]) +{ + char *query_string; + int i; + + query_string = talloc_strdup (ctx, ""); + if (query_string == NULL) + return NULL; + + for (i = 0; i < argc; i++) { + if (i != 0) { + query_string = talloc_strdup_append (query_string, " "); + if (query_string == NULL) + return NULL; + } + + query_string = talloc_strdup_append (query_string, argv[i]); + if (query_string == NULL) + return NULL; + } + + return query_string; +} + static int search_command (int argc, char *argv[]) { @@ -610,8 +677,8 @@ search_command (int argc, char *argv[]) notmuch_query_t *query; notmuch_thread_results_t *results; notmuch_thread_t *thread; + notmuch_tags_t *tags; char *query_str; - int i; notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS; notmuch = notmuch_database_open (NULL); @@ -620,15 +687,7 @@ search_command (int argc, char *argv[]) goto DONE; } - /* XXX: Should add xtalloc wrappers here and use them. */ - query_str = talloc_strdup (local, ""); - - for (i = 0; i < argc; i++) { - if (i != 0) - query_str = talloc_asprintf_append (query_str, " "); - - query_str = talloc_asprintf_append (query_str, "%s", argv[i]); - } + query_str = query_string_from_args (local, argc, argv); query = notmuch_query_create (notmuch, query_str); if (query == NULL) { @@ -641,9 +700,25 @@ search_command (int argc, char *argv[]) notmuch_thread_results_has_more (results); notmuch_thread_results_advance (results)) { + int first = 1; + thread = notmuch_thread_results_get (results); - printf ("%s\n", notmuch_thread_get_thread_id (thread)); + printf ("%s %s", + notmuch_thread_get_thread_id (thread), + notmuch_thread_get_subject (thread)); + + printf (" ("); + for (tags = notmuch_thread_get_tags (thread); + notmuch_tags_has_more (tags); + notmuch_tags_advance (tags)) + { + if (! first) + printf (" "); + printf ("%s", notmuch_tags_get (tags)); + first = 0; + } + printf (")\n"); notmuch_thread_destroy (thread); } @@ -665,6 +740,111 @@ show_command (unused (int argc), unused (char *argv[])) return 1; } +static int +tag_command (unused (int argc), unused (char *argv[])) +{ + void *local; + int *add_tags, *remove_tags; + int add_tags_count = 0; + int remove_tags_count = 0; + char *query_string; + notmuch_database_t *notmuch = NULL; + notmuch_query_t *query; + notmuch_message_results_t *results; + notmuch_message_t *message; + int ret = 0; + int i; + + local = talloc_new (NULL); + if (local == NULL) { + ret = 1; + goto DONE; + } + + add_tags = talloc_size (local, argc * sizeof (int)); + if (add_tags == NULL) { + ret = 1; + goto DONE; + } + + remove_tags = talloc_size (local, argc * sizeof (int)); + if (remove_tags == NULL) { + ret = 1; + goto DONE; + } + + for (i = 0; i < argc; i++) { + if (strcmp (argv[i], "--") == 0) { + i++; + break; + } + if (argv[i][0] == '+') { + add_tags[add_tags_count++] = i; + } else if (argv[i][0] == '-') { + remove_tags[remove_tags_count++] = i; + } else { + break; + } + } + + if (add_tags_count == 0 && remove_tags_count == 0) { + fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n"); + ret = 1; + goto DONE; + } + + if (i == argc) { + fprintf (stderr, "Error: 'notmuch tag' requires at least one search term.\n"); + ret = 1; + goto DONE; + } + + notmuch = notmuch_database_open (NULL); + if (notmuch == NULL) { + ret = 1; + goto DONE; + } + + query_string = query_string_from_args (local, argc - i, &argv[i]); + + query = notmuch_query_create (notmuch, query_string); + if (query == NULL) { + fprintf (stderr, "Out of memory.\n"); + ret = 1; + goto DONE; + } + + for (results = notmuch_query_search_messages (query); + notmuch_message_results_has_more (results); + notmuch_message_results_advance (results)) + { + message = notmuch_message_results_get (results); + + notmuch_message_freeze (message); + + for (i = 0; i < remove_tags_count; i++) + notmuch_message_remove_tag (message, + argv[remove_tags[i]] + 1); + + for (i = 0; i < add_tags_count; i++) + notmuch_message_add_tag (message, argv[add_tags[i]] + 1); + + notmuch_message_thaw (message); + + notmuch_message_destroy (message); + } + + notmuch_query_destroy (query); + + DONE: + if (notmuch) + notmuch_database_close (notmuch); + + talloc_free (local); + + return ret; +} + static int dump_command (int argc, char *argv[]) { @@ -805,35 +985,34 @@ restore_command (int argc, char *argv[]) message = notmuch_database_find_message (notmuch, message_id); if (message == NULL) { - fprintf (stderr, "Warning: Cannot apply tags to missing message: %s (", + fprintf (stderr, "Warning: Cannot apply tags to missing message: %s\n", message_id); + goto NEXT_LINE; } + notmuch_message_freeze (message); + + notmuch_message_remove_all_tags (message); + next = tags; while (next) { tag = strsep (&next, " "); if (*tag == '\0') continue; - if (message) { - status = notmuch_message_add_tag (message, tag); - if (status) { - fprintf (stderr, - "Error applying tag %s to message %s:\n", - tag, message_id); - fprintf (stderr, "%s\n", - notmuch_status_to_string (status)); - } - } else { - fprintf (stderr, "%s%s", - tag == tags ? "" : " ", tag); + status = notmuch_message_add_tag (message, tag); + if (status) { + fprintf (stderr, + "Error applying tag %s to message %s:\n", + tag, message_id); + fprintf (stderr, "%s\n", + notmuch_status_to_string (status)); } } - if (message) - notmuch_message_destroy (message); - else - fprintf (stderr, ")\n"); + notmuch_message_thaw (message); + notmuch_message_destroy (message); } + NEXT_LINE: free (message_id); free (tags); } @@ -858,10 +1037,13 @@ command_t commands[] = { "\t\tthe setup command has not previously been completed." }, { "new", new_command, "Find and import any new messages.\n\n" - "\t\tScans all sub-directories of the database, adding new files\n" - "\t\tthat are found. Note: \"notmuch new\" will skip any\n" - "\t\tread-only directories, so you can use that to mark\n" - "\t\tdirectories that will not receive any new mail."}, + "\t\tScans all sub-directories of the database, adding new messages\n" + "\t\tthat are found. Each new message will be tagges as both\n" + "\t\t\"inbox\" and \"unread\".\n" + "\n" + "\t\tNote: \"notmuch new\" will skip any read-only directories,\n" + "\t\tso you can use that to mark tdirectories that will not\n" + "\t\treceive any new mail (and make \"notmuch new\" faster)." }, { "search", search_command, " [...]\n\n" "\t\tSearch for threads matching the given search terms.\n" @@ -870,6 +1052,19 @@ command_t commands[] = { { "show", show_command, "\n\n" "\t\tShow the thread with the given thread ID (see 'search')." }, + { "tag", tag_command, + "+|- [...] [--] [...]\n\n" + "\t\tAdd or remove the specified tags to all messages matching\n" + "\t\tthe specified search terms. The search terms are handled\n" + "\t\texactly as in 'search' so one can use that command first\n" + "\t\tto see what will be modified.\n\n" + "\t\tTags prefixed by '+' are added while those prefixed by '-' are\n" + "\t\tremoved. For each message, tag removal is before tag addition.\n\n" + "\t\tThe beginning of is recognized by the first\n" + "\t\targument that begins with neither '+' nor '-'. Support for\n" + "\t\tan initial search term beginning with '+' or '-' is provided\n" + "\t\tby allowing the user to specify a \"--\" argument to separate\n" + "\t\tthe tags from the search terms." }, { "dump", dump_command, "[]\n\n" "\t\tCreate a plain-text dump of the tags for each message\n" @@ -924,11 +1119,11 @@ main (int argc, char *argv[]) strcmp (argv[1], "--help") == 0) { fprintf (stderr, "The notmuch mail system.\n\n"); + usage (); + return 0; } else { fprintf (stderr, "Error: Unknown command '%s'\n\n", argv[1]); + usage (); + return 1; } - usage (); - exit (1); - - return 0; }