From 0a1305bbcbf51f1b774cd5a3e06e1b02b36c9b1e Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Tue, 23 Feb 2010 15:49:52 +0100 Subject: [PATCH] Add ">" before subject line is we are a recipient of the email --- Makefile | 2 +- lib/notmuch.h | 16 ++++++++++++ lib/thread.cc | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ notmuch-search.c | 44 ++++++++++++++++++++++++++++++-- 4 files changed, 130 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 64b9d4a..4d652d0 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ extra_cflags := extra_cxxflags := # Now smash together user's values with our extra values -FINAL_CFLAGS = $(CFLAGS) $(WARN_CFLAGS) $(CONFIGURE_CFLAGS) $(extra_cflags) +FINAL_CFLAGS = $(CFLAGS) $(WARN_CFLAGS) $(CONFIGURE_CFLAGS) $(extra_cflags) -g FINAL_CXXFLAGS = $(CXXFLAGS) $(WARN_CXXFLAGS) $(CONFIGURE_CXXFLAGS) $(extra_cflags) $(extra_cxxflags) FINAL_LDFLAGS = $(LDFLAGS) $(CONFIGURE_LDFLAGS) diff --git a/lib/notmuch.h b/lib/notmuch.h index d3e50a7..64ccb0a 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -547,6 +547,22 @@ notmuch_thread_get_matched_messages (notmuch_thread_t *thread); const char * notmuch_thread_get_authors (notmuch_thread_t *thread); +/* Get the recipients of 'thread' + * + * The returned string is a comma-separated list of the names of the + * recipients of mail messages in the query results that belong to this + * thread. + * + * The returned string belongs to 'thread' and as such, should not be + * modified by the caller and will only be valid for as long as the + * thread is valid, (which is until notmuch_thread_destroy or until + * the query from which it derived is destroyed). + */ +const char * +notmuch_thread_get_recipients (notmuch_thread_t *thread); + + + /* Get the subject of 'thread' * * The subject is taken from the first message (according to the query diff --git a/lib/thread.cc b/lib/thread.cc index 321937b..a87f005 100644 --- a/lib/thread.cc +++ b/lib/thread.cc @@ -32,6 +32,8 @@ struct _notmuch_thread { char *subject; GHashTable *authors_hash; char *authors; + GHashTable *recipients_hash; + char *recipients; GHashTable *tags; notmuch_message_list_t *message_list; @@ -46,6 +48,7 @@ static int _notmuch_thread_destructor (notmuch_thread_t *thread) { g_hash_table_unref (thread->authors_hash); + g_hash_table_unref (thread->recipients_hash); g_hash_table_unref (thread->tags); g_hash_table_unref (thread->message_hash); @@ -73,6 +76,28 @@ _thread_add_author (notmuch_thread_t *thread, thread->authors = talloc_strdup (thread, author); } +static void +_thread_add_recipient (notmuch_thread_t *thread, + const char *recipient) +{ + if (recipient == NULL) + return; + + if (g_hash_table_lookup_extended (thread->recipients_hash, + recipient, NULL, NULL)) + return; + + printf ("Adding recipient: %s\n", recipient); + g_hash_table_insert (thread->recipients_hash, xstrdup (recipient), NULL); + + if (thread->recipients) + thread->recipients = talloc_asprintf (thread, "%s, %s", + thread->recipients, + recipient); + else + thread->recipients = talloc_strdup (thread, recipient); +} + /* Add 'message' as a message that belongs to 'thread'. * * The 'thread' will talloc_steal the 'message' and hold onto a @@ -87,6 +112,8 @@ _thread_add_message (notmuch_thread_t *thread, InternetAddressList *list; InternetAddress *address; const char *from, *author; + const char *to, *cc, *recipient; + int i; _notmuch_message_list_add_message (thread->message_list, talloc_steal (thread, message)); @@ -112,6 +139,42 @@ _thread_add_message (notmuch_thread_t *thread, g_object_unref (G_OBJECT (list)); } + to = notmuch_message_get_header (message, "to"); + list = internet_address_list_parse_string (to); + if (list) { + for (i = 0; i < internet_address_list_length (list); i++) { + address = internet_address_list_get_address (list, i); + if (address) { + recipient = internet_address_get_name (address); + if (recipient == NULL) { + InternetAddressMailbox *mailbox; + mailbox = INTERNET_ADDRESS_MAILBOX (address); + recipient = internet_address_mailbox_get_addr (mailbox); + } + _thread_add_recipient (thread, recipient); + } + } + g_object_unref (G_OBJECT (list)); + } + + cc = notmuch_message_get_header (message, "cc"); + list = internet_address_list_parse_string (to); + if (list) { + for (i = 0; i < internet_address_list_length (list); i++) { + address = internet_address_list_get_address (list, i); + if (address) { + recipient = internet_address_get_name (address); + if (recipient == NULL) { + InternetAddressMailbox *mailbox; + mailbox = INTERNET_ADDRESS_MAILBOX (address); + recipient = internet_address_mailbox_get_addr (mailbox); + } + _thread_add_recipient (thread, recipient); + } + } + g_object_unref (G_OBJECT (list)); + } + if (! thread->subject) { const char *subject; subject = notmuch_message_get_header (message, "subject"); @@ -251,6 +314,9 @@ _notmuch_thread_create (void *ctx, thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL); thread->authors = NULL; + thread->recipients_hash = g_hash_table_new_full (g_str_hash, g_str_equal, + free, NULL); + thread->recipients = NULL; thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL); @@ -326,6 +392,12 @@ notmuch_thread_get_authors (notmuch_thread_t *thread) } const char * +notmuch_thread_get_recipients (notmuch_thread_t *thread) +{ + return thread->recipients; +} + +const char * notmuch_thread_get_subject (notmuch_thread_t *thread) { return thread->subject; diff --git a/notmuch-search.c b/notmuch-search.c index dc44eb6..a9c7ce0 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -20,8 +20,45 @@ #include "notmuch-client.h" +static int +is_for_me (notmuch_config_t *config, notmuch_thread_t *thread) +{ + static const char *user_name = NULL; + static const char *primary_email = NULL; + static char **other_emails = NULL; + static size_t other_emails_lenght = 0; + + const char *recipients; + + if (user_name == NULL) { + user_name = notmuch_config_get_user_name (config); + primary_email = notmuch_config_get_user_primary_email (config); + other_emails = notmuch_config_get_user_other_email (config, &other_emails_lenght); + } + + recipients = notmuch_thread_get_recipients (thread); + + if (recipients == NULL) { + return 0; + } else if (user_name != NULL && strstr (recipients, user_name) != NULL) { + return 1; + } else if (primary_email != NULL && strstr (recipients, primary_email) != NULL) { + return 1; + } else if (other_emails != NULL) { + int i; + for (i = 0; i < (int) other_emails_lenght; i++) { + if (other_emails[i] != NULL && strstr (recipients, other_emails[i]) != NULL) { + return 1; + } + } + } + + return 0; +} + static void -do_search_threads (const void *ctx, +do_search_threads (notmuch_config_t *config, + const void *ctx, notmuch_query_t *query, notmuch_sort_t sort) { @@ -46,12 +83,13 @@ do_search_threads (const void *ctx, relative_date = notmuch_time_relative_date (ctx, date); - printf ("thread:%s %12s [%d/%d] %s; %s", + printf ("thread:%s %12s [%d/%d] %s; %s%s", notmuch_thread_get_thread_id (thread), relative_date, notmuch_thread_get_matched_messages (thread), notmuch_thread_get_total_messages (thread), notmuch_thread_get_authors (thread), + is_for_me (config, thread) ? ">" : "", notmuch_thread_get_subject (thread)); printf (" ("); @@ -132,7 +170,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) notmuch_query_set_sort (query, sort); - do_search_threads (ctx, query, sort); + do_search_threads (config, ctx, query, sort); notmuch_query_destroy (query); notmuch_database_close (notmuch); -- 1.6.6