From: Michal Sojka Date: Fri, 31 Oct 2014 21:53:59 +0000 (+0100) Subject: [PATCH v6 5/7] cli: search: Do not output duplicate addresses X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=de2eb82190295c3f9242bce4a2efcde198b22333;p=notmuch-archives.git [PATCH v6 5/7] cli: search: Do not output duplicate addresses --- diff --git a/85/a0dbba96fd90a1c126ca63da73533f98faa36d b/85/a0dbba96fd90a1c126ca63da73533f98faa36d new file mode 100644 index 000000000..40eaffd76 --- /dev/null +++ b/85/a0dbba96fd90a1c126ca63da73533f98faa36d @@ -0,0 +1,315 @@ +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 22E45429E32 + for ; Fri, 31 Oct 2014 14:54:40 -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 ohMM3sjlw1Wm for ; + Fri, 31 Oct 2014 14:54:32 -0700 (PDT) +Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36]) + by olra.theworths.org (Postfix) with ESMTP id 97F87431FCB + for ; Fri, 31 Oct 2014 14:54:21 -0700 (PDT) +Received: from localhost (unknown [192.168.200.7]) + by max.feld.cvut.cz (Postfix) with ESMTP id 841805CD10C + for ; Fri, 31 Oct 2014 22:54:18 +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 lE4ZSZ6EAiv0 for ; + Fri, 31 Oct 2014 22:54:15 +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 EDD7D3D3E93 + for ; Fri, 31 Oct 2014 22:54:14 +0100 (CET) +Received: from wsh by steelpick.2x.cz with local (Exim 4.84) + (envelope-from ) + id 1XkK92-0007ht-Oj; Fri, 31 Oct 2014 22:54:08 +0100 +From: Michal Sojka +To: notmuch@notmuchmail.org +Subject: [PATCH v6 5/7] cli: search: Do not output duplicate addresses +Date: Fri, 31 Oct 2014 22:53:59 +0100 +Message-Id: <1414792441-29555-6-git-send-email-sojkam1@fel.cvut.cz> +X-Mailer: git-send-email 2.1.1 +In-Reply-To: <1414792441-29555-1-git-send-email-sojkam1@fel.cvut.cz> +References: <1414792441-29555-1-git-send-email-sojkam1@fel.cvut.cz> +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit +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 21:54:40 -0000 + +This filters out duplicate addresses from address outputs (sender, +receivers). + +It also also adds tests for the new outputs. + +The code here is an extended version of a patch from Jani Nikula. +--- + doc/man1/notmuch-search.rst | 2 ++ + notmuch-search.c | 51 ++++++++++++++++++++++---- + test/T090-search-output.sh | 87 +++++++++++++++++++++++++++++++++++++++++++++ + 3 files changed, 134 insertions(+), 6 deletions(-) + +diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst +index b6607c9..42f17e4 100644 +--- a/doc/man1/notmuch-search.rst ++++ b/doc/man1/notmuch-search.rst +@@ -85,6 +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. ++ + Note: Searching for **sender** should be much faster than + searching for **recipients**, because sender addresses are + cached directly in the database whereas other addresses +diff --git a/notmuch-search.c b/notmuch-search.c +index 671fe41..43d42c6 100644 +--- a/notmuch-search.c ++++ b/notmuch-search.c +@@ -229,6 +229,27 @@ do_search_threads (search_options_t *opt) + return 0; + } + ++/* Returns TRUE iff name and addr is duplicate. */ ++static notmuch_bool_t ++is_duplicate (const search_options_t *opt, GHashTable *addrs, const char *name, const char *addr) ++{ ++ notmuch_bool_t duplicate; ++ char *key; ++ ++ key = talloc_asprintf (opt->format, "%s <%s>", name, addr); ++ if (! key) ++ return FALSE; ++ ++ duplicate = g_hash_table_lookup_extended (addrs, key, NULL, NULL); ++ ++ if (! duplicate) ++ g_hash_table_insert (addrs, key, NULL); ++ else ++ talloc_free (key); ++ ++ return duplicate; ++} ++ + static void + print_mailbox (const search_options_t *opt, const mailbox_t *mailbox) + { +@@ -263,7 +284,8 @@ print_mailbox (const search_options_t *opt, const mailbox_t *mailbox) + + /* Print addresses from InternetAddressList. */ + static void +-process_address_list (const search_options_t *opt, InternetAddressList *list) ++process_address_list (const search_options_t *opt, GHashTable *addrs, ++ InternetAddressList *list) + { + InternetAddress *address; + int i; +@@ -279,7 +301,7 @@ process_address_list (const search_options_t *opt, InternetAddressList *list) + if (group_list == NULL) + continue; + +- process_address_list (opt, group_list); ++ process_address_list (opt, addrs, group_list); + } else { + InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address); + mailbox_t mbx = { +@@ -287,6 +309,9 @@ process_address_list (const search_options_t *opt, InternetAddressList *list) + .addr = internet_address_mailbox_get_addr (mailbox), + }; + ++ if (is_duplicate (opt, addrs, mbx.name, mbx.addr)) ++ continue; ++ + print_mailbox (opt, &mbx); + } + } +@@ -294,7 +319,7 @@ process_address_list (const search_options_t *opt, InternetAddressList *list) + + /* Print addresses from a message header. */ + static void +-process_address_header (const search_options_t *opt, const char *value) ++process_address_header (const search_options_t *opt, GHashTable *addrs, const char *value) + { + InternetAddressList *list; + +@@ -305,11 +330,17 @@ process_address_header (const search_options_t *opt, const char *value) + if (list == NULL) + return; + +- process_address_list (opt, list); ++ process_address_list (opt, addrs, list); + + g_object_unref (list); + } + ++static void ++_my_talloc_free_for_g_hash (void *ptr) ++{ ++ talloc_free (ptr); ++} ++ + static int + do_search_messages (search_options_t *opt) + { +@@ -317,8 +348,13 @@ do_search_messages (search_options_t *opt) + notmuch_messages_t *messages; + notmuch_filenames_t *filenames; + sprinter_t *format = opt->format; ++ GHashTable *addresses = NULL; + int i; + ++ if (opt->output & OUTPUT_ADDRESS_FLAGS) ++ addresses = g_hash_table_new_full (g_str_hash, g_str_equal, ++ _my_talloc_free_for_g_hash, NULL); ++ + if (opt->offset < 0) { + opt->offset += notmuch_query_count_messages (opt->query); + if (opt->offset < 0) +@@ -366,7 +402,7 @@ do_search_messages (search_options_t *opt) + const char *addrs; + + addrs = notmuch_message_get_header (message, "from"); +- process_address_header (opt, addrs); ++ process_address_header (opt, addresses, addrs); + } + + if (opt->output & OUTPUT_RECIPIENTS) { +@@ -376,7 +412,7 @@ do_search_messages (search_options_t *opt) + + for (j = 0; j < ARRAY_SIZE (hdrs); j++) { + addrs = notmuch_message_get_header (message, hdrs[j]); +- process_address_header (opt, addrs); ++ process_address_header (opt, addresses, addrs); + } + } + } +@@ -384,6 +420,9 @@ do_search_messages (search_options_t *opt) + notmuch_message_destroy (message); + } + ++ if (addresses) ++ g_hash_table_unref (addresses); ++ + notmuch_messages_destroy (messages); + + format->end (format); +diff --git a/test/T090-search-output.sh b/test/T090-search-output.sh +index 947d572..82380ac 100755 +--- a/test/T090-search-output.sh ++++ b/test/T090-search-output.sh +@@ -387,6 +387,93 @@ cat <EXPECTED + EOF + test_expect_equal_file OUTPUT EXPECTED + ++test_begin_subtest "--output=sender" ++notmuch search --output=sender '*' >OUTPUT ++cat <EXPECTED ++François Boulogne ++Olivier Berger ++Chris Wilson ++Carl Worth ++Alexander Botero-Lowry ++Keith Packard ++Jjgod Jiang ++Rolland Santimano ++Jan Janak ++Stewart Smith ++Lars Kellogg-Stedman ++Alex Botero-Lowry ++Ingmar Vanhassel ++Aron Griffis ++Adrian Perez de Castro ++Israel Herraiz ++Mikhail Gusarov ++EOF ++test_expect_equal_file OUTPUT EXPECTED ++ ++test_begin_subtest "--output=sender --format=json" ++notmuch search --output=sender --format=json '*' >OUTPUT ++cat <EXPECTED ++[{"name": "François Boulogne", "address": "boulogne.f@gmail.com", "name-addr": "François Boulogne "}, ++{"name": "Olivier Berger", "address": "olivier.berger@it-sudparis.eu", "name-addr": "Olivier Berger "}, ++{"name": "Chris Wilson", "address": "chris@chris-wilson.co.uk", "name-addr": "Chris Wilson "}, ++{"name": "Carl Worth", "address": "cworth@cworth.org", "name-addr": "Carl Worth "}, ++{"name": "Alexander Botero-Lowry", "address": "alex.boterolowry@gmail.com", "name-addr": "Alexander Botero-Lowry "}, ++{"name": "Keith Packard", "address": "keithp@keithp.com", "name-addr": "Keith Packard "}, ++{"name": "Jjgod Jiang", "address": "gzjjgod@gmail.com", "name-addr": "Jjgod Jiang "}, ++{"name": "Rolland Santimano", "address": "rollandsantimano@yahoo.com", "name-addr": "Rolland Santimano "}, ++{"name": "Jan Janak", "address": "jan@ryngle.com", "name-addr": "Jan Janak "}, ++{"name": "Stewart Smith", "address": "stewart@flamingspork.com", "name-addr": "Stewart Smith "}, ++{"name": "Lars Kellogg-Stedman", "address": "lars@seas.harvard.edu", "name-addr": "Lars Kellogg-Stedman "}, ++{"name": "Alex Botero-Lowry", "address": "alex.boterolowry@gmail.com", "name-addr": "Alex Botero-Lowry "}, ++{"name": "Ingmar Vanhassel", "address": "ingmar@exherbo.org", "name-addr": "Ingmar Vanhassel "}, ++{"name": "Aron Griffis", "address": "agriffis@n01se.net", "name-addr": "Aron Griffis "}, ++{"name": "Adrian Perez de Castro", "address": "aperez@igalia.com", "name-addr": "Adrian Perez de Castro "}, ++{"name": "Israel Herraiz", "address": "isra@herraiz.org", "name-addr": "Israel Herraiz "}, ++{"name": "Mikhail Gusarov", "address": "dottedmag@dottedmag.net", "name-addr": "Mikhail Gusarov "}] ++EOF ++test_expect_equal_file OUTPUT EXPECTED ++ ++test_begin_subtest "--output=recipients" ++notmuch search --output=recipients '*' >OUTPUT ++cat <EXPECTED ++Allan McRae ++"Discussion about the Arch User Repository (AUR)" ++olivier.berger@it-sudparis.eu ++notmuch@notmuchmail.org ++notmuch ++Keith Packard ++Mikhail Gusarov ++EOF ++test_expect_equal_file OUTPUT EXPECTED ++ ++test_begin_subtest "--output=sender --output=recipients" ++notmuch search --output=sender --output=recipients '*' >OUTPUT ++cat <EXPECTED ++François Boulogne ++Allan McRae ++"Discussion about the Arch User Repository (AUR)" ++Olivier Berger ++olivier.berger@it-sudparis.eu ++Chris Wilson ++notmuch@notmuchmail.org ++Carl Worth ++Alexander Botero-Lowry ++Keith Packard ++Jjgod Jiang ++Rolland Santimano ++Jan Janak ++Stewart Smith ++Lars Kellogg-Stedman ++notmuch ++Alex Botero-Lowry ++Ingmar Vanhassel ++Aron Griffis ++Adrian Perez de Castro ++Israel Herraiz ++Mikhail Gusarov ++EOF ++test_expect_equal_file OUTPUT EXPECTED ++ + test_begin_subtest "sanitize output for quoted-printable line-breaks in author and subject" + add_message "[subject]='two =?ISO-8859-1?Q?line=0A_subject?= + headers'" +-- +2.1.1 +