+static mailbox_t *new_mailbox (void *ctx, const char *name, const char *addr)
+{
+ mailbox_t *mailbox;
+
+ mailbox = talloc (ctx, mailbox_t);
+ if (! mailbox)
+ return NULL;
+
+ mailbox->name = talloc_strdup (mailbox, name);
+ mailbox->addr = talloc_strdup (mailbox, addr);
+ mailbox->count = 1;
+
+ return mailbox;
+}
+
+/* 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)
+{
+ char *key;
+ mailbox_t *mailbox;
+
+ key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
+ if (! key)
+ return FALSE;
+
+ mailbox = g_hash_table_lookup (ctx->addresses, key);
+ if (mailbox) {
+ mailbox->count++;
+ talloc_free (key);
+ return TRUE;
+ }
+
+ mailbox = new_mailbox (ctx->format, name, addr);
+ if (! mailbox)
+ return FALSE;
+
+ g_hash_table_insert (ctx->addresses, key, mailbox);
+
+ return FALSE;
+}
+
+static void
+print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
+{
+ const char *name = mailbox->name;
+ const char *addr = mailbox->addr;
+ int count = mailbox->count;
+ sprinter_t *format = ctx->format;
+ InternetAddress *ia = internet_address_mailbox_new (name, addr);
+ char *name_addr;
+
+ /* name_addr has the name part quoted if necessary. Compare
+ * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
+ name_addr = internet_address_to_string (ia, FALSE);
+
+ if (format->is_text_printer) {
+ if (count > 0) {
+ format->integer (format, count);
+ format->string (format, "\t");
+ }
+ format->string (format, name_addr);
+ format->separator (format);
+ } else {
+ format->begin_map (format);
+ format->map_key (format, "name");
+ format->string (format, name);
+ format->map_key (format, "address");
+ format->string (format, addr);
+ format->map_key (format, "name-addr");
+ format->string (format, name_addr);
+ if (count > 0) {
+ format->map_key (format, "count");
+ format->integer (format, count);
+ }
+ format->end (format);
+ format->separator (format);
+ }
+
+ g_object_unref (ia);
+ g_free (name_addr);
+}
+
+/* Print or prepare for printing addresses from InternetAddressList. */
+static void
+process_address_list (const search_context_t *ctx,
+ 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;
+
+ process_address_list (ctx, group_list);
+ } else {
+ InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
+ mailbox_t mbx = {
+ .name = internet_address_get_name (address),
+ .addr = internet_address_mailbox_get_addr (mailbox),
+ .count = 0,
+ };
+
+ if (is_duplicate (ctx, mbx.name, mbx.addr))
+ continue;
+
+ if (ctx->output & OUTPUT_COUNT)
+ continue;
+
+ print_mailbox (ctx, &mbx);
+ }
+ }
+}
+
+/* Print or prepare for printing addresses from a message header. */
+static void
+process_address_header (const search_context_t *ctx, const char *value)
+{
+ InternetAddressList *list;
+
+ if (value == NULL)
+ return;
+
+ list = internet_address_list_parse_string (value);
+ if (list == NULL)
+ return;
+
+ process_address_list (ctx, list);
+
+ 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 void
+print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)
+{
+ const mailbox_t *mailbox = value;
+ search_context_t *ctx = user_data;
+
+ print_mailbox (ctx, mailbox);
+}
+
+static int
+_count_filenames (notmuch_message_t *message)
+{
+ notmuch_filenames_t *filenames;
+ int i = 0;
+
+ filenames = notmuch_message_get_filenames (message);
+
+ while (notmuch_filenames_valid (filenames)) {
+ notmuch_filenames_move_to_next (filenames);
+ i++;
+ }
+
+ notmuch_filenames_destroy (filenames);
+
+ return i;
+}
+