1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
22 #define _GNU_SOURCE /* for getline */
28 /* This is separate from notmuch-private.h because we're trying to
29 * keep notmuch.c from looking into any internals, (which helps us
30 * develop notmuch.h into a plausible library interface).
44 #define unused(x) x __attribute__ ((unused))
46 /* There's no point in continuing when we've detected that we've done
47 * something wrong internally (as opposed to the user passing in a
50 * Note that __location__ comes from talloc.h.
52 #define INTERNAL_ERROR(format, ...) \
55 "Internal error: " format " (%s)\n", \
56 ##__VA_ARGS__, __location__); \
60 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
62 typedef int (*command_function_t) (int argc, char *argv[]);
64 typedef struct command {
66 command_function_t function;
68 const char *documentation;
71 typedef void (*add_files_callback_t) (notmuch_message_t *message);
74 int ignore_read_only_directories;
75 int saw_read_only_directory;
80 struct timeval tv_start;
82 add_files_callback_t callback;
86 chomp_newline (char *str)
88 if (str && str[strlen(str)-1] == '\n')
89 str[strlen(str)-1] = '\0';
92 /* Compute the number of seconds elapsed from start to end. */
94 tv_elapsed (struct timeval start, struct timeval end)
96 return ((end.tv_sec - start.tv_sec) +
97 (end.tv_usec - start.tv_usec) / 1e6);
101 print_formatted_seconds (double seconds)
107 printf ("almost no time");
111 if (seconds > 3600) {
112 hours = (int) seconds / 3600;
113 printf ("%dh ", hours);
114 seconds -= hours * 3600;
118 minutes = (int) seconds / 60;
119 printf ("%dm ", minutes);
120 seconds -= minutes * 60;
123 printf ("%ds", (int) seconds);
127 add_files_print_progress (add_files_state_t *state)
129 struct timeval tv_now;
130 double elapsed_overall, rate_overall;
132 gettimeofday (&tv_now, NULL);
134 elapsed_overall = tv_elapsed (state->tv_start, tv_now);
135 rate_overall = (state->processed_files) / elapsed_overall;
137 printf ("Processed %d", state->processed_files);
139 if (state->total_files) {
140 printf (" of %d files (", state->total_files);
141 print_formatted_seconds ((state->total_files - state->processed_files) /
143 printf (" remaining). \r");
145 printf (" files (%d files/sec.) \r", (int) rate_overall);
151 /* Examine 'path' recursively as follows:
153 * o Ask the filesystem for the mtime of 'path' (path_mtime)
155 * o Ask the database for its timestamp of 'path' (path_dbtime)
157 * o If 'path_mtime' > 'path_dbtime'
159 * o For each regular file in 'path' with mtime newer than the
160 * 'path_dbtime' call add_message to add the file to the
163 * o For each sub-directory of path, recursively call into this
166 * o Tell the database to update its time of 'path' to 'path_mtime'
168 * The 'struct stat *st' must point to a structure that has already
169 * been initialized for 'path' by calling stat().
171 static notmuch_status_t
172 add_files_recursive (notmuch_database_t *notmuch,
175 add_files_state_t *state)
178 struct dirent *e, *entry = NULL;
182 time_t path_mtime, path_dbtime;
183 notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
184 notmuch_message_t *message = NULL, **closure;
186 /* If we're told to, we bail out on encountering a read-only
187 * directory, (with this being a clear clue from the user to
188 * Notmuch that new mail won't be arriving there and we need not
190 if (state->ignore_read_only_directories &&
191 (st->st_mode & S_IWUSR) == 0)
193 state->saw_read_only_directory = TRUE;
197 path_mtime = st->st_mtime;
199 path_dbtime = notmuch_database_get_timestamp (notmuch, path);
201 dir = opendir (path);
203 fprintf (stderr, "Error opening directory %s: %s\n",
204 path, strerror (errno));
205 ret = NOTMUCH_STATUS_FILE_ERROR;
209 entry_length = offsetof (struct dirent, d_name) +
210 pathconf (path, _PC_NAME_MAX) + 1;
211 entry = malloc (entry_length);
214 err = readdir_r (dir, entry, &e);
216 fprintf (stderr, "Error reading directory: %s\n",
218 ret = NOTMUCH_STATUS_FILE_ERROR;
225 /* If this directory hasn't been modified since the last
226 * add_files, then we only need to look further for
227 * sub-directories. */
228 if (path_mtime <= path_dbtime && entry->d_type != DT_DIR)
231 /* Ignore special directories to avoid infinite recursion.
232 * Also ignore the .notmuch directory.
234 /* XXX: Eventually we'll want more sophistication to let the
235 * user specify files to be ignored. */
236 if (strcmp (entry->d_name, ".") == 0 ||
237 strcmp (entry->d_name, "..") == 0 ||
238 strcmp (entry->d_name, ".notmuch") ==0)
243 next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
245 if (stat (next, st)) {
246 fprintf (stderr, "Error reading %s: %s\n",
247 next, strerror (errno));
248 ret = NOTMUCH_STATUS_FILE_ERROR;
252 if (S_ISREG (st->st_mode)) {
253 /* If the file hasn't been modified since the last
254 * add_files, then we need not look at it. */
255 if (st->st_mtime > path_dbtime) {
256 state->processed_files++;
263 status = notmuch_database_add_message (notmuch, next, closure);
266 case NOTMUCH_STATUS_SUCCESS:
267 state->added_messages++;
269 (state->callback) (message);
271 /* Non-fatal issues (go on to next file) */
272 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
273 /* Stay silent on this one. */
275 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
276 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
279 /* Fatal issues. Don't process anymore. */
280 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
281 case NOTMUCH_STATUS_OUT_OF_MEMORY:
282 fprintf (stderr, "Error: %s. Halting processing.\n",
283 notmuch_status_to_string (status));
287 case NOTMUCH_STATUS_FILE_ERROR:
288 case NOTMUCH_STATUS_NULL_POINTER:
289 case NOTMUCH_STATUS_TAG_TOO_LONG:
290 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
291 case NOTMUCH_STATUS_LAST_STATUS:
292 INTERNAL_ERROR ("add_message returned unexpected value: %d", status);
297 notmuch_message_destroy (message);
301 if (state->processed_files % 1000 == 0)
302 add_files_print_progress (state);
304 } else if (S_ISDIR (st->st_mode)) {
305 status = add_files_recursive (notmuch, next, st, state);
306 if (status && ret == NOTMUCH_STATUS_SUCCESS)
314 status = notmuch_database_set_timestamp (notmuch, path, path_mtime);
315 if (status && ret == NOTMUCH_STATUS_SUCCESS)
329 /* This is the top-level entry point for add_files. It does a couple
330 * of error checks, and then calls into the recursive function,
331 * (avoiding the repeating of these error checks at every
332 * level---which would be useless becaues we already do a stat() at
333 * the level above). */
334 static notmuch_status_t
335 add_files (notmuch_database_t *notmuch,
337 add_files_state_t *state)
341 if (stat (path, &st)) {
342 fprintf (stderr, "Error reading directory %s: %s\n",
343 path, strerror (errno));
344 return NOTMUCH_STATUS_FILE_ERROR;
347 if (! S_ISDIR (st.st_mode)) {
348 fprintf (stderr, "Error: %s is not a directory.\n", path);
349 return NOTMUCH_STATUS_FILE_ERROR;
352 return add_files_recursive (notmuch, path, &st, state);
355 /* Recursively count all regular files in path and all sub-direcotries
356 * of path. The result is added to *count (which should be
357 * initialized to zero by the top-level caller before calling
360 count_files (const char *path, int *count)
363 struct dirent *e, *entry = NULL;
369 dir = opendir (path);
372 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
373 path, strerror (errno));
377 entry_length = offsetof (struct dirent, d_name) +
378 pathconf (path, _PC_NAME_MAX) + 1;
379 entry = malloc (entry_length);
382 err = readdir_r (dir, entry, &e);
384 fprintf (stderr, "Error reading directory: %s\n",
393 /* Ignore special directories to avoid infinite recursion.
394 * Also ignore the .notmuch directory.
396 /* XXX: Eventually we'll want more sophistication to let the
397 * user specify files to be ignored. */
398 if (strcmp (entry->d_name, ".") == 0 ||
399 strcmp (entry->d_name, "..") == 0 ||
400 strcmp (entry->d_name, ".notmuch") == 0)
405 if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
407 fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
408 path, entry->d_name);
414 if (S_ISREG (st.st_mode)) {
416 if (*count % 1000 == 0) {
417 printf ("Found %d files so far.\r", *count);
420 } else if (S_ISDIR (st.st_mode)) {
421 count_files (next, count);
435 setup_command (unused (int argc), unused (char *argv[]))
437 notmuch_database_t *notmuch = NULL;
438 char *default_path, *mail_directory = NULL;
441 add_files_state_t add_files_state;
443 struct timeval tv_now;
444 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
446 printf ("Welcome to notmuch!\n\n");
448 printf ("The goal of notmuch is to help you manage and search your collection of\n"
449 "email, and to efficiently keep up with the flow of email as it comes in.\n\n");
451 printf ("Notmuch needs to know the top-level directory of your email archive,\n"
452 "(where you already have mail stored and where messages will be delivered\n"
453 "in the future). This directory can contain any number of sub-directories\n"
454 "and primarily just files with indvidual email messages (eg. maildir or mh\n"
455 "archives are perfect). If there are other, non-email files (such as\n"
456 "indexes maintained by other email programs) then notmuch will do its\n"
457 "best to detect those and ignore them.\n\n");
459 printf ("Mail storage that uses mbox format, (where one mbox file contains many\n"
460 "messages), will not work with notmuch. If that's how your mail is currently\n"
461 "stored, we recommend you first convert it to maildir format with a utility\n"
462 "such as mb2md. In that case, press Control-C now and run notmuch again\n"
463 "once the conversion is complete.\n\n");
466 default_path = notmuch_database_default_path ();
467 printf ("Top-level mail directory [%s]: ", default_path);
470 getline (&mail_directory, &line_size, stdin);
471 chomp_newline (mail_directory);
475 if (mail_directory == NULL || strlen (mail_directory) == 0) {
477 free (mail_directory);
478 mail_directory = default_path;
480 /* XXX: Instead of telling the user to use an environment
481 * variable here, we should really be writing out a configuration
482 * file and loading that on the next run. */
483 if (strcmp (mail_directory, default_path)) {
484 printf ("Note: Since you are not using the default path, you will want to set\n"
485 "the NOTMUCH_BASE environment variable to %s so that\n"
486 "future calls to notmuch commands will know where to find your mail.\n",
488 printf ("For example, if you are using bash for your shell, add:\n\n");
489 printf ("\texport NOTMUCH_BASE=%s\n\n", mail_directory);
490 printf ("to your ~/.bashrc file.\n\n");
495 notmuch = notmuch_database_create (mail_directory);
496 if (notmuch == NULL) {
497 fprintf (stderr, "Failed to create new notmuch database at %s\n",
499 ret = NOTMUCH_STATUS_FILE_ERROR;
503 printf ("OK. Let's take a look at the mail we can find in the directory\n");
504 printf ("%s ...\n", mail_directory);
507 count_files (mail_directory, &count);
509 printf ("Found %d total files. That's not much mail.\n\n", count);
511 printf ("Next, we'll inspect the messages and create a database of threads:\n");
513 add_files_state.ignore_read_only_directories = FALSE;
514 add_files_state.saw_read_only_directory = FALSE;
515 add_files_state.total_files = count;
516 add_files_state.processed_files = 0;
517 add_files_state.added_messages = 0;
518 add_files_state.callback = NULL;
519 gettimeofday (&add_files_state.tv_start, NULL);
521 ret = add_files (notmuch, mail_directory, &add_files_state);
523 gettimeofday (&tv_now, NULL);
524 elapsed = tv_elapsed (add_files_state.tv_start,
526 printf ("Processed %d %s in ", add_files_state.processed_files,
527 add_files_state.processed_files == 1 ?
528 "file" : "total files");
529 print_formatted_seconds (elapsed);
531 printf (" (%d files/sec.). \n",
532 (int) (add_files_state.processed_files / elapsed));
536 if (add_files_state.added_messages) {
537 printf ("Added %d %s to the database.\n\n",
538 add_files_state.added_messages,
539 add_files_state.added_messages == 1 ?
540 "message" : "unique messages");
543 printf ("When new mail is delivered to %s in the future,\n"
544 "run \"notmuch new\" to add it to the database.\n\n",
548 printf ("Note: At least one error was encountered: %s\n",
549 notmuch_status_to_string (ret));
554 free (mail_directory);
556 notmuch_database_close (notmuch);
562 tag_inbox_and_unread (notmuch_message_t *message)
564 notmuch_message_add_tag (message, "inbox");
565 notmuch_message_add_tag (message, "unread");
569 new_command (unused (int argc), unused (char *argv[]))
571 notmuch_database_t *notmuch;
572 const char *mail_directory;
573 add_files_state_t add_files_state;
575 struct timeval tv_now;
578 notmuch = notmuch_database_open (NULL);
579 if (notmuch == NULL) {
584 mail_directory = notmuch_database_get_path (notmuch);
586 add_files_state.ignore_read_only_directories = TRUE;
587 add_files_state.saw_read_only_directory = FALSE;
588 add_files_state.total_files = 0;
589 add_files_state.processed_files = 0;
590 add_files_state.added_messages = 0;
591 add_files_state.callback = tag_inbox_and_unread;
592 gettimeofday (&add_files_state.tv_start, NULL);
594 ret = add_files (notmuch, mail_directory, &add_files_state);
596 gettimeofday (&tv_now, NULL);
597 elapsed = tv_elapsed (add_files_state.tv_start,
599 if (add_files_state.processed_files) {
600 printf ("Processed %d %s in ", add_files_state.processed_files,
601 add_files_state.processed_files == 1 ?
602 "file" : "total files");
603 print_formatted_seconds (elapsed);
605 printf (" (%d files/sec.). \n",
606 (int) (add_files_state.processed_files / elapsed));
611 if (add_files_state.added_messages) {
612 printf ("Added %d new %s to the database (not much, really).\n",
613 add_files_state.added_messages,
614 add_files_state.added_messages == 1 ?
615 "message" : "messages");
617 printf ("No new mail---and that's not much.\n");
620 if (elapsed > 1 && ! add_files_state.saw_read_only_directory) {
621 printf ("\nTip: If you have any sub-directories that are archives (that is,\n"
622 "they will never receive new mail), marking these directores as\n"
623 "read-only (chmod u-w /path/to/dir) will make \"notmuch new\"\n"
624 "much more efficient (it won't even look in those directories).\n");
628 printf ("\nNote: At least one error was encountered: %s\n",
629 notmuch_status_to_string (ret));
634 notmuch_database_close (notmuch);
639 /* Construct a single query string from the passed arguments, using
640 * 'ctx' as the talloc owner for all allocations.
642 * Currently, the arguments are just connected with space characters,
643 * but we might do more processing in the future, (such as inserting
644 * any AND operators needed to work around Xapian QueryParser bugs).
646 * This function returns NULL in case of insufficient memory.
649 query_string_from_args (void *ctx, int argc, char *argv[])
654 query_string = talloc_strdup (ctx, "");
655 if (query_string == NULL)
658 for (i = 0; i < argc; i++) {
660 query_string = talloc_strdup_append (query_string, " ");
661 if (query_string == NULL)
665 query_string = talloc_strdup_append (query_string, argv[i]);
666 if (query_string == NULL)
674 search_command (int argc, char *argv[])
676 void *local = talloc_new (NULL);
677 notmuch_database_t *notmuch = NULL;
678 notmuch_query_t *query;
679 notmuch_thread_results_t *results;
680 notmuch_thread_t *thread;
681 notmuch_tags_t *tags;
683 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
685 notmuch = notmuch_database_open (NULL);
686 if (notmuch == NULL) {
691 query_str = query_string_from_args (local, argc, argv);
693 query = notmuch_query_create (notmuch, query_str);
695 fprintf (stderr, "Out of memory\n");
700 for (results = notmuch_query_search_threads (query);
701 notmuch_thread_results_has_more (results);
702 notmuch_thread_results_advance (results))
706 thread = notmuch_thread_results_get (results);
709 notmuch_thread_get_thread_id (thread),
710 notmuch_thread_get_subject (thread));
713 for (tags = notmuch_thread_get_tags (thread);
714 notmuch_tags_has_more (tags);
715 notmuch_tags_advance (tags))
719 printf ("%s", notmuch_tags_get (tags));
724 notmuch_thread_destroy (thread);
727 notmuch_query_destroy (query);
731 notmuch_database_close (notmuch);
738 show_command (unused (int argc), unused (char *argv[]))
740 fprintf (stderr, "Error: show is not implemented yet.\n");
745 tag_command (unused (int argc), unused (char *argv[]))
748 int *add_tags, *remove_tags;
749 int add_tags_count = 0;
750 int remove_tags_count = 0;
752 notmuch_database_t *notmuch = NULL;
753 notmuch_query_t *query;
754 notmuch_message_results_t *results;
755 notmuch_message_t *message;
759 local = talloc_new (NULL);
765 add_tags = talloc_size (local, argc * sizeof (int));
766 if (add_tags == NULL) {
771 remove_tags = talloc_size (local, argc * sizeof (int));
772 if (remove_tags == NULL) {
777 for (i = 0; i < argc; i++) {
778 if (strcmp (argv[i], "--") == 0) {
782 if (argv[i][0] == '+') {
783 add_tags[add_tags_count++] = i;
784 } else if (argv[i][0] == '-') {
785 remove_tags[remove_tags_count++] = i;
791 if (add_tags_count == 0 && remove_tags_count == 0) {
792 fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
798 fprintf (stderr, "Error: 'notmuch tag' requires at least one search term.\n");
803 notmuch = notmuch_database_open (NULL);
804 if (notmuch == NULL) {
809 query_string = query_string_from_args (local, argc - i, &argv[i]);
811 query = notmuch_query_create (notmuch, query_string);
813 fprintf (stderr, "Out of memory.\n");
818 for (results = notmuch_query_search_messages (query);
819 notmuch_message_results_has_more (results);
820 notmuch_message_results_advance (results))
822 message = notmuch_message_results_get (results);
824 notmuch_message_freeze (message);
826 for (i = 0; i < remove_tags_count; i++)
827 notmuch_message_remove_tag (message,
828 argv[remove_tags[i]] + 1);
830 for (i = 0; i < add_tags_count; i++)
831 notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
833 notmuch_message_thaw (message);
835 notmuch_message_destroy (message);
838 notmuch_query_destroy (query);
842 notmuch_database_close (notmuch);
850 dump_command (int argc, char *argv[])
853 notmuch_database_t *notmuch = NULL;
854 notmuch_query_t *query;
855 notmuch_message_results_t *results;
856 notmuch_message_t *message;
857 notmuch_tags_t *tags;
861 output = fopen (argv[0], "w");
862 if (output == NULL) {
863 fprintf (stderr, "Error opening %s for writing: %s\n",
864 argv[0], strerror (errno));
872 notmuch = notmuch_database_open (NULL);
873 if (notmuch == NULL) {
878 query = notmuch_query_create (notmuch, "");
880 fprintf (stderr, "Out of memory\n");
885 notmuch_query_set_sort (query, NOTMUCH_SORT_MESSAGE_ID);
887 for (results = notmuch_query_search_messages (query);
888 notmuch_message_results_has_more (results);
889 notmuch_message_results_advance (results))
892 message = notmuch_message_results_get (results);
895 "%s (", notmuch_message_get_message_id (message));
897 for (tags = notmuch_message_get_tags (message);
898 notmuch_tags_has_more (tags);
899 notmuch_tags_advance (tags))
902 fprintf (output, " ");
904 fprintf (output, "%s", notmuch_tags_get (tags));
909 fprintf (output, ")\n");
911 notmuch_message_destroy (message);
914 notmuch_query_destroy (query);
918 notmuch_database_close (notmuch);
919 if (output && output != stdout)
926 restore_command (int argc, char *argv[])
929 notmuch_database_t *notmuch = NULL;
938 input = fopen (argv[0], "r");
940 fprintf (stderr, "Error opening %s for reading: %s\n",
941 argv[0], strerror (errno));
946 printf ("No filename given. Reading dump from stdin.\n");
950 notmuch = notmuch_database_open (NULL);
951 if (notmuch == NULL) {
956 /* Dump output is one line per message. We match a sequence of
957 * non-space characters for the message-id, then one or more
958 * spaces, then a list of space-separated tags as a sequence of
959 * characters within literal '(' and ')'. */
961 "^([^ ]+) \\(([^)]*)\\)$",
964 while ((line_len = getline (&line, &line_size, input)) != -1) {
966 char *message_id, *tags, *tag, *next;
967 notmuch_message_t *message;
968 notmuch_status_t status;
970 chomp_newline (line);
972 rerr = xregexec (®ex, line, 3, match, 0);
973 if (rerr == REG_NOMATCH)
975 fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
980 message_id = xstrndup (line + match[1].rm_so,
981 match[1].rm_eo - match[1].rm_so);
982 tags = xstrndup (line + match[2].rm_so,
983 match[2].rm_eo - match[2].rm_so);
987 message = notmuch_database_find_message (notmuch, message_id);
988 if (message == NULL) {
989 fprintf (stderr, "Warning: Cannot apply tags to missing message: %s\n",
994 notmuch_message_freeze (message);
996 notmuch_message_remove_all_tags (message);
1000 tag = strsep (&next, " ");
1003 status = notmuch_message_add_tag (message, tag);
1006 "Error applying tag %s to message %s:\n",
1008 fprintf (stderr, "%s\n",
1009 notmuch_status_to_string (status));
1013 notmuch_message_thaw (message);
1014 notmuch_message_destroy (message);
1027 notmuch_database_close (notmuch);
1028 if (input && input != stdin)
1035 help_command (int argc, char *argv[]);
1037 command_t commands[] = {
1038 { "setup", setup_command,
1039 "Interactively setup notmuch for first use.",
1040 "\t\tThe setup command is the first command you will run in order\n"
1041 "\t\tto start using notmuch. It will prompt you for the directory\n"
1042 "\t\tcontaining your email archives, and will then proceed to build\n"
1043 "\t\ta database to allow fast searching of that mail.\n\n"
1044 "\t\tInvoking notmuch with no command argument will run setup if\n"
1045 "\t\tthe setup command has not previously been completed." },
1046 { "new", new_command,
1047 "Find and import any new messages.",
1048 "\t\tScans all sub-directories of the database, adding new messages\n"
1049 "\t\tthat are found. Each new message will be tagged as both\n"
1050 "\t\t\"inbox\" and \"unread\".\n"
1052 "\t\tNote: \"notmuch new\" will skip any read-only directories,\n"
1053 "\t\tso you can use that to mark tdirectories that will not\n"
1054 "\t\treceive any new mail (and make \"notmuch new\" faster)." },
1055 { "search", search_command,
1056 "<search-term> [...]\n\n"
1057 "\t\tSearch for threads matching the given search terms.",
1058 "\t\tNote that the individual mail messages will be matched\n"
1059 "\t\tagainst the search terms, but the results will be the\n"
1060 "\t\tthreads containing the matched messages.\n\n"
1061 "\t\tCurrently, the supported search terms are as follows, (where\n"
1062 "\t\t<brackets> indicate user-supplied values):\n\n"
1064 "\t\t\tid:<message-id>\n"
1065 "\t\t\tthread:<thread-id>\n\n"
1066 "\t\tValid tag values include \"inbox\" and \"unread\" by default\n"
1067 "\t\tfor new messages added by \"notmuch new\" as well as any other\n"
1068 "\t\ttag values added manually with \"notmuch tag\".\n\n"
1069 "\t\tMessage ID values are the literal contents of the Message-ID:\n"
1070 "\t\theader of email messages, but without the '<','>' delimiters.\n\n"
1071 "\t\tThread ID values are generated internally by notmuch but can\n"
1072 "\t\tbe seen in the output of \"notmuch search\" for example.\n\n"
1073 "\t\tIn addition to individual terms, multiple terms can be\n"
1074 "\t\tcombined with Boolean operators (\"and\", \"or\", \"not\", etc.).\n"
1075 "\t\tEach term in the query will be implicitly connected by a\n"
1076 "\t\tlogical AND if no explicit operator is provided.\n\n"
1077 "\t\tParentheses can also be used to control the combination of\n"
1078 "\t\tthe Boolean operators, but will have to be protected from\n"
1079 "\t\tinterpretation by the shell, (such as by putting quotation\n"
1080 "\t\tmarks around any parenthesized expression)." },
1081 { "show", show_command,
1083 "\t\tNote: The \"notmuch show\" command is not implemented yet.\n\n"
1084 "\t\tShow the thread with the given thread ID (see 'search').",
1085 "\t\tThread ID values are given as the first column in the\n"
1086 "\t\toutput of the \"notmuch search\" command. These are the\n"
1087 "\t\trandom-looking strings of 32 characters." },
1088 { "tag", tag_command,
1089 "+<tag>|-<tag> [...] [--] <search-term> [...]\n\n"
1090 "\t\tAdd/remove tags for all messages matching the search terms.",
1091 "\t\tThe search terms are handled exactly as in 'search' so one\n"
1092 "\t\tcan use that command first to see what will be modified.\n\n"
1093 "\t\tTags prefixed by '+' are added while those prefixed by '-' are\n"
1094 "\t\tremoved. For each message, tag removal is before tag addition.\n\n"
1095 "\t\tThe beginning of <search-terms> is recognized by the first\n"
1096 "\t\targument that begins with neither '+' nor '-'. Support for\n"
1097 "\t\tan initial search term beginning with '+' or '-' is provided\n"
1098 "\t\tby allowing the user to specify a \"--\" argument to separate\n"
1099 "\t\tthe tags from the search terms.\n\n"
1100 "\t\tNote: If you run \"notmuch new\" between reading a thread with\n"
1101 "\t\t\"notmuch show\" and removing the \"inbox\" tag for that thread\n"
1102 "\t\twith \"notmuch tag\" then you create the possibility of moving\n"
1103 "\t\tsome messages from that thread out of your inbox without ever\n"
1104 "\t\treading them. The easiest way to avoid this problem is to not\n"
1105 "\t\trun \"notmuch new\" between reading and removing tags." },
1106 { "dump", dump_command,
1108 "\t\tCreate a plain-text dump of the tags for each message.",
1109 "\t\tOutput is to the given filename, if any, or to stdout.\n"
1110 "\t\tThese tags are the only data in the notmuch database\n"
1111 "\t\tthat can't be recreated from the messages themselves.\n"
1112 "\t\tThe output of notmuch dump is therefore the only\n"
1113 "\t\tcritical thing to backup (and much more friendly to\n"
1114 "\t\tincremental backup than the native database files.)" },
1115 { "restore", restore_command,
1117 "\t\tRestore the tags from the given dump file (see 'dump').",
1118 "\t\tNote: The dump file format is specifically chosen to be\n"
1119 "\t\tcompatible with the format of files produced by sup-dump.\n"
1120 "\t\tSo if you've previously been using sup for mail, then the\n"
1121 "\t\t\"notmuch restore\" command provides you a way to import\n"
1122 "\t\tall of your tags (or labels as sup calls them)." },
1123 { "help", help_command,
1125 "\t\tThis message, or more detailed help for the named command.",
1126 "\t\tExcept in this case, where there's not much more detailed\n"
1127 "\t\thelp available." }
1136 fprintf (stderr, "Usage: notmuch <command> [args...]\n");
1137 fprintf (stderr, "\n");
1138 fprintf (stderr, "Where <command> and [args...] are as follows:\n");
1139 fprintf (stderr, "\n");
1141 for (i = 0; i < ARRAY_SIZE (commands); i++) {
1142 command = &commands[i];
1144 fprintf (stderr, "\t%s\t%s\n\n", command->name, command->summary);
1147 fprintf (stderr, "Use \"notmuch help <command>\" for more details on each command.\n\n");
1151 help_command (int argc, char *argv[])
1157 fprintf (stderr, "The notmuch mail system.\n\n");
1162 for (i = 0; i < ARRAY_SIZE (commands); i++) {
1163 command = &commands[i];
1165 if (strcmp (argv[0], command->name) == 0) {
1166 fprintf (stderr, "Help for \"notmuch %s\":\n\n", argv[0]);
1167 fprintf (stderr, "\t%s\t%s\n\n%s\n\n", command->name,
1168 command->summary, command->documentation);
1174 "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
1180 main (int argc, char *argv[])
1186 return setup_command (0, NULL);
1188 for (i = 0; i < ARRAY_SIZE (commands); i++) {
1189 command = &commands[i];
1191 if (strcmp (argv[1], command->name) == 0)
1192 return (command->function) (argc - 2, &argv[2]);
1195 /* Don't complain about "help" being an unknown command when we're
1196 about to provide exactly what's wanted anyway. */
1197 fprintf (stderr, "Error: Unknown command '%s'\n\n", argv[1]);