From: Michal Sojka Date: Wed, 5 Nov 2014 00:25:57 +0000 (+0100) Subject: [PATCH v3 08/10] cli: address: Do not output duplicate addresses X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=b5c1d956c337c394db76504437cc6ff3fb511b67;p=notmuch-archives.git [PATCH v3 08/10] cli: address: Do not output duplicate addresses --- diff --git a/50/2f81f262aa714e766c82a5b869dcc83dc9eaa6 b/50/2f81f262aa714e766c82a5b869dcc83dc9eaa6 new file mode 100644 index 000000000..4945403b3 --- /dev/null +++ b/50/2f81f262aa714e766c82a5b869dcc83dc9eaa6 @@ -0,0 +1,281 @@ +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 DB37B431FBC + for ; Tue, 4 Nov 2014 16:26:44 -0800 (PST) +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 oQcn1OFzQJGU for ; + Tue, 4 Nov 2014 16:26:34 -0800 (PST) +Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36]) + by olra.theworths.org (Postfix) with ESMTP id 208D6431FBF + for ; Tue, 4 Nov 2014 16:26:22 -0800 (PST) +Received: from localhost (unknown [192.168.200.7]) + by max.feld.cvut.cz (Postfix) with ESMTP id 85CEC5CD1A6 + for ; Wed, 5 Nov 2014 01:26:21 +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 MoROSqm_jVtP for ; + Wed, 5 Nov 2014 01:26:18 +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 D2CB15CD1B0 + for ; Wed, 5 Nov 2014 01:26:14 +0100 (CET) +Received: from wsh by steelpick.2x.cz with local (Exim 4.84) + (envelope-from ) + id 1XloQI-0005DD-Va; Wed, 05 Nov 2014 01:26:06 +0100 +From: Michal Sojka +To: notmuch@notmuchmail.org +Subject: [PATCH v3 08/10] cli: address: Do not output duplicate addresses +Date: Wed, 5 Nov 2014 01:25:57 +0100 +Message-Id: <1415147159-19946-9-git-send-email-sojkam1@fel.cvut.cz> +X-Mailer: git-send-email 2.1.1 +In-Reply-To: <1415147159-19946-1-git-send-email-sojkam1@fel.cvut.cz> +References: <1415147159-19946-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: Wed, 05 Nov 2014 00:26:45 -0000 + +This filters out duplicate addresses from address command output. + +It also also adds tests for the address command. + +The code here is an extended version of a patch from Jani Nikula. +--- + doc/man1/notmuch-address.rst | 2 +- + notmuch-search.c | 42 ++++++++++++++++++- + test/T095-address.sh | 99 ++++++++++++++++++++++++++++++++++++++++++++ + 3 files changed, 141 insertions(+), 2 deletions(-) + create mode 100755 test/T095-address.sh + +diff --git a/doc/man1/notmuch-address.rst b/doc/man1/notmuch-address.rst +index d349237..01eb811 100644 +--- a/doc/man1/notmuch-address.rst ++++ b/doc/man1/notmuch-address.rst +@@ -11,7 +11,7 @@ DESCRIPTION + =========== + + Search for messages matching the given search terms, and display the +-addresses from them. ++addresses from them. Duplicate addresses are filtered out. + + See **notmuch-search-terms(7)** for details of the supported syntax for + . +diff --git a/notmuch-search.c b/notmuch-search.c +index e084d25..86d54ba 100644 +--- a/notmuch-search.c ++++ b/notmuch-search.c +@@ -53,6 +53,7 @@ typedef struct { + int offset; + int limit; + int dupe; ++ GHashTable *addresses; + } search_context_t; + + typedef struct { +@@ -240,6 +241,28 @@ do_search_threads (search_context_t *ctx) + return 0; + } + ++/* Returns TRUE iff name and addr is duplicate. If not, stores the ++ * name/addr pair in order to detect subsequent duplicates. */ ++static notmuch_bool_t ++is_duplicate (const search_context_t *ctx, const char *name, const char *addr) ++{ ++ notmuch_bool_t duplicate; ++ char *key; ++ ++ key = talloc_asprintf (ctx->format, "%s <%s>", name, addr); ++ if (! key) ++ return FALSE; ++ ++ duplicate = g_hash_table_lookup_extended (ctx->addresses, key, NULL, NULL); ++ ++ if (! duplicate) ++ g_hash_table_insert (ctx->addresses, key, NULL); ++ else ++ talloc_free (key); ++ ++ return duplicate; ++} ++ + static void + print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox) + { +@@ -274,7 +297,8 @@ print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox) + + /* Print addresses from InternetAddressList. */ + static void +-process_address_list (const search_context_t *ctx, InternetAddressList *list) ++process_address_list (const search_context_t *ctx, ++ InternetAddressList *list) + { + InternetAddress *address; + int i; +@@ -298,6 +322,9 @@ process_address_list (const search_context_t *ctx, InternetAddressList *list) + .addr = internet_address_mailbox_get_addr (mailbox), + }; + ++ if (is_duplicate (ctx, mbx.name, mbx.addr)) ++ continue; ++ + print_mailbox (ctx, &mbx); + } + } +@@ -321,6 +348,13 @@ process_address_header (const search_context_t *ctx, const char *value) + g_object_unref (list); + } + ++/* Destructor for talloc-allocated GHashTable keys and values. */ ++static void ++_talloc_free_for_g_hash (void *ptr) ++{ ++ talloc_free (ptr); ++} ++ + static int + _count_filenames (notmuch_message_t *message) + { +@@ -673,8 +707,14 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[]) + argc - opt_index, argv + opt_index)) + return EXIT_FAILURE; + ++ ctx->addresses = g_hash_table_new_full (g_str_hash, g_str_equal, ++ _talloc_free_for_g_hash, NULL); ++ + ret = do_search_messages (ctx); + ++ g_hash_table_unref (ctx->addresses); ++ ++ + _notmuch_search_cleanup (ctx); + + return ret ? EXIT_FAILURE : EXIT_SUCCESS; +diff --git a/test/T095-address.sh b/test/T095-address.sh +new file mode 100755 +index 0000000..0d47c0d +--- /dev/null ++++ b/test/T095-address.sh +@@ -0,0 +1,99 @@ ++#!/usr/bin/env bash ++test_description='"notmuch address" in several variants' ++. ./test-lib.sh ++ ++add_email_corpus ++ ++test_begin_subtest "--output=sender" ++notmuch address --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 address --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 address --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 address --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 "without --output" ++notmuch address '*' >OUTPUT ++# Use EXPECTED from previous subtest ++test_expect_equal_file OUTPUT EXPECTED ++ ++test_done +-- +2.1.1 +