]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
Merge branch 'release'
[notmuch] / notmuch-search.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 https://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22 #include "sprinter.h"
23 #include "string-util.h"
24
25 typedef enum {
26     /* Search command */
27     OUTPUT_SUMMARY      = 1 << 0,
28     OUTPUT_THREADS      = 1 << 1,
29     OUTPUT_MESSAGES     = 1 << 2,
30     OUTPUT_FILES        = 1 << 3,
31     OUTPUT_TAGS         = 1 << 4,
32
33     /* Address command */
34     OUTPUT_SENDER       = 1 << 5,
35     OUTPUT_RECIPIENTS   = 1 << 6,
36     OUTPUT_COUNT        = 1 << 7,
37 } output_t;
38
39 typedef enum {
40     DEDUP_NONE,
41     DEDUP_MAILBOX,
42     DEDUP_ADDRESS,
43 } dedup_t;
44
45 typedef enum {
46     NOTMUCH_FORMAT_JSON,
47     NOTMUCH_FORMAT_TEXT,
48     NOTMUCH_FORMAT_TEXT0,
49     NOTMUCH_FORMAT_SEXP
50 } format_sel_t;
51
52 typedef struct {
53     notmuch_database_t *notmuch;
54     format_sel_t format_sel;
55     sprinter_t *format;
56     notmuch_exclude_t exclude;
57     notmuch_query_t *query;
58     notmuch_sort_t sort;
59     output_t output;
60     int offset;
61     int limit;
62     int dupe;
63     GHashTable *addresses;
64     dedup_t dedup;
65 } search_context_t;
66
67 typedef struct {
68     const char *name;
69     const char *addr;
70     int count;
71 } mailbox_t;
72
73 /* Return two stable query strings that identify exactly the matched
74  * and unmatched messages currently in thread.  If there are no
75  * matched or unmatched messages, the returned buffers will be
76  * NULL. */
77 static int
78 get_thread_query (notmuch_thread_t *thread,
79                   char **matched_out, char **unmatched_out)
80 {
81     notmuch_messages_t *messages;
82     char *escaped = NULL;
83     size_t escaped_len = 0;
84
85     *matched_out = *unmatched_out = NULL;
86
87     for (messages = notmuch_thread_get_messages (thread);
88          notmuch_messages_valid (messages);
89          notmuch_messages_move_to_next (messages))
90     {
91         notmuch_message_t *message = notmuch_messages_get (messages);
92         const char *mid = notmuch_message_get_message_id (message);
93         /* Determine which query buffer to extend */
94         char **buf = notmuch_message_get_flag (
95             message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
96         /* Add this message's id: query.  Since "id" is an exclusive
97          * prefix, it is implicitly 'or'd together, so we only need to
98          * join queries with a space. */
99         if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
100             return -1;
101         if (*buf)
102             *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
103         else
104             *buf = talloc_strdup (thread, escaped);
105         if (!*buf)
106             return -1;
107     }
108     talloc_free (escaped);
109     return 0;
110 }
111
112 static int
113 do_search_threads (search_context_t *ctx)
114 {
115     notmuch_thread_t *thread;
116     notmuch_threads_t *threads;
117     notmuch_tags_t *tags;
118     sprinter_t *format = ctx->format;
119     time_t date;
120     int i;
121     notmuch_status_t status;
122
123     if (ctx->offset < 0) {
124         unsigned count;
125         notmuch_status_t status;
126         status = notmuch_query_count_threads_st (ctx->query, &count);
127         if (print_status_query ("notmuch search", ctx->query, status))
128             return 1;
129
130         ctx->offset += count;
131         if (ctx->offset < 0)
132             ctx->offset = 0;
133     }
134
135     status = notmuch_query_search_threads_st (ctx->query, &threads);
136     if (print_status_query("notmuch search", ctx->query, status))
137         return 1;
138
139     format->begin_list (format);
140
141     for (i = 0;
142          notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
143          notmuch_threads_move_to_next (threads), i++)
144     {
145         thread = notmuch_threads_get (threads);
146
147         if (i < ctx->offset) {
148             notmuch_thread_destroy (thread);
149             continue;
150         }
151
152         if (ctx->output == OUTPUT_THREADS) {
153             format->set_prefix (format, "thread");
154             format->string (format,
155                             notmuch_thread_get_thread_id (thread));
156             format->separator (format);
157         } else { /* output == OUTPUT_SUMMARY */
158             void *ctx_quote = talloc_new (thread);
159             const char *authors = notmuch_thread_get_authors (thread);
160             const char *subject = notmuch_thread_get_subject (thread);
161             const char *thread_id = notmuch_thread_get_thread_id (thread);
162             int matched = notmuch_thread_get_matched_messages (thread);
163             int total = notmuch_thread_get_total_messages (thread);
164             const char *relative_date = NULL;
165             notmuch_bool_t first_tag = TRUE;
166
167             format->begin_map (format);
168
169             if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
170                 date = notmuch_thread_get_oldest_date (thread);
171             else
172                 date = notmuch_thread_get_newest_date (thread);
173
174             relative_date = notmuch_time_relative_date (ctx_quote, date);
175
176             if (format->is_text_printer) {
177                 /* Special case for the text formatter */
178                 printf ("thread:%s %12s [%d/%d] %s; %s (",
179                         thread_id,
180                         relative_date,
181                         matched,
182                         total,
183                         sanitize_string (ctx_quote, authors),
184                         sanitize_string (ctx_quote, subject));
185             } else { /* Structured Output */
186                 format->map_key (format, "thread");
187                 format->string (format, thread_id);
188                 format->map_key (format, "timestamp");
189                 format->integer (format, date);
190                 format->map_key (format, "date_relative");
191                 format->string (format, relative_date);
192                 format->map_key (format, "matched");
193                 format->integer (format, matched);
194                 format->map_key (format, "total");
195                 format->integer (format, total);
196                 format->map_key (format, "authors");
197                 format->string (format, authors);
198                 format->map_key (format, "subject");
199                 format->string (format, subject);
200                 if (notmuch_format_version >= 2) {
201                     char *matched_query, *unmatched_query;
202                     if (get_thread_query (thread, &matched_query,
203                                           &unmatched_query) < 0) {
204                         fprintf (stderr, "Out of memory\n");
205                         return 1;
206                     }
207                     format->map_key (format, "query");
208                     format->begin_list (format);
209                     if (matched_query)
210                         format->string (format, matched_query);
211                     else
212                         format->null (format);
213                     if (unmatched_query)
214                         format->string (format, unmatched_query);
215                     else
216                         format->null (format);
217                     format->end (format);
218                 }
219             }
220
221             talloc_free (ctx_quote);
222
223             format->map_key (format, "tags");
224             format->begin_list (format);
225
226             for (tags = notmuch_thread_get_tags (thread);
227                  notmuch_tags_valid (tags);
228                  notmuch_tags_move_to_next (tags))
229             {
230                 const char *tag = notmuch_tags_get (tags);
231
232                 if (format->is_text_printer) {
233                   /* Special case for the text formatter */
234                     if (first_tag)
235                         first_tag = FALSE;
236                     else
237                         fputc (' ', stdout);
238                     fputs (tag, stdout);
239                 } else { /* Structured Output */
240                     format->string (format, tag);
241                 }
242             }
243
244             if (format->is_text_printer)
245                 printf (")");
246
247             format->end (format);
248             format->end (format);
249             format->separator (format);
250         }
251
252         notmuch_thread_destroy (thread);
253     }
254
255     format->end (format);
256
257     return 0;
258 }
259
260 static mailbox_t *new_mailbox (void *ctx, const char *name, const char *addr)
261 {
262     mailbox_t *mailbox;
263
264     mailbox = talloc (ctx, mailbox_t);
265     if (! mailbox)
266         return NULL;
267
268     mailbox->name = talloc_strdup (mailbox, name);
269     mailbox->addr = talloc_strdup (mailbox, addr);
270     mailbox->count = 1;
271
272     return mailbox;
273 }
274
275 static int mailbox_compare (const void *v1, const void *v2)
276 {
277     const mailbox_t *m1 = v1, *m2 = v2;
278     int ret;
279
280     ret = strcmp_null (m1->name, m2->name);
281     if (! ret)
282         ret = strcmp (m1->addr, m2->addr);
283
284     return ret;
285 }
286
287 /* Returns TRUE iff name and addr is duplicate. If not, stores the
288  * name/addr pair in order to detect subsequent duplicates. */
289 static notmuch_bool_t
290 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
291 {
292     char *key;
293     GList *list, *l;
294     mailbox_t *mailbox;
295
296     list = g_hash_table_lookup (ctx->addresses, addr);
297     if (list) {
298         mailbox_t find = {
299             .name = name,
300             .addr = addr,
301         };
302
303         l = g_list_find_custom (list, &find, mailbox_compare);
304         if (l) {
305             mailbox = l->data;
306             mailbox->count++;
307             return TRUE;
308         }
309
310         mailbox = new_mailbox (ctx->format, name, addr);
311         if (! mailbox)
312             return FALSE;
313
314         /*
315          * XXX: It would be more efficient to prepend to the list, but
316          * then we'd have to store the changed list head back to the
317          * hash table. This check is here just to avoid the compiler
318          * warning for unused result.
319          */
320         if (list != g_list_append (list, mailbox))
321             INTERNAL_ERROR ("appending to list changed list head\n");
322
323         return FALSE;
324     }
325
326     key = talloc_strdup (ctx->format, addr);
327     if (! key)
328         return FALSE;
329
330     mailbox = new_mailbox (ctx->format, name, addr);
331     if (! mailbox)
332         return FALSE;
333
334     list = g_list_append (NULL, mailbox);
335     if (! list)
336         return FALSE;
337
338     g_hash_table_insert (ctx->addresses, key, list);
339
340     return FALSE;
341 }
342
343 static void
344 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
345 {
346     const char *name = mailbox->name;
347     const char *addr = mailbox->addr;
348     int count = mailbox->count;
349     sprinter_t *format = ctx->format;
350     InternetAddress *ia = internet_address_mailbox_new (name, addr);
351     char *name_addr;
352
353     /* name_addr has the name part quoted if necessary. Compare
354      * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
355     name_addr = internet_address_to_string (ia, FALSE);
356
357     if (format->is_text_printer) {
358         if (ctx->output & OUTPUT_COUNT) {
359             format->integer (format, count);
360             format->string (format, "\t");
361         }
362         format->string (format, name_addr);
363         format->separator (format);
364     } else {
365         format->begin_map (format);
366         format->map_key (format, "name");
367         format->string (format, name);
368         format->map_key (format, "address");
369         format->string (format, addr);
370         format->map_key (format, "name-addr");
371         format->string (format, name_addr);
372         if (ctx->output & OUTPUT_COUNT) {
373             format->map_key (format, "count");
374             format->integer (format, count);
375         }
376         format->end (format);
377         format->separator (format);
378     }
379
380     g_object_unref (ia);
381     g_free (name_addr);
382 }
383
384 /* Print or prepare for printing addresses from InternetAddressList. */
385 static void
386 process_address_list (const search_context_t *ctx,
387                       InternetAddressList *list)
388 {
389     InternetAddress *address;
390     int i;
391
392     for (i = 0; i < internet_address_list_length (list); i++) {
393         address = internet_address_list_get_address (list, i);
394         if (INTERNET_ADDRESS_IS_GROUP (address)) {
395             InternetAddressGroup *group;
396             InternetAddressList *group_list;
397
398             group = INTERNET_ADDRESS_GROUP (address);
399             group_list = internet_address_group_get_members (group);
400             if (group_list == NULL)
401                 continue;
402
403             process_address_list (ctx, group_list);
404         } else {
405             InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
406             mailbox_t mbx = {
407                 .name = internet_address_get_name (address),
408                 .addr = internet_address_mailbox_get_addr (mailbox),
409             };
410
411             /* OUTPUT_COUNT only works with deduplication */
412             if (ctx->dedup != DEDUP_NONE &&
413                 is_duplicate (ctx, mbx.name, mbx.addr))
414                 continue;
415
416             /* OUTPUT_COUNT and DEDUP_ADDRESS require a full pass. */
417             if (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS)
418                 continue;
419
420             print_mailbox (ctx, &mbx);
421         }
422     }
423 }
424
425 /* Print or prepare for printing addresses from a message header. */
426 static void
427 process_address_header (const search_context_t *ctx, const char *value)
428 {
429     InternetAddressList *list;
430
431     if (value == NULL)
432         return;
433
434     list = internet_address_list_parse_string (value);
435     if (list == NULL)
436         return;
437
438     process_address_list (ctx, list);
439
440     g_object_unref (list);
441 }
442
443 /* Destructor for talloc-allocated GHashTable keys and values. */
444 static void
445 _talloc_free_for_g_hash (void *ptr)
446 {
447     talloc_free (ptr);
448 }
449
450 static void
451 _list_free_for_g_hash (void *ptr)
452 {
453     g_list_free_full (ptr, _talloc_free_for_g_hash);
454 }
455
456 /* Print the most common variant of a list of unique mailboxes, and
457  * conflate the counts. */
458 static void
459 print_popular (const search_context_t *ctx, GList *list)
460 {
461     GList *l;
462     mailbox_t *mailbox = NULL, *m;
463     int max = 0;
464     int total = 0;
465
466     for (l = list; l; l = l->next) {
467         m = l->data;
468         total += m->count;
469         if (m->count > max) {
470             mailbox = m;
471             max = m->count;
472         }
473     }
474
475     if (! mailbox)
476         INTERNAL_ERROR("Empty list in address hash table\n");
477
478     /* The original count is no longer needed, so overwrite. */
479     mailbox->count = total;
480
481     print_mailbox (ctx, mailbox);
482 }
483
484 static void
485 print_list_value (void *mailbox, void *context)
486 {
487     print_mailbox (context, mailbox);
488 }
489
490 static void
491 print_hash_value (unused (void *key), void *list, void *context)
492 {
493     const search_context_t *ctx = context;
494
495     if (ctx->dedup == DEDUP_ADDRESS)
496         print_popular (ctx, list);
497     else
498         g_list_foreach (list, print_list_value, context);
499 }
500
501 static int
502 _count_filenames (notmuch_message_t *message)
503 {
504     notmuch_filenames_t *filenames;
505     int i = 0;
506
507     filenames = notmuch_message_get_filenames (message);
508
509     while (notmuch_filenames_valid (filenames)) {
510         notmuch_filenames_move_to_next (filenames);
511         i++;
512     }
513
514     notmuch_filenames_destroy (filenames);
515
516     return i;
517 }
518
519 static int
520 do_search_messages (search_context_t *ctx)
521 {
522     notmuch_message_t *message;
523     notmuch_messages_t *messages;
524     notmuch_filenames_t *filenames;
525     sprinter_t *format = ctx->format;
526     int i;
527     notmuch_status_t status;
528
529     if (ctx->offset < 0) {
530         unsigned count;
531         notmuch_status_t status;
532         status = notmuch_query_count_messages_st (ctx->query, &count);
533         if (print_status_query ("notmuch search", ctx->query, status))
534             return 1;
535
536         ctx->offset += count;
537         if (ctx->offset < 0)
538             ctx->offset = 0;
539     }
540
541     status = notmuch_query_search_messages_st (ctx->query, &messages);
542     if (print_status_query ("notmuch search", ctx->query, status))
543         return 1;
544
545     format->begin_list (format);
546
547     for (i = 0;
548          notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
549          notmuch_messages_move_to_next (messages), i++)
550     {
551         if (i < ctx->offset)
552             continue;
553
554         message = notmuch_messages_get (messages);
555
556         if (ctx->output == OUTPUT_FILES) {
557             int j;
558             filenames = notmuch_message_get_filenames (message);
559
560             for (j = 1;
561                  notmuch_filenames_valid (filenames);
562                  notmuch_filenames_move_to_next (filenames), j++)
563             {
564                 if (ctx->dupe < 0 || ctx->dupe == j) {
565                     format->string (format, notmuch_filenames_get (filenames));
566                     format->separator (format);
567                 }
568             }
569             
570             notmuch_filenames_destroy( filenames );
571
572         } else if (ctx->output == OUTPUT_MESSAGES) {
573             /* special case 1 for speed */
574             if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
575                 format->set_prefix (format, "id");
576                 format->string (format,
577                                 notmuch_message_get_message_id (message));
578                 format->separator (format);
579             }
580         } else {
581             if (ctx->output & OUTPUT_SENDER) {
582                 const char *addrs;
583
584                 addrs = notmuch_message_get_header (message, "from");
585                 process_address_header (ctx, addrs);
586             }
587
588             if (ctx->output & OUTPUT_RECIPIENTS) {
589                 const char *hdrs[] = { "to", "cc", "bcc" };
590                 const char *addrs;
591                 size_t j;
592
593                 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
594                     addrs = notmuch_message_get_header (message, hdrs[j]);
595                     process_address_header (ctx, addrs);
596                 }
597             }
598         }
599
600         notmuch_message_destroy (message);
601     }
602
603     if (ctx->addresses &&
604         (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS))
605         g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
606
607     notmuch_messages_destroy (messages);
608
609     format->end (format);
610
611     return 0;
612 }
613
614 static int
615 do_search_tags (const search_context_t *ctx)
616 {
617     notmuch_messages_t *messages = NULL;
618     notmuch_tags_t *tags;
619     const char *tag;
620     sprinter_t *format = ctx->format;
621     notmuch_query_t *query = ctx->query;
622     notmuch_database_t *notmuch = ctx->notmuch;
623
624     /* should the following only special case if no excluded terms
625      * specified? */
626
627     /* Special-case query of "*" for better performance. */
628     if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
629         tags = notmuch_database_get_all_tags (notmuch);
630     } else {
631         notmuch_status_t status;
632         status = notmuch_query_search_messages_st (query, &messages);
633         if (print_status_query ("notmuch search", query, status))
634             return 1;
635
636         tags = notmuch_messages_collect_tags (messages);
637     }
638     if (tags == NULL)
639         return 1;
640
641     format->begin_list (format);
642
643     for (;
644          notmuch_tags_valid (tags);
645          notmuch_tags_move_to_next (tags))
646     {
647         tag = notmuch_tags_get (tags);
648
649         format->string (format, tag);
650         format->separator (format);
651
652     }
653
654     notmuch_tags_destroy (tags);
655
656     if (messages)
657         notmuch_messages_destroy (messages);
658
659     format->end (format);
660
661     return 0;
662 }
663
664 static int
665 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
666 {
667     char *query_str;
668     unsigned int i;
669     char *status_string = NULL;
670
671     switch (ctx->format_sel) {
672     case NOTMUCH_FORMAT_TEXT:
673         ctx->format = sprinter_text_create (config, stdout);
674         break;
675     case NOTMUCH_FORMAT_TEXT0:
676         if (ctx->output == OUTPUT_SUMMARY) {
677             fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
678             return EXIT_FAILURE;
679         }
680         ctx->format = sprinter_text0_create (config, stdout);
681         break;
682     case NOTMUCH_FORMAT_JSON:
683         ctx->format = sprinter_json_create (config, stdout);
684         break;
685     case NOTMUCH_FORMAT_SEXP:
686         ctx->format = sprinter_sexp_create (config, stdout);
687         break;
688     default:
689         /* this should never happen */
690         INTERNAL_ERROR("no output format selected");
691     }
692
693     notmuch_exit_if_unsupported_format ();
694
695     if (notmuch_database_open_verbose (
696             notmuch_config_get_database_path (config),
697             NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
698
699         if (status_string) {
700             fputs (status_string, stderr);
701             free (status_string);
702         }
703
704         return EXIT_FAILURE;
705     }
706
707     notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);
708
709     query_str = query_string_from_args (ctx->notmuch, argc, argv);
710     if (query_str == NULL) {
711         fprintf (stderr, "Out of memory.\n");
712         return EXIT_FAILURE;
713     }
714     if (*query_str == '\0') {
715         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
716         return EXIT_FAILURE;
717     }
718
719     ctx->query = notmuch_query_create (ctx->notmuch, query_str);
720     if (ctx->query == NULL) {
721         fprintf (stderr, "Out of memory\n");
722         return EXIT_FAILURE;
723     }
724
725     notmuch_query_set_sort (ctx->query, ctx->sort);
726
727     if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
728         /* If we are not doing summary output there is nowhere to
729          * print the excluded flag so fall back on including the
730          * excluded messages. */
731         fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
732         ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
733     }
734
735     if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
736         const char **search_exclude_tags;
737         size_t search_exclude_tags_length;
738
739         search_exclude_tags = notmuch_config_get_search_exclude_tags
740             (config, &search_exclude_tags_length);
741         for (i = 0; i < search_exclude_tags_length; i++)
742             notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
743         notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
744     }
745
746     return 0;
747 }
748
749 static void
750 _notmuch_search_cleanup (search_context_t *ctx)
751 {
752     notmuch_query_destroy (ctx->query);
753     notmuch_database_destroy (ctx->notmuch);
754
755     talloc_free (ctx->format);
756 }
757
758 static search_context_t search_context = {
759     .format_sel = NOTMUCH_FORMAT_TEXT,
760     .exclude = NOTMUCH_EXCLUDE_TRUE,
761     .sort = NOTMUCH_SORT_NEWEST_FIRST,
762     .output = 0,
763     .offset = 0,
764     .limit = -1, /* unlimited */
765     .dupe = -1,
766     .dedup = DEDUP_MAILBOX,
767 };
768
769 static const notmuch_opt_desc_t common_options[] = {
770     { NOTMUCH_OPT_KEYWORD, &search_context.sort, "sort", 's',
771       (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
772                               { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
773                               { 0, 0 } } },
774     { NOTMUCH_OPT_KEYWORD, &search_context.format_sel, "format", 'f',
775       (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
776                               { "sexp", NOTMUCH_FORMAT_SEXP },
777                               { "text", NOTMUCH_FORMAT_TEXT },
778                               { "text0", NOTMUCH_FORMAT_TEXT0 },
779                               { 0, 0 } } },
780     { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
781     { 0, 0, 0, 0, 0 }
782 };
783
784 int
785 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
786 {
787     search_context_t *ctx = &search_context;
788     int opt_index, ret;
789
790     notmuch_opt_desc_t options[] = {
791         { NOTMUCH_OPT_KEYWORD, &ctx->output, "output", 'o',
792           (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
793                                   { "threads", OUTPUT_THREADS },
794                                   { "messages", OUTPUT_MESSAGES },
795                                   { "files", OUTPUT_FILES },
796                                   { "tags", OUTPUT_TAGS },
797                                   { 0, 0 } } },
798         { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
799           (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
800                                   { "false", NOTMUCH_EXCLUDE_FALSE },
801                                   { "flag", NOTMUCH_EXCLUDE_FLAG },
802                                   { "all", NOTMUCH_EXCLUDE_ALL },
803                                   { 0, 0 } } },
804         { NOTMUCH_OPT_INT, &ctx->offset, "offset", 'O', 0 },
805         { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0  },
806         { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0  },
807         { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
808         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
809         { 0, 0, 0, 0, 0 }
810     };
811
812     ctx->output = OUTPUT_SUMMARY;
813     opt_index = parse_arguments (argc, argv, options, 1);
814     if (opt_index < 0)
815         return EXIT_FAILURE;
816
817     notmuch_process_shared_options (argv[0]);
818
819     if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
820         ctx->dupe != -1) {
821         fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
822         return EXIT_FAILURE;
823     }
824
825     if (_notmuch_search_prepare (ctx, config,
826                                  argc - opt_index, argv + opt_index))
827         return EXIT_FAILURE;
828
829     switch (ctx->output) {
830     case OUTPUT_SUMMARY:
831     case OUTPUT_THREADS:
832         ret = do_search_threads (ctx);
833         break;
834     case OUTPUT_MESSAGES:
835     case OUTPUT_FILES:
836         ret = do_search_messages (ctx);
837         break;
838     case OUTPUT_TAGS:
839         ret = do_search_tags (ctx);
840         break;
841     default:
842         INTERNAL_ERROR ("Unexpected output");
843     }
844
845     _notmuch_search_cleanup (ctx);
846
847     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
848 }
849
850 int
851 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
852 {
853     search_context_t *ctx = &search_context;
854     int opt_index, ret;
855
856     notmuch_opt_desc_t options[] = {
857         { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
858           (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
859                                   { "recipients", OUTPUT_RECIPIENTS },
860                                   { "count", OUTPUT_COUNT },
861                                   { 0, 0 } } },
862         { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
863           (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
864                                   { "false", NOTMUCH_EXCLUDE_FALSE },
865                                   { 0, 0 } } },
866         { NOTMUCH_OPT_KEYWORD, &ctx->dedup, "deduplicate", 'D',
867           (notmuch_keyword_t []){ { "no", DEDUP_NONE },
868                                   { "mailbox", DEDUP_MAILBOX },
869                                   { "address", DEDUP_ADDRESS },
870                                   { 0, 0 } } },
871         { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
872         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
873         { 0, 0, 0, 0, 0 }
874     };
875
876     opt_index = parse_arguments (argc, argv, options, 1);
877     if (opt_index < 0)
878         return EXIT_FAILURE;
879
880     notmuch_process_shared_options (argv[0]);
881
882     if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
883         ctx->output |= OUTPUT_SENDER;
884
885     if (ctx->output & OUTPUT_COUNT && ctx->dedup == DEDUP_NONE) {
886         fprintf (stderr, "--output=count is not applicable with --deduplicate=no\n");
887         return EXIT_FAILURE;
888     }
889
890     if (_notmuch_search_prepare (ctx, config,
891                                  argc - opt_index, argv + opt_index))
892         return EXIT_FAILURE;
893
894     ctx->addresses = g_hash_table_new_full (strcase_hash, strcase_equal,
895                                             _talloc_free_for_g_hash,
896                                             _list_free_for_g_hash);
897
898     /* The order is not guaranteed if a full pass is required, so go
899      * for fastest. */
900     if (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS)
901         notmuch_query_set_sort (ctx->query, NOTMUCH_SORT_UNSORTED);
902
903     ret = do_search_messages (ctx);
904
905     g_hash_table_unref (ctx->addresses);
906
907
908     _notmuch_search_cleanup (ctx);
909
910     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
911 }