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