+/* Returns TRUE iff name and/or addr is considered unique. */
+static notmuch_bool_t
+check_unique (const search_options_t *o, GHashTable *addrs, const char *name, const char *addr)
+{
+ notmuch_bool_t unique;
+ char *key;
+
+ if (o->unique == UNIQUE_NONE)
+ return TRUE;
+
+ if (o->unique & UNIQUE_ADDR_CASEFOLD) {
+ gchar *folded = g_utf8_casefold (addr, -1);
+ addr = talloc_strdup (o->format, folded);
+ g_free (folded);
+ }
+ switch (o->unique & UNIQUE_BOTH) {
+ case UNIQUE_NAME:
+ key = talloc_strdup (o->format, name); /* !name results in !key */
+ break;
+ case UNIQUE_ADDR:
+ key = talloc_strdup (o->format, addr);
+ break;
+ case UNIQUE_BOTH:
+ key = talloc_asprintf (o->format, "%s <%s>", name, addr);
+ break;
+ default:
+ INTERNAL_ERROR("invalid --unique flags");
+ }
+
+ if (! key)
+ return FALSE;
+
+ unique = !g_hash_table_lookup_extended (addrs, key, NULL, NULL);
+
+ if (unique)
+ g_hash_table_insert (addrs, key, NULL);
+ else
+ talloc_free (key);
+
+ return unique;
+}
+
+static void
+print_address_list (const search_options_t *o, GHashTable *addrs,
+ InternetAddressList *list)
+{
+ InternetAddress *address;
+ int i;
+
+ for (i = 0; i < internet_address_list_length (list); i++) {
+ address = internet_address_list_get_address (list, i);
+ if (INTERNET_ADDRESS_IS_GROUP (address)) {
+ InternetAddressGroup *group;
+ InternetAddressList *group_list;
+
+ group = INTERNET_ADDRESS_GROUP (address);
+ group_list = internet_address_group_get_members (group);
+ if (group_list == NULL)
+ continue;
+
+ print_address_list (o, addrs, group_list);
+ } else {
+ InternetAddressMailbox *mailbox;
+ const char *name;
+ const char *addr;
+ char *full_address;
+
+ mailbox = INTERNET_ADDRESS_MAILBOX (address);
+
+ name = internet_address_get_name (address);
+ addr = internet_address_mailbox_get_addr (mailbox);
+
+ if (!check_unique (o, addrs, name, addr))
+ continue;
+
+ if (name && *name)
+ full_address = talloc_asprintf (o->format, "%s <%s>", name, addr);
+ else
+ full_address = talloc_strdup (o->format, addr);
+
+ if (!full_address) {
+ fprintf (stderr, "Error: out of memory\n");
+ break;
+ }
+ o->format->string (o->format, full_address);
+ o->format->separator (o->format);
+
+ talloc_free (full_address);
+ }
+ }
+}
+
+static void
+print_address_string (const search_options_t *o, GHashTable *addrs, const char *recipients)
+{
+ InternetAddressList *list;
+
+ if (recipients == NULL)
+ return;
+
+ list = internet_address_list_parse_string (recipients);
+ if (list == NULL)
+ return;
+
+ print_address_list (o, addrs, list);
+}
+
+static void
+_my_talloc_free_for_g_hash (void *ptr)
+{
+ talloc_free (ptr);
+}
+