From c4fda9359b171ed5fdae109b62a8e7e34d3f89ac Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Fri, 31 Oct 2014 22:53:58 +0100 Subject: [PATCH] [PATCH v6 4/7] cli: search: Add --output={sender,recipients} --- f3/4e2b894d0ceb296a6307fdc6f79771afb004b4 | 311 ++++++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100644 f3/4e2b894d0ceb296a6307fdc6f79771afb004b4 diff --git a/f3/4e2b894d0ceb296a6307fdc6f79771afb004b4 b/f3/4e2b894d0ceb296a6307fdc6f79771afb004b4 new file mode 100644 index 000000000..e2fbe7bfe --- /dev/null +++ b/f3/4e2b894d0ceb296a6307fdc6f79771afb004b4 @@ -0,0 +1,311 @@ +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 18D8D431FD5 + for ; Fri, 31 Oct 2014 14:54:28 -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 v3bliKDCBR8e for ; + Fri, 31 Oct 2014 14:54:20 -0700 (PDT) +Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36]) + by olra.theworths.org (Postfix) with ESMTP id 053D9431FBC + for ; Fri, 31 Oct 2014 14:54:19 -0700 (PDT) +Received: from localhost (unknown [192.168.200.7]) + by max.feld.cvut.cz (Postfix) with ESMTP id 472565CD10E + for ; Fri, 31 Oct 2014 22:54:19 +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 ouMaMJScYFDy for ; + Fri, 31 Oct 2014 22:54:14 +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 B516B5CD10D + 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-0007hp-Na; Fri, 31 Oct 2014 22:54:08 +0100 +From: Michal Sojka +To: notmuch@notmuchmail.org +Subject: [PATCH v6 4/7] cli: search: Add --output={sender,recipients} +Date: Fri, 31 Oct 2014 22:53:58 +0100 +Message-Id: <1414792441-29555-5-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> +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:28 -0000 + +The new outputs allow printing senders, recipients or both of matching +messages. To print both, the user can use --output=sender and +--output=recipients simultaneously. + +Currently, the same address can appear multiple times in the output. +The next commit will change this. For this reason, tests are +introduced there. + +We use mailbox_t rather than InternetAddressMailbox because we will +need to extend it in a following commit. + +This code is based on a patch from Jani Nikula. +--- + completion/notmuch-completion.bash | 2 +- + completion/notmuch-completion.zsh | 3 +- + doc/man1/notmuch-search.rst | 22 ++++++- + notmuch-search.c | 115 ++++++++++++++++++++++++++++++++++++- + 4 files changed, 137 insertions(+), 5 deletions(-) + +diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash +index 0571dc9..cfbd389 100644 +--- a/completion/notmuch-completion.bash ++++ b/completion/notmuch-completion.bash +@@ -294,7 +294,7 @@ _notmuch_search() + return + ;; + --output) +- COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) ) ++ COMPREPLY=( $( compgen -W "summary threads messages files tags sender recipients" -- "${cur}" ) ) + return + ;; + --sort) +diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh +index 67a9aba..3e52a00 100644 +--- a/completion/notmuch-completion.zsh ++++ b/completion/notmuch-completion.zsh +@@ -52,7 +52,8 @@ _notmuch_search() + _arguments -s : \ + '--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"))' ++ '--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))' + } + + _notmuch() +diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst +index 90160f2..b6607c9 100644 +--- a/doc/man1/notmuch-search.rst ++++ b/doc/man1/notmuch-search.rst +@@ -35,7 +35,7 @@ Supported options for **search** include + intended for programs that invoke **notmuch(1)** internally. If + omitted, the latest supported version will be used. + +- ``--output=(summary|threads|messages|files|tags)`` ++ ``--output=(summary|threads|messages|files|tags|sender|recipients)`` + + **summary** + Output a summary of each thread with any message matching +@@ -78,6 +78,26 @@ Supported options for **search** include + by null characters (--format=text0), as a JSON array + (--format=json), or as an S-Expression list (--format=sexp). + ++ **sender** ++ Output all addresses from the *From* header that appear on ++ any message matching the search terms, either one per line ++ (--format=text), separated by null characters ++ (--format=text0), as a JSON array (--format=json), or as ++ an S-Expression list (--format=sexp). ++ ++ Note: Searching for **sender** should be much faster than ++ searching for **recipients**, because sender addresses are ++ cached directly in the database whereas other addresses ++ need to be fetched from message files. ++ ++ **recipients** ++ Like **sender** but for addresses from *To*, *Cc* and ++ *Bcc* headers. ++ ++ This option can be given multiple times to combine different ++ outputs. Currently, this is only supported for **sender** and ++ **recipients** outputs. ++ + ``--sort=``\ (**newest-first**\ \|\ **oldest-first**) + This option can be used to present results in either + chronological order (**oldest-first**) or reverse chronological +diff --git a/notmuch-search.c b/notmuch-search.c +index ce46877..671fe41 100644 +--- a/notmuch-search.c ++++ b/notmuch-search.c +@@ -28,8 +28,12 @@ typedef enum { + OUTPUT_MESSAGES = 1 << 2, + OUTPUT_FILES = 1 << 3, + OUTPUT_TAGS = 1 << 4, ++ OUTPUT_SENDER = 1 << 5, ++ OUTPUT_RECIPIENTS = 1 << 6, + } output_t; + ++#define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS) ++ + typedef struct { + sprinter_t *format; + notmuch_query_t *query; +@@ -40,6 +44,11 @@ typedef struct { + int dupe; + } search_options_t; + ++typedef struct { ++ const char *name; ++ const char *addr; ++} mailbox_t; ++ + /* Return two stable query strings that identify exactly the matched + * and unmatched messages currently in thread. If there are no + * matched or unmatched messages, the returned buffers will be +@@ -220,6 +229,87 @@ do_search_threads (search_options_t *opt) + return 0; + } + ++static void ++print_mailbox (const search_options_t *opt, const mailbox_t *mailbox) ++{ ++ const char *name = mailbox->name; ++ const char *addr = mailbox->addr; ++ sprinter_t *format = opt->format; ++ InternetAddress *ia = internet_address_mailbox_new (name, addr); ++ char *name_addr; ++ ++ /* name_addr has the name part quoted if necessary. Compare ++ * 'John Doe ' vs. '"Doe, John" ' */ ++ name_addr = internet_address_to_string (ia, FALSE); ++ ++ if (format->is_text_printer) { ++ format->string (format, name_addr); ++ format->separator (format); ++ } else { ++ format->begin_map (format); ++ format->map_key (format, "name"); ++ format->string (format, name); ++ format->map_key (format, "address"); ++ format->string (format, addr); ++ format->map_key (format, "name-addr"); ++ format->string (format, name_addr); ++ format->end (format); ++ format->separator (format); ++ } ++ ++ g_object_unref (ia); ++ g_free (name_addr); ++} ++ ++/* Print addresses from InternetAddressList. */ ++static void ++process_address_list (const search_options_t *opt, InternetAddressList *list) ++{ ++ InternetAddress *address; ++ int i; ++ ++ for (i = 0; i < internet_address_list_length (list); i++) { ++ address = internet_address_list_get_address (list, i); ++ if (INTERNET_ADDRESS_IS_GROUP (address)) { ++ InternetAddressGroup *group; ++ InternetAddressList *group_list; ++ ++ group = INTERNET_ADDRESS_GROUP (address); ++ group_list = internet_address_group_get_members (group); ++ if (group_list == NULL) ++ continue; ++ ++ process_address_list (opt, group_list); ++ } else { ++ InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address); ++ mailbox_t mbx = { ++ .name = internet_address_get_name (address), ++ .addr = internet_address_mailbox_get_addr (mailbox), ++ }; ++ ++ print_mailbox (opt, &mbx); ++ } ++ } ++} ++ ++/* Print addresses from a message header. */ ++static void ++process_address_header (const search_options_t *opt, const char *value) ++{ ++ InternetAddressList *list; ++ ++ if (value == NULL) ++ return; ++ ++ list = internet_address_list_parse_string (value); ++ if (list == NULL) ++ return; ++ ++ process_address_list (opt, list); ++ ++ g_object_unref (list); ++} ++ + static int + do_search_messages (search_options_t *opt) + { +@@ -266,11 +356,29 @@ do_search_messages (search_options_t *opt) + + notmuch_filenames_destroy( filenames ); + +- } else { /* output == OUTPUT_MESSAGES */ ++ } else if (opt->output == OUTPUT_MESSAGES) { + format->set_prefix (format, "id"); + format->string (format, + notmuch_message_get_message_id (message)); + format->separator (format); ++ } else { ++ if (opt->output & OUTPUT_SENDER) { ++ const char *addrs; ++ ++ addrs = notmuch_message_get_header (message, "from"); ++ process_address_header (opt, addrs); ++ } ++ ++ if (opt->output & OUTPUT_RECIPIENTS) { ++ const char *hdrs[] = { "to", "cc", "bcc" }; ++ const char *addrs; ++ size_t j; ++ ++ for (j = 0; j < ARRAY_SIZE (hdrs); j++) { ++ addrs = notmuch_message_get_header (message, hdrs[j]); ++ process_address_header (opt, addrs); ++ } ++ } + } + + notmuch_message_destroy (message); +@@ -371,6 +479,8 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) + (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY }, + { "threads", OUTPUT_THREADS }, + { "messages", OUTPUT_MESSAGES }, ++ { "sender", OUTPUT_SENDER }, ++ { "recipients", OUTPUT_RECIPIENTS }, + { "files", OUTPUT_FILES }, + { "tags", OUTPUT_TAGS }, + { 0, 0 } } }, +@@ -462,7 +572,8 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) + opt.output == OUTPUT_THREADS) + ret = do_search_threads (&opt); + else if (opt.output == OUTPUT_MESSAGES || +- opt.output == OUTPUT_FILES) ++ opt.output == OUTPUT_FILES || ++ (opt.output & OUTPUT_ADDRESS_FLAGS && !(opt.output & ~OUTPUT_ADDRESS_FLAGS))) + ret = do_search_messages (&opt); + else if (opt.output == OUTPUT_TAGS) + ret = do_search_tags (notmuch, &opt); +-- +2.1.1 + -- 2.26.2