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