From: Michal Sojka <sojkam1@fel.cvut.cz>
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 [thread overview]
Message-ID: <1414792441-29555-6-git-send-email-sojkam1@fel.cvut.cz> (raw)
In-Reply-To: <1414792441-29555-1-git-send-email-sojkam1@fel.cvut.cz>
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 <<EOF >EXPECTED
EOF
test_expect_equal_file OUTPUT EXPECTED
+test_begin_subtest "--output=sender"
+notmuch search --output=sender '*' >OUTPUT
+cat <<EOF >EXPECTED
+François Boulogne <boulogne.f@gmail.com>
+Olivier Berger <olivier.berger@it-sudparis.eu>
+Chris Wilson <chris@chris-wilson.co.uk>
+Carl Worth <cworth@cworth.org>
+Alexander Botero-Lowry <alex.boterolowry@gmail.com>
+Keith Packard <keithp@keithp.com>
+Jjgod Jiang <gzjjgod@gmail.com>
+Rolland Santimano <rollandsantimano@yahoo.com>
+Jan Janak <jan@ryngle.com>
+Stewart Smith <stewart@flamingspork.com>
+Lars Kellogg-Stedman <lars@seas.harvard.edu>
+Alex Botero-Lowry <alex.boterolowry@gmail.com>
+Ingmar Vanhassel <ingmar@exherbo.org>
+Aron Griffis <agriffis@n01se.net>
+Adrian Perez de Castro <aperez@igalia.com>
+Israel Herraiz <isra@herraiz.org>
+Mikhail Gusarov <dottedmag@dottedmag.net>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=sender --format=json"
+notmuch search --output=sender --format=json '*' >OUTPUT
+cat <<EOF >EXPECTED
+[{"name": "François Boulogne", "address": "boulogne.f@gmail.com", "name-addr": "François Boulogne <boulogne.f@gmail.com>"},
+{"name": "Olivier Berger", "address": "olivier.berger@it-sudparis.eu", "name-addr": "Olivier Berger <olivier.berger@it-sudparis.eu>"},
+{"name": "Chris Wilson", "address": "chris@chris-wilson.co.uk", "name-addr": "Chris Wilson <chris@chris-wilson.co.uk>"},
+{"name": "Carl Worth", "address": "cworth@cworth.org", "name-addr": "Carl Worth <cworth@cworth.org>"},
+{"name": "Alexander Botero-Lowry", "address": "alex.boterolowry@gmail.com", "name-addr": "Alexander Botero-Lowry <alex.boterolowry@gmail.com>"},
+{"name": "Keith Packard", "address": "keithp@keithp.com", "name-addr": "Keith Packard <keithp@keithp.com>"},
+{"name": "Jjgod Jiang", "address": "gzjjgod@gmail.com", "name-addr": "Jjgod Jiang <gzjjgod@gmail.com>"},
+{"name": "Rolland Santimano", "address": "rollandsantimano@yahoo.com", "name-addr": "Rolland Santimano <rollandsantimano@yahoo.com>"},
+{"name": "Jan Janak", "address": "jan@ryngle.com", "name-addr": "Jan Janak <jan@ryngle.com>"},
+{"name": "Stewart Smith", "address": "stewart@flamingspork.com", "name-addr": "Stewart Smith <stewart@flamingspork.com>"},
+{"name": "Lars Kellogg-Stedman", "address": "lars@seas.harvard.edu", "name-addr": "Lars Kellogg-Stedman <lars@seas.harvard.edu>"},
+{"name": "Alex Botero-Lowry", "address": "alex.boterolowry@gmail.com", "name-addr": "Alex Botero-Lowry <alex.boterolowry@gmail.com>"},
+{"name": "Ingmar Vanhassel", "address": "ingmar@exherbo.org", "name-addr": "Ingmar Vanhassel <ingmar@exherbo.org>"},
+{"name": "Aron Griffis", "address": "agriffis@n01se.net", "name-addr": "Aron Griffis <agriffis@n01se.net>"},
+{"name": "Adrian Perez de Castro", "address": "aperez@igalia.com", "name-addr": "Adrian Perez de Castro <aperez@igalia.com>"},
+{"name": "Israel Herraiz", "address": "isra@herraiz.org", "name-addr": "Israel Herraiz <isra@herraiz.org>"},
+{"name": "Mikhail Gusarov", "address": "dottedmag@dottedmag.net", "name-addr": "Mikhail Gusarov <dottedmag@dottedmag.net>"}]
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=recipients"
+notmuch search --output=recipients '*' >OUTPUT
+cat <<EOF >EXPECTED
+Allan McRae <allan@archlinux.org>
+"Discussion about the Arch User Repository (AUR)" <aur-general@archlinux.org>
+olivier.berger@it-sudparis.eu
+notmuch@notmuchmail.org
+notmuch <notmuch@notmuchmail.org>
+Keith Packard <keithp@keithp.com>
+Mikhail Gusarov <dottedmag@dottedmag.net>
+EOF
+test_expect_equal_file OUTPUT EXPECTED
+
+test_begin_subtest "--output=sender --output=recipients"
+notmuch search --output=sender --output=recipients '*' >OUTPUT
+cat <<EOF >EXPECTED
+François Boulogne <boulogne.f@gmail.com>
+Allan McRae <allan@archlinux.org>
+"Discussion about the Arch User Repository (AUR)" <aur-general@archlinux.org>
+Olivier Berger <olivier.berger@it-sudparis.eu>
+olivier.berger@it-sudparis.eu
+Chris Wilson <chris@chris-wilson.co.uk>
+notmuch@notmuchmail.org
+Carl Worth <cworth@cworth.org>
+Alexander Botero-Lowry <alex.boterolowry@gmail.com>
+Keith Packard <keithp@keithp.com>
+Jjgod Jiang <gzjjgod@gmail.com>
+Rolland Santimano <rollandsantimano@yahoo.com>
+Jan Janak <jan@ryngle.com>
+Stewart Smith <stewart@flamingspork.com>
+Lars Kellogg-Stedman <lars@seas.harvard.edu>
+notmuch <notmuch@notmuchmail.org>
+Alex Botero-Lowry <alex.boterolowry@gmail.com>
+Ingmar Vanhassel <ingmar@exherbo.org>
+Aron Griffis <agriffis@n01se.net>
+Adrian Perez de Castro <aperez@igalia.com>
+Israel Herraiz <isra@herraiz.org>
+Mikhail Gusarov <dottedmag@dottedmag.net>
+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
next prev parent reply other threads:[~2014-10-31 21:54 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-31 21:53 [PATCH v6 0/7] notmuch search --output=sender/recipients Michal Sojka
2014-10-31 21:53 ` [PATCH v6 1/7] cli: search: Refactor passing of command line options Michal Sojka
2014-10-31 21:53 ` [PATCH v6 2/7] cli: Add support for parsing keyword-flag arguments Michal Sojka
2014-10-31 21:53 ` [PATCH v6 3/7] cli: search: Convert --output to keyword-flag argument Michal Sojka
2014-10-31 21:53 ` [PATCH v6 4/7] cli: search: Add --output={sender,recipients} Michal Sojka
2014-11-01 7:14 ` David Bremner
2014-10-31 21:53 ` Michal Sojka [this message]
2014-10-31 21:54 ` [PATCH v6 6/7] cli: search: Add --output=count Michal Sojka
2014-11-01 1:16 ` Mark Walters
2014-11-02 0:53 ` Michal Sojka
2014-11-02 9:29 ` Mark Walters
2014-11-02 9:34 ` Michal Sojka
2014-11-03 9:44 ` Tomi Ollila
2014-11-03 21:39 ` Michal Sojka
2014-10-31 21:54 ` [PATCH v6 7/7] cli: search: Add --filter-by option to configure address filtering Michal Sojka
2014-11-01 8:55 ` Jani Nikula
2014-11-01 9:21 ` Michal Sojka
2014-11-01 11:30 ` [RFC PATCH 0/2] cli: split 'notmuch address' from 'notmuch search' Jani Nikula
2014-11-01 11:30 ` [RFC PATCH 1/2] cli: add support for hierarchical command line option arrays Jani Nikula
2014-11-01 11:30 ` [RFC PATCH 2/2] cli: add notmuch address command Jani Nikula
2014-11-04 9:14 ` [RFC PATCH 0/2] cli: split 'notmuch address' from 'notmuch search' Mark Walters
2014-11-01 0:22 ` [PATCH v6 0/7] notmuch search --output=sender/recipients Mark Walters
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1414792441-29555-6-git-send-email-sojkam1@fel.cvut.cz \
--to=sojkam1@fel.cvut.cz \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).