[PATCH v2 4/4] cli: Add configurable address deduplication for --output=addresses

Michal Sojka sojkam1 at fel.cvut.cz
Sun Oct 5 13:51:59 PDT 2014


The code here is an extended version of a patch from Jani Nikula.
---
 completion/notmuch-completion.bash |   6 ++-
 completion/notmuch-completion.zsh  |   3 +-
 doc/man1/notmuch-search.rst        |  32 ++++++++++++
 notmuch-search.c                   | 101 ++++++++++++++++++++++++++++++++++---
 test/T090-search-output.sh         |   6 +--
 test/T095-search-unique.sh         |  63 +++++++++++++++++++++++
 6 files changed, 200 insertions(+), 11 deletions(-)
 create mode 100755 test/T095-search-unique.sh

diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash
index c37ddf5..8bc7874 100644
--- a/completion/notmuch-completion.bash
+++ b/completion/notmuch-completion.bash
@@ -305,12 +305,16 @@ _notmuch_search()
 	    COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
 	    return
 	    ;;
+	--unique)
+	    COMPREPLY=( $( compgen -W "none addr addrfold name" -- "${cur}" ) )
+	    return
+	    ;;
     esac
 
     ! $split &&
     case "${cur}" in
 	-*)
-	    local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate="
+	    local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= --unique="
 	    compopt -o nospace
 	    COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
 	    ;;
diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh
index bff8fd5..cf4968c 100644
--- a/completion/notmuch-completion.zsh
+++ b/completion/notmuch-completion.zsh
@@ -53,7 +53,8 @@ _notmuch_search()
     '--max-threads=[display only the first x threads from the search results]:number of threads to show: ' \
     '--first=[omit the first x threads from the search results]:number of threads to omit: ' \
     '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \
-    '--output=[select what to output]:output:((summary threads messages files tags sender recipients addresses))'
+    '--output=[select what to output]:output:((summary threads messages files tags sender recipients addresses))' \
+    '--unique=[address deduplication]:unique:((none\:"no deduplication" addr\:"deduplicate by address" addrfold\:"deduplicate by case-insensitive address" name\:"deduplicate by name"))'
 }
 
 _notmuch()
diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst
index 3447820..9a9d9c3 100644
--- a/doc/man1/notmuch-search.rst
+++ b/doc/man1/notmuch-search.rst
@@ -85,6 +85,9 @@ Supported options for **search** include
             (--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 --unique option.
+
 	    Note: Searching for **sender** should be much faster than
 	    searching for **recipients** or **addresses**, because
 	    sender addresses are cached directly in the database
@@ -151,6 +154,35 @@ Supported options for **search** include
         prefix. The prefix matches messages based on filenames. This
         option filters filenames of the matching messages.
 
+    ``--unique=``\ (**none**\ \|\ **addr**\ \|\ **addrfold**\ \|\ **name**)
+
+        Can be used with ``--output=addresses``, ``--output=sender``
+        or ``--output=recipients`` to control the address
+        deduplication algorithm.
+
+	**none** means that no deduplication is performed. The same
+	address can appear multiple times in the output.
+
+	**addr** means that case-sensitive deduplication is performed
+	on the address part. For example, given the addresses "John
+	Doe <john at example.com>" and "Dr. John Doe <john at example.com>",
+	only one will be printed.
+
+	**addrfold** is the same as **addr** but with case folding
+	applied. For example, given the addresses "John Doe
+	<john at example.com>" and "John Doe <JOHN at EXAMPLE.COM>", only
+	one will be printed. This is the default.
+
+	**name** means that case-sensitive deduplication is performed
+	on the name part. For example, given the addresses "John Doe
+	<john at example.com>" and "John Doe <john at doe.name>", only one
+	will be printed.
+
+	This option can be given multiple times to output unique
+	combinations of names and addresses. For example,
+	``--unique=name --unique=addr`` will print unique
+	case-sensitive combinations of name and address.
+
 EXIT STATUS
 ===========
 
diff --git a/notmuch-search.c b/notmuch-search.c
index 0614f10..94d400e 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -33,6 +33,15 @@ typedef enum {
     OUTPUT_ADDRESSES	= OUTPUT_SENDER | OUTPUT_RECIPIENTS,
 } output_t;
 
+typedef enum {
+    UNIQUE_NONE 	  = 1 << 0,
+    UNIQUE_ADDR 	  = 1 << 1,
+    UNIQUE_NAME 	  = 1 << 2,
+    UNIQUE_ADDR_CASEFOLD  = 1 << 3,
+
+    UNIQUE_BOTH = UNIQUE_NAME | UNIQUE_ADDR,
+} unique_t;
+
 typedef struct {
     sprinter_t *format;
     notmuch_query_t *query;
@@ -41,6 +50,7 @@ typedef struct {
     int offset;
     int limit;
     int dupe;
+    unique_t unique;
 } search_options_t;
 
 /* Return two stable query strings that identify exactly the matched
@@ -223,8 +233,51 @@ do_search_threads (search_options_t *o)
     return 0;
 }
 
+/* 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, InternetAddressList *list)
+print_address_list (const search_options_t *o, GHashTable *addrs,
+		    InternetAddressList *list)
 {
     InternetAddress *address;
     int i;
@@ -240,7 +293,7 @@ print_address_list (const search_options_t *o, InternetAddressList *list)
 	    if (group_list == NULL)
 		continue;
 
-	    print_address_list (o, group_list);
+	    print_address_list (o, addrs, group_list);
 	} else {
 	    InternetAddressMailbox *mailbox;
 	    const char *name;
@@ -252,6 +305,9 @@ print_address_list (const search_options_t *o, InternetAddressList *list)
 	    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
@@ -270,7 +326,7 @@ print_address_list (const search_options_t *o, InternetAddressList *list)
 }
 
 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;
 
@@ -281,7 +337,13 @@ print_address_string (const search_options_t *o, const char *recipients)
     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
@@ -291,8 +353,13 @@ do_search_messages (search_options_t *o)
     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)
@@ -340,7 +407,7 @@ do_search_messages (search_options_t *o)
 		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) {
@@ -350,7 +417,7 @@ do_search_messages (search_options_t *o)
 
 		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);
 		}
 	    }
 	}
@@ -358,6 +425,9 @@ do_search_messages (search_options_t *o)
 	notmuch_message_destroy (message);
     }
 
+    if (addresses)
+	g_hash_table_unref (addresses);
+
     notmuch_messages_destroy (messages);
 
     format->end (format);
@@ -423,6 +493,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
 	.offset = 0,
 	.limit = -1, /* unlimited */
 	.dupe = -1,
+	.unique = 0,
     };
     char *query_str;
     int opt_index, ret;
@@ -467,6 +538,12 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
 	{ 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.unique, "unique", 'u',
+	  (notmuch_keyword_t []){ { "none", UNIQUE_NONE },
+				  { "name", UNIQUE_NAME },
+				  { "addr", UNIQUE_ADDR },
+				  { "addrfold", UNIQUE_ADDR | UNIQUE_ADDR_CASEFOLD },
+				  { 0, 0 } } },
 	{ 0, 0, 0, 0, 0 }
     };
 
