From: Mark Walters Date: Thu, 30 Oct 2014 08:10:11 +0000 (+0000) Subject: Re: [PATCH v4 4/6] cli: search: Add --output={sender,recipients} X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=b648365bc41bf4158a70b97c8c4df4b3e22983e5;p=notmuch-archives.git Re: [PATCH v4 4/6] cli: search: Add --output={sender,recipients} --- diff --git a/48/1b79df44f4475ceb6087e50f7f1ab2699ff34c b/48/1b79df44f4475ceb6087e50f7f1ab2699ff34c new file mode 100644 index 000000000..28f86e2c7 --- /dev/null +++ b/48/1b79df44f4475ceb6087e50f7f1ab2699ff34c @@ -0,0 +1,345 @@ +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 2A76C431FC7 + for ; Thu, 30 Oct 2014 01:10:28 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -1.098 +X-Spam-Level: +X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5 + tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001, + NML_ADSP_CUSTOM_MED=1.2, 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 q3uYmRTXMrNq for ; + Thu, 30 Oct 2014 01:10:20 -0700 (PDT) +Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 2E694431FB6 + for ; Thu, 30 Oct 2014 01:10:20 -0700 (PDT) +Received: from smtp.qmul.ac.uk ([138.37.6.40]) + by mail2.qmul.ac.uk with esmtp (Exim 4.71) + (envelope-from ) + id 1Xjko9-0004Oe-9t; Thu, 30 Oct 2014 08:10:18 +0000 +Received: from 5751dfa2.skybroadband.com ([87.81.223.162] helo=localhost) + by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71) + (envelope-from ) + id 1Xjko8-000887-AY; Thu, 30 Oct 2014 08:10:12 +0000 +From: Mark Walters +To: Michal Sojka , notmuch@notmuchmail.org +Subject: Re: [PATCH v4 4/6] cli: search: Add --output={sender,recipients} +In-Reply-To: <1414421455-3037-5-git-send-email-sojkam1@fel.cvut.cz> +References: <1414421455-3037-1-git-send-email-sojkam1@fel.cvut.cz> + <1414421455-3037-5-git-send-email-sojkam1@fel.cvut.cz> +User-Agent: Notmuch/0.18.1+86~gef5e66a (http://notmuchmail.org) Emacs/23.4.1 + (x86_64-pc-linux-gnu) +Date: Thu, 30 Oct 2014 08:10:11 +0000 +Message-ID: <87h9ymugf0.fsf@qmul.ac.uk> +MIME-Version: 1.0 +Content-Type: text/plain; charset=us-ascii +X-Sender-Host-Address: 87.81.223.162 +X-QM-Geographic: According to ripencc, + this message was delivered by a machine in Britain (UK) (GB). +X-QM-SPAM-Info: Sender has good ham record. :) +X-QM-Body-MD5: dce63ec75da32219de800dd1bb597c76 (of first 20000 bytes) +X-SpamAssassin-Score: -0.1 +X-SpamAssassin-SpamBar: / +X-SpamAssassin-Report: The QM spam filters have analysed this message to + determine if it is + spam. We require at least 5.0 points to mark a message as spam. + This message scored -0.1 points. + Summary of the scoring: + * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail + provider * (markwalters1009[at]gmail.com) + * -0.1 AWL AWL: From: address is in the auto white-list +X-QM-Scan-Virus: ClamAV says the message is clean +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: Thu, 30 Oct 2014 08:10:28 -0000 + +On Mon, 27 Oct 2014, Michal Sojka wrote: +> 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 patch will change this. For this reason, the test are introduced in +> the next patch as well. +> +> We use mailbox_t rather than InternetAddressMailbox because we will need +> to extend it in a following patch. +> +> This code 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 | 112 ++++++++++++++++++++++++++++++++++++- +> 4 files changed, 134 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..ce3bfb2 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,84 @@ 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; +> + +> + if (opt->format->is_text_printer) { +> + char *mailbox_str; +> + +> + if (name && *name) +> + mailbox_str = talloc_asprintf (opt->format, "%s <%s>", name, addr); +> + else +> + mailbox_str = talloc_strdup (opt->format, addr); +> + +> + if (! mailbox_str) { +> + fprintf (stderr, "Error: out of memory\n"); +> + return; +> + } +> + opt->format->string (opt->format, mailbox_str); +> + opt->format->separator (opt->format); +> + +> + talloc_free (mailbox_str); +> + } else { +> + opt->format->begin_map (opt->format); +> + opt->format->map_key (opt->format, "name"); +> + opt->format->string (opt->format, name); +> + opt->format->map_key (opt->format, "address"); +> + opt->format->string (opt->format, addr); +> + opt->format->end (opt->format); +> + opt->format->separator (opt->format); +> + } +> +} + +How about letting format = opt->format in all the above. I think it +would improve readability and look more similar to the other cases. + +> + +> +static void +> +process_address_list (const search_options_t *opt, InternetAddressList *list) +> +{ + +It might be worth documenting these functions: just something brief +saying what arguments are expected and what is returned. + +Otherwise LGTM. + +Best wishes + +Mark + + +> + 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); +> + } +> + } +> +} +> + +> +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); +> +} +> + +> static int +> do_search_messages (search_options_t *opt) +> { +> @@ -266,11 +353,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 +476,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 +569,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 +> +> _______________________________________________ +> notmuch mailing list +> notmuch@notmuchmail.org +> http://notmuchmail.org/mailman/listinfo/notmuch