1 /* thread.cc - Results of thread-based searches from a notmuch database
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
24 #include <gmime/gmime.h>
25 #include <glib.h> /* GHashTable */
27 #ifdef DEBUG_THREADING
28 #define THREAD_DEBUG(format, ...) fprintf(stderr, format " (%s).\n", ##__VA_ARGS__, __location__)
30 #define THREAD_DEBUG(format, ...) do {} while (0) /* ignored */
33 #define EMPTY_STRING(s) ((s)[0] == '\0')
35 struct _notmuch_thread {
36 notmuch_database_t *notmuch;
39 GHashTable *authors_hash;
40 GPtrArray *authors_array;
41 GHashTable *matched_authors_hash;
42 GPtrArray *matched_authors_array;
46 /* All messages, oldest first. */
47 notmuch_message_list_t *message_list;
48 /* Top-level messages, oldest first. */
49 notmuch_message_list_t *toplevel_list;
51 GHashTable *message_hash;
60 _notmuch_thread_destructor (notmuch_thread_t *thread)
62 g_hash_table_unref (thread->authors_hash);
63 g_hash_table_unref (thread->matched_authors_hash);
64 g_hash_table_unref (thread->tags);
65 g_hash_table_unref (thread->message_hash);
67 if (thread->authors_array) {
68 g_ptr_array_free (thread->authors_array, true);
69 thread->authors_array = NULL;
72 if (thread->matched_authors_array) {
73 g_ptr_array_free (thread->matched_authors_array, true);
74 thread->matched_authors_array = NULL;
80 /* Add each author of the thread to the thread's authors_hash and to
81 * the thread's authors_array. */
83 _thread_add_author (notmuch_thread_t *thread,
91 if (g_hash_table_lookup_extended (thread->authors_hash,
95 author_copy = talloc_strdup (thread, author);
97 g_hash_table_insert (thread->authors_hash, author_copy, NULL);
99 g_ptr_array_add (thread->authors_array, author_copy);
102 /* Add each matched author of the thread to the thread's
103 * matched_authors_hash and to the thread's matched_authors_array. */
105 _thread_add_matched_author (notmuch_thread_t *thread,
113 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
117 author_copy = talloc_strdup (thread, author);
119 g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
121 g_ptr_array_add (thread->matched_authors_array, author_copy);
124 /* Construct an authors string from matched_authors_array and
125 * authors_array. The string contains matched authors first, then
126 * non-matched authors (with the two groups separated by '|'). Within
127 * each group, authors are listed in date order. */
129 _resolve_thread_authors_string (notmuch_thread_t *thread)
133 int first_non_matched_author = 1;
135 /* First, list all matched authors in date order. */
136 for (i = 0; i < thread->matched_authors_array->len; i++) {
137 author = (char *) g_ptr_array_index (thread->matched_authors_array, i);
139 thread->authors = talloc_asprintf (thread, "%s, %s",
143 thread->authors = author;
146 /* Next, append any non-matched authors that haven't already appeared. */
147 for (i = 0; i < thread->authors_array->len; i++) {
148 author = (char *) g_ptr_array_index (thread->authors_array, i);
149 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
152 if (first_non_matched_author) {
153 thread->authors = talloc_asprintf (thread, "%s| %s",
157 thread->authors = talloc_asprintf (thread, "%s, %s",
162 first_non_matched_author = 0;
165 g_ptr_array_free (thread->authors_array, true);
166 thread->authors_array = NULL;
167 g_ptr_array_free (thread->matched_authors_array, true);
168 thread->matched_authors_array = NULL;
170 if (!thread->authors)
171 thread->authors = talloc_strdup(thread, "");
174 /* clean up the ugly "Lastname, Firstname" format that some mail systems
175 * (most notably, Exchange) are creating to be "Firstname Lastname"
176 * To make sure that we don't change other potential situations where a
177 * comma is in the name, we check that we match one of these patterns
178 * "Last, First" <first.last@company.com>
179 * "Last, First MI" <first.mi.last@company.com>
182 _thread_cleanup_author (notmuch_thread_t *thread,
183 const char *author, const char *from)
185 char *clean_author,*test_author;
192 clean_author = talloc_strdup(thread, author);
193 if (clean_author == NULL)
195 /* check if there's a comma in the name and that there's a
196 * component of the name behind it (so the name doesn't end with
197 * the comma - in which case the string that strchr finds is just
198 * one character long ",\0").
199 * Otherwise just return the copy of the original author name that
201 comma = strchr(author,',');
202 if (comma && strlen(comma) > 1) {
203 /* let's assemble what we think is the correct name */
204 lname = comma - author;
206 /* Skip all the spaces after the comma */
207 fname = strlen(author) - lname - 1;
209 while (*comma == ' ') {
213 strncpy(clean_author, comma, fname);
215 *(clean_author+fname) = ' ';
216 strncpy(clean_author + fname + 1, author, lname);
217 *(clean_author+fname+1+lname) = '\0';
218 /* make a temporary copy and see if it matches the email */
219 test_author = talloc_strdup(thread,clean_author);
221 blank=strchr(test_author,' ');
222 while (blank != NULL) {
224 blank=strchr(test_author,' ');
226 if (strcasestr(from, test_author) == NULL)
227 /* we didn't identify this as part of the email address
228 * so let's punt and return the original author */
229 strcpy (clean_author, author);
234 /* Add 'message' as a message that belongs to 'thread'.
236 * The 'thread' will talloc_steal the 'message' and hold onto a
240 _thread_add_message (notmuch_thread_t *thread,
241 notmuch_message_t *message,
242 notmuch_string_list_t *exclude_terms,
243 notmuch_exclude_t omit_exclude)
245 notmuch_tags_t *tags;
247 InternetAddressList *list = NULL;
248 InternetAddress *address;
249 const char *from, *author;
251 bool message_excluded = false;
253 if (omit_exclude != NOTMUCH_EXCLUDE_FALSE) {
254 for (tags = notmuch_message_get_tags (message);
255 notmuch_tags_valid (tags);
256 notmuch_tags_move_to_next (tags))
258 tag = notmuch_tags_get (tags);
259 /* Is message excluded? */
260 for (notmuch_string_node_t *term = exclude_terms->head;
264 /* Check for an empty string, and then ignore initial 'K'. */
265 if (*(term->string) && strcmp(tag, (term->string + 1)) == 0) {
266 message_excluded = true;
273 if (message_excluded && omit_exclude == NOTMUCH_EXCLUDE_ALL)
276 _notmuch_message_list_add_message (thread->message_list,
277 talloc_steal (thread, message));
278 thread->total_messages++;
279 thread->total_files += notmuch_message_count_files (message);
281 g_hash_table_insert (thread->message_hash,
282 xstrdup (notmuch_message_get_message_id (message)),
285 from = notmuch_message_get_header (message, "from");
287 list = internet_address_list_parse_string (from);
290 address = internet_address_list_get_address (list, 0);
292 author = internet_address_get_name (address);
293 /* We treat quoted empty names as if they were empty. */
294 if (author == NULL || author[0] == '\0') {
295 InternetAddressMailbox *mailbox;
296 mailbox = INTERNET_ADDRESS_MAILBOX (address);
297 author = internet_address_mailbox_get_addr (mailbox);
299 clean_author = _thread_cleanup_author (thread, author, from);
300 _thread_add_author (thread, clean_author);
301 _notmuch_message_set_author (message, clean_author);
303 g_object_unref (G_OBJECT (list));
306 if (! thread->subject) {
308 subject = notmuch_message_get_header (message, "subject");
309 thread->subject = talloc_strdup (thread, subject ? subject : "");
312 for (tags = notmuch_message_get_tags (message);
313 notmuch_tags_valid (tags);
314 notmuch_tags_move_to_next (tags))
316 tag = notmuch_tags_get (tags);
317 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
320 /* Mark excluded messages. */
321 if (message_excluded)
322 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
326 _thread_set_subject_from_message (notmuch_thread_t *thread,
327 notmuch_message_t *message)
330 const char *cleaned_subject;
332 subject = notmuch_message_get_header (message, "subject");
336 if ((strncasecmp (subject, "Re: ", 4) == 0) ||
337 (strncasecmp (subject, "Aw: ", 4) == 0) ||
338 (strncasecmp (subject, "Vs: ", 4) == 0) ||
339 (strncasecmp (subject, "Sv: ", 4) == 0)) {
341 cleaned_subject = talloc_strndup (thread,
343 strlen(subject) - 4);
345 cleaned_subject = talloc_strdup (thread, subject);
348 if (! EMPTY_STRING(cleaned_subject)) {
350 talloc_free (thread->subject);
352 thread->subject = talloc_strdup (thread, cleaned_subject);
356 /* Add a message to this thread which is known to match the original
357 * search specification. The 'sort' parameter controls whether the
358 * oldest or newest matching subject is applied to the thread as a
361 _thread_add_matched_message (notmuch_thread_t *thread,
362 notmuch_message_t *message,
366 notmuch_message_t *hashed_message;
368 date = notmuch_message_get_date (message);
370 if (date < thread->oldest || ! thread->matched_messages) {
371 thread->oldest = date;
372 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
373 _thread_set_subject_from_message (thread, message);
376 if (date > thread->newest || ! thread->matched_messages) {
377 thread->newest = date;
378 const char *cur_subject = notmuch_thread_get_subject(thread);
379 if (sort != NOTMUCH_SORT_OLDEST_FIRST || EMPTY_STRING(cur_subject))
380 _thread_set_subject_from_message (thread, message);
383 if (!notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED))
384 thread->matched_messages++;
386 if (g_hash_table_lookup_extended (thread->message_hash,
387 notmuch_message_get_message_id (message), NULL,
388 (void **) &hashed_message)) {
389 notmuch_message_set_flag (hashed_message,
390 NOTMUCH_MESSAGE_FLAG_MATCH, 1);
393 _thread_add_matched_author (thread, _notmuch_message_get_author (hashed_message));
397 _parent_via_in_reply_to (notmuch_thread_t *thread, notmuch_message_t *message) {
398 notmuch_message_t *parent;
399 const char *in_reply_to;
401 in_reply_to = _notmuch_message_get_in_reply_to (message);
402 THREAD_DEBUG("checking message = %s in_reply_to=%s\n",
403 notmuch_message_get_message_id (message), in_reply_to);
405 if (in_reply_to && (! EMPTY_STRING(in_reply_to)) &&
406 g_hash_table_lookup_extended (thread->message_hash,
408 (void **) &parent)) {
409 _notmuch_message_add_reply (parent, message);
417 _parent_or_toplevel (notmuch_thread_t *thread, notmuch_message_t *message)
419 size_t max_depth = 0;
420 notmuch_message_t *new_parent;
421 notmuch_message_t *parent = NULL;
422 const notmuch_string_list_t *references =
423 _notmuch_message_get_references (message);
425 THREAD_DEBUG("trying to reparent via references: %s\n",
426 notmuch_message_get_message_id (message));
428 for (notmuch_string_node_t *ref_node = references->head;
429 ref_node; ref_node = ref_node->next) {
430 THREAD_DEBUG("checking reference=%s\n", ref_node->string);
431 if ((g_hash_table_lookup_extended (thread->message_hash,
432 ref_node->string, NULL,
433 (void **) &new_parent))) {
434 size_t new_depth = _notmuch_message_get_thread_depth (new_parent);
435 THREAD_DEBUG("got depth %lu\n", new_depth);
436 if (new_depth > max_depth || !parent) {
437 THREAD_DEBUG("adding at depth %lu parent=%s\n", new_depth, ref_node->string);
438 max_depth = new_depth;
444 THREAD_DEBUG("adding reply %s to parent=%s\n",
445 notmuch_message_get_message_id (message),
446 notmuch_message_get_message_id (parent));
447 _notmuch_message_add_reply (parent, message);
449 THREAD_DEBUG("adding as toplevel %s\n",
450 notmuch_message_get_message_id (message));
451 _notmuch_message_list_add_message (thread->toplevel_list, message);
456 _resolve_thread_relationships (notmuch_thread_t *thread)
458 notmuch_message_node_t *node, *first_node;
459 notmuch_message_t *message;
461 notmuch_message_list_t *maybe_toplevel_list;
463 first_node = thread->message_list->head;
467 local = talloc_new (thread);
468 maybe_toplevel_list = _notmuch_message_list_create (local);
470 for (node = first_node->next; node; node = node->next) {
471 message = node->message;
472 if (! _parent_via_in_reply_to (thread, message))
473 _notmuch_message_list_add_message (maybe_toplevel_list, message);
476 for (notmuch_messages_t *roots = _notmuch_messages_create (maybe_toplevel_list);
477 notmuch_messages_valid (roots);
478 notmuch_messages_move_to_next (roots)) {
479 notmuch_message_t *message = notmuch_messages_get (roots);
480 _parent_or_toplevel (thread, message);
483 * if we reach the end of the list without finding a top-level
484 * message, that means the thread is a cycle (or set of cycles)
485 * and any message can be considered top-level. Choose the oldest
486 * message, which happens to be first in our list.
489 message = first_node->message;
490 if (_notmuch_message_list_empty(thread->toplevel_list) ||
491 ! _parent_via_in_reply_to (thread, message)) {
493 * If the oldest message happens to be in-reply-to a
494 * missing message, we only check for references if there
495 * is some other candidate for root message.
497 if (! _notmuch_message_list_empty (thread->toplevel_list))
498 _parent_or_toplevel (thread, message);
500 _notmuch_message_list_add_message (thread->toplevel_list, message);
504 /* XXX this could be made conditional on messages being inserted
505 * (out of order) in later passes
507 thread->toplevel_list = _notmuch_message_sort_subtrees (thread, thread->toplevel_list);
510 /* XXX: After scanning through the entire list looking for parents
511 * via "In-Reply-To", we should do a second pass that looks at the
512 * list of messages IDs in the "References" header instead. (And
513 * for this the parent would be the "deepest" message of all the
514 * messages found in the "References" list.)
516 * Doing this will allow messages and sub-threads to be positioned
517 * correctly in the thread even when an intermediate message is
518 * missing from the thread.
523 /* Create a new notmuch_thread_t object by finding the thread
524 * containing the message with the given doc ID, treating any messages
525 * contained in match_set as "matched". Remove all messages in the
526 * thread from match_set.
528 * Creating the thread will perform a database search to get all
529 * messages belonging to the thread and will get the first subject
530 * line, the total count of messages, and all authors in the thread.
531 * Each message in the thread is checked against match_set to allow
532 * for a separate count of matched messages, and to allow a viewer to
533 * display these messages differently.
535 * Here, 'ctx' is talloc context for the resulting thread object.
537 * This function returns NULL in the case of any error.
540 _notmuch_thread_create (void *ctx,
541 notmuch_database_t *notmuch,
542 unsigned int seed_doc_id,
543 notmuch_doc_id_set_t *match_set,
544 notmuch_string_list_t *exclude_terms,
545 notmuch_exclude_t omit_excluded,
548 void *local = talloc_new (ctx);
549 notmuch_thread_t *thread = NULL;
550 notmuch_message_t *seed_message;
551 const char *thread_id;
552 char *thread_id_query_string;
553 notmuch_query_t *thread_id_query;
555 notmuch_messages_t *messages;
556 notmuch_message_t *message;
557 notmuch_status_t status;
559 seed_message = _notmuch_message_create (local, notmuch, seed_doc_id, NULL);
561 INTERNAL_ERROR ("Thread seed message %u does not exist", seed_doc_id);
563 thread_id = notmuch_message_get_thread_id (seed_message);
564 thread_id_query_string = talloc_asprintf (local, "thread:%s", thread_id);
565 if (unlikely (thread_id_query_string == NULL))
568 thread_id_query = talloc_steal (
569 local, notmuch_query_create (notmuch, thread_id_query_string));
570 if (unlikely (thread_id_query == NULL))
573 thread = talloc (local, notmuch_thread_t);
574 if (unlikely (thread == NULL))
577 talloc_set_destructor (thread, _notmuch_thread_destructor);
579 thread->notmuch = notmuch;
580 thread->thread_id = talloc_strdup (thread, thread_id);
581 thread->subject = NULL;
582 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
584 thread->authors_array = g_ptr_array_new ();
585 thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
588 thread->matched_authors_array = g_ptr_array_new ();
589 thread->authors = NULL;
590 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
593 thread->message_list = _notmuch_message_list_create (thread);
594 thread->toplevel_list = _notmuch_message_list_create (thread);
595 if (unlikely (thread->message_list == NULL ||
596 thread->toplevel_list == NULL)) {
601 thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
604 thread->total_messages = 0;
605 thread->total_files = 0;
606 thread->matched_messages = 0;
610 /* We use oldest-first order unconditionally here to obtain the
611 * proper author ordering for the thread. The 'sort' parameter
612 * passed to this function is used only to indicate whether the
613 * oldest or newest subject is desired. */
614 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
616 status = notmuch_query_search_messages (thread_id_query, &messages);
621 notmuch_messages_valid (messages);
622 notmuch_messages_move_to_next (messages))
626 message = notmuch_messages_get (messages);
627 doc_id = _notmuch_message_get_doc_id (message);
628 if (doc_id == seed_doc_id)
629 message = seed_message;
631 _thread_add_message (thread, message, exclude_terms, omit_excluded);
633 if ( _notmuch_doc_id_set_contains (match_set, doc_id)) {
634 _notmuch_doc_id_set_remove (match_set, doc_id);
635 _thread_add_matched_message (thread, message, sort);
638 _notmuch_message_close (message);
641 _resolve_thread_authors_string (thread);
643 _resolve_thread_relationships (thread);
645 /* Commit to returning thread. */
646 (void) talloc_steal (ctx, thread);
654 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
656 return _notmuch_messages_create (thread->toplevel_list);
660 notmuch_thread_get_messages (notmuch_thread_t *thread)
662 return _notmuch_messages_create (thread->message_list);
666 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
668 return thread->thread_id;
672 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
674 return thread->total_messages;
678 notmuch_thread_get_total_files (notmuch_thread_t *thread)
680 return thread->total_files;
684 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
686 return thread->matched_messages;
690 notmuch_thread_get_authors (notmuch_thread_t *thread)
692 return thread->authors;
696 notmuch_thread_get_subject (notmuch_thread_t *thread)
698 return thread->subject;
702 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
704 return thread->oldest;
708 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
710 return thread->newest;
714 notmuch_thread_get_tags (notmuch_thread_t *thread)
716 notmuch_string_list_t *tags;
719 tags = _notmuch_string_list_create (thread);
720 if (unlikely (tags == NULL))
723 keys = g_hash_table_get_keys (thread->tags);
725 for (l = keys; l; l = l->next)
726 _notmuch_string_list_append (tags, (char *) l->data);
730 _notmuch_string_list_sort (tags);
732 return _notmuch_tags_create (thread, tags);
736 notmuch_thread_destroy (notmuch_thread_t *thread)
738 talloc_free (thread);