(--format=text0), as a JSON array (--format=json), or as
an S-Expression list (--format=sexp).
+ Handling of duplicate addresses and/or names can be
+ controlled with the --filter-by option.
+
Note: Searching for **sender** should be much faster than
searching for **recipients**, because sender addresses are
cached directly in the database whereas other addresses
prefix. The prefix matches messages based on filenames. This
option filters filenames of the matching messages.
+ ``--filter-by=``\ (**addr**\ \|\ **addrfold**\ \|\ **name**)
+
+ Can be used with ``--output=sender`` or
+ ``--output=recipients`` to filter out duplicate addresses. The
+ filtering algorithm receives a sequence of email addresses and
+ outputs the same sequence without the addresses that are
+ considered a duplicate of a previously output address. What is
+ considered a duplicate depends on how two addresses are
+ compared and this can be controlled by the follwing flags:
+
+ **addr** means that the address part is compared in
+ case-sensitive manner. For example, the addresses "John Doe
+ <john@example.com>" and "Dr. John Doe <john@example.com>" will
+ be considered duplicate.
+
+ **addrfold** is similar to **addr**, but in addition to it
+ case folding is performed before comparison. For example, the
+ addresses "John Doe <john@example.com>" and "Dr. John Doe
+ <JOHN@EXAMPLE.COM>" will be considered duplicate.
+
+ **name** means that the name part is compared in case-sensitive
+ manner. For example, the addresses "John Doe <me@example.com>"
+ and "John Doe <john@doe.name>" will be considered duplicate.
+
+ This option can be given multiple times to combine the effects
+ of the flags. For example,
+ ``--filter-by=name --filter-by=addr`` will print unique
+ case-sensitive combinations of both name and address parts.
+
EXIT STATUS
===========
OUTPUT_ADDRESSES = OUTPUT_SENDER | OUTPUT_RECIPIENTS,
} output_t;
+typedef enum {
+ FILTER_FLAG_ADDR = 1 << 0,
+ FILTER_FLAG_NAME = 1 << 1,
+ FILTER_FLAG_AFOLD = 1 << 2,
+} filter_flag_t;
+
typedef struct {
sprinter_t *format;
notmuch_query_t *query;
int offset;
int limit;
int dupe;
+ filter_flag_t filter_flags;
} search_options_t;
/* Return two stable query strings that identify exactly the matched
return 0;
}
+/* Returns TRUE iff name and/or addr is considered duplicite. */
+static notmuch_bool_t
+check_duplicite (const search_options_t *o, GHashTable *addrs, const char *name, const char *addr)
+{
+ notmuch_bool_t duplicite;
+ char *key;
+
+ if (o->filter_flags == 0)
+ return FALSE;
+
+ if (o->filter_flags & FILTER_FLAG_AFOLD) {
+ gchar *folded = g_utf8_casefold (addr, -1);
+ addr = talloc_strdup (o->format, folded);
+ g_free (folded);
+ }
+ switch (o->filter_flags & (FILTER_FLAG_ADDR | FILTER_FLAG_NAME)) {
+ case FILTER_FLAG_NAME:
+ key = talloc_strdup (o->format, name); /* !name results in !key */
+ break;
+ case FILTER_FLAG_ADDR:
+ key = talloc_strdup (o->format, addr);
+ break;
+ case FILTER_FLAG_NAME | FILTER_FLAG_ADDR:
+ key = talloc_asprintf (o->format, "%s <%s>", name, addr);
+ break;
+ default:
+ INTERNAL_ERROR("invalid --filter_by flags");
+ }
+
+ if (o->filter_flags & FILTER_FLAG_AFOLD)
+ talloc_free ((char*)addr);
+
+ if (! key)
+ return FALSE;
+
+ duplicite = g_hash_table_lookup_extended (addrs, key, NULL, NULL);
+
+ if (!duplicite)
+ g_hash_table_insert (addrs, key, NULL);
+ else
+ talloc_free (key);
+
+ return duplicite;
+}
+
static void
-print_address_list (const search_options_t *o, InternetAddressList *list)
+print_address_list (const search_options_t *o, GHashTable *addrs,
+ InternetAddressList *list)
{
InternetAddress *address;
int i;
if (group_list == NULL)
continue;
- print_address_list (o, group_list);
+ print_address_list (o, addrs, group_list);
} else {
InternetAddressMailbox *mailbox;
const char *name;
name = internet_address_get_name (address);
addr = internet_address_mailbox_get_addr (mailbox);
+ if (check_duplicite (o, addrs, name, addr))
+ continue;
+
if (name && *name)
full_address = talloc_asprintf (o->format, "%s <%s>", name, addr);
else
}
static void
-print_address_string (const search_options_t *o, const char *recipients)
+print_address_string (const search_options_t *o, GHashTable *addrs, const char *recipients)
{
InternetAddressList *list;
if (list == NULL)
return;
- print_address_list (o, list);
+ print_address_list (o, addrs, list);
+}
+
+static void
+_my_talloc_free_for_g_hash (void *ptr)
+{
+ talloc_free (ptr);
}
static int
notmuch_messages_t *messages;
notmuch_filenames_t *filenames;
sprinter_t *format = o->format;
+ GHashTable *addresses = NULL;
int i;
+ if (o->output & OUTPUT_ADDRESSES)
+ addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
+ _my_talloc_free_for_g_hash, NULL);
+
if (o->offset < 0) {
o->offset += notmuch_query_count_messages (o->query);
if (o->offset < 0)
const char *addrs;
addrs = notmuch_message_get_header (message, "from");
- print_address_string (o, addrs);
+ print_address_string (o, addresses, addrs);
}
if (o->output & OUTPUT_RECIPIENTS) {
for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
addrs = notmuch_message_get_header (message, hdrs[j]);
- print_address_string (o, addrs);
+ print_address_string (o, addresses, addrs);
}
}
}
notmuch_message_destroy (message);
}
+ if (addresses)
+ g_hash_table_unref (addresses);
+
notmuch_messages_destroy (messages);
format->end (format);
.offset = 0,
.limit = -1, /* unlimited */
.dupe = -1,
+ .filter_flags = 0,
};
char *query_str;
int opt_index, ret;
{ NOTMUCH_OPT_INT, &o.offset, "offset", 'O', 0 },
{ NOTMUCH_OPT_INT, &o.limit, "limit", 'L', 0 },
{ NOTMUCH_OPT_INT, &o.dupe, "duplicate", 'D', 0 },
+ { NOTMUCH_OPT_KEYWORD_FLAGS, &o.filter_flags, "filter-by", 'b',
+ (notmuch_keyword_t []){ { "name", FILTER_FLAG_NAME },
+ { "addr", FILTER_FLAG_ADDR },
+ { "addrfold", FILTER_FLAG_ADDR | FILTER_FLAG_AFOLD },
+ { 0, 0 } } },
{ 0, 0, 0, 0, 0 }
};
if (! o.output)
o.output = OUTPUT_SUMMARY;
+ if (o.filter_flags && (o.output & ~OUTPUT_ADDRESSES)) {
+ fprintf (stderr, "Error: --filter_flag can only be used with address output.\n");
+ return EXIT_FAILURE;
+ }
+
switch (format_sel) {
case NOTMUCH_FORMAT_TEXT:
o.format = sprinter_text_create (config, stdout);
--- /dev/null
+#!/usr/bin/env bash
+test_description='address deduplication in "notmuch search --output=addresses"'
+. ./test-lib.sh
+
+add_message '[to]="Real Name <foo@example.com>, Real Name <bar@example.com>"'
+add_message '[to]="Nickname <foo@example.com>"' '[cc]="Real Name <Bar@Example.COM>"'
+add_message '[to]="Nickname <foo@example.com>"' '[bcc]="Real Name <Bar@Example.COM>"'
+
+test_begin_subtest "--output=recipients"
+notmuch search --output=recipients "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo@example.com>
+Real Name <bar@example.com>
+Nickname <foo@example.com>
+Real Name <Bar@Example.COM>
+Nickname <foo@example.com>
+Real Name <Bar@Example.COM>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=recipients --filter-by=addr"
+notmuch search --output=recipients --filter-by=addr "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo@example.com>
+Real Name <bar@example.com>
+Real Name <Bar@Example.COM>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=recipients --filter-by=addrfold"
+notmuch search --output=recipients --filter-by=addrfold "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo@example.com>
+Real Name <bar@example.com>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=recipients --filter-by=name"
+notmuch search --output=recipients --filter-by=name "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo@example.com>
+Nickname <foo@example.com>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=recipients --filter-by=name --filter-by=addrfold"
+notmuch search --output=recipients --filter-by=name --filter-by=addrfold "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo@example.com>
+Real Name <bar@example.com>
+Nickname <foo@example.com>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_done