+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;
+}
+
+static int mailbox_compare (const void *v1, const void *v2)
+{
+ const mailbox_t *m1 = v1, *m2 = v2;
+ int ret;
+
+ ret = strcmp_null (m1->name, m2->name);
+ if (! ret)
+ ret = strcmp (m1->addr, m2->addr);
+
+ return ret;
+}
+
+/* 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;
+ GList *list, *l;
+ mailbox_t *mailbox;
+
+ list = g_hash_table_lookup (ctx->addresses, addr);
+ if (list) {
+ mailbox_t find = {
+ .name = name,
+ .addr = addr,
+ };
+
+ l = g_list_find_custom (list, &find, mailbox_compare);
+ if (l) {
+ mailbox = l->data;
+ mailbox->count++;
+ return TRUE;
+ }
+
+ mailbox = new_mailbox (ctx->format, name, addr);
+ if (! mailbox)
+ return FALSE;
+
+ /*
+ * XXX: It would be more efficient to prepend to the list, but
+ * then we'd have to store the changed list head back to the
+ * hash table. This check is here just to avoid the compiler
+ * warning for unused result.
+ */
+ if (list != g_list_append (list, mailbox))
+ INTERNAL_ERROR ("appending to list changed list head\n");
+
+ return FALSE;
+ }
+
+ key = talloc_strdup (ctx->format, addr);
+ if (! key)
+ return FALSE;
+
+ mailbox = new_mailbox (ctx->format, name, addr);
+ if (! mailbox)
+ return FALSE;
+
+ list = g_list_append (NULL, mailbox);
+ if (! list)
+ return FALSE;
+
+ g_hash_table_insert (ctx->addresses, key, list);
+
+ 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 (ctx->output & OUTPUT_COUNT) {
+ 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 (ctx->output & OUTPUT_COUNT) {
+ 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),
+ };
+
+ /* OUTPUT_COUNT only works with deduplication */
+ if (ctx->dedup != DEDUP_NONE &&
+ is_duplicate (ctx, mbx.name, mbx.addr))
+ continue;
+
+ /* OUTPUT_COUNT and DEDUP_ADDRESS require a full pass. */
+ if (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS)
+ 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
+_list_free_for_g_hash (void *ptr)
+{
+ g_list_free_full (ptr, _talloc_free_for_g_hash);
+}
+
+/* Print the most common variant of a list of unique mailboxes, and
+ * conflate the counts. */
+static void
+print_popular (const search_context_t *ctx, GList *list)
+{
+ GList *l;
+ mailbox_t *mailbox = NULL, *m;
+ int max = 0;
+ int total = 0;
+
+ for (l = list; l; l = l->next) {
+ m = l->data;
+ total += m->count;
+ if (m->count > max) {
+ mailbox = m;
+ max = m->count;
+ }
+ }
+
+ if (! mailbox)
+ INTERNAL_ERROR("Empty list in address hash table\n");
+
+ /* The original count is no longer needed, so overwrite. */
+ mailbox->count = total;
+
+ print_mailbox (ctx, mailbox);
+}
+
+static void
+print_list_value (void *mailbox, void *context)
+{
+ print_mailbox (context, mailbox);
+}
+
+static void
+print_hash_value (unused (void *key), void *list, void *context)
+{
+ const search_context_t *ctx = context;
+
+ if (ctx->dedup == DEDUP_ADDRESS)
+ print_popular (ctx, list);
+ else
+ g_list_foreach (list, print_list_value, context);
+}
+
+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;
+}
+