[PATCH v4 4/6] cli: search: Add --output={sender,recipients}
authorMichal Sojka <sojkam1@fel.cvut.cz>
Mon, 27 Oct 2014 14:50:53 +0000 (15:50 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:05:42 +0000 (10:05 -0800)
28/e0cecbf67889850074408b3fc2c2cc4bbf4334 [new file with mode: 0644]

diff --git a/28/e0cecbf67889850074408b3fc2c2cc4bbf4334 b/28/e0cecbf67889850074408b3fc2c2cc4bbf4334
new file mode 100644 (file)
index 0000000..cd40d9d
--- /dev/null
@@ -0,0 +1,308 @@
+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 F3627431E84\r
+       for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 07:51:18 -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 wOZjTa-O--B1 for <notmuch@notmuchmail.org>;\r
+       Mon, 27 Oct 2014 07:51:14 -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 6071F431FC3\r
+       for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 07:51:14 -0700 (PDT)\r
+Received: from localhost (unknown [192.168.200.7])\r
+       by max.feld.cvut.cz (Postfix) with ESMTP id 7F6625CCF9F\r
+       for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 15:51:13 +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 dZ6NSsgk-BVZ for <notmuch@notmuchmail.org>;\r
+       Mon, 27 Oct 2014 15:51:08 +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 9A5225CCF93\r
+       for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 15:51:07 +0100 (CET)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
+       (envelope-from <sojkam1@fel.cvut.cz>)\r
+       id 1XildT-0000om-Ic; Mon, 27 Oct 2014 15:51:07 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v4 4/6] cli: search: Add --output={sender,recipients}\r
+Date: Mon, 27 Oct 2014 15:50:53 +0100\r
+Message-Id: <1414421455-3037-5-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.1.1\r
+In-Reply-To: <1414421455-3037-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1414421455-3037-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: Mon, 27 Oct 2014 14:51:19 -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. The\r
+next patch will change this. For this reason, the test are introduced in\r
+the next patch as well.\r
+\r
+We use mailbox_t rather than InternetAddressMailbox because we will need\r
+to extend it in a following patch.\r
+\r
+This code 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                   | 112 ++++++++++++++++++++++++++++++++++++-\r
+ 4 files changed, 134 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..ce3bfb2 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,84 @@ 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
++\r
++    if (opt->format->is_text_printer) {\r
++      char *mailbox_str;\r
++\r
++      if (name && *name)\r
++          mailbox_str = talloc_asprintf (opt->format, "%s <%s>", name, addr);\r
++      else\r
++          mailbox_str = talloc_strdup (opt->format, addr);\r
++\r
++      if (! mailbox_str) {\r
++          fprintf (stderr, "Error: out of memory\n");\r
++          return;\r
++      }\r
++      opt->format->string (opt->format, mailbox_str);\r
++      opt->format->separator (opt->format);\r
++\r
++      talloc_free (mailbox_str);\r
++    } else {\r
++      opt->format->begin_map (opt->format);\r
++      opt->format->map_key (opt->format, "name");\r
++      opt->format->string (opt->format, name);\r
++      opt->format->map_key (opt->format, "address");\r
++      opt->format->string (opt->format, addr);\r
++      opt->format->end (opt->format);\r
++      opt->format->separator (opt->format);\r
++    }\r
++}\r
++\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
++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 +353,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 +476,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 +569,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