[PATCH] cli: add --output=address-{from,to,all} to notmuch search
authorJani Nikula <jani@nikula.org>
Sat, 6 Sep 2014 08:14:27 +0000 (11:14 +0300)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:04:28 +0000 (10:04 -0800)
2d/d71f9493b59c0b8905b666e24513cab92270f1 [new file with mode: 0644]

diff --git a/2d/d71f9493b59c0b8905b666e24513cab92270f1 b/2d/d71f9493b59c0b8905b666e24513cab92270f1
new file mode 100644 (file)
index 0000000..e613caa
--- /dev/null
@@ -0,0 +1,264 @@
+Return-Path: <jani@nikula.org>\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 879C6431FBC\r
+       for <notmuch@notmuchmail.org>; Sat,  6 Sep 2014 01:14:37 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 eVo-VvX2wfGu for <notmuch@notmuchmail.org>;\r
+       Sat,  6 Sep 2014 01:14:32 -0700 (PDT)\r
+Received: from mail-wi0-f169.google.com (mail-wi0-f169.google.com\r
+       [209.85.212.169]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id B70B3431FB6\r
+       for <notmuch@notmuchmail.org>; Sat,  6 Sep 2014 01:14:31 -0700 (PDT)\r
+Received: by mail-wi0-f169.google.com with SMTP id n3so530900wiv.2\r
+       for <notmuch@notmuchmail.org>; Sat, 06 Sep 2014 01:14:29 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=1e100.net; s=20130820;\r
+       h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+       :references;\r
+       bh=8piD5/djC/oP3F3HSSx4MMOg11QLHWXFuCbOS8mJePo=;\r
+       b=HWi/HjSzRWv+4mUIR9D2gGZwt661ltAdinUsztHLokaiFoONhmo/X1L3SZt3zGEG27\r
+       ZGcYtu2cfocWcFxyXnFFwsXOBJ9irsOfEQijRR/NHPcBa1t5TGqODJ3ET6p564KE64uc\r
+       QdB1q+BztZbnxZ4H+eSbP+kRWqOBK6IPOiMcXT8MS+5Z0HNcp1++tpkY2cXQ+Qy7Iv9T\r
+       yJ1CosWeAaqpRnBiszVNF0aVZ3xIK3ClK5N4ZH3SF9RfE9flXzvyI8pzDq3J0oCClk4F\r
+       O8Hv+rduk9F2SsmCaKNcnXCMMXWvyiiLrhTXIHcorAssG/4ZGgH79D0YvG18C6rZETB+\r
+       DEgA==\r
+X-Gm-Message-State:\r
+ ALoCoQkupmCoQRnCRfdzlL05GicrJsoFLYL+yham7iZryynivtSWCh70RP9heWaJ0UcHqGt0qOoP\r
+X-Received: by 10.181.13.116 with SMTP id ex20mr8874161wid.31.1409991269071;\r
+       Sat, 06 Sep 2014 01:14:29 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw2-58c374-75.dhcp.inet.fi.\r
+       [88.195.116.75])\r
+       by mx.google.com with ESMTPSA id i6sm1832089wib.7.2014.09.06.01.14.27\r
+       for <multiple recipients>\r
+       (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+       Sat, 06 Sep 2014 01:14:28 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org, David Edmondson <dme@dme.org>,\r
+       Mark Walters <markwalters1009@gmail.com>\r
+Subject: [PATCH] cli: add --output=address-{from,to,all} to notmuch search\r
+Date: Sat,  6 Sep 2014 11:14:27 +0300\r
+Message-Id: <1409991267-20104-1-git-send-email-jani@nikula.org>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <m2y4tygih7.fsf@dme.org>\r
+References: <m2y4tygih7.fsf@dme.org>\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: Sat, 06 Sep 2014 08:14:37 -0000\r
+\r
+address-from prints reply-to or from, address-to prints to, cc, and\r
+bcc, and address-all prints all of them.\r
+\r
+---\r
+\r
+Mark, David -\r
+\r
+I wrote most of this almost two years ago, but wasn't really happy\r
+with it. There's address deduplication, but for large result sets that\r
+might use lots of memory. Maybe the --duplicate option could be\r
+overloaded for doing or not doing deduplication. I'd like to have some\r
+way of picking the prettiest (that's subjective too) name part to go\r
+with the address, now it's just the first encountered. And so on.\r
+\r
+But maybe this will be useful for you, and you can pick some ideas. I\r
+won't have the time to do much on this.\r
+\r
+Cheers,\r
+Jani.\r
+---\r
+ notmuch-search.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-\r
+ 1 file changed, 113 insertions(+), 1 deletion(-)\r
+\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index bc9be4593ecc..33da90eaceec 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -26,6 +26,9 @@ typedef enum {\r
+     OUTPUT_SUMMARY,\r
+     OUTPUT_THREADS,\r
+     OUTPUT_MESSAGES,\r
++    OUTPUT_ADDRESS_FROM,\r
++    OUTPUT_ADDRESS_TO,\r
++    OUTPUT_ADDRESS_ALL,\r
+     OUTPUT_FILES,\r
+     OUTPUT_TAGS\r
+ } output_t;\r
+@@ -214,6 +217,78 @@ do_search_threads (sprinter_t *format,\r
+     return 0;\r
+ }\r
\r
++static void\r
++print_address_list (sprinter_t *format, GHashTable *addrs,\r
++                  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
++          print_address_list (format, addrs, group_list);\r
++      } else {\r
++          InternetAddressMailbox *mailbox;\r
++          const char *name;\r
++          const char *addr;\r
++          char *full_address;\r
++\r
++          mailbox = INTERNET_ADDRESS_MAILBOX (address);\r
++\r
++          name = internet_address_get_name (address);\r
++          addr = internet_address_mailbox_get_addr (mailbox);\r
++\r
++          if (g_hash_table_lookup_extended (addrs, addr, NULL, NULL))\r
++              continue;\r
++\r
++          g_hash_table_insert (addrs, talloc_strdup (NULL, addr), NULL);\r
++\r
++          if (name && *name)\r
++              full_address = talloc_asprintf (NULL, "%s <%s>", name, addr);\r
++          else\r
++              full_address = talloc_asprintf (NULL, "<%s>", addr);\r
++\r
++          if (!full_address)\r
++              break;\r
++\r
++          format->string (format, full_address);\r
++          format->separator (format);\r
++\r
++          talloc_free (full_address);\r
++      }\r
++    }\r
++}\r
++\r
++static void\r
++print_address_string (sprinter_t *format, GHashTable *addrs, const char *recipients)\r
++{\r
++    InternetAddressList *list;\r
++\r
++    if (recipients == NULL)\r
++      return;\r
++\r
++    list = internet_address_list_parse_string (recipients);\r
++    if (list == NULL)\r
++      return;\r
++\r
++    print_address_list (format, addrs, list);\r
++}\r
++\r
++static void\r
++_my_talloc_free_for_g_hash (void *ptr)\r
++{\r
++    talloc_free (ptr);\r
++}\r
++\r
+ static int\r
+ do_search_messages (sprinter_t *format,\r
+                   notmuch_query_t *query,\r
+@@ -225,8 +300,14 @@ do_search_messages (sprinter_t *format,\r
+     notmuch_message_t *message;\r
+     notmuch_messages_t *messages;\r
+     notmuch_filenames_t *filenames;\r
++    GHashTable *addresses = NULL;\r
+     int i;\r
\r
++    if (output == OUTPUT_ADDRESS_FROM || output == OUTPUT_ADDRESS_TO ||\r
++      output == OUTPUT_ADDRESS_ALL)\r
++      addresses = g_hash_table_new_full (g_str_hash, g_str_equal,\r
++                                     _my_talloc_free_for_g_hash, NULL);\r
++\r
+     if (offset < 0) {\r
+       offset += notmuch_query_count_messages (query);\r
+       if (offset < 0)\r
+@@ -264,16 +345,41 @@ do_search_messages (sprinter_t *format,\r
+           \r
+           notmuch_filenames_destroy( filenames );\r
\r
+-      } else { /* output == OUTPUT_MESSAGES */\r
++      } else if (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 (output == OUTPUT_ADDRESS_FROM || output == OUTPUT_ADDRESS_ALL) {\r
++              const char *addrs;\r
++\r
++              addrs = notmuch_message_get_header (message, "reply-to");\r
++\r
++              if (addrs == NULL || *addrs == '\0')\r
++                  addrs = notmuch_message_get_header (message, "from");\r
++\r
++              print_address_string (format, addresses, addrs);\r
++          }\r
++\r
++          if (output == OUTPUT_ADDRESS_TO || output == OUTPUT_ADDRESS_ALL) {\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
++                  print_address_string (format, addresses, addrs);\r
++              }\r
++          }\r
+       }\r
\r
+       notmuch_message_destroy (message);\r
+     }\r
\r
++    if (addresses)\r
++      g_hash_table_unref (addresses);\r
++\r
+     notmuch_messages_destroy (messages);\r
\r
+     format->end (format);\r
+@@ -368,6 +474,9 @@ 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
++                                { "address-from", OUTPUT_ADDRESS_FROM },\r
++                                { "address-to", OUTPUT_ADDRESS_TO },\r
++                                { "address-all", OUTPUT_ADDRESS_ALL },\r
+                                 { "files", OUTPUT_FILES },\r
+                                 { "tags", OUTPUT_TAGS },\r
+                                 { 0, 0 } } },\r
+@@ -459,6 +568,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+       ret = do_search_threads (format, query, sort, output, offset, limit);\r
+       break;\r
+     case OUTPUT_MESSAGES:\r
++    case OUTPUT_ADDRESS_FROM:\r
++    case OUTPUT_ADDRESS_TO:\r
++    case OUTPUT_ADDRESS_ALL:\r
+     case OUTPUT_FILES:\r
+       ret = do_search_messages (format, query, output, offset, limit, dupe);\r
+       break;\r
+-- \r
+2.1.0\r
+\r