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