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