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