From c39a0931a885cc1cfa00032c9c97bc81a6e01c11 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Fri, 31 Oct 2014 00:59:33 +0100 Subject: [PATCH] [PATCH v5 7/7] cli: search: Add --filter-by option to configure address filtering --- 55/053ebbc3187c3646040b751671a0d996b2359b | 339 ++++++++++++++++++++++ 1 file changed, 339 insertions(+) create mode 100644 55/053ebbc3187c3646040b751671a0d996b2359b diff --git a/55/053ebbc3187c3646040b751671a0d996b2359b b/55/053ebbc3187c3646040b751671a0d996b2359b new file mode 100644 index 000000000..069ba8682 --- /dev/null +++ b/55/053ebbc3187c3646040b751671a0d996b2359b @@ -0,0 +1,339 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 2677C431FDF + for ; Thu, 30 Oct 2014 17:00:12 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -2.3 +X-Spam-Level: +X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id NcJ1EEB2t2CV for ; + Thu, 30 Oct 2014 17:00:06 -0700 (PDT) +Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36]) + by olra.theworths.org (Postfix) with ESMTP id 0192A431E84 + for ; Thu, 30 Oct 2014 16:59:49 -0700 (PDT) +Received: from localhost (unknown [192.168.200.7]) + by max.feld.cvut.cz (Postfix) with ESMTP id 654CB5CCFF6 + for ; Fri, 31 Oct 2014 00:59:48 +0100 (CET) +X-Virus-Scanned: IMAP STYX AMAVIS +Received: from max.feld.cvut.cz ([192.168.200.1]) + by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new, + port 10044) with ESMTP id BU1HWu43zUQ7 for ; + Fri, 31 Oct 2014 00:59:44 +0100 (CET) +Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34]) + by max.feld.cvut.cz (Postfix) with ESMTP id 2E5B35CCFFC + for ; Fri, 31 Oct 2014 00:59:41 +0100 (CET) +Received: from wsh by steelpick.2x.cz with local (Exim 4.84) + (envelope-from ) + id 1Xjzcs-0005bg-Th; Fri, 31 Oct 2014 00:59:34 +0100 +From: Michal Sojka +To: notmuch@notmuchmail.org +Subject: [PATCH v5 7/7] cli: search: Add --filter-by option to configure + address filtering +Date: Fri, 31 Oct 2014 00:59:33 +0100 +Message-Id: <1414713573-21461-8-git-send-email-sojkam1@fel.cvut.cz> +X-Mailer: git-send-email 2.1.1 +In-Reply-To: <1414713573-21461-1-git-send-email-sojkam1@fel.cvut.cz> +References: <1414713573-21461-1-git-send-email-sojkam1@fel.cvut.cz> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Fri, 31 Oct 2014 00:00:12 -0000 + +This option allows to configure the criterion for duplicate address +filtering. Without this option, all unique combinations of name and +address parts are printed. This option allows to filter the output +more, for example to only contain unique address parts. +--- + completion/notmuch-completion.bash | 6 +++- + completion/notmuch-completion.zsh | 3 +- + doc/man1/notmuch-search.rst | 39 ++++++++++++++++++++++- + notmuch-search.c | 51 ++++++++++++++++++++++++++++-- + test/T095-search-filter-by.sh | 64 ++++++++++++++++++++++++++++++++++++++ + 5 files changed, 158 insertions(+), 5 deletions(-) + create mode 100755 test/T095-search-filter-by.sh + +diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash +index 39cd829..b625b02 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 + ;; ++ --filter-by) ++ COMPREPLY=( $( compgen -W "nameaddr name addr addrfold nameaddrfold" -- "${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= --filter-by=" + compopt -o nospace + COMPREPLY=( $(compgen -W "$options" -- ${cur}) ) + ;; +diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh +index d7e5a5e..c1ccc32 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 count))' ++ '--output=[select what to output]:output:((summary threads messages files tags sender recipients count))' \ ++ '--filter-by=[filter out duplicate addresses]:filter-by:((nameaddr\:"both name and address part" name\:"name part" addr\:"address part" addrfold\:"case-insensitive address part" nameaddrfold\:"name and case-insensitive address part"))' + } + + _notmuch() +diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst +index ec89200..3a5556b 100644 +--- a/doc/man1/notmuch-search.rst ++++ b/doc/man1/notmuch-search.rst +@@ -85,7 +85,8 @@ Supported options for **search** include + (--format=text0), as a JSON array (--format=json), or as + an S-Expression list (--format=sexp). + +- Duplicate addresses are filtered out. ++ Duplicate addresses are filtered out. Filtering can be ++ configured with the --filter-by option. + + Note: Searching for **sender** should be much faster than + searching for **recipients**, because sender addresses are +@@ -158,6 +159,42 @@ Supported options for **search** include + prefix. The prefix matches messages based on filenames. This + option filters filenames of the matching messages. + ++ ``--filter-by=``\ (**nameaddr**\ \|\ **name** \|\ **addr**\ \|\ **addrfold**\ \|\ **nameaddrfold**\) ++ ++ 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 the two addresses are ++ compared and this can be controlled with the following ++ keywords: ++ ++ **nameaddr** means that both name and address parts are ++ compared in case-sensitive manner. Therefore, all same looking ++ addresses strings are considered duplicate. This is the ++ default. ++ ++ **name** means that only the name part is compared (in ++ case-sensitive manner). For example, the addresses "John Doe ++ " and "John Doe " will be ++ considered duplicate. ++ ++ **addr** means that only the address part is compared (in ++ case-sensitive manner). For example, the addresses "John Doe ++ " and "Dr. John Doe " will ++ be considered duplicate. ++ ++ **addrfold** is like **addr**, but comparison is done in ++ canse-insensitive manner. For example, the addresses "John Doe ++ " and "Dr. John Doe " will ++ be considered duplicate. ++ ++ **nameaddrfold** is like **nameaddr**, but address comparison ++ is done in canse-insensitive manner. For example, the ++ addresses "John Doe " and "John Doe ++ " will be considered duplicate. ++ + EXIT STATUS + =========== + +diff --git a/notmuch-search.c b/notmuch-search.c +index 15527c4..8bc80d3 100644 +--- a/notmuch-search.c ++++ b/notmuch-search.c +@@ -35,6 +35,14 @@ typedef enum { + + #define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS | OUTPUT_COUNT) + ++typedef enum { ++ FILTER_BY_NAMEADDR = 0, ++ FILTER_BY_NAME, ++ FILTER_BY_ADDR, ++ FILTER_BY_ADDRFOLD, ++ FILTER_BY_NAMEADDRFOLD, ++} filter_by_t; ++ + typedef struct { + sprinter_t *format; + notmuch_query_t *query; +@@ -43,6 +51,7 @@ typedef struct { + int offset; + int limit; + int dupe; ++ filter_by_t filter_by; + } search_options_t; + + typedef struct { +@@ -231,7 +240,7 @@ do_search_threads (search_options_t *opt) + return 0; + } + +-/* Returns TRUE iff name and addr is duplicate. */ ++/* Returns TRUE iff name and/or addr is considered duplicate. */ + static notmuch_bool_t + is_duplicate (const search_options_t *opt, GHashTable *addrs, const char *name, const char *addr) + { +@@ -239,7 +248,32 @@ is_duplicate (const search_options_t *opt, GHashTable *addrs, const char *name, + char *key; + mailbox_t *mailbox; + +- key = talloc_asprintf (opt->format, "%s <%s>", name, addr); ++ if (opt->filter_by == FILTER_BY_ADDRFOLD || ++ opt->filter_by == FILTER_BY_NAMEADDRFOLD) { ++ gchar *folded = g_utf8_casefold (addr, -1); ++ addr = talloc_strdup (opt->format, folded); ++ g_free (folded); ++ } ++ switch (opt->filter_by) { ++ case FILTER_BY_NAMEADDR: ++ case FILTER_BY_NAMEADDRFOLD: ++ key = talloc_asprintf (opt->format, "%s <%s>", name, addr); ++ break; ++ case FILTER_BY_NAME: ++ key = talloc_strdup (opt->format, name); /* !name results in !key */ ++ break; ++ case FILTER_BY_ADDR: ++ case FILTER_BY_ADDRFOLD: ++ key = talloc_strdup (opt->format, addr); ++ break; ++ default: ++ INTERNAL_ERROR("invalid --filter-by flags"); ++ } ++ ++ if (opt->filter_by == FILTER_BY_ADDRFOLD || ++ opt->filter_by == FILTER_BY_NAMEADDRFOLD) ++ talloc_free ((char*)addr); ++ + if (! key) + return FALSE; + +@@ -523,6 +557,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) + .offset = 0, + .limit = -1, /* unlimited */ + .dupe = -1, ++ .filter_by = FILTER_BY_NAMEADDR, + }; + char *query_str; + int opt_index, ret; +@@ -567,6 +602,13 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) + { NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 }, + { NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0 }, + { NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0 }, ++ { NOTMUCH_OPT_KEYWORD, &opt.filter_by, "filter-by", 'b', ++ (notmuch_keyword_t []){ { "nameaddr", FILTER_BY_NAMEADDR }, ++ { "name", FILTER_BY_NAME }, ++ { "addr", FILTER_BY_ADDR }, ++ { "addrfold", FILTER_BY_ADDRFOLD }, ++ { "nameaddrfold", FILTER_BY_NAMEADDRFOLD }, ++ { 0, 0 } } }, + { 0, 0, 0, 0, 0 } + }; + +@@ -577,6 +619,11 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) + if (! opt.output) + opt.output = OUTPUT_SUMMARY; + ++ if (opt.filter_by && !(opt.output & OUTPUT_ADDRESS_FLAGS)) { ++ fprintf (stderr, "Error: --filter-by can only be used with address output.\n"); ++ return EXIT_FAILURE; ++ } ++ + switch (format_sel) { + case NOTMUCH_FORMAT_TEXT: + opt.format = sprinter_text_create (config, stdout); +diff --git a/test/T095-search-filter-by.sh b/test/T095-search-filter-by.sh +new file mode 100755 +index 0000000..97d9a9b +--- /dev/null ++++ b/test/T095-search-filter-by.sh +@@ -0,0 +1,64 @@ ++#!/usr/bin/env bash ++test_description='duplicite address filtering in "notmuch search --output=recipients"' ++. ./test-lib.sh ++ ++add_message '[to]="Real Name , Real Name "' ++add_message '[to]="Nickname "' '[cc]="Real Name "' ++add_message '[to]="Nickname "' '[bcc]="Real Name "' ++ ++test_begin_subtest "--output=recipients" ++notmuch search --output=recipients "*" >OUTPUT ++cat <EXPECTED ++Real Name ++Real Name ++Nickname ++Real Name ++EOF ++test_expect_equal_file OUTPUT EXPECTED ++ ++test_begin_subtest "--output=recipients --filter-by=nameaddr" ++notmuch search --output=recipients --filter-by=nameaddr "*" >OUTPUT ++# The same as above ++cat <EXPECTED ++Real Name ++Real Name ++Nickname ++Real Name ++EOF ++test_expect_equal_file OUTPUT EXPECTED ++ ++test_begin_subtest "--output=recipients --filter-by=name" ++notmuch search --output=recipients --filter-by=name "*" >OUTPUT ++cat <EXPECTED ++Real Name ++Nickname ++EOF ++test_expect_equal_file OUTPUT EXPECTED ++ ++test_begin_subtest "--output=recipients --filter-by=addr" ++notmuch search --output=recipients --filter-by=addr "*" >OUTPUT ++cat <EXPECTED ++Real Name ++Real Name ++Real Name ++EOF ++test_expect_equal_file OUTPUT EXPECTED ++ ++test_begin_subtest "--output=recipients --filter-by=addrfold" ++notmuch search --output=recipients --filter-by=addrfold "*" >OUTPUT ++cat <EXPECTED ++Real Name ++Real Name ++EOF ++test_expect_equal_file OUTPUT EXPECTED ++ ++test_begin_subtest "--output=recipients --filter-by=nameaddrfold" ++notmuch search --output=recipients --filter-by=nameaddrfold "*" >OUTPUT ++cat <EXPECTED ++Real Name ++Real Name ++Nickname ++EOF ++test_expect_equal_file OUTPUT EXPECTED ++ ++test_done +-- +2.1.1 + -- 2.26.2