]> git.notmuchmail.org Git - notmuch/blobdiff - lib/thread.cc
lib: Audit calls to notmuch_message_get_header to handle NULL return
[notmuch] / lib / thread.cc
index 3aa9d4807292f873970780092dd7741f2c6bedee..e1da060e5158f5e1f0d8c4709645de1e6604efad 100644 (file)
@@ -84,7 +84,7 @@ _thread_add_message (notmuch_thread_t *thread,
 {
     notmuch_tags_t *tags;
     const char *tag;
-    InternetAddressList *list;
+    InternetAddressList *list = NULL;
     InternetAddress *address;
     const char *from, *author;
 
@@ -97,7 +97,9 @@ _thread_add_message (notmuch_thread_t *thread,
                         message);
 
     from = notmuch_message_get_header (message, "from");
-    list = internet_address_list_parse_string (from);
+    if (from)
+       list = internet_address_list_parse_string (from);
+
     if (list) {
        address = internet_address_list_get_address (list, 0);
        if (address) {
@@ -115,7 +117,7 @@ _thread_add_message (notmuch_thread_t *thread,
     if (! thread->subject) {
        const char *subject;
        subject = notmuch_message_get_header (message, "subject");
-       thread->subject = talloc_strdup (thread, subject);
+       thread->subject = talloc_strdup (thread, subject ? subject : "");
     }
 
     for (tags = notmuch_message_get_tags (message);
@@ -127,9 +129,39 @@ _thread_add_message (notmuch_thread_t *thread,
     }
 }
 
+static void
+_thread_set_subject_from_message (notmuch_thread_t *thread,
+                                 notmuch_message_t *message)
+{
+    const char *subject;
+    const char *cleaned_subject;
+
+    subject = notmuch_message_get_header (message, "subject");
+    if (! subject)
+       return;
+
+    if ((strncasecmp (subject, "Re: ", 4) == 0) ||
+       (strncasecmp (subject, "Aw: ", 4) == 0) ||
+       (strncasecmp (subject, "Vs: ", 4) == 0) ||
+       (strncasecmp (subject, "Sv: ", 4) == 0)) {
+
+       cleaned_subject = talloc_strndup (thread,
+                                         subject + 4,
+                                         strlen(subject) - 4);
+    } else {
+       cleaned_subject = talloc_strdup (thread, subject);
+    }
+
+    if (thread->subject)
+       talloc_free (thread->subject);
+
+    thread->subject = talloc_strdup (thread, cleaned_subject);
+}
+
 static void
 _thread_add_matched_message (notmuch_thread_t *thread,
-                            notmuch_message_t *message)
+                            notmuch_message_t *message,
+                            notmuch_sort_t sort)
 {
     time_t date;
     notmuch_message_t *hashed_message;
@@ -150,6 +182,12 @@ _thread_add_matched_message (notmuch_thread_t *thread,
        notmuch_message_set_flag (hashed_message,
                                  NOTMUCH_MESSAGE_FLAG_MATCH, 1);
     }
+
+    if ((sort == NOTMUCH_SORT_OLDEST_FIRST && date <= thread->newest) ||
+       (sort != NOTMUCH_SORT_OLDEST_FIRST && date == thread->newest))
+    {
+       _thread_set_subject_from_message (thread, message);
+    }
 }
 
 static void
@@ -209,7 +247,8 @@ notmuch_thread_t *
 _notmuch_thread_create (void *ctx,
                        notmuch_database_t *notmuch,
                        const char *thread_id,
-                       const char *query_string)
+                       const char *query_string,
+                       notmuch_sort_t sort)
 {
     notmuch_thread_t *thread;
     const char *thread_id_query_string;
@@ -296,7 +335,7 @@ _notmuch_thread_create (void *ctx,
        _thread_add_message (thread, message);
 
        if (! matched_is_subset_of_thread)
-           _thread_add_matched_message (thread, message);
+           _thread_add_matched_message (thread, message, sort);
 
        _notmuch_message_close (message);
     }
@@ -323,7 +362,7 @@ _notmuch_thread_create (void *ctx,
             notmuch_messages_move_to_next (messages))
        {
            message = notmuch_messages_get (messages);
-           _thread_add_matched_message (thread, message);
+           _thread_add_matched_message (thread, message, sort);
            _notmuch_message_close (message);
        }