@@ -474,6 +551,18 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
+    if (o.unique && (o.output & ~OUTPUT_ADDRESSES)) {
+	fprintf (stderr, "Error: --unique can only be used with address output.\n");
+	return EXIT_FAILURE;
+    }
+    if ((o.unique & UNIQUE_NONE) &&
+	(o.unique & ~UNIQUE_NONE)) {
+	fprintf (stderr, "Error: --unique=none cannot be combined with other flags.\n");
+	return EXIT_FAILURE;
+    }
+    if (! o.unique)
+	o.unique = UNIQUE_ADDR | UNIQUE_ADDR_CASEFOLD;
+
     switch (format_sel) {
     case NOTMUCH_FORMAT_TEXT:
 	o.format = sprinter_text_create (config, stdout);
diff --git a/test/T090-search-output.sh b/test/T090-search-output.sh
index 5458de1..e11cb52 100755
--- a/test/T090-search-output.sh
+++ b/test/T090-search-output.sh
@@ -388,7 +388,7 @@ EOF
 test_expect_equal_file OUTPUT EXPECTED
 
 test_begin_subtest "--output=sender"
-notmuch search --output=sender '*' | sort | uniq --count >OUTPUT
+notmuch search --output=sender --unique=none '*' | sort | uniq --count >OUTPUT
 cat <<EOF >EXPECTED
       1 Adrian Perez de Castro <aperez at igalia.com>
       2 Alex Botero-Lowry <alex.boterolowry at gmail.com>
@@ -411,7 +411,7 @@ EOF
 test_expect_equal_file OUTPUT EXPECTED
 
 test_begin_subtest "--output=recipients"
-notmuch search --output=recipients '*' | sort | uniq --count >OUTPUT
+notmuch search --output=recipients --unique=none '*' | sort | uniq --count >OUTPUT
 cat <<EOF >EXPECTED
       1 Allan McRae <allan at archlinux.org>
       1 Discussion about the Arch User Repository (AUR) <aur-general at archlinux.org>
@@ -424,7 +424,7 @@ EOF
 test_expect_equal_file OUTPUT EXPECTED
 
 test_begin_subtest "--output=addresses"
-notmuch search --output=addresses '*' | sort | uniq --count >OUTPUT
+notmuch search --output=addresses --unique=none '*' | sort | uniq --count >OUTPUT
 cat <<EOF >EXPECTED
       1 Adrian Perez de Castro <aperez at igalia.com>
       2 Alex Botero-Lowry <alex.boterolowry at gmail.com>
diff --git a/test/T095-search-unique.sh b/test/T095-search-unique.sh
new file mode 100755
index 0000000..1d8afac
--- /dev/null
+++ b/test/T095-search-unique.sh
@@ -0,0 +1,63 @@
+#!/usr/bin/env bash
+test_description='address deduplication in "notmuch search --output=addresses"'
+. ./test-lib.sh
+
+add_message '[to]="Real Name <foo at example.com>, Real Name <bar at example.com>"'
+add_message '[to]="Nickname <foo at example.com>"' '[cc]="Real Name <Bar at Example.COM>"'
+add_message '[to]="Nickname <foo at example.com>"' '[bcc]="Real Name <Bar at Example.COM>"'
+
+test_begin_subtest "--output=recipients"
+notmuch search --output=recipients "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo at example.com>
+Real Name <bar at example.com>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=recipients --unique=none"
+notmuch search --output=recipients --unique=none "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo at example.com>
+Real Name <bar at example.com>
+Nickname <foo at example.com>
+Real Name <Bar at Example.COM>
+Nickname <foo at example.com>
+Real Name <Bar at Example.COM>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=recipients --unique=addr"
+notmuch search --output=recipients --unique=addr "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo at example.com>
+Real Name <bar at example.com>
+Real Name <Bar at Example.COM>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=recipients --unique=addrfold"
+notmuch search --output=recipients --unique=addrfold "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo at example.com>
+Real Name <bar at example.com>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=recipients --unique=name"
+notmuch search --output=recipients --unique=name "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo at example.com>
+Nickname <foo at example.com>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=recipients --unique=name --unique=addrfold"
+notmuch search --output=recipients --unique=name --unique=addrfold "*" >OUTPUT
+cat <<EOF >EXPECTED
+Real Name <foo at example.com>
+Real Name <bar at example.com>
+Nickname <foo at example.com>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_done
-- 
2.1.1



More information about the notmuch mailing list