]> git.cworth.org Git - notmuch/blob - notmuch-search.c
cli: change the data structure for notmuch address deduplication
[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 static int mailbox_compare (const void *v1, const void *v2)
269 {
270     const mailbox_t *m1 = v1, *m2 = v2;
271     int ret;
272
273     ret = strcmp_null (m1->name, m2->name);
274     if (! ret)
275         ret = strcmp (m1->addr, m2->addr);
276
277     return ret;
278 }
279
280 /* Returns TRUE iff name and addr is duplicate. If not, stores the
281  * name/addr pair in order to detect subsequent duplicates. */
282 static notmuch_bool_t
283 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
284 {
285     char *key;
286     GList *list, *l;
287     mailbox_t *mailbox;
288
289     list = g_hash_table_lookup (ctx->addresses, addr);
290     if (list) {
291         mailbox_t find = {
292             .name = name,
293             .addr = addr,
294         };
295
296         l = g_list_find_custom (list, &find, mailbox_compare);
297         if (l) {
298             mailbox = l->data;
299             mailbox->count++;
300             return TRUE;
301         }
302
303         mailbox = new_mailbox (ctx->format, name, addr);
304         if (! mailbox)
305             return FALSE;
306
307         /*
308          * XXX: It would be more efficient to prepend to the list, but
309          * then we'd have to store the changed list head back to the
310          * hash table. This check is here just to avoid the compiler
311          * warning for unused result.
312          */
313         if (list != g_list_append (list, mailbox))
314             INTERNAL_ERROR ("appending to list changed list head\n");
315
316         return FALSE;
317     }
318
319     key = talloc_strdup (ctx->format, addr);
320     if (! key)
321         return FALSE;
322
323     mailbox = new_mailbox (ctx->format, name, addr);
324     if (! mailbox)
325         return FALSE;
326
327     list = g_list_append (NULL, mailbox);
328     if (! list)
329         return FALSE;
330
331     g_hash_table_insert (ctx->addresses, key, list);
332
333     return FALSE;
334 }
335
336 static void
337 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
338 {
339     const char *name = mailbox->name;
340     const char *addr = mailbox->addr;
341     int count = mailbox->count;
342     sprinter_t *format = ctx->format;
343     InternetAddress *ia = internet_address_mailbox_new (name, addr);
344     char *name_addr;
345
346     /* name_addr has the name part quoted if necessary. Compare
347      * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
348     name_addr = internet_address_to_string (ia, FALSE);
349
350     if (format->is_text_printer) {
351         if (count > 0) {
352             format->integer (format, count);
353             format->string (format, "\t");
354         }
355         format->string (format, name_addr);
356         format->separator (format);
357     } else {
358         format->begin_map (format);
359         format->map_key (format, "name");
360         format->string (format, name);
361         format->map_key (format, "address");
362         format->string (format, addr);
363         format->map_key (format, "name-addr");
364         format->string (format, name_addr);
365         if (count > 0) {
366             format->map_key (format, "count");
367             format->integer (format, count);
368         }
369         format->end (format);
370         format->separator (format);
371     }
372
373     g_object_unref (ia);
374     g_free (name_addr);
375 }
376
377 /* Print or prepare for printing addresses from InternetAddressList. */
378 static void
379 process_address_list (const search_context_t *ctx,
380                       InternetAddressList *list)
381 {
382     InternetAddress *address;
383     int i;
384
385     for (i = 0; i < internet_address_list_length (list); i++) {
386         address = internet_address_list_get_address (list, i);
387         if (INTERNET_ADDRESS_IS_GROUP (address)) {
388             InternetAddressGroup *group;
389             InternetAddressList *group_list;
390
391             group = INTERNET_ADDRESS_GROUP (address);
392             group_list = internet_address_group_get_members (group);
393             if (group_list == NULL)
394                 continue;
395
396             process_address_list (ctx, group_list);
397         } else {
398             InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
399             mailbox_t mbx = {
400                 .name = internet_address_get_name (address),
401                 .addr = internet_address_mailbox_get_addr (mailbox),
402                 .count = 0,
403             };
404
405             /* OUTPUT_COUNT only works with deduplication */
406             if (ctx->dedup != DEDUP_NONE &&
407                 is_duplicate (ctx, mbx.name, mbx.addr))
408                 continue;
409
410             if (ctx->output & OUTPUT_COUNT)
411                 continue;
412
413             print_mailbox (ctx, &mbx);
414         }
415     }
416 }
417
418 /* Print or prepare for printing addresses from a message header. */
419 static void
420 process_address_header (const search_context_t *ctx, const char *value)
421 {
422     InternetAddressList *list;
423
424     if (value == NULL)
425         return;
426
427     list = internet_address_list_parse_string (value);
428     if (list == NULL)
429         return;
430
431     process_address_list (ctx, list);
432
433     g_object_unref (list);
434 }
435
436 /* Destructor for talloc-allocated GHashTable keys and values. */
437 static void
438 _talloc_free_for_g_hash (void *ptr)
439 {
440     talloc_free (ptr);
441 }
442
443 static void
444 _list_free_for_g_hash (void *ptr)
445 {
446     g_list_free_full (ptr, _talloc_free_for_g_hash);
447 }
448
449 static void
450 print_list_value (void *mailbox, void *context)
451 {
452     print_mailbox (context, mailbox);
453 }
454
455 static void
456 print_hash_value (unused (void *key), void *list, void *context)
457 {
458     g_list_foreach (list, print_list_value, context);
459 }
460
461 static int
462 _count_filenames (notmuch_message_t *message)
463 {
464     notmuch_filenames_t *filenames;
465     int i = 0;
466
467     filenames = notmuch_message_get_filenames (message);
468
469     while (notmuch_filenames_valid (filenames)) {
470         notmuch_filenames_move_to_next (filenames);
471         i++;
472     }
473
474     notmuch_filenames_destroy (filenames);
475
476     return i;
477 }
478
479 static int
480 do_search_messages (search_context_t *ctx)
481 {
482     notmuch_message_t *message;
483     notmuch_messages_t *messages;
484     notmuch_filenames_t *filenames;
485     sprinter_t *format = ctx->format;
486     int i;
487     notmuch_status_t status;
488
489     if (ctx->offset < 0) {
490         ctx->offset += notmuch_query_count_messages (ctx->query);
491         if (ctx->offset < 0)
492             ctx->offset = 0;
493     }
494
495     status = notmuch_query_search_messages_st (ctx->query, &messages);
496     if (print_status_query ("notmuch search", ctx->query, status))
497         return 1;
498
499     format->begin_list (format);
500
501     for (i = 0;
502          notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
503          notmuch_messages_move_to_next (messages), i++)
504     {
505         if (i < ctx->offset)
506             continue;
507
508         message = notmuch_messages_get (messages);
509
510         if (ctx->output == OUTPUT_FILES) {
511             int j;
512             filenames = notmuch_message_get_filenames (message);
513
514             for (j = 1;
515                  notmuch_filenames_valid (filenames);
516                  notmuch_filenames_move_to_next (filenames), j++)
517             {
518                 if (ctx->dupe < 0 || ctx->dupe == j) {
519                     format->string (format, notmuch_filenames_get (filenames));
520                     format->separator (format);
521                 }
522             }
523             
524             notmuch_filenames_destroy( filenames );
525
526         } else if (ctx->output == OUTPUT_MESSAGES) {
527             /* special case 1 for speed */
528             if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
529                 format->set_prefix (format, "id");
530                 format->string (format,
531                                 notmuch_message_get_message_id (message));
532                 format->separator (format);
533             }
534         } else {
535             if (ctx->output & OUTPUT_SENDER) {
536                 const char *addrs;
537
538                 addrs = notmuch_message_get_header (message, "from");
539                 process_address_header (ctx, addrs);
540             }
541
542             if (ctx->output & OUTPUT_RECIPIENTS) {
543                 const char *hdrs[] = { "to", "cc", "bcc" };
544                 const char *addrs;
545                 size_t j;
546
547                 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
548                     addrs = notmuch_message_get_header (message, hdrs[j]);
549                     process_address_header (ctx, addrs);
550                 }
551             }
552         }
553
554         notmuch_message_destroy (message);
555     }
556
557     if (ctx->addresses && ctx->output & OUTPUT_COUNT)
558         g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
559
560     notmuch_messages_destroy (messages);
561
562     format->end (format);
563
564     return 0;
565 }
566
567 static int
568 do_search_tags (const search_context_t *ctx)
569 {
570     notmuch_messages_t *messages = NULL;
571     notmuch_tags_t *tags;
572     const char *tag;
573     sprinter_t *format = ctx->format;
574     notmuch_query_t *query = ctx->query;
575     notmuch_database_t *notmuch = ctx->notmuch;
576
577     /* should the following only special case if no excluded terms
578      * specified? */
579
580     /* Special-case query of "*" for better performance. */
581     if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
582         tags = notmuch_database_get_all_tags (notmuch);
583     } else {
584         notmuch_status_t status;
585         status = notmuch_query_search_messages_st (query, &messages);
586         if (print_status_query ("notmuch search", query, status))
587             return 1;
588
589         tags = notmuch_messages_collect_tags (messages);
590     }
591     if (tags == NULL)
592         return 1;
593
594     format->begin_list (format);
595
596     for (;
597          notmuch_tags_valid (tags);
598          notmuch_tags_move_to_next (tags))
599     {
600         tag = notmuch_tags_get (tags);
601
602         format->string (format, tag);
603         format->separator (format);
604
605     }
606
607     notmuch_tags_destroy (tags);
608
609     if (messages)
610         notmuch_messages_destroy (messages);
611
612     format->end (format);
613
614     return 0;
615 }
616
617 static int
618 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
619 {
620     char *query_str;
621     unsigned int i;
622     char *status_string = NULL;
623
624     switch (ctx->format_sel) {
625     case NOTMUCH_FORMAT_TEXT:
626         ctx->format = sprinter_text_create (config, stdout);
627         break;
628     case NOTMUCH_FORMAT_TEXT0:
629         if (ctx->output == OUTPUT_SUMMARY) {
630             fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
631             return EXIT_FAILURE;
632         }
633         ctx->format = sprinter_text0_create (config, stdout);
634         break;
635     case NOTMUCH_FORMAT_JSON:
636         ctx->format = sprinter_json_create (config, stdout);
637         break;
638     case NOTMUCH_FORMAT_SEXP:
639         ctx->format = sprinter_sexp_create (config, stdout);
640         break;
641     default:
642         /* this should never happen */
643         INTERNAL_ERROR("no output format selected");
644     }
645
646     notmuch_exit_if_unsupported_format ();
647
648     if (notmuch_database_open_verbose (
649             notmuch_config_get_database_path (config),
650             NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
651
652         if (status_string) {
653             fputs (status_string, stderr);
654             free (status_string);
655         }
656
657         return EXIT_FAILURE;
658     }
659
660     notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);
661
662     query_str = query_string_from_args (ctx->notmuch, argc, argv);
663     if (query_str == NULL) {
664         fprintf (stderr, "Out of memory.\n");
665         return EXIT_FAILURE;
666     }
667     if (*query_str == '\0') {
668         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
669         return EXIT_FAILURE;
670     }
671
672     ctx->query = notmuch_query_create (ctx->notmuch, query_str);
673     if (ctx->query == NULL) {
674         fprintf (stderr, "Out of memory\n");
675         return EXIT_FAILURE;
676     }
677
678     notmuch_query_set_sort (ctx->query, ctx->sort);
679
680     if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
681         /* If we are not doing summary output there is nowhere to
682          * print the excluded flag so fall back on including the
683          * excluded messages. */
684         fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
685         ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
686     }
687
688     if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
689         const char **search_exclude_tags;
690         size_t search_exclude_tags_length;
691
692         search_exclude_tags = notmuch_config_get_search_exclude_tags
693             (config, &search_exclude_tags_length);
694         for (i = 0; i < search_exclude_tags_length; i++)
695             notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
696         notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
697     }
698
699     return 0;
700 }
701
702 static void
703 _notmuch_search_cleanup (search_context_t *ctx)
704 {
705     notmuch_query_destroy (ctx->query);
706     notmuch_database_destroy (ctx->notmuch);
707
708     talloc_free (ctx->format);
709 }
710
711 static search_context_t search_context = {
712     .format_sel = NOTMUCH_FORMAT_TEXT,
713     .exclude = NOTMUCH_EXCLUDE_TRUE,
714     .sort = NOTMUCH_SORT_NEWEST_FIRST,
715     .output = 0,
716     .offset = 0,
717     .limit = -1, /* unlimited */
718     .dupe = -1,
719     .dedup = DEDUP_MAILBOX,
720 };
721
722 static const notmuch_opt_desc_t common_options[] = {
723     { NOTMUCH_OPT_KEYWORD, &search_context.sort, "sort", 's',
724       (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
725                               { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
726                               { 0, 0 } } },
727     { NOTMUCH_OPT_KEYWORD, &search_context.format_sel, "format", 'f',
728       (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
729                               { "sexp", NOTMUCH_FORMAT_SEXP },
730                               { "text", NOTMUCH_FORMAT_TEXT },
731                               { "text0", NOTMUCH_FORMAT_TEXT0 },
732                               { 0, 0 } } },
733     { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
734     { 0, 0, 0, 0, 0 }
735 };
736
737 int
738 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
739 {
740     search_context_t *ctx = &search_context;
741     int opt_index, ret;
742
743     notmuch_opt_desc_t options[] = {
744         { NOTMUCH_OPT_KEYWORD, &ctx->output, "output", 'o',
745           (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
746                                   { "threads", OUTPUT_THREADS },
747                                   { "messages", OUTPUT_MESSAGES },
748                                   { "files", OUTPUT_FILES },
749                                   { "tags", OUTPUT_TAGS },
750                                   { 0, 0 } } },
751         { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
752           (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
753                                   { "false", NOTMUCH_EXCLUDE_FALSE },
754                                   { "flag", NOTMUCH_EXCLUDE_FLAG },
755                                   { "all", NOTMUCH_EXCLUDE_ALL },
756                                   { 0, 0 } } },
757         { NOTMUCH_OPT_INT, &ctx->offset, "offset", 'O', 0 },
758         { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0  },
759         { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0  },
760         { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
761         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
762         { 0, 0, 0, 0, 0 }
763     };
764
765     ctx->output = OUTPUT_SUMMARY;
766     opt_index = parse_arguments (argc, argv, options, 1);
767     if (opt_index < 0)
768         return EXIT_FAILURE;
769
770     notmuch_process_shared_options (argv[0]);
771
772     if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
773         ctx->dupe != -1) {
774         fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
775         return EXIT_FAILURE;
776     }
777
778     if (_notmuch_search_prepare (ctx, config,
779                                  argc - opt_index, argv + opt_index))
780         return EXIT_FAILURE;
781
782     switch (ctx->output) {
783     case OUTPUT_SUMMARY:
784     case OUTPUT_THREADS:
785         ret = do_search_threads (ctx);
786         break;
787     case OUTPUT_MESSAGES:
788     case OUTPUT_FILES:
789         ret = do_search_messages (ctx);
790         break;
791     case OUTPUT_TAGS:
792         ret = do_search_tags (ctx);
793         break;
794     default:
795         INTERNAL_ERROR ("Unexpected output");
796     }
797
798     _notmuch_search_cleanup (ctx);
799
800     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
801 }
802
803 int
804 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
805 {
806     search_context_t *ctx = &search_context;
807     int opt_index, ret;
808
809     notmuch_opt_desc_t options[] = {
810         { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
811           (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
812                                   { "recipients", OUTPUT_RECIPIENTS },
813                                   { "count", OUTPUT_COUNT },
814                                   { 0, 0 } } },
815         { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
816           (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
817                                   { "false", NOTMUCH_EXCLUDE_FALSE },
818                                   { 0, 0 } } },
819         { NOTMUCH_OPT_KEYWORD, &ctx->dedup, "deduplicate", 'D',
820           (notmuch_keyword_t []){ { "no", DEDUP_NONE },
821                                   { "mailbox", DEDUP_MAILBOX },
822                                   { 0, 0 } } },
823         { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
824         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
825         { 0, 0, 0, 0, 0 }
826     };
827
828     opt_index = parse_arguments (argc, argv, options, 1);
829     if (opt_index < 0)
830         return EXIT_FAILURE;
831
832     notmuch_process_shared_options (argv[0]);
833
834     if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
835         ctx->output |= OUTPUT_SENDER;
836
837     if (ctx->output & OUTPUT_COUNT && ctx->dedup == DEDUP_NONE) {
838         fprintf (stderr, "--output=count is not applicable with --deduplicate=no\n");
839         return EXIT_FAILURE;
840     }
841
842     if (_notmuch_search_prepare (ctx, config,
843                                  argc - opt_index, argv + opt_index))
844         return EXIT_FAILURE;
845
846     ctx->addresses = g_hash_table_new_full (strcase_hash, strcase_equal,
847                                             _talloc_free_for_g_hash,
848                                             _list_free_for_g_hash);
849
850     ret = do_search_messages (ctx);
851
852     g_hash_table_unref (ctx->addresses);
853
854
855     _notmuch_search_cleanup (ctx);
856
857     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
858 }