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 D70F6431FBD for ; Fri, 24 Oct 2014 10:44:14 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.699 X-Spam-Level: X-Spam-Status: No, score=-0.699 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7, UNPARSEABLE_RELAY=0.001] 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 tqOMfgOSZwAI for ; Fri, 24 Oct 2014 10:44:09 -0700 (PDT) Received: from mail-wi0-f171.google.com (mail-wi0-f171.google.com [209.85.212.171]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 2B129431FC9 for ; Fri, 24 Oct 2014 10:44:09 -0700 (PDT) Received: by mail-wi0-f171.google.com with SMTP id em10so1829387wid.4 for ; Fri, 24 Oct 2014 10:44:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:user-agent:from:to:subject:date:message-id :in-reply-to:references; bh=opJEioawjY0kER38ub6Pm8UXSXCZK/juFaTe6XuKjeU=; b=RKNU4HsCAY3x+w7vMNWIPAF5Cx4PqbDE+OMT1HQ9PFlo140lxedGrj9oZvA6Q6Sd2X SDPVdPZvm0HYyAwSSItbPa33IvdjoMAOWLgFdtlYwkuwuV2VuKAto8vLyC+A+A2KrCDt 8bXTGEmNbn239MgqZlXBbPmacnHIuhQz0ajEf4auDwhbtnOKBtJH5PPk18u/ggcdTEkJ Ujd1jiIb98WD2WpWrqVIotPm1SDjDDaaGVZgjmKFwM0Bd6AkXdzdP2urfR+8W0sq0vja 7eG4FIKyWYyHtI7a94BsOkIoK+u3xcIAd474iCi0MiAOnzkQmS4Z66DRKnJE3SIPEfWE /fDQ== X-Gm-Message-State: ALoCoQlfZ17TPby3V3zqcOLzVS1KypQf7CYbvpsn7lwdqCKH2fda+8o7EhMJY63EqqSyTbmARnwt X-Received: by 10.194.5.227 with SMTP id v3mr6439097wjv.63.1414172647609; Fri, 24 Oct 2014 10:44:07 -0700 (PDT) Received: from disaster-area.hh.sledj.net (disaster-area.hh.sledj.net. [81.149.164.25]) by mx.google.com with ESMTPSA id dq7sm2726263wid.12.2014.10.24.10.44.06 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Oct 2014 10:44:07 -0700 (PDT) Received: from localhost (30000@localhost [local]); by localhost (OpenSMTPD) with ESMTPA id b52aed99; for ; Fri, 24 Oct 2014 17:44:03 +0000 (UTC) User-Agent: OpenSMTPD enqueuer (Demoostik) From: David Edmondson To: notmuch@notmuchmail.org Subject: [PATCH v2 1/3] search: Separately report matching and non-matching authors. Date: Fri, 24 Oct 2014 18:44:01 +0100 Message-Id: <1414172643-28270-2-git-send-email-dme@dme.org> X-Mailer: git-send-email 2.1.1 In-Reply-To: <1414172643-28270-1-git-send-email-dme@dme.org> References: <1414172643-28270-1-git-send-email-dme@dme.org> 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: Fri, 24 Oct 2014 17:44:15 -0000 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 bc9be45..18c3b20 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -22,6 +22,8 @@ #include "sprinter.h" #include "string-util.h" +#include + typedef enum { OUTPUT_SUMMARY, OUTPUT_THREADS, @@ -69,6 +71,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 (sprinter_t *format, notmuch_query_t *query, @@ -152,6 +253,10 @@ do_search_threads (sprinter_t *format, 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.1