[PATCH v2 (rebased) 1/3] search: Separately report matching and non-matching authors.
Mark Walters
markwalters1009 at gmail.com
Mon Dec 14 09:22:44 PST 2015
From: David Edmondson <dme at dme.org>
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 <glib.h>
+
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
More information about the notmuch
mailing list