Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 51F946DE179D for ; Mon, 14 Dec 2015 09:23:15 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.266 X-Spam-Level: X-Spam-Status: No, score=-0.266 tagged_above=-999 required=5 tests=[AWL=0.304, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bGCt-vbPss6c for ; Mon, 14 Dec 2015 09:23:13 -0800 (PST) Received: from mail-wm0-f47.google.com (mail-wm0-f47.google.com [74.125.82.47]) by arlo.cworth.org (Postfix) with ESMTPS id 82D296DE0FB0 for ; Mon, 14 Dec 2015 09:23:06 -0800 (PST) Received: by mail-wm0-f47.google.com with SMTP id p66so54448619wmp.0 for ; Mon, 14 Dec 2015 09:23:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ipsZGs1ZgLEXYbpaTMpRWBYYwPeo3MYpsgykBe8BoC4=; b=sC+4D2MerAT2Ym5WiZiiXQKGDmUGWpysAYHcROMocCnNbwQkNYNDTGi3ZLEGqe/1Pq YV93PH0bVuV8WaVSvIOvlhy9D7N+ExGosidTg2GksmyXaJcYNH5WAs6k2ReZxdRHjc7j zKxryaik7EBUpfMkVrUGfPUpMpOoI88cj9Plu0B9VaAsR9xkqKmQLeRSO372URh5Gaak DJFPXCvbcju8a4QsBvMVtDJ4hGmzn1m7lbC/QD2H48Q+EBzZXkchq7EF1a/esKs+xGB8 9A0UndZZfGUVsPoVW1SShYpPUrzMHc3CG8frd/QiN7X2Q5/NreGPlcuBqJOzb9rJs9Df WKTg== X-Received: by 10.194.77.51 with SMTP id p19mr38359624wjw.159.1450113783260; Mon, 14 Dec 2015 09:23:03 -0800 (PST) Received: from localhost (92.40.77.76.threembb.co.uk. [92.40.77.76]) by smtp.gmail.com with ESMTPSA id s11sm17180610wmb.14.2015.12.14.09.22.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Dec 2015 09:23:00 -0800 (PST) From: Mark Walters To: notmuch@notmuchmail.org Subject: [PATCH v2 (rebased) 1/3] search: Separately report matching and non-matching authors. Date: Mon, 14 Dec 2015 17:22:44 +0000 Message-Id: <1450113766-20518-2-git-send-email-markwalters1009@gmail.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1450113766-20518-1-git-send-email-markwalters1009@gmail.com> References: <1450113766-20518-1-git-send-email-markwalters1009@gmail.com> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.20 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: Mon, 14 Dec 2015 17:23:15 -0000 From: David Edmondson In addition to the 'authors' attribute of each search result, include 'authors_matched' and 'authors_non_matched' attributes. Both attributes are always included and are formatted as a list of authors. If there are no matching authors, the 'authors_non_matched' attribute is set to the empty list. --- notmuch-search.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/notmuch-search.c b/notmuch-search.c index 6d08c25..6c6e497 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -22,6 +22,8 @@ #include "sprinter.h" #include "string-util.h" +#include + typedef enum { /* Search command */ OUTPUT_SUMMARY = 1 << 0, @@ -109,6 +111,105 @@ get_thread_query (notmuch_thread_t *thread, return 0; } +/* Return a more pleasent rendering of the mail address + * `nasty_author'. */ +static const char * +_nice_author (void *ctx, const char *nasty_author) +{ + const char *nice_author = NULL; + + InternetAddressList *list = internet_address_list_parse_string (nasty_author); + if (list) { + InternetAddress *address = internet_address_list_get_address (list, 0); + if (address) { + nice_author = internet_address_get_name (address); + if (nice_author == NULL) { + InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address); + nice_author = internet_address_mailbox_get_addr (mailbox); + } + } + /* Duplicate the string before `g_object_unref' destroys + * it. */ + if (nice_author) + nice_author = talloc_strdup (ctx, nice_author); + + g_object_unref (G_OBJECT (list)); + } + + if (nice_author) + return nice_author; + else + return nasty_author; +} + +static int +_enumerate_authors (sprinter_t *format, + notmuch_thread_t *thread) +{ + notmuch_messages_t *messages; + GHashTable *matched_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL); + GHashTable *unmatched_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL); + GPtrArray *matched_array = g_ptr_array_new (); + GPtrArray *unmatched_array = g_ptr_array_new (); + + /* Iterate over the messages in the thread collecting matching and + * non-matching authors. */ + for (messages = notmuch_thread_get_messages (thread); + notmuch_messages_valid (messages); + notmuch_messages_move_to_next (messages)) + { + notmuch_message_t *message = notmuch_messages_get (messages); + const char *author = _nice_author (thread, notmuch_message_get_header (message, "from")); + + if (author) { + GHashTable *hash; + GPtrArray *array; + + if (notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH)) { + hash = matched_hash; + array = matched_array; + } else { + hash = unmatched_hash; + array = unmatched_array; + } + + if (!g_hash_table_lookup_extended (hash, author, NULL, NULL)) { + char *copy = talloc_strdup (thread, author); + g_hash_table_insert (hash, copy, NULL); + g_ptr_array_add (array, (char *) copy); + } + } + } + + /* Output the matched authors. */ + unsigned int i; + format->map_key (format, "authors_matched"); + format->begin_list (format); + for (i = 0; i < matched_array->len; i++) + format->string (format, (char *) g_ptr_array_index( matched_array, i)); + format->end (format); + + /* Output the non-matched authors, but not if they were seen + * already in the matched authors list. */ + format->map_key (format, "authors_non_matched"); + format->begin_list (format); + for (i = 0; i < unmatched_array->len; i++) { + char *author = (char *) g_ptr_array_index( unmatched_array, i); + + if (!g_hash_table_lookup_extended (matched_hash, author, NULL, NULL)) + format->string (format, author); + } + format->end (format); + + g_hash_table_unref (matched_hash); + g_hash_table_unref (unmatched_hash); + + g_ptr_array_free (matched_array, TRUE); + g_ptr_array_free (unmatched_array, TRUE); + + return 0; +} + static int do_search_threads (search_context_t *ctx) { @@ -195,6 +296,10 @@ do_search_threads (search_context_t *ctx) format->integer (format, total); format->map_key (format, "authors"); format->string (format, authors); + if (_enumerate_authors (format, thread) < 0) { + fprintf (stderr, "Out of memory\n"); + return 1; + } format->map_key (format, "subject"); format->string (format, subject); if (notmuch_format_version >= 2) { -- 2.1.4