--- /dev/null
+Return-Path: <sojkam1@fel.cvut.cz>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 9C03E431FC2\r
+ for <notmuch@notmuchmail.org>; Thu, 30 Oct 2014 17:00:01 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id WrXDYXUEyIl4 for <notmuch@notmuchmail.org>;\r
+ Thu, 30 Oct 2014 16:59:57 -0700 (PDT)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+ by olra.theworths.org (Postfix) with ESMTP id 0A4AF431FDD\r
+ for <notmuch@notmuchmail.org>; Thu, 30 Oct 2014 16:59:46 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 6F2105CCFF4\r
+ for <notmuch@notmuchmail.org>; Fri, 31 Oct 2014 00:59:45 +0100 (CET)\r
+X-Virus-Scanned: IMAP STYX AMAVIS\r
+Received: from max.feld.cvut.cz ([192.168.200.1])\r
+ by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,\r
+ port 10044) with ESMTP id JMar2SnvSiS3 for <notmuch@notmuchmail.org>;\r
+ Fri, 31 Oct 2014 00:59:40 +0100 (CET)\r
+Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 9C7065CCFF9\r
+ for <notmuch@notmuchmail.org>; Fri, 31 Oct 2014 00:59:40 +0100 (CET)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1Xjzcs-0005bS-RQ; Fri, 31 Oct 2014 00:59:34 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v5 4/7] cli: search: Add --output={sender,recipients}\r
+Date: Fri, 31 Oct 2014 00:59:30 +0100\r
+Message-Id: <1414713573-21461-5-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.1\r
+In-Reply-To: <1414713573-21461-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1414713573-21461-1-git-send-email-sojkam1@fel.cvut.cz>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 31 Oct 2014 00:00:01 -0000\r
+\r
+The new outputs allow printing senders, recipients or both of matching\r
+messages. To print both, the user can use --output=sender and\r
+--output=recipients simultaneously.\r
+\r
+Currently, the same address can appear multiple times in the output.\r
+The next commit will change this. For this reason, tests are\r
+introduced there.\r
+\r
+We use mailbox_t rather than InternetAddressMailbox because we will\r
+need to extend it in a following commit.\r
+\r
+This code is based on a patch from Jani Nikula.\r
+---\r
+ completion/notmuch-completion.bash | 2 +-\r
+ completion/notmuch-completion.zsh | 3 +-\r
+ doc/man1/notmuch-search.rst | 22 ++++++-\r
+ notmuch-search.c | 115 ++++++++++++++++++++++++++++++++++++-\r
+ 4 files changed, 137 insertions(+), 5 deletions(-)\r
+\r
+diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash\r
+index 0571dc9..cfbd389 100644\r
+--- a/completion/notmuch-completion.bash\r
++++ b/completion/notmuch-completion.bash\r
+@@ -294,7 +294,7 @@ _notmuch_search()\r
+ return\r
+ ;;\r
+ --output)\r
+- COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )\r
++ COMPREPLY=( $( compgen -W "summary threads messages files tags sender recipients" -- "${cur}" ) )\r
+ return\r
+ ;;\r
+ --sort)\r
+diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh\r
+index 67a9aba..3e52a00 100644\r
+--- a/completion/notmuch-completion.zsh\r
++++ b/completion/notmuch-completion.zsh\r
+@@ -52,7 +52,8 @@ _notmuch_search()\r
+ _arguments -s : \\r
+ '--max-threads=[display only the first x threads from the search results]:number of threads to show: ' \\r
+ '--first=[omit the first x threads from the search results]:number of threads to omit: ' \\r
+- '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))'\r
++ '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \\r
++ '--output=[select what to output]:output:((summary threads messages files tags sender recipients))'\r
+ }\r
+ \r
+ _notmuch()\r
+diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst\r
+index 90160f2..b6607c9 100644\r
+--- a/doc/man1/notmuch-search.rst\r
++++ b/doc/man1/notmuch-search.rst\r
+@@ -35,7 +35,7 @@ Supported options for **search** include\r
+ intended for programs that invoke **notmuch(1)** internally. If\r
+ omitted, the latest supported version will be used.\r
+ \r
+- ``--output=(summary|threads|messages|files|tags)``\r
++ ``--output=(summary|threads|messages|files|tags|sender|recipients)``\r
+ \r
+ **summary**\r
+ Output a summary of each thread with any message matching\r
+@@ -78,6 +78,26 @@ Supported options for **search** include\r
+ by null characters (--format=text0), as a JSON array\r
+ (--format=json), or as an S-Expression list (--format=sexp).\r
+ \r
++ **sender**\r
++ Output all addresses from the *From* header that appear on\r
++ any message matching the search terms, either one per line\r
++ (--format=text), separated by null characters\r
++ (--format=text0), as a JSON array (--format=json), or as\r
++ an S-Expression list (--format=sexp).\r
++\r
++ Note: Searching for **sender** should be much faster than\r
++ searching for **recipients**, because sender addresses are\r
++ cached directly in the database whereas other addresses\r
++ need to be fetched from message files.\r
++\r
++ **recipients**\r
++ Like **sender** but for addresses from *To*, *Cc* and\r
++ *Bcc* headers.\r
++\r
++ This option can be given multiple times to combine different\r
++ outputs. Currently, this is only supported for **sender** and\r
++ **recipients** outputs.\r
++\r
+ ``--sort=``\ (**newest-first**\ \|\ **oldest-first**)\r
+ This option can be used to present results in either\r
+ chronological order (**oldest-first**) or reverse chronological\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index ce46877..31e4221 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -28,8 +28,12 @@ typedef enum {\r
+ OUTPUT_MESSAGES = 1 << 2,\r
+ OUTPUT_FILES = 1 << 3,\r
+ OUTPUT_TAGS = 1 << 4,\r
++ OUTPUT_SENDER = 1 << 5,\r
++ OUTPUT_RECIPIENTS = 1 << 6,\r
+ } output_t;\r
+ \r
++#define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS)\r
++\r
+ typedef struct {\r
+ sprinter_t *format;\r
+ notmuch_query_t *query;\r
+@@ -40,6 +44,11 @@ typedef struct {\r
+ int dupe;\r
+ } search_options_t;\r
+ \r
++typedef struct {\r
++ const char *name;\r
++ const char *addr;\r
++} mailbox_t;\r
++\r
+ /* Return two stable query strings that identify exactly the matched\r
+ * and unmatched messages currently in thread. If there are no\r
+ * matched or unmatched messages, the returned buffers will be\r
+@@ -220,6 +229,87 @@ do_search_threads (search_options_t *opt)\r
+ return 0;\r
+ }\r
+ \r
++static void\r
++print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)\r
++{\r
++ const char *name = mailbox->name;\r
++ const char *addr = mailbox->addr;\r
++ sprinter_t *format = opt->format;\r
++\r
++ if (format->is_text_printer) {\r
++ char *mailbox_str;\r
++\r
++ if (name && *name)\r
++ mailbox_str = talloc_asprintf (format, "%s <%s>", name, addr);\r
++ else\r
++ mailbox_str = talloc_strdup (format, addr);\r
++\r
++ if (! mailbox_str) {\r
++ fprintf (stderr, "Error: out of memory\n");\r
++ return;\r
++ }\r
++ format->string (format, mailbox_str);\r
++ format->separator (format);\r
++\r
++ talloc_free (mailbox_str);\r
++ } else {\r
++ format->begin_map (format);\r
++ format->map_key (format, "name");\r
++ format->string (format, name);\r
++ format->map_key (format, "address");\r
++ format->string (format, addr);\r
++ format->end (format);\r
++ format->separator (format);\r
++ }\r
++}\r
++\r
++/* Print addresses from InternetAddressList. */\r
++static void\r
++process_address_list (const search_options_t *opt, InternetAddressList *list)\r
++{\r
++ InternetAddress *address;\r
++ int i;\r
++\r
++ for (i = 0; i < internet_address_list_length (list); i++) {\r
++ address = internet_address_list_get_address (list, i);\r
++ if (INTERNET_ADDRESS_IS_GROUP (address)) {\r
++ InternetAddressGroup *group;\r
++ InternetAddressList *group_list;\r
++\r
++ group = INTERNET_ADDRESS_GROUP (address);\r
++ group_list = internet_address_group_get_members (group);\r
++ if (group_list == NULL)\r
++ continue;\r
++\r
++ process_address_list (opt, group_list);\r
++ } else {\r
++ InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);\r
++ mailbox_t mbx = {\r
++ .name = internet_address_get_name (address),\r
++ .addr = internet_address_mailbox_get_addr (mailbox),\r
++ };\r
++\r
++ print_mailbox (opt, &mbx);\r
++ }\r
++ }\r
++}\r
++\r
++/* Print addresses from a message header. */\r
++static void\r
++process_address_header (const search_options_t *opt, const char *value)\r
++{\r
++ InternetAddressList *list;\r
++\r
++ if (value == NULL)\r
++ return;\r
++\r
++ list = internet_address_list_parse_string (value);\r
++ if (list == NULL)\r
++ return;\r
++\r
++ process_address_list (opt, list);\r
++}\r
++\r
+ static int\r
+ do_search_messages (search_options_t *opt)\r
+ {\r
+@@ -266,11 +356,29 @@ do_search_messages (search_options_t *opt)\r
+ \r
+ notmuch_filenames_destroy( filenames );\r
+ \r
+- } else { /* output == OUTPUT_MESSAGES */\r
++ } else if (opt->output == OUTPUT_MESSAGES) {\r
+ format->set_prefix (format, "id");\r
+ format->string (format,\r
+ notmuch_message_get_message_id (message));\r
+ format->separator (format);\r
++ } else {\r
++ if (opt->output & OUTPUT_SENDER) {\r
++ const char *addrs;\r
++\r
++ addrs = notmuch_message_get_header (message, "from");\r
++ process_address_header (opt, addrs);\r
++ }\r
++\r
++ if (opt->output & OUTPUT_RECIPIENTS) {\r
++ const char *hdrs[] = { "to", "cc", "bcc" };\r
++ const char *addrs;\r
++ size_t j;\r
++\r
++ for (j = 0; j < ARRAY_SIZE (hdrs); j++) {\r
++ addrs = notmuch_message_get_header (message, hdrs[j]);\r
++ process_address_header (opt, addrs);\r
++ }\r
++ }\r
+ }\r
+ \r
+ notmuch_message_destroy (message);\r
+@@ -371,6 +479,8 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },\r
+ { "threads", OUTPUT_THREADS },\r
+ { "messages", OUTPUT_MESSAGES },\r
++ { "sender", OUTPUT_SENDER },\r
++ { "recipients", OUTPUT_RECIPIENTS },\r
+ { "files", OUTPUT_FILES },\r
+ { "tags", OUTPUT_TAGS },\r
+ { 0, 0 } } },\r
+@@ -462,7 +572,8 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+ opt.output == OUTPUT_THREADS)\r
+ ret = do_search_threads (&opt);\r
+ else if (opt.output == OUTPUT_MESSAGES ||\r
+- opt.output == OUTPUT_FILES)\r
++ opt.output == OUTPUT_FILES ||\r
++ (opt.output & OUTPUT_ADDRESS_FLAGS && !(opt.output & ~OUTPUT_ADDRESS_FLAGS)))\r
+ ret = do_search_messages (&opt);\r
+ else if (opt.output == OUTPUT_TAGS)\r
+ ret = do_search_tags (notmuch, &opt);\r
+-- \r
+2.1.1\r
+\r