]> git.cworth.org Git - notmuch/blobdiff - notmuch-search.c
cli: address: Do not output duplicate addresses
[notmuch] / notmuch-search.c
index 69938d67be154302df04481329a8ba9124c7ac30..86d54ba044357001c6e46f10d2fb2b76a3f00eba 100644 (file)
@@ -53,6 +53,7 @@ typedef struct {
     int offset;
     int limit;
     int dupe;
+    GHashTable *addresses;
 } search_context_t;
 
 typedef struct {
@@ -240,6 +241,28 @@ do_search_threads (search_context_t *ctx)
     return 0;
 }
 
+/* Returns TRUE iff name and addr is duplicate. If not, stores the
+ * name/addr pair in order to detect subsequent duplicates. */
+static notmuch_bool_t
+is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
+{
+    notmuch_bool_t duplicate;
+    char *key;
+
+    key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
+    if (! key)
+       return FALSE;
+
+    duplicate = g_hash_table_lookup_extended (ctx->addresses, key, NULL, NULL);
+
+    if (! duplicate)
+       g_hash_table_insert (ctx->addresses, key, NULL);
+    else
+       talloc_free (key);
+
+    return duplicate;
+}
+
 static void
 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
 {
@@ -274,7 +297,8 @@ print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
 
 /* Print addresses from InternetAddressList.  */
 static void
-process_address_list (const search_context_t *ctx, InternetAddressList *list)
+process_address_list (const search_context_t *ctx,
+                     InternetAddressList *list)
 {
     InternetAddress *address;
     int i;
@@ -298,6 +322,9 @@ process_address_list (const search_context_t *ctx, InternetAddressList *list)
                .addr = internet_address_mailbox_get_addr (mailbox),
            };
 
+           if (is_duplicate (ctx, mbx.name, mbx.addr))
+               continue;
+
            print_mailbox (ctx, &mbx);
        }
     }
@@ -321,6 +348,13 @@ process_address_header (const search_context_t *ctx, const char *value)
     g_object_unref (list);
 }
 
+/* Destructor for talloc-allocated GHashTable keys and values. */
+static void
+_talloc_free_for_g_hash (void *ptr)
+{
+    talloc_free (ptr);
+}
+
 static int
 _count_filenames (notmuch_message_t *message)
 {
@@ -587,7 +621,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
     int opt_index, ret;
 
     notmuch_opt_desc_t options[] = {
-       { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
+       { NOTMUCH_OPT_KEYWORD, &ctx->output, "output", 'o',
          (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
                                  { "threads", OUTPUT_THREADS },
                                  { "messages", OUTPUT_MESSAGES },
@@ -607,13 +641,11 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
        { 0, 0, 0, 0, 0 }
     };
 
+    ctx->output = OUTPUT_SUMMARY;
     opt_index = parse_arguments (argc, argv, options, 1);
     if (opt_index < 0)
        return EXIT_FAILURE;
 
-    if (! ctx->output)
-       ctx->output = OUTPUT_SUMMARY;
-
     if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
        ctx->dupe != -1) {
         fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
@@ -624,17 +656,20 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
                                 argc - opt_index, argv + opt_index))
        return EXIT_FAILURE;
 
-    if (ctx->output == OUTPUT_SUMMARY ||
-       ctx->output == OUTPUT_THREADS)
+    switch (ctx->output) {
+    case OUTPUT_SUMMARY:
+    case OUTPUT_THREADS:
        ret = do_search_threads (ctx);
-    else if (ctx->output == OUTPUT_MESSAGES ||
-            ctx->output == OUTPUT_FILES)
+       break;
+    case OUTPUT_MESSAGES:
+    case OUTPUT_FILES:
        ret = do_search_messages (ctx);
-    else if (ctx->output == OUTPUT_TAGS)
+       break;
+    case OUTPUT_TAGS:
        ret = do_search_tags (ctx);
-    else {
-       fprintf (stderr, "Error: the combination of outputs is not supported.\n");
-       ret = 1;
+       break;
+    default:
+       INTERNAL_ERROR ("Unexpected output");
     }
 
     _notmuch_search_cleanup (ctx);
@@ -672,8 +707,14 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
                                 argc - opt_index, argv + opt_index))
        return EXIT_FAILURE;
 
+    ctx->addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                           _talloc_free_for_g_hash, NULL);
+
     ret = do_search_messages (ctx);
 
+    g_hash_table_unref (ctx->addresses);
+
+
     _notmuch_search_cleanup (ctx);
 
     return ret ? EXIT_FAILURE : EXIT_SUCCESS;