]> git.notmuchmail.org Git - notmuch/blob - notmuch.c
notmuch show: Print tags for each message.
[notmuch] / notmuch.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE /* for getline */
23 #endif
24 #include <stdio.h>
25
26 #include <gmime/gmime.h>
27
28 #include "notmuch.h"
29
30 /* This is separate from notmuch-private.h because we're trying to
31  * keep notmuch.c from looking into any internals, (which helps us
32  * develop notmuch.h into a plausible library interface).
33  */
34 #include "xutil.h"
35
36 #include <stddef.h>
37 #include <string.h>
38 #include <sys/stat.h>
39 #include <sys/time.h>
40 #include <unistd.h>
41 #include <dirent.h>
42 #include <errno.h>
43 #include <signal.h>
44
45 #include <talloc.h>
46
47 #define unused(x) x __attribute__ ((unused))
48
49 /* There's no point in continuing when we've detected that we've done
50  * something wrong internally (as opposed to the user passing in a
51  * bogus value).
52  *
53  * Note that __location__ comes from talloc.h.
54  */
55 #define INTERNAL_ERROR(format, ...)                     \
56     do {                                                \
57         fprintf(stderr,                                 \
58                 "Internal error: " format " (%s)\n",    \
59                 ##__VA_ARGS__, __location__);           \
60         exit (1);                                       \
61     } while (0)
62
63 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
64
65 typedef int (*command_function_t) (void *ctx, int argc, char *argv[]);
66
67 typedef struct command {
68     const char *name;
69     command_function_t function;
70     const char *summary;
71     const char *documentation;
72 } command_t;
73
74 typedef void (*add_files_callback_t) (notmuch_message_t *message);
75
76 typedef struct {
77     int ignore_read_only_directories;
78     int saw_read_only_directory;
79
80     int total_files;
81     int processed_files;
82     int added_messages;
83     struct timeval tv_start;
84
85     add_files_callback_t callback;
86 } add_files_state_t;
87
88 static void
89 chomp_newline (char *str)
90 {
91     if (str && str[strlen(str)-1] == '\n')
92         str[strlen(str)-1] = '\0';
93 }
94
95 /* Compute the number of seconds elapsed from start to end. */
96 static double
97 tv_elapsed (struct timeval start, struct timeval end)
98 {
99     return ((end.tv_sec - start.tv_sec) +
100             (end.tv_usec - start.tv_usec) / 1e6);
101 }
102
103 static void
104 print_formatted_seconds (double seconds)
105 {
106     int hours;
107     int minutes;
108
109     if (seconds < 1) {
110         printf ("almost no time");
111         return;
112     }
113
114     if (seconds > 3600) {
115         hours = (int) seconds / 3600;
116         printf ("%dh ", hours);
117         seconds -= hours * 3600;
118     }
119
120     if (seconds > 60) {
121         minutes = (int) seconds / 60;
122         printf ("%dm ", minutes);
123         seconds -= minutes * 60;
124     }
125
126     printf ("%ds", (int) seconds);
127 }
128
129 static volatile sig_atomic_t do_add_files_print_progress = 0;
130
131 static void
132 handle_sigalrm (unused (int signal))
133 {
134     do_add_files_print_progress = 1;
135 }
136
137 static void
138 add_files_print_progress (add_files_state_t *state)
139 {
140     struct timeval tv_now;
141     double elapsed_overall, rate_overall;
142
143     gettimeofday (&tv_now, NULL);
144
145     elapsed_overall = tv_elapsed (state->tv_start, tv_now);
146     rate_overall = (state->processed_files) / elapsed_overall;
147
148     printf ("Processed %d", state->processed_files);
149
150     if (state->total_files) {
151         printf (" of %d files (", state->total_files);
152         print_formatted_seconds ((state->total_files - state->processed_files) /
153                                  rate_overall);
154         printf (" remaining).      \r");
155     } else {
156         printf (" files (%d files/sec.)    \r", (int) rate_overall);
157     }
158
159     fflush (stdout);
160 }
161
162 /* Examine 'path' recursively as follows:
163  *
164  *   o Ask the filesystem for the mtime of 'path' (path_mtime)
165  *
166  *   o Ask the database for its timestamp of 'path' (path_dbtime)
167  *
168  *   o If 'path_mtime' > 'path_dbtime'
169  *
170  *       o For each regular file in 'path' with mtime newer than the
171  *         'path_dbtime' call add_message to add the file to the
172  *         database.
173  *
174  *       o For each sub-directory of path, recursively call into this
175  *         same function.
176  *
177  *   o Tell the database to update its time of 'path' to 'path_mtime'
178  *
179  * The 'struct stat *st' must point to a structure that has already
180  * been initialized for 'path' by calling stat().
181  */
182 static notmuch_status_t
183 add_files_recursive (notmuch_database_t *notmuch,
184                      const char *path,
185                      struct stat *st,
186                      add_files_state_t *state)
187 {
188     DIR *dir = NULL;
189     struct dirent *e, *entry = NULL;
190     int entry_length;
191     int err;
192     char *next = NULL;
193     time_t path_mtime, path_dbtime;
194     notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
195     notmuch_message_t *message = NULL, **closure;
196
197     /* If we're told to, we bail out on encountering a read-only
198      * directory, (with this being a clear clue from the user to
199      * Notmuch that new mail won't be arriving there and we need not
200      * look. */
201     if (state->ignore_read_only_directories &&
202         (st->st_mode & S_IWUSR) == 0)
203     {
204         state->saw_read_only_directory = TRUE;
205         goto DONE;
206     }
207
208     path_mtime = st->st_mtime;
209
210     path_dbtime = notmuch_database_get_timestamp (notmuch, path);
211
212     dir = opendir (path);
213     if (dir == NULL) {
214         fprintf (stderr, "Error opening directory %s: %s\n",
215                  path, strerror (errno));
216         ret = NOTMUCH_STATUS_FILE_ERROR;
217         goto DONE;
218     }
219
220     entry_length = offsetof (struct dirent, d_name) +
221         pathconf (path, _PC_NAME_MAX) + 1;
222     entry = malloc (entry_length);
223
224     while (1) {
225         err = readdir_r (dir, entry, &e);
226         if (err) {
227             fprintf (stderr, "Error reading directory: %s\n",
228                      strerror (errno));
229             ret = NOTMUCH_STATUS_FILE_ERROR;
230             goto DONE;
231         }
232
233         if (e == NULL)
234             break;
235
236         /* If this directory hasn't been modified since the last
237          * add_files, then we only need to look further for
238          * sub-directories. */
239         if (path_mtime <= path_dbtime && entry->d_type != DT_DIR)
240             continue;
241
242         /* Ignore special directories to avoid infinite recursion.
243          * Also ignore the .notmuch directory.
244          */
245         /* XXX: Eventually we'll want more sophistication to let the
246          * user specify files to be ignored. */
247         if (strcmp (entry->d_name, ".") == 0 ||
248             strcmp (entry->d_name, "..") == 0 ||
249             strcmp (entry->d_name, ".notmuch") ==0)
250         {
251             continue;
252         }
253
254         next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
255
256         if (stat (next, st)) {
257             fprintf (stderr, "Error reading %s: %s\n",
258                      next, strerror (errno));
259             ret = NOTMUCH_STATUS_FILE_ERROR;
260             continue;
261         }
262
263         if (S_ISREG (st->st_mode)) {
264             /* If the file hasn't been modified since the last
265              * add_files, then we need not look at it. */
266             if (st->st_mtime > path_dbtime) {
267                 state->processed_files++;
268
269                 if (state->callback)
270                     closure = &message;
271                 else
272                     closure = NULL;
273
274                 status = notmuch_database_add_message (notmuch, next, closure);
275                 switch (status) {
276                     /* success */
277                     case NOTMUCH_STATUS_SUCCESS:
278                         state->added_messages++;
279                         if (state->callback)
280                             (state->callback) (message);
281                         break;
282                     /* Non-fatal issues (go on to next file) */
283                     case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
284                         /* Stay silent on this one. */
285                         break;
286                     case NOTMUCH_STATUS_FILE_NOT_EMAIL:
287                         fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
288                                  next);
289                         break;
290                     /* Fatal issues. Don't process anymore. */
291                     case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
292                     case NOTMUCH_STATUS_OUT_OF_MEMORY:
293                         fprintf (stderr, "Error: %s. Halting processing.\n",
294                                  notmuch_status_to_string (status));
295                         ret = status;
296                         goto DONE;
297                     default:
298                     case NOTMUCH_STATUS_FILE_ERROR:
299                     case NOTMUCH_STATUS_NULL_POINTER:
300                     case NOTMUCH_STATUS_TAG_TOO_LONG:
301                     case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
302                     case NOTMUCH_STATUS_LAST_STATUS:
303                         INTERNAL_ERROR ("add_message returned unexpected value: %d",  status);
304                         goto DONE;
305                 }
306
307                 if (message) {
308                     notmuch_message_destroy (message);
309                     message = NULL;
310                 }
311
312                 if (do_add_files_print_progress) {
313                     do_add_files_print_progress = 0;
314                     add_files_print_progress (state);
315                 }
316             }
317         } else if (S_ISDIR (st->st_mode)) {
318             status = add_files_recursive (notmuch, next, st, state);
319             if (status && ret == NOTMUCH_STATUS_SUCCESS)
320                 ret = status;
321         }
322
323         talloc_free (next);
324         next = NULL;
325     }
326
327     status = notmuch_database_set_timestamp (notmuch, path, path_mtime);
328     if (status && ret == NOTMUCH_STATUS_SUCCESS)
329         ret = status;
330
331   DONE:
332     if (next)
333         talloc_free (next);
334     if (entry)
335         free (entry);
336     if (dir)
337         closedir (dir);
338
339     return ret;
340 }
341
342 /* This is the top-level entry point for add_files. It does a couple
343  * of error checks, sets up the progress-printing timer and then calls
344  * into the recursive function. */
345 static notmuch_status_t
346 add_files (notmuch_database_t *notmuch,
347            const char *path,
348            add_files_state_t *state)
349 {
350     struct stat st;
351     notmuch_status_t status;
352     struct sigaction action;
353     struct itimerval timerval;
354
355     if (stat (path, &st)) {
356         fprintf (stderr, "Error reading directory %s: %s\n",
357                  path, strerror (errno));
358         return NOTMUCH_STATUS_FILE_ERROR;
359     }
360
361     if (! S_ISDIR (st.st_mode)) {
362         fprintf (stderr, "Error: %s is not a directory.\n", path);
363         return NOTMUCH_STATUS_FILE_ERROR;
364     }
365
366     /* Setup our handler for SIGALRM */
367     memset (&action, 0, sizeof (struct sigaction));
368     action.sa_handler = handle_sigalrm;
369     sigemptyset (&action.sa_mask);
370     action.sa_flags = SA_RESTART;
371     sigaction (SIGALRM, &action, NULL);
372
373     /* Then start a timer to send SIGALRM once per second. */
374     timerval.it_interval.tv_sec = 1;
375     timerval.it_interval.tv_usec = 0;
376     timerval.it_value.tv_sec = 1;
377     timerval.it_value.tv_usec = 0;
378     setitimer (ITIMER_REAL, &timerval, NULL);
379
380     status = add_files_recursive (notmuch, path, &st, state);
381
382     /* Now stop the timer. */
383     timerval.it_interval.tv_sec = 0;
384     timerval.it_interval.tv_usec = 0;
385     timerval.it_value.tv_sec = 0;
386     timerval.it_value.tv_usec = 0;
387     setitimer (ITIMER_REAL, &timerval, NULL);
388
389     /* And disable the signal handler. */
390     action.sa_handler = SIG_IGN;
391     sigaction (SIGALRM, &action, NULL);
392
393     return status;
394 }
395
396 /* Recursively count all regular files in path and all sub-direcotries
397  * of path.  The result is added to *count (which should be
398  * initialized to zero by the top-level caller before calling
399  * count_files). */
400 static void
401 count_files (const char *path, int *count)
402 {
403     DIR *dir;
404     struct dirent *e, *entry = NULL;
405     int entry_length;
406     int err;
407     char *next;
408     struct stat st;
409
410     dir = opendir (path);
411
412     if (dir == NULL) {
413         fprintf (stderr, "Warning: failed to open directory %s: %s\n",
414                  path, strerror (errno));
415         goto DONE;
416     }
417
418     entry_length = offsetof (struct dirent, d_name) +
419         pathconf (path, _PC_NAME_MAX) + 1;
420     entry = malloc (entry_length);
421
422     while (1) {
423         err = readdir_r (dir, entry, &e);
424         if (err) {
425             fprintf (stderr, "Error reading directory: %s\n",
426                      strerror (errno));
427             free (entry);
428             goto DONE;
429         }
430
431         if (e == NULL)
432             break;
433
434         /* Ignore special directories to avoid infinite recursion.
435          * Also ignore the .notmuch directory.
436          */
437         /* XXX: Eventually we'll want more sophistication to let the
438          * user specify files to be ignored. */
439         if (strcmp (entry->d_name, ".") == 0 ||
440             strcmp (entry->d_name, "..") == 0 ||
441             strcmp (entry->d_name, ".notmuch") == 0)
442         {
443             continue;
444         }
445
446         if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {
447             next = NULL;
448             fprintf (stderr, "Error descending from %s to %s: Out of memory\n",
449                      path, entry->d_name);
450             continue;
451         }
452
453         stat (next, &st);
454
455         if (S_ISREG (st.st_mode)) {
456             *count = *count + 1;
457             if (*count % 1000 == 0) {
458                 printf ("Found %d files so far.\r", *count);
459                 fflush (stdout);
460             }
461         } else if (S_ISDIR (st.st_mode)) {
462             count_files (next, count);
463         }
464
465         free (next);
466     }
467
468   DONE:
469     if (entry)
470         free (entry);
471
472     closedir (dir);
473 }
474
475 static int
476 setup_command (unused (void *ctx), unused (int argc), unused (char *argv[]))
477 {
478     notmuch_database_t *notmuch = NULL;
479     char *default_path, *mail_directory = NULL;
480     size_t line_size;
481     int count;
482     add_files_state_t add_files_state;
483     double elapsed;
484     struct timeval tv_now;
485     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
486
487     printf ("Welcome to notmuch!\n\n");
488
489     printf ("The goal of notmuch is to help you manage and search your collection of\n"
490             "email, and to efficiently keep up with the flow of email as it comes in.\n\n");
491
492     printf ("Notmuch needs to know the top-level directory of your email archive,\n"
493             "(where you already have mail stored and where messages will be delivered\n"
494             "in the future). This directory can contain any number of sub-directories\n"
495             "and primarily just files with indvidual email messages (eg. maildir or mh\n"
496             "archives are perfect). If there are other, non-email files (such as\n"
497             "indexes maintained by other email programs) then notmuch will do its\n"
498             "best to detect those and ignore them.\n\n");
499
500     printf ("Mail storage that uses mbox format, (where one mbox file contains many\n"
501             "messages), will not work with notmuch. If that's how your mail is currently\n"
502             "stored, we recommend you first convert it to maildir format with a utility\n"
503             "such as mb2md. In that case, press Control-C now and run notmuch again\n"
504             "once the conversion is complete.\n\n");
505
506
507     default_path = notmuch_database_default_path ();
508     printf ("Top-level mail directory [%s]: ", default_path);
509     fflush (stdout);
510
511     getline (&mail_directory, &line_size, stdin);
512     chomp_newline (mail_directory);
513
514     printf ("\n");
515
516     if (mail_directory == NULL || strlen (mail_directory) == 0) {
517         if (mail_directory)
518             free (mail_directory);
519         mail_directory = default_path;
520     } else {
521         /* XXX: Instead of telling the user to use an environment
522          * variable here, we should really be writing out a configuration
523          * file and loading that on the next run. */
524         if (strcmp (mail_directory, default_path)) {
525             printf ("Note: Since you are not using the default path, you will want to set\n"
526                     "the NOTMUCH_BASE environment variable to %s so that\n"
527                     "future calls to notmuch commands will know where to find your mail.\n",
528                     mail_directory);
529             printf ("For example, if you are using bash for your shell, add:\n\n");
530             printf ("\texport NOTMUCH_BASE=%s\n\n", mail_directory);
531             printf ("to your ~/.bashrc file.\n\n");
532         }
533         free (default_path);
534     }
535
536     /* Coerce th directory into an absolute directory name. */
537     if (*mail_directory != '/') {
538         char *cwd, *absolute_mail_directory;
539
540         cwd = getcwd (NULL, 0);
541         if (cwd == NULL) {
542             fprintf (stderr, "Out of memory.\n");
543             exit (1);
544         }
545
546         if (asprintf (&absolute_mail_directory, "%s/%s",
547                       cwd, mail_directory) < 0)
548         {
549             fprintf (stderr, "Out of memory.\n");
550             exit (1);
551         }
552
553         free (cwd);
554         free (mail_directory);
555         mail_directory = absolute_mail_directory;
556
557         printf ("Abs: %s\n", mail_directory);
558     }
559
560     notmuch = notmuch_database_create (mail_directory);
561     if (notmuch == NULL) {
562         fprintf (stderr, "Failed to create new notmuch database at %s\n",
563                  mail_directory);
564         ret = NOTMUCH_STATUS_FILE_ERROR;
565         goto DONE;
566     }
567
568     printf ("OK. Let's take a look at the mail we can find in the directory\n");
569     printf ("%s ...\n", mail_directory);
570
571     count = 0;
572     count_files (mail_directory, &count);
573
574     printf ("Found %d total files. That's not much mail.\n\n", count);
575
576     printf ("Next, we'll inspect the messages and create a database of threads:\n");
577
578     add_files_state.ignore_read_only_directories = FALSE;
579     add_files_state.saw_read_only_directory = FALSE;
580     add_files_state.total_files = count;
581     add_files_state.processed_files = 0;
582     add_files_state.added_messages = 0;
583     add_files_state.callback = NULL;
584     gettimeofday (&add_files_state.tv_start, NULL);
585
586     ret = add_files (notmuch, mail_directory, &add_files_state);
587
588     gettimeofday (&tv_now, NULL);
589     elapsed = tv_elapsed (add_files_state.tv_start,
590                           tv_now);
591     printf ("Processed %d %s in ", add_files_state.processed_files,
592             add_files_state.processed_files == 1 ?
593             "file" : "total files");
594     print_formatted_seconds (elapsed);
595     if (elapsed > 1) {
596         printf (" (%d files/sec.).                 \n",
597                 (int) (add_files_state.processed_files / elapsed));
598     } else {
599         printf (".                    \n");
600     }
601     if (add_files_state.added_messages) {
602         printf ("Added %d %s to the database.\n\n",
603                 add_files_state.added_messages,
604                 add_files_state.added_messages == 1 ?
605                 "message" : "unique messages");
606     }
607
608     printf ("When new mail is delivered to %s in the future,\n"
609             "run \"notmuch new\" to add it to the database.\n\n",
610             mail_directory);
611
612     if (ret) {
613         printf ("Note: At least one error was encountered: %s\n",
614                 notmuch_status_to_string (ret));
615     }
616
617   DONE:
618     if (mail_directory)
619         free (mail_directory);
620     if (notmuch)
621         notmuch_database_close (notmuch);
622
623     return ret;
624 }
625
626 static void
627 tag_inbox_and_unread (notmuch_message_t *message)
628 {
629     notmuch_message_add_tag (message, "inbox");
630     notmuch_message_add_tag (message, "unread");
631 }
632
633 static int
634 new_command (unused (void *ctx), unused (int argc), unused (char *argv[]))
635 {
636     notmuch_database_t *notmuch;
637     const char *mail_directory;
638     add_files_state_t add_files_state;
639     double elapsed;
640     struct timeval tv_now;
641     int ret = 0;
642
643     notmuch = notmuch_database_open (NULL);
644     if (notmuch == NULL) {
645         ret = 1;
646         goto DONE;
647     }
648
649     mail_directory = notmuch_database_get_path (notmuch);
650
651     add_files_state.ignore_read_only_directories = TRUE;
652     add_files_state.saw_read_only_directory = FALSE;
653     add_files_state.total_files = 0;
654     add_files_state.processed_files = 0;
655     add_files_state.added_messages = 0;
656     add_files_state.callback = tag_inbox_and_unread;
657     gettimeofday (&add_files_state.tv_start, NULL);
658
659     ret = add_files (notmuch, mail_directory, &add_files_state);
660
661     gettimeofday (&tv_now, NULL);
662     elapsed = tv_elapsed (add_files_state.tv_start,
663                           tv_now);
664     if (add_files_state.processed_files) {
665         printf ("Processed %d %s in ", add_files_state.processed_files,
666                 add_files_state.processed_files == 1 ?
667                 "file" : "total files");
668         print_formatted_seconds (elapsed);
669         if (elapsed > 1) {
670             printf (" (%d files/sec.).                 \n",
671                     (int) (add_files_state.processed_files / elapsed));
672         } else {
673             printf (".                    \n");
674         }
675     }
676     if (add_files_state.added_messages) {
677         printf ("Added %d new %s to the database (not much, really).\n",
678                 add_files_state.added_messages,
679                 add_files_state.added_messages == 1 ?
680                 "message" : "messages");
681     } else {
682         printf ("No new mail---and that's not much.\n");
683     }
684
685     if (elapsed > 1 && ! add_files_state.saw_read_only_directory) {
686         printf ("\nTip: If you have any sub-directories that are archives (that is,\n"
687                 "they will never receive new mail), marking these directores as\n"
688                 "read-only (chmod u-w /path/to/dir) will make \"notmuch new\"\n"
689                 "much more efficient (it won't even look in those directories).\n");
690     }
691
692     if (ret) {
693         printf ("\nNote: At least one error was encountered: %s\n",
694                 notmuch_status_to_string (ret));
695     }
696
697   DONE:
698     if (notmuch)
699         notmuch_database_close (notmuch);
700
701     return ret;
702 }
703
704 /* Construct a single query string from the passed arguments, using
705  * 'ctx' as the talloc owner for all allocations.
706  *
707  * Currently, the arguments are just connected with space characters,
708  * but we might do more processing in the future, (such as inserting
709  * any AND operators needed to work around Xapian QueryParser bugs).
710  *
711  * This function returns NULL in case of insufficient memory.
712  */
713 static char *
714 query_string_from_args (void *ctx, int argc, char *argv[])
715 {
716     char *query_string;
717     int i;
718
719     query_string = talloc_strdup (ctx, "");
720     if (query_string == NULL)
721         return NULL;
722
723     for (i = 0; i < argc; i++) {
724         if (i != 0) {
725             query_string = talloc_strdup_append (query_string, " ");
726             if (query_string == NULL)
727                 return NULL;
728         }
729
730         query_string = talloc_strdup_append (query_string, argv[i]);
731         if (query_string == NULL)
732             return NULL;
733     }
734
735     return query_string;
736 }
737
738 /* Format a nice representation of 'time' relative to the current time.
739  *
740  * Examples include:
741  *
742  *      5 mins. ago     (For times less than 60 minutes ago)
743  *      Today 12:30     (For times >60 minutes but still today)
744  *      Yest. 12:30
745  *      Mon.  12:30     (Before yesterday but fewer than 7 days ago)
746  *      October 12      (Between 7 and 180 days ago (about 6 months))
747  *      2008-06-30      (More than 180 days ago)
748  */
749 #define MINUTE (60)
750 #define HOUR (60 * MINUTE)
751 #define DAY (24 * HOUR)
752 #define RELATIVE_DATE_MAX 20
753 static const char *
754 _format_relative_date (void *ctx, time_t then)
755 {
756     struct tm tm_now, tm_then;
757     time_t now = time(NULL);
758     time_t delta;
759     char *result;
760
761     localtime_r (&now, &tm_now);
762     localtime_r (&then, &tm_then);
763
764     result = talloc_zero_size (ctx, RELATIVE_DATE_MAX);
765     if (result == NULL)
766         return "when?";
767
768     if (then > now)
769         return "the future";
770
771     delta = now - then;
772
773     if (delta > 180 * DAY) {
774         strftime (result, RELATIVE_DATE_MAX,
775                   "%F", &tm_then); /* 2008-06-30 */
776         return result;
777     }
778
779     if (delta < 3600) {
780         snprintf (result, RELATIVE_DATE_MAX,
781                   "%d mins. ago", (int) (delta / 60));
782         return result;
783     }
784
785     if (delta <= 7 * DAY) {
786         if (tm_then.tm_wday == tm_now.tm_wday &&
787             delta < DAY)
788         {
789             strftime (result, RELATIVE_DATE_MAX,
790                       "Today %R", &tm_then); /* Today 12:30 */
791             return result;
792         } else if ((tm_now.tm_wday + 7 - tm_then.tm_wday) % 7 == 1) {
793             strftime (result, RELATIVE_DATE_MAX,
794                       "Yest. %R", &tm_then); /* Yest. 12:30 */
795             return result;
796         } else {
797             if (tm_then.tm_wday != tm_now.tm_wday) {
798                 strftime (result, RELATIVE_DATE_MAX,
799                           "%a. %R", &tm_then); /* Mon. 12:30 */
800                 return result;
801             }
802         }
803     }
804
805     strftime (result, RELATIVE_DATE_MAX,
806               "%B %d", &tm_then); /* October 12 */
807     return result;
808 }
809 #undef MINUTE
810 #undef HOUR
811 #undef DAY
812
813 static int
814 search_command (void *ctx, int argc, char *argv[])
815 {
816     void *local = talloc_new (ctx);
817     notmuch_database_t *notmuch = NULL;
818     notmuch_query_t *query;
819     notmuch_threads_t *threads;
820     notmuch_thread_t *thread;
821     notmuch_tags_t *tags;
822     char *query_str;
823     const char *relative_date;
824     time_t date;
825     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
826
827     notmuch = notmuch_database_open (NULL);
828     if (notmuch == NULL) {
829         ret = 1;
830         goto DONE;
831     }
832
833     query_str = query_string_from_args (local, argc, argv);
834
835     query = notmuch_query_create (notmuch, query_str);
836     if (query == NULL) {
837         fprintf (stderr, "Out of memory\n");
838         ret = 1;
839         goto DONE;
840     }
841
842     for (threads = notmuch_query_search_threads (query);
843          notmuch_threads_has_more (threads);
844          notmuch_threads_advance (threads))
845     {
846         int first = 1;
847
848         thread = notmuch_threads_get (threads);
849
850         date = notmuch_thread_get_oldest_date (thread);
851         relative_date = _format_relative_date (local, date);
852
853         printf ("%s %12s %s",
854                 notmuch_thread_get_thread_id (thread),
855                 relative_date,
856                 notmuch_thread_get_subject (thread));
857
858         printf (" (");
859         for (tags = notmuch_thread_get_tags (thread);
860              notmuch_tags_has_more (tags);
861              notmuch_tags_advance (tags))
862         {
863             if (! first)
864                 printf (" ");
865             printf ("%s", notmuch_tags_get (tags));
866             first = 0;
867         }
868         printf (")\n");
869
870         notmuch_thread_destroy (thread);
871     }
872
873     notmuch_query_destroy (query);
874
875   DONE:
876     if (notmuch)
877         notmuch_database_close (notmuch);
878     talloc_free (local);
879
880     return ret;
881 }
882
883 static const char *
884 _get_tags_as_string (void *ctx, notmuch_message_t *message)
885 {
886     notmuch_tags_t *tags;
887     int first = 1;
888     const char *tag;
889     char *result;
890
891     result = talloc_strdup (ctx, "");
892     if (result == NULL)
893         return NULL;
894
895     for (tags = notmuch_message_get_tags (message);
896          notmuch_tags_has_more (tags);
897          notmuch_tags_advance (tags))
898     {
899         tag = notmuch_tags_get (tags);
900
901         result = talloc_asprintf_append (result, "%s%s",
902                                          first ? "" : " ", tag);
903         first = 0;
904     }
905
906     return result;
907 }
908
909 /* Get a nice, single-line summary of message. */
910 static const char *
911 _get_one_line_summary (void *ctx, notmuch_message_t *message)
912 {
913     const char *from;
914     time_t date;
915     const char *relative_date;
916     const char *subject;
917     const char *tags;
918
919     from = notmuch_message_get_header (message, "from");
920
921     date = notmuch_message_get_date (message);
922     relative_date = _format_relative_date (ctx, date);
923
924     subject = notmuch_message_get_header (message, "subject");
925
926     tags = _get_tags_as_string (ctx, message);
927
928     return talloc_asprintf (ctx, "%s (%s) %s (%s)",
929                             from, relative_date, subject, tags);
930 }
931
932 static void
933 show_message_part (GMimeObject *part, int *part_count)
934 {
935     GMimeStream *stream;
936     GMimeDataWrapper *wrapper;
937     GMimeContentDisposition *disposition;
938     GMimeContentType *content_type;
939
940     *part_count = *part_count + 1;
941
942     if (GMIME_IS_MULTIPART (part)) {
943         GMimeMultipart *multipart = GMIME_MULTIPART (part);
944         int i;
945
946         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
947             if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
948                 /* Don't index the signature. */
949                 if (i == 1)
950                     continue;
951                 if (i > 1)
952                     fprintf (stderr, "Warning: Unexpected extra parts of mutlipart/signed. Continuing.\n");
953             }
954             show_message_part (g_mime_multipart_get_part (multipart, i),
955                                part_count);
956         }
957         return;
958     }
959
960     if (GMIME_IS_MESSAGE_PART (part)) {
961         GMimeMessage *mime_message;
962
963         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
964
965         show_message_part (g_mime_message_get_mime_part (mime_message),
966                            part_count);
967
968         return;
969     }
970
971     if (! (GMIME_IS_PART (part))) {
972         fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
973                  g_type_name (G_OBJECT_TYPE (part)));
974         return;
975     }
976
977     disposition = g_mime_object_get_content_disposition (part);
978     if (disposition &&
979         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
980     {
981         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
982         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
983
984         printf ("\fattachment{ ID: %d, Content-type: %s\n",
985                 *part_count,
986                 g_mime_content_type_to_string (content_type));
987         printf ("Attachment: %s (%s)\n", filename,
988                 g_mime_content_type_to_string (content_type));
989         printf ("\fattachment}\n");
990
991         return;
992     }
993
994     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
995
996     printf ("\fpart{ ID: %d, Content-type: %s\n",
997             *part_count,
998             g_mime_content_type_to_string (content_type));
999
1000     if (g_mime_content_type_is_type (content_type, "text", "*") &&
1001         !g_mime_content_type_is_type (content_type, "text", "html"))
1002     {
1003         stream = g_mime_stream_file_new (stdout);
1004         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
1005
1006         wrapper = g_mime_part_get_content_object (GMIME_PART (part));
1007         if (wrapper)
1008             g_mime_data_wrapper_write_to_stream (wrapper, stream);
1009     }
1010     else
1011     {
1012         printf ("Non-text part: %s\n",
1013                 g_mime_content_type_to_string (content_type));
1014     }
1015
1016     printf ("\fpart}\n");
1017
1018     g_object_unref (stream);
1019 }
1020
1021 static notmuch_status_t
1022 show_message_body (const char *filename)
1023 {
1024     GMimeStream *stream = NULL;
1025     GMimeParser *parser = NULL;
1026     GMimeMessage *mime_message = NULL;
1027     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
1028     static int initialized = 0;
1029     FILE *file = NULL;
1030     int part_count = 0;
1031
1032     if (! initialized) {
1033         g_mime_init (0);
1034         initialized = 1;
1035     }
1036
1037     file = fopen (filename, "r");
1038     if (! file) {
1039         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
1040         ret = NOTMUCH_STATUS_FILE_ERROR;
1041         goto DONE;
1042     }
1043
1044     stream = g_mime_stream_file_new (file);
1045     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
1046
1047     parser = g_mime_parser_new_with_stream (stream);
1048
1049     mime_message = g_mime_parser_construct_message (parser);
1050
1051     show_message_part (g_mime_message_get_mime_part (mime_message),
1052                        &part_count);
1053
1054   DONE:
1055     if (mime_message)
1056         g_object_unref (mime_message);
1057
1058     if (parser)
1059         g_object_unref (parser);
1060
1061     if (stream)
1062         g_object_unref (stream);
1063
1064     if (file)
1065         fclose (file);
1066
1067     return ret;
1068 }
1069
1070 static int
1071 _message_is_unread (notmuch_message_t *message)
1072 {
1073     notmuch_tags_t *tags;
1074     const char *tag;
1075
1076     for (tags = notmuch_message_get_tags (message);
1077          notmuch_tags_has_more (tags);
1078          notmuch_tags_advance (tags))
1079     {
1080         tag = notmuch_tags_get (tags);
1081
1082         if (strcmp (tag, "unread") == 0)
1083             return 1;
1084     }
1085
1086     return 0;
1087 }
1088
1089 static int
1090 show_command (void *ctx, unused (int argc), unused (char *argv[]))
1091 {
1092     void *local = talloc_new (ctx);
1093     char *query_string;
1094     notmuch_database_t *notmuch = NULL;
1095     notmuch_query_t *query = NULL;
1096     notmuch_messages_t *messages;
1097     notmuch_message_t *message;
1098     int ret = 0;
1099     int unread;
1100
1101     const char *headers[] = {
1102         "Subject", "From", "To", "Cc", "Bcc", "Date"
1103     };
1104     const char *name, *value;
1105     unsigned int i;
1106
1107     if (argc != 1) {
1108         fprintf (stderr, "Error: \"notmuch show\" requires exactly one thread-ID argument.\n");
1109         ret = 1;
1110         goto DONE;
1111     }
1112
1113     notmuch = notmuch_database_open (NULL);
1114     if (notmuch == NULL) {
1115         ret = 1;
1116         goto DONE;
1117     }
1118
1119     query_string = talloc_asprintf (local, "thread:%s", argv[0]);
1120     if (query_string == NULL) {
1121         fprintf (stderr, "Out of memory\n");
1122         ret = 1;
1123         goto DONE;
1124     }
1125
1126     query = notmuch_query_create (notmuch, query_string);
1127     if (query == NULL) {
1128         fprintf (stderr, "Out of memory\n");
1129         ret = 1;
1130         goto DONE;
1131     }
1132
1133     for (messages = notmuch_query_search_messages (query);
1134          notmuch_messages_has_more (messages);
1135          notmuch_messages_advance (messages))
1136     {
1137         message = notmuch_messages_get (messages);
1138         unread = _message_is_unread (message);
1139
1140         printf ("\fmessage{ ID: %s %s\n",
1141                 notmuch_message_get_message_id (message),
1142                 unread ? "unread" : "");
1143
1144         printf ("\fheader{\n");
1145
1146         printf ("%s\n", _get_one_line_summary (local, message));
1147
1148         for (i = 0; i < ARRAY_SIZE (headers); i++) {
1149             name = headers[i];
1150             value = notmuch_message_get_header (message, name);
1151             if (value)
1152                 printf ("%s: %s\n", name, value);
1153         }
1154
1155         printf ("\fheader}\n");
1156         printf ("\fbody{\n");
1157
1158         show_message_body (notmuch_message_get_filename (message));
1159
1160         printf ("\fbody}\n");
1161
1162         printf ("\fmessage}\n");
1163
1164         notmuch_message_destroy (message);
1165     }
1166
1167   DONE:
1168     if (local)
1169         talloc_free (local);
1170
1171     if (query)
1172         notmuch_query_destroy (query);
1173
1174     if (notmuch)
1175         notmuch_database_close (notmuch);
1176
1177     return ret;
1178 }
1179
1180 static int
1181 tag_command (void *ctx, unused (int argc), unused (char *argv[]))
1182 {
1183     void *local;
1184     int *add_tags, *remove_tags;
1185     int add_tags_count = 0;
1186     int remove_tags_count = 0;
1187     char *query_string;
1188     notmuch_database_t *notmuch = NULL;
1189     notmuch_query_t *query;
1190     notmuch_messages_t *messages;
1191     notmuch_message_t *message;
1192     int ret = 0;
1193     int i;
1194
1195     local = talloc_new (ctx);
1196     if (local == NULL) {
1197         ret = 1;
1198         goto DONE;
1199     }
1200
1201     add_tags = talloc_size (local, argc * sizeof (int));
1202     if (add_tags == NULL) {
1203         ret = 1;
1204         goto DONE;
1205     }
1206
1207     remove_tags = talloc_size (local, argc * sizeof (int));
1208     if (remove_tags == NULL) {
1209         ret = 1;
1210         goto DONE;
1211     }
1212
1213     for (i = 0; i < argc; i++) {
1214         if (strcmp (argv[i], "--") == 0) {
1215             i++;
1216             break;
1217         }
1218         if (argv[i][0] == '+') {
1219             add_tags[add_tags_count++] = i;
1220         } else if (argv[i][0] == '-') {
1221             remove_tags[remove_tags_count++] = i;
1222         } else {
1223             break;
1224         }
1225     }
1226
1227     if (add_tags_count == 0 && remove_tags_count == 0) {
1228         fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
1229         ret = 1;
1230         goto DONE;
1231     }
1232
1233     if (i == argc) {
1234         fprintf (stderr, "Error: 'notmuch tag' requires at least one search term.\n");
1235         ret = 1;
1236         goto DONE;
1237     }
1238
1239     notmuch = notmuch_database_open (NULL);
1240     if (notmuch == NULL) {
1241         ret = 1;
1242         goto DONE;
1243     }
1244
1245     query_string = query_string_from_args (local, argc - i, &argv[i]);
1246
1247     query = notmuch_query_create (notmuch, query_string);
1248     if (query == NULL) {
1249         fprintf (stderr, "Out of memory.\n");
1250         ret = 1;
1251         goto DONE;
1252     }
1253
1254     for (messages = notmuch_query_search_messages (query);
1255          notmuch_messages_has_more (messages);
1256          notmuch_messages_advance (messages))
1257     {
1258         message = notmuch_messages_get (messages);
1259
1260         notmuch_message_freeze (message);
1261
1262         for (i = 0; i < remove_tags_count; i++)
1263             notmuch_message_remove_tag (message,
1264                                         argv[remove_tags[i]] + 1);
1265
1266         for (i = 0; i < add_tags_count; i++)
1267             notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
1268
1269         notmuch_message_thaw (message);
1270
1271         notmuch_message_destroy (message);
1272     }
1273
1274     notmuch_query_destroy (query);
1275
1276   DONE:
1277     if (notmuch)
1278         notmuch_database_close (notmuch);
1279
1280     talloc_free (local);
1281
1282     return ret;
1283 }
1284
1285 static int
1286 dump_command (unused (void *ctx), int argc, char *argv[])
1287 {
1288     FILE *output = NULL;
1289     notmuch_database_t *notmuch = NULL;
1290     notmuch_query_t *query;
1291     notmuch_messages_t *messages;
1292     notmuch_message_t *message;
1293     notmuch_tags_t *tags;
1294     int ret = 0;
1295
1296     if (argc) {
1297         output = fopen (argv[0], "w");
1298         if (output == NULL) {
1299             fprintf (stderr, "Error opening %s for writing: %s\n",
1300                      argv[0], strerror (errno));
1301             ret = 1;
1302             goto DONE;
1303         }
1304     } else {
1305         output = stdout;
1306     }
1307
1308     notmuch = notmuch_database_open (NULL);
1309     if (notmuch == NULL) {
1310         ret = 1;
1311         goto DONE;
1312     }
1313
1314     query = notmuch_query_create (notmuch, "");
1315     if (query == NULL) {
1316         fprintf (stderr, "Out of memory\n");
1317         ret = 1;
1318         goto DONE;
1319     }
1320
1321     notmuch_query_set_sort (query, NOTMUCH_SORT_MESSAGE_ID);
1322
1323     for (messages = notmuch_query_search_messages (query);
1324          notmuch_messages_has_more (messages);
1325          notmuch_messages_advance (messages))
1326     {
1327         int first = 1;
1328         message = notmuch_messages_get (messages);
1329
1330         fprintf (output,
1331                  "%s (", notmuch_message_get_message_id (message));
1332
1333         for (tags = notmuch_message_get_tags (message);
1334              notmuch_tags_has_more (tags);
1335              notmuch_tags_advance (tags))
1336         {
1337             if (! first)
1338                 fprintf (output, " ");
1339
1340             fprintf (output, "%s", notmuch_tags_get (tags));
1341
1342             first = 0;
1343         }
1344
1345         fprintf (output, ")\n");
1346
1347         notmuch_message_destroy (message);
1348     }
1349
1350     notmuch_query_destroy (query);
1351
1352   DONE:
1353     if (notmuch)
1354         notmuch_database_close (notmuch);
1355     if (output && output != stdout)
1356         fclose (output);
1357
1358     return ret;
1359 }
1360
1361 static int
1362 restore_command (unused (void *ctx), int argc, char *argv[])
1363 {
1364     FILE *input = NULL;
1365     notmuch_database_t *notmuch = NULL;
1366     char *line = NULL;
1367     size_t line_size;
1368     ssize_t line_len;
1369     regex_t regex;
1370     int rerr;
1371     int ret = 0;
1372
1373     if (argc) {
1374         input = fopen (argv[0], "r");
1375         if (input == NULL) {
1376             fprintf (stderr, "Error opening %s for reading: %s\n",
1377                      argv[0], strerror (errno));
1378             ret = 1;
1379             goto DONE;
1380         }
1381     } else {
1382         printf ("No filename given. Reading dump from stdin.\n");
1383         input = stdin;
1384     }
1385
1386     notmuch = notmuch_database_open (NULL);
1387     if (notmuch == NULL) {
1388         ret = 1;
1389         goto DONE;
1390     }
1391
1392     /* Dump output is one line per message. We match a sequence of
1393      * non-space characters for the message-id, then one or more
1394      * spaces, then a list of space-separated tags as a sequence of
1395      * characters within literal '(' and ')'. */
1396     xregcomp (&regex,
1397               "^([^ ]+) \\(([^)]*)\\)$",
1398               REG_EXTENDED);
1399
1400     while ((line_len = getline (&line, &line_size, input)) != -1) {
1401         regmatch_t match[3];
1402         char *message_id, *tags, *tag, *next;
1403         notmuch_message_t *message;
1404         notmuch_status_t status;
1405
1406         chomp_newline (line);
1407
1408         rerr = xregexec (&regex, line, 3, match, 0);
1409         if (rerr == REG_NOMATCH)
1410         {
1411             fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
1412                      line);
1413             continue;
1414         }
1415
1416         message_id = xstrndup (line + match[1].rm_so,
1417                                match[1].rm_eo - match[1].rm_so);
1418         tags = xstrndup (line + match[2].rm_so,
1419                          match[2].rm_eo - match[2].rm_so);
1420
1421         if (strlen (tags)) {
1422
1423             message = notmuch_database_find_message (notmuch, message_id);
1424             if (message == NULL) {
1425                 fprintf (stderr, "Warning: Cannot apply tags to missing message: %s\n",
1426                          message_id);
1427                 goto NEXT_LINE;
1428             }
1429
1430             notmuch_message_freeze (message);
1431
1432             notmuch_message_remove_all_tags (message);
1433
1434             next = tags;
1435             while (next) {
1436                 tag = strsep (&next, " ");
1437                 if (*tag == '\0')
1438                     continue;
1439                 status = notmuch_message_add_tag (message, tag);
1440                 if (status) {
1441                     fprintf (stderr,
1442                              "Error applying tag %s to message %s:\n",
1443                              tag, message_id);
1444                     fprintf (stderr, "%s\n",
1445                              notmuch_status_to_string (status));
1446                 }
1447             }
1448
1449             notmuch_message_thaw (message);
1450             notmuch_message_destroy (message);
1451         }
1452       NEXT_LINE:
1453         free (message_id);
1454         free (tags);
1455     }
1456
1457     regfree (&regex);
1458
1459   DONE:
1460     if (line)
1461         free (line);
1462     if (notmuch)
1463         notmuch_database_close (notmuch);
1464     if (input && input != stdin)
1465         fclose (input);
1466
1467     return ret;
1468 }
1469
1470 static int
1471 help_command (void *ctx, int argc, char *argv[]);
1472
1473 command_t commands[] = {
1474     { "setup", setup_command,
1475       "Interactively setup notmuch for first use.",
1476       "\t\tThe setup command is the first command you will run in order\n"
1477       "\t\tto start using notmuch. It will prompt you for the directory\n"
1478       "\t\tcontaining your email archives, and will then proceed to build\n"
1479       "\t\ta database to allow fast searching of that mail.\n\n"
1480       "\t\tInvoking notmuch with no command argument will run setup if\n"
1481       "\t\tthe setup command has not previously been completed." },
1482     { "new", new_command,
1483       "Find and import any new messages.",
1484       "\t\tScans all sub-directories of the database, adding new messages\n"
1485       "\t\tthat are found. Each new message will be tagged as both\n"
1486       "\t\t\"inbox\" and \"unread\".\n"
1487       "\n"
1488       "\t\tNote: \"notmuch new\" will skip any read-only directories,\n"
1489       "\t\tso you can use that to mark directories that will not\n"
1490       "\t\treceive any new mail (and make \"notmuch new\" faster)." },
1491     { "search", search_command,
1492       "<search-term> [...]\n\n"
1493       "\t\tSearch for threads matching the given search terms.",
1494       "\t\tNote that the individual mail messages will be matched\n"
1495       "\t\tagainst the search terms, but the results will be the\n"
1496       "\t\tthreads containing the matched messages.\n\n"
1497       "\t\tCurrently, the supported search terms are as follows, (where\n"
1498       "\t\t<brackets> indicate user-supplied values):\n\n"
1499       "\t\t\ttag:<tag>\n"
1500       "\t\t\tid:<message-id>\n"
1501       "\t\t\tthread:<thread-id>\n\n"
1502       "\t\tValid tag values include \"inbox\" and \"unread\" by default\n"
1503       "\t\tfor new messages added by \"notmuch new\" as well as any other\n"
1504       "\t\ttag values added manually with \"notmuch tag\".\n\n"
1505       "\t\tMessage ID values are the literal contents of the Message-ID:\n"
1506       "\t\theader of email messages, but without the '<','>' delimiters.\n\n"
1507       "\t\tThread ID values are generated internally by notmuch but can\n"
1508       "\t\tbe seen in the output of \"notmuch search\" for example.\n\n"
1509       "\t\tIn addition to individual terms, multiple terms can be\n"
1510       "\t\tcombined with Boolean operators (\"and\", \"or\", \"not\", etc.).\n"
1511       "\t\tEach term in the query will be implicitly connected by a\n"
1512       "\t\tlogical AND if no explicit operator is provided, (except\n"
1513       "\t\tthat terms with a common prefix will be implicitly combined\n"
1514       "\t\twith OR until we get Xapian defect #402 fixed).\n\n"
1515       "\t\tParentheses can also be used to control the combination of\n"
1516       "\t\tthe Boolean operators, but will have to be protected from\n"
1517       "\t\tinterpretation by the shell, (such as by putting quotation\n"
1518       "\t\tmarks around any parenthesized expression)." },
1519     { "show", show_command,
1520       "<thread-id>\n\n"
1521       "\t\tShow the thread with the given thread ID (see 'search').",
1522       "\t\tThread ID values are given as the first column in the\n"
1523       "\t\toutput of the \"notmuch search\" command. These are the\n"
1524       "\t\trandom-looking strings of 32 characters." },
1525     { "tag", tag_command,
1526       "+<tag>|-<tag> [...] [--] <search-term> [...]\n\n"
1527       "\t\tAdd/remove tags for all messages matching the search terms.",
1528       "\t\tThe search terms are handled exactly as in 'search' so one\n"
1529       "\t\tcan use that command first to see what will be modified.\n\n"
1530       "\t\tTags prefixed by '+' are added while those prefixed by '-' are\n"
1531       "\t\tremoved. For each message, tag removal is before tag addition.\n\n"
1532       "\t\tThe beginning of <search-terms> is recognized by the first\n"
1533       "\t\targument that begins with neither '+' nor '-'. Support for\n"
1534       "\t\tan initial search term beginning with '+' or '-' is provided\n"
1535       "\t\tby allowing the user to specify a \"--\" argument to separate\n"
1536       "\t\tthe tags from the search terms.\n\n"
1537       "\t\tNote: If you run \"notmuch new\" between reading a thread with\n"
1538       "\t\t\"notmuch show\" and removing the \"inbox\" tag for that thread\n"
1539       "\t\twith \"notmuch tag\" then you create the possibility of moving\n"
1540       "\t\tsome messages from that thread out of your inbox without ever\n"
1541       "\t\treading them. The easiest way to avoid this problem is to not\n"
1542       "\t\trun \"notmuch new\" between reading and removing tags." },
1543     { "dump", dump_command,
1544       "[<filename>]\n\n"
1545       "\t\tCreate a plain-text dump of the tags for each message.",
1546       "\t\tOutput is to the given filename, if any, or to stdout.\n"
1547       "\t\tThese tags are the only data in the notmuch database\n"
1548       "\t\tthat can't be recreated from the messages themselves.\n"
1549       "\t\tThe output of notmuch dump is therefore the only\n"
1550       "\t\tcritical thing to backup (and much more friendly to\n"
1551       "\t\tincremental backup than the native database files.)" },
1552     { "restore", restore_command,
1553       "<filename>\n\n"
1554       "\t\tRestore the tags from the given dump file (see 'dump').",
1555       "\t\tNote: The dump file format is specifically chosen to be\n"
1556       "\t\tcompatible with the format of files produced by sup-dump.\n"
1557       "\t\tSo if you've previously been using sup for mail, then the\n"
1558       "\t\t\"notmuch restore\" command provides you a way to import\n"
1559       "\t\tall of your tags (or labels as sup calls them)." },
1560     { "help", help_command,
1561       "[<command>]\n\n"
1562       "\t\tThis message, or more detailed help for the named command.",
1563       "\t\tExcept in this case, where there's not much more detailed\n"
1564       "\t\thelp available." }
1565 };
1566
1567 static void
1568 usage (void)
1569 {
1570     command_t *command;
1571     unsigned int i;
1572
1573     fprintf (stderr, "Usage: notmuch <command> [args...]\n");
1574     fprintf (stderr, "\n");
1575     fprintf (stderr, "Where <command> and [args...] are as follows:\n");
1576     fprintf (stderr, "\n");
1577
1578     for (i = 0; i < ARRAY_SIZE (commands); i++) {
1579         command = &commands[i];
1580
1581         fprintf (stderr, "\t%s\t%s\n\n", command->name, command->summary);
1582     }
1583
1584     fprintf (stderr, "Use \"notmuch help <command>\" for more details on each command.\n\n");
1585 }
1586
1587 static int
1588 help_command (unused (void *ctx), int argc, char *argv[])
1589 {
1590     command_t *command;
1591     unsigned int i;
1592
1593     if (argc == 0) {
1594         fprintf (stderr, "The notmuch mail system.\n\n");
1595         usage ();
1596         return 0;
1597     }
1598
1599     for (i = 0; i < ARRAY_SIZE (commands); i++) {
1600         command = &commands[i];
1601
1602         if (strcmp (argv[0], command->name) == 0) {
1603             fprintf (stderr, "Help for \"notmuch %s\":\n\n", argv[0]);
1604             fprintf (stderr, "\t%s\t%s\n\n%s\n\n", command->name,
1605                      command->summary, command->documentation);
1606             return 0;
1607         }
1608     }
1609
1610     fprintf (stderr,
1611              "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
1612              argv[0]);
1613     return 1;
1614 }
1615     
1616 int
1617 main (int argc, char *argv[])
1618 {
1619     void *local = talloc_new (NULL);
1620     command_t *command;
1621     unsigned int i;
1622
1623     if (argc == 1)
1624         return setup_command (local, 0, NULL);
1625
1626     for (i = 0; i < ARRAY_SIZE (commands); i++) {
1627         command = &commands[i];
1628
1629         if (strcmp (argv[1], command->name) == 0)
1630             return (command->function) (local, argc - 2, &argv[2]);
1631     }
1632
1633     /* Don't complain about "help" being an unknown command when we're
1634        about to provide exactly what's wanted anyway. */
1635     fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
1636              argv[1]);
1637
1638     talloc_free (local);
1639
1640     return 1;
1641 }