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