1 /* thread.cc - Results of thread-based searches from a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20 
21 #include "notmuch-private.h"
22 #include "database-private.h"
23 
24 #include <gmime/gmime.h>
25 #include <glib.h>                                       /* GHashTable */
26 
27 #ifdef DEBUG_THREADING
28 #define THREAD_DEBUG(format, ...) fprintf (stderr, "DT: " format " (%s).\n", ##__VA_ARGS__, \
29 					   __location__)
30 #else
31 #define THREAD_DEBUG(format, ...) do {} while (0)       /* ignored */
32 #endif
33 
34 struct _notmuch_thread {
35     notmuch_database_t *notmuch;
36     char *thread_id;
37     char *subject;
38     GHashTable *authors_hash;
39     GPtrArray *authors_array;
40     GHashTable *matched_authors_hash;
41     GPtrArray *matched_authors_array;
42     char *authors;
43     GHashTable *tags;
44 
45     /* All messages, oldest first. */
46     notmuch_message_list_t *message_list;
47     /* Top-level messages, oldest first. */
48     notmuch_message_list_t *toplevel_list;
49 
50     GHashTable *message_hash;
51     int total_messages;
52     int total_files;
53     int matched_messages;
54     time_t oldest;
55     time_t newest;
56 };
57 
58 static int
_notmuch_thread_destructor(notmuch_thread_t * thread)59 _notmuch_thread_destructor (notmuch_thread_t *thread)
60 {
61     g_hash_table_unref (thread->authors_hash);
62     g_hash_table_unref (thread->matched_authors_hash);
63     g_hash_table_unref (thread->tags);
64     g_hash_table_unref (thread->message_hash);
65 
66     if (thread->authors_array) {
67 	g_ptr_array_free (thread->authors_array, true);
68 	thread->authors_array = NULL;
69     }
70 
71     if (thread->matched_authors_array) {
72 	g_ptr_array_free (thread->matched_authors_array, true);
73 	thread->matched_authors_array = NULL;
74     }
75 
76     return 0;
77 }
78 
79 /* Add each author of the thread to the thread's authors_hash and to
80  * the thread's authors_array. */
81 static void
_thread_add_author(notmuch_thread_t * thread,const char * author)82 _thread_add_author (notmuch_thread_t *thread,
83 		    const char *author)
84 {
85     char *author_copy;
86 
87     if (author == NULL)
88 	return;
89 
90     if (g_hash_table_lookup_extended (thread->authors_hash,
91 				      author, NULL, NULL))
92 	return;
93 
94     author_copy = talloc_strdup (thread, author);
95 
96     g_hash_table_insert (thread->authors_hash, author_copy, NULL);
97 
98     g_ptr_array_add (thread->authors_array, author_copy);
99 }
100 
101 /* Add each matched author of the thread to the thread's
102  * matched_authors_hash and to the thread's matched_authors_array. */
103 static void
_thread_add_matched_author(notmuch_thread_t * thread,const char * author)104 _thread_add_matched_author (notmuch_thread_t *thread,
105 			    const char *author)
106 {
107     char *author_copy;
108 
109     if (author == NULL)
110 	return;
111 
112     if (g_hash_table_lookup_extended (thread->matched_authors_hash,
113 				      author, NULL, NULL))
114 	return;
115 
116     author_copy = talloc_strdup (thread, author);
117 
118     g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
119 
120     g_ptr_array_add (thread->matched_authors_array, author_copy);
121 }
122 
123 /* Construct an authors string from matched_authors_array and
124  * authors_array. The string contains matched authors first, then
125  * non-matched authors (with the two groups separated by '|'). Within
126  * each group, authors are listed in date order. */
127 static void
_resolve_thread_authors_string(notmuch_thread_t * thread)128 _resolve_thread_authors_string (notmuch_thread_t *thread)
129 {
130     unsigned int i;
131     char *author;
132     int first_non_matched_author = 1;
133 
134     /* First, list all matched authors in date order. */
135     for (i = 0; i < thread->matched_authors_array->len; i++) {
136 	author = (char *) g_ptr_array_index (thread->matched_authors_array, i);
137 	if (thread->authors)
138 	    thread->authors = talloc_asprintf (thread, "%s, %s",
139 					       thread->authors,
140 					       author);
141 	else
142 	    thread->authors = author;
143     }
144 
145     /* Next, append any non-matched authors that haven't already appeared. */
146     for (i = 0; i < thread->authors_array->len; i++) {
147 	author = (char *) g_ptr_array_index (thread->authors_array, i);
148 	if (g_hash_table_lookup_extended (thread->matched_authors_hash,
149 					  author, NULL, NULL))
150 	    continue;
151 	if (first_non_matched_author) {
152 	    thread->authors = talloc_asprintf (thread, "%s| %s",
153 					       thread->authors,
154 					       author);
155 	} else {
156 	    thread->authors = talloc_asprintf (thread, "%s, %s",
157 					       thread->authors,
158 					       author);
159 	}
160 
161 	first_non_matched_author = 0;
162     }
163 
164     g_ptr_array_free (thread->authors_array, true);
165     thread->authors_array = NULL;
166     g_ptr_array_free (thread->matched_authors_array, true);
167     thread->matched_authors_array = NULL;
168 
169     if (! thread->authors)
170 	thread->authors = talloc_strdup (thread, "");
171 }
172 
173 /* clean up the ugly "Lastname, Firstname" format that some mail systems
174  * (most notably, Exchange) are creating to be "Firstname Lastname"
175  * To make sure that we don't change other potential situations where a
176  * comma is in the name, we check that we match one of these patterns
177  * "Last, First" <first.last@company.com>
178  * "Last, First MI" <first.mi.last@company.com>
179  */
180 static char *
_thread_cleanup_author(notmuch_thread_t * thread,const char * author,const char * from)181 _thread_cleanup_author (notmuch_thread_t *thread,
182 			const char *author, const char *from)
183 {
184     char *clean_author, *test_author;
185     const char *comma;
186     char *blank;
187     int fname, lname;
188 
189     if (author == NULL)
190 	return NULL;
191     clean_author = talloc_strdup (thread, author);
192     if (clean_author == NULL)
193 	return NULL;
194     /* check if there's a comma in the name and that there's a
195      * component of the name behind it (so the name doesn't end with
196      * the comma - in which case the string that strchr finds is just
197      * one character long ",\0").
198      * Otherwise just return the copy of the original author name that
199      * we just made*/
200     comma = strchr (author, ',');
201     if (comma && strlen (comma) > 1) {
202 	/* let's assemble what we think is the correct name */
203 	lname = comma - author;
204 
205 	/* Skip all the spaces after the comma */
206 	fname = strlen (author) - lname - 1;
207 	comma += 1;
208 	while (*comma == ' ') {
209 	    fname -= 1;
210 	    comma += 1;
211 	}
212 	strncpy (clean_author, comma, fname);
213 
214 	*(clean_author + fname) = ' ';
215 	strncpy (clean_author + fname + 1, author, lname);
216 	*(clean_author + fname + 1 + lname) = '\0';
217 	/* make a temporary copy and see if it matches the email */
218 	test_author = talloc_strdup (thread, clean_author);
219 
220 	blank = strchr (test_author, ' ');
221 	while (blank != NULL) {
222 	    *blank = '.';
223 	    blank = strchr (test_author, ' ');
224 	}
225 	if (strcasestr (from, test_author) == NULL)
226 	    /* we didn't identify this as part of the email address
227 	     * so let's punt and return the original author */
228 	    strcpy (clean_author, author);
229     }
230     return clean_author;
231 }
232 
233 /* Add 'message' as a message that belongs to 'thread'.
234  *
235  * The 'thread' will talloc_steal the 'message' and hold onto a
236  * reference to it.
237  */
238 static void
_thread_add_message(notmuch_thread_t * thread,notmuch_message_t * message,notmuch_string_list_t * exclude_terms,notmuch_exclude_t omit_exclude)239 _thread_add_message (notmuch_thread_t *thread,
240 		     notmuch_message_t *message,
241 		     notmuch_string_list_t *exclude_terms,
242 		     notmuch_exclude_t omit_exclude)
243 {
244     notmuch_tags_t *tags;
245     const char *tag;
246     InternetAddressList *list = NULL;
247     InternetAddress *address;
248     const char *from, *author;
249     char *clean_author;
250     bool message_excluded = false;
251 
252     if (omit_exclude != NOTMUCH_EXCLUDE_FALSE) {
253 	for (tags = notmuch_message_get_tags (message);
254 	     notmuch_tags_valid (tags);
255 	     notmuch_tags_move_to_next (tags)) {
256 	    tag = notmuch_tags_get (tags);
257 	    /* Is message excluded? */
258 	    for (notmuch_string_node_t *term = exclude_terms->head;
259 		 term != NULL;
260 		 term = term->next) {
261 		/* Check for an empty string, and then ignore initial 'K'. */
262 		if (*(term->string) && strcmp (tag, (term->string + 1)) == 0) {
263 		    message_excluded = true;
264 		    break;
265 		}
266 	    }
267 	}
268     }
269 
270     if (message_excluded && omit_exclude == NOTMUCH_EXCLUDE_ALL)
271 	return;
272 
273     _notmuch_message_list_add_message (thread->message_list,
274 				       talloc_steal (thread, message));
275     thread->total_messages++;
276     thread->total_files += notmuch_message_count_files (message);
277 
278     g_hash_table_insert (thread->message_hash,
279 			 xstrdup (notmuch_message_get_message_id (message)),
280 			 message);
281 
282     from = notmuch_message_get_header (message, "from");
283     if (from)
284 	list = internet_address_list_parse (NULL, from);
285 
286     if (list) {
287 	address = internet_address_list_get_address (list, 0);
288 	if (address) {
289 	    author = internet_address_get_name (address);
290 	    /* We treat quoted empty names as if they were empty. */
291 	    if (author == NULL || author[0] == '\0') {
292 		InternetAddressMailbox *mailbox;
293 		mailbox = INTERNET_ADDRESS_MAILBOX (address);
294 		author = internet_address_mailbox_get_addr (mailbox);
295 	    }
296 	    clean_author = _thread_cleanup_author (thread, author, from);
297 	    _thread_add_author (thread, clean_author);
298 	    _notmuch_message_set_author (message, clean_author);
299 	}
300 	g_object_unref (G_OBJECT (list));
301     }
302 
303     if (! thread->subject) {
304 	const char *subject;
305 	subject = notmuch_message_get_header (message, "subject");
306 	thread->subject = talloc_strdup (thread, subject ? subject : "");
307     }
308 
309     for (tags = notmuch_message_get_tags (message);
310 	 notmuch_tags_valid (tags);
311 	 notmuch_tags_move_to_next (tags)) {
312 	tag = notmuch_tags_get (tags);
313 	g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
314     }
315 
316     /* Mark excluded messages. */
317     if (message_excluded)
318 	notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
319 }
320 
321 static void
_thread_set_subject_from_message(notmuch_thread_t * thread,notmuch_message_t * message)322 _thread_set_subject_from_message (notmuch_thread_t *thread,
323 				  notmuch_message_t *message)
324 {
325     const char *subject;
326     const char *cleaned_subject;
327 
328     subject = notmuch_message_get_header (message, "subject");
329     if (! subject)
330 	return;
331 
332     if ((strncasecmp (subject, "Re: ", 4) == 0) ||
333 	(strncasecmp (subject, "Aw: ", 4) == 0) ||
334 	(strncasecmp (subject, "Vs: ", 4) == 0) ||
335 	(strncasecmp (subject, "Sv: ", 4) == 0)) {
336 
337 	cleaned_subject = talloc_strndup (thread,
338 					  subject + 4,
339 					  strlen (subject) - 4);
340     } else {
341 	cleaned_subject = talloc_strdup (thread, subject);
342     }
343 
344     if (! EMPTY_STRING (cleaned_subject)) {
345 	if (thread->subject)
346 	    talloc_free (thread->subject);
347 
348 	thread->subject = talloc_strdup (thread, cleaned_subject);
349     }
350 }
351 
352 /* Add a message to this thread which is known to match the original
353  * search specification. The 'sort' parameter controls whether the
354  * oldest or newest matching subject is applied to the thread as a
355  * whole. Returns 0 on success.
356  */
357 static int
_thread_add_matched_message(notmuch_thread_t * thread,notmuch_message_t * message,notmuch_sort_t sort)358 _thread_add_matched_message (notmuch_thread_t *thread,
359 			     notmuch_message_t *message,
360 			     notmuch_sort_t sort)
361 {
362     time_t date;
363     notmuch_message_t *hashed_message;
364     notmuch_bool_t is_set;
365 
366     date = notmuch_message_get_date (message);
367 
368     if (date < thread->oldest || ! thread->matched_messages) {
369 	thread->oldest = date;
370 	if (sort == NOTMUCH_SORT_OLDEST_FIRST)
371 	    _thread_set_subject_from_message (thread, message);
372     }
373 
374     if (date > thread->newest || ! thread->matched_messages) {
375 	thread->newest = date;
376 	const char *cur_subject = notmuch_thread_get_subject (thread);
377 	if (sort != NOTMUCH_SORT_OLDEST_FIRST || EMPTY_STRING (cur_subject))
378 	    _thread_set_subject_from_message (thread, message);
379     }
380 
381     if (notmuch_message_get_flag_st (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, &is_set))
382 	return -1;
383     if (! is_set)
384 	thread->matched_messages++;
385 
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);
391     }
392 
393     _thread_add_matched_author (thread, _notmuch_message_get_author (hashed_message));
394     return 0;
395 }
396 
397 static bool
_parent_via_in_reply_to(notmuch_thread_t * thread,notmuch_message_t * message)398 _parent_via_in_reply_to (notmuch_thread_t *thread, notmuch_message_t *message)
399 {
400     notmuch_message_t *parent;
401     const char *in_reply_to;
402 
403     in_reply_to = _notmuch_message_get_in_reply_to (message);
404     THREAD_DEBUG ("checking message = %s in_reply_to=%s",
405 		  notmuch_message_get_message_id (message), in_reply_to);
406 
407     if (in_reply_to && (! EMPTY_STRING (in_reply_to)) &&
408 	g_hash_table_lookup_extended (thread->message_hash,
409 				      in_reply_to, NULL,
410 				      (void **) &parent)) {
411 	_notmuch_message_add_reply (parent, message);
412 	return true;
413     } else {
414 	return false;
415     }
416 }
417 
418 static void
_parent_or_toplevel(notmuch_thread_t * thread,notmuch_message_t * message)419 _parent_or_toplevel (notmuch_thread_t *thread, notmuch_message_t *message)
420 {
421     size_t max_depth = 0;
422     notmuch_message_t *new_parent;
423     notmuch_message_t *parent = NULL;
424     const notmuch_string_list_t *references =
425 	_notmuch_message_get_references (message);
426 
427     THREAD_DEBUG ("trying to reparent via references: %s",
428 		  notmuch_message_get_message_id (message));
429 
430     for (notmuch_string_node_t *ref_node = references->head;
431 	 ref_node; ref_node = ref_node->next) {
432 	THREAD_DEBUG ("checking reference=%s", ref_node->string);
433 	if ((g_hash_table_lookup_extended (thread->message_hash,
434 					   ref_node->string, NULL,
435 					   (void **) &new_parent))) {
436 	    size_t new_depth = _notmuch_message_get_thread_depth (new_parent);
437 	    THREAD_DEBUG ("got depth %lu", new_depth);
438 	    if (new_depth > max_depth || ! parent) {
439 		THREAD_DEBUG ("adding at depth %lu parent=%s", new_depth, ref_node->string);
440 		max_depth = new_depth;
441 		parent = new_parent;
442 	    }
443 	}
444     }
445     if (parent) {
446 	THREAD_DEBUG ("adding reply %s to parent=%s",
447 		      notmuch_message_get_message_id (message),
448 		      notmuch_message_get_message_id (parent));
449 	_notmuch_message_add_reply (parent, message);
450     } else {
451 	THREAD_DEBUG ("adding as toplevel %s",
452 		      notmuch_message_get_message_id (message));
453 	_notmuch_message_list_add_message (thread->toplevel_list, message);
454     }
455 }
456 
457 static void
_resolve_thread_relationships(notmuch_thread_t * thread)458 _resolve_thread_relationships (notmuch_thread_t *thread)
459 {
460     notmuch_message_node_t *node, *first_node;
461     notmuch_message_t *message;
462     void *local;
463     notmuch_message_list_t *maybe_toplevel_list;
464 
465     first_node = thread->message_list->head;
466     if (! first_node)
467 	return;
468 
469     local = talloc_new (thread);
470     maybe_toplevel_list = _notmuch_message_list_create (local);
471 
472     for (node = first_node->next; node; node = node->next) {
473 	message = node->message;
474 	if (! _parent_via_in_reply_to (thread, message))
475 	    _notmuch_message_list_add_message (maybe_toplevel_list, message);
476     }
477 
478     /*
479      * if we reach the end of the list without finding a top-level
480      * message, that means the thread is a cycle (or set of cycles)
481      * and any message can be considered top-level.  Choose the oldest
482      * message, which happens to be first in our list.
483      */
484     if (first_node) {
485 	message = first_node->message;
486 	THREAD_DEBUG ("checking first message  %s",
487 		      notmuch_message_get_message_id (message));
488 
489 	if (_notmuch_message_list_empty (maybe_toplevel_list) ||
490 	    ! _parent_via_in_reply_to (thread, message)) {
491 
492 	    THREAD_DEBUG ("adding first message as toplevel = %s",
493 			  notmuch_message_get_message_id (message));
494 	    _notmuch_message_list_add_message (maybe_toplevel_list, message);
495 	}
496     }
497 
498     for (notmuch_messages_t *messages = _notmuch_messages_create (maybe_toplevel_list);
499 	 notmuch_messages_valid (messages);
500 	 notmuch_messages_move_to_next (messages)) {
501 	notmuch_message_t *message = notmuch_messages_get (messages);
502 	_notmuch_message_label_depths (message, 0);
503     }
504 
505     for (notmuch_messages_t *roots = _notmuch_messages_create (maybe_toplevel_list);
506 	 notmuch_messages_valid (roots);
507 	 notmuch_messages_move_to_next (roots)) {
508 	notmuch_message_t *message = notmuch_messages_get (roots);
509 	if (_notmuch_messages_has_next (roots) || ! _notmuch_message_list_empty (
510 		thread->toplevel_list))
511 	    _parent_or_toplevel (thread, message);
512 	else
513 	    _notmuch_message_list_add_message (thread->toplevel_list, message);
514     }
515 
516     /* XXX this could be made conditional on messages being inserted
517      * (out of order) in later passes
518      */
519     thread->toplevel_list = _notmuch_message_sort_subtrees (thread, thread->toplevel_list);
520 
521     talloc_free (local);
522 }
523 
524 /* Create a new notmuch_thread_t object by finding the thread
525  * containing the message with the given doc ID, treating any messages
526  * contained in match_set as "matched".  Remove all messages in the
527  * thread from match_set.
528  *
529  * Creating the thread will perform a database search to get all
530  * messages belonging to the thread and will get the first subject
531  * line, the total count of messages, and all authors in the thread.
532  * Each message in the thread is checked against match_set to allow
533  * for a separate count of matched messages, and to allow a viewer to
534  * display these messages differently.
535  *
536  * Here, 'ctx' is talloc context for the resulting thread object.
537  *
538  * This function returns NULL in the case of any error.
539  */
540 notmuch_thread_t *
_notmuch_thread_create(void * ctx,notmuch_database_t * notmuch,unsigned int seed_doc_id,notmuch_doc_id_set_t * match_set,notmuch_string_list_t * exclude_terms,notmuch_exclude_t omit_excluded,notmuch_sort_t sort)541 _notmuch_thread_create (void *ctx,
542 			notmuch_database_t *notmuch,
543 			unsigned int seed_doc_id,
544 			notmuch_doc_id_set_t *match_set,
545 			notmuch_string_list_t *exclude_terms,
546 			notmuch_exclude_t omit_excluded,
547 			notmuch_sort_t sort)
548 {
549     void *local = talloc_new (ctx);
550     notmuch_thread_t *thread = NULL;
551     notmuch_message_t *seed_message;
552     const char *thread_id;
553     char *thread_id_query_string;
554     notmuch_query_t *thread_id_query;
555 
556     notmuch_messages_t *messages;
557     notmuch_message_t *message;
558     notmuch_status_t status;
559 
560     seed_message = _notmuch_message_create (local, notmuch, seed_doc_id, NULL);
561     if (! seed_message)
562 	INTERNAL_ERROR ("Thread seed message %u does not exist", seed_doc_id);
563 
564     thread_id = notmuch_message_get_thread_id (seed_message);
565     thread_id_query_string = talloc_asprintf (local, "thread:%s", thread_id);
566     if (unlikely (thread_id_query_string == NULL))
567 	goto DONE;
568 
569     thread_id_query = talloc_steal (
570 	local, notmuch_query_create (notmuch, thread_id_query_string));
571     if (unlikely (thread_id_query == NULL))
572 	goto DONE;
573 
574     thread = talloc (local, notmuch_thread_t);
575     if (unlikely (thread == NULL))
576 	goto DONE;
577 
578     talloc_set_destructor (thread, _notmuch_thread_destructor);
579 
580     thread->notmuch = notmuch;
581     thread->thread_id = talloc_strdup (thread, thread_id);
582     thread->subject = NULL;
583     thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
584 						  NULL, NULL);
585     thread->authors_array = g_ptr_array_new ();
586     thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
587 							  g_str_equal,
588 							  NULL, NULL);
589     thread->matched_authors_array = g_ptr_array_new ();
590     thread->authors = NULL;
591     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
592 					  free, NULL);
593 
594     thread->message_list = _notmuch_message_list_create (thread);
595     thread->toplevel_list = _notmuch_message_list_create (thread);
596     if (unlikely (thread->message_list == NULL ||
597 		  thread->toplevel_list == NULL)) {
598 	thread = NULL;
599 	goto DONE;
600     }
601 
602     thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
603 						  free, NULL);
604 
605     thread->total_messages = 0;
606     thread->total_files = 0;
607     thread->matched_messages = 0;
608     thread->oldest = 0;
609     thread->newest = 0;
610 
611     /* We use oldest-first order unconditionally here to obtain the
612      * proper author ordering for the thread. The 'sort' parameter
613      * passed to this function is used only to indicate whether the
614      * oldest or newest subject is desired. */
615     notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
616 
617     status = notmuch_query_search_messages (thread_id_query, &messages);
618     if (status)
619 	goto DONE;
620 
621     for (;
622 	 notmuch_messages_valid (messages);
623 	 notmuch_messages_move_to_next (messages)) {
624 	unsigned int doc_id;
625 
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;
630 
631 	_thread_add_message (thread, message, exclude_terms, omit_excluded);
632 
633 	if ( _notmuch_doc_id_set_contains (match_set, doc_id)) {
634 	    _notmuch_doc_id_set_remove (match_set, doc_id);
635 	    if (_thread_add_matched_message (thread, message, sort)) {
636 		thread = NULL;
637 		goto DONE;
638 	    }
639 	}
640 
641 	_notmuch_message_close (message);
642     }
643 
644     _resolve_thread_authors_string (thread);
645 
646     _resolve_thread_relationships (thread);
647 
648     /* Commit to returning thread. */
649     (void) talloc_steal (ctx, thread);
650 
651   DONE:
652     talloc_free (local);
653     return thread;
654 }
655 
656 notmuch_messages_t *
notmuch_thread_get_toplevel_messages(notmuch_thread_t * thread)657 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
658 {
659     return _notmuch_messages_create (thread->toplevel_list);
660 }
661 
662 notmuch_messages_t *
notmuch_thread_get_messages(notmuch_thread_t * thread)663 notmuch_thread_get_messages (notmuch_thread_t *thread)
664 {
665     return _notmuch_messages_create (thread->message_list);
666 }
667 
668 const char *
notmuch_thread_get_thread_id(notmuch_thread_t * thread)669 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
670 {
671     return thread->thread_id;
672 }
673 
674 int
notmuch_thread_get_total_messages(notmuch_thread_t * thread)675 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
676 {
677     return thread->total_messages;
678 }
679 
680 int
notmuch_thread_get_total_files(notmuch_thread_t * thread)681 notmuch_thread_get_total_files (notmuch_thread_t *thread)
682 {
683     return thread->total_files;
684 }
685 
686 int
notmuch_thread_get_matched_messages(notmuch_thread_t * thread)687 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
688 {
689     return thread->matched_messages;
690 }
691 
692 const char *
notmuch_thread_get_authors(notmuch_thread_t * thread)693 notmuch_thread_get_authors (notmuch_thread_t *thread)
694 {
695     return thread->authors;
696 }
697 
698 const char *
notmuch_thread_get_subject(notmuch_thread_t * thread)699 notmuch_thread_get_subject (notmuch_thread_t *thread)
700 {
701     return thread->subject;
702 }
703 
704 time_t
notmuch_thread_get_oldest_date(notmuch_thread_t * thread)705 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
706 {
707     return thread->oldest;
708 }
709 
710 time_t
notmuch_thread_get_newest_date(notmuch_thread_t * thread)711 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
712 {
713     return thread->newest;
714 }
715 
716 notmuch_tags_t *
notmuch_thread_get_tags(notmuch_thread_t * thread)717 notmuch_thread_get_tags (notmuch_thread_t *thread)
718 {
719     notmuch_string_list_t *tags;
720     GList *keys, *l;
721 
722     tags = _notmuch_string_list_create (thread);
723     if (unlikely (tags == NULL))
724 	return NULL;
725 
726     keys = g_hash_table_get_keys (thread->tags);
727 
728     for (l = keys; l; l = l->next)
729 	_notmuch_string_list_append (tags, (char *) l->data);
730 
731     g_list_free (keys);
732 
733     _notmuch_string_list_sort (tags);
734 
735     return _notmuch_tags_create (thread, tags);
736 }
737 
738 void
notmuch_thread_destroy(notmuch_thread_t * thread)739 notmuch_thread_destroy (notmuch_thread_t *thread)
740 {
741     talloc_free (thread);
742 }
743