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 91A25431FDC for ; Sun, 12 Oct 2014 14:42:04 -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 88L-L+bd0e5d for ; Sun, 12 Oct 2014 14:41:59 -0700 (PDT) Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36]) by olra.theworths.org (Postfix) with ESMTP id 90689431FDE for ; Sun, 12 Oct 2014 14:41:52 -0700 (PDT) Received: from localhost (unknown [192.168.200.7]) by max.feld.cvut.cz (Postfix) with ESMTP id 3BE655CCF10 for ; Sun, 12 Oct 2014 23:41:50 +0200 (CEST) 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 RUoNIur_7BCd for ; Sun, 12 Oct 2014 23:41:46 +0200 (CEST) Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34]) by max.feld.cvut.cz (Postfix) with ESMTP id 463D35CCF22 for ; Sun, 12 Oct 2014 23:41:45 +0200 (CEST) Received: from wsh by steelpick.2x.cz with local (Exim 4.84) (envelope-from ) id 1XdQtb-0002CM-7J; Sun, 12 Oct 2014 23:41:43 +0200 From: Michal Sojka To: notmuch@notmuchmail.org Subject: [PATCH v3 4/4] cli: Add an option to filter our duplicate addresses Date: Sun, 12 Oct 2014 23:41:33 +0200 Message-Id: <1413150093-8383-5-git-send-email-sojkam1@fel.cvut.cz> X-Mailer: git-send-email 2.1.1 In-Reply-To: <1413150093-8383-1-git-send-email-sojkam1@fel.cvut.cz> References: <87zjd51phx.fsf@steelpick.2x.cz> <1413150093-8383-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: Sun, 12 Oct 2014 21:42:04 -0000 This adds a --filter-by option to "notmuch search". It can be used to filter out duplicate addresses in --output=sender/receivers. 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 | 93 +++++++++++++++++++++++++++++++++++--- test/T095-search-filter-by.sh | 55 ++++++++++++++++++++++ 5 files changed, 181 insertions(+), 8 deletions(-) create mode 100755 test/T095-search-filter-by.sh diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash index cfbd389..41dd85b 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 "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= --filter-by=" compopt -o nospace COMPREPLY=( $(compgen -W "$options" -- ${cur}) ) ;; diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh index 3e52a00..17b345f 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))' + '--output=[select what to output]:output:((summary threads messages files tags sender recipients))' \ + '--filter-by=[filter out duplicate addresses]:filter-by:((addr\:"address part" addrfold\:"case-insensitive address part" name\:"name part"))' } _notmuch() diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst index c9d38b1..0fed76e 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 --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 @@ -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. + ``--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 + " and "Dr. John Doe " 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 " and "Dr. John Doe + " will be considered duplicate. + + **name** means that the name part is compared in case-sensitive + manner. For example, the addresses "John Doe " + and "John Doe " 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 =========== diff --git a/notmuch-search.c b/notmuch-search.c index 74588f8..df678ad 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -33,6 +33,12 @@ typedef enum { 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; @@ -41,6 +47,7 @@ typedef struct { int offset; int limit; int dupe; + filter_flag_t filter_flags; } search_options_t; /* Return two stable query strings that identify exactly the matched @@ -223,8 +230,54 @@ do_search_threads (search_options_t *o) 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; @@ -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_duplicite (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, + .filter_flags = 0, }; char *query_str; int opt_index, ret; @@ -466,6 +537,11 @@ 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.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 } }; @@ -476,6 +552,11 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) 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); diff --git a/test/T095-search-filter-by.sh b/test/T095-search-filter-by.sh new file mode 100755 index 0000000..20d6a34 --- /dev/null +++ b/test/T095-search-filter-by.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +test_description='address deduplication in "notmuch search --output=addresses"' +. ./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 +Nickname +Real Name +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=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=name --filter-by=addrfold" +notmuch search --output=recipients --filter-by=name --filter-by=addrfold "*" >OUTPUT +cat <EXPECTED +Real Name +Real Name +Nickname +EOF +test_expect_equal_file OUTPUT EXPECTED + +test_done -- 2.1.1