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 #define EMPTY_STRING(s) ((s)[0] == '\0')
29 struct _notmuch_thread {
30 notmuch_database_t *notmuch;
33 GHashTable *authors_hash;
34 GPtrArray *authors_array;
35 GHashTable *matched_authors_hash;
36 GPtrArray *matched_authors_array;
40 /* All messages, oldest first. */
41 notmuch_message_list_t *message_list;
42 /* Top-level messages, oldest first. */
43 notmuch_message_list_t *toplevel_list;
45 GHashTable *message_hash;
54 _notmuch_thread_destructor (notmuch_thread_t *thread)
56 g_hash_table_unref (thread->authors_hash);
57 g_hash_table_unref (thread->matched_authors_hash);
58 g_hash_table_unref (thread->tags);
59 g_hash_table_unref (thread->message_hash);
61 if (thread->authors_array) {
62 g_ptr_array_free (thread->authors_array, TRUE);
63 thread->authors_array = NULL;
66 if (thread->matched_authors_array) {
67 g_ptr_array_free (thread->matched_authors_array, TRUE);
68 thread->matched_authors_array = NULL;
74 /* Add each author of the thread to the thread's authors_hash and to
75 * the thread's authors_array. */
77 _thread_add_author (notmuch_thread_t *thread,
85 if (g_hash_table_lookup_extended (thread->authors_hash,
89 author_copy = talloc_strdup (thread, author);
91 g_hash_table_insert (thread->authors_hash, author_copy, NULL);
93 g_ptr_array_add (thread->authors_array, author_copy);
96 /* Add each matched author of the thread to the thread's
97 * matched_authors_hash and to the thread's matched_authors_array. */
99 _thread_add_matched_author (notmuch_thread_t *thread,
107 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
111 author_copy = talloc_strdup (thread, author);
113 g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
115 g_ptr_array_add (thread->matched_authors_array, author_copy);
118 /* Construct an authors string from matched_authors_array and
119 * authors_array. The string contains matched authors first, then
120 * non-matched authors (with the two groups separated by '|'). Within
121 * each group, authors are listed in date order. */
123 _resolve_thread_authors_string (notmuch_thread_t *thread)
127 int first_non_matched_author = 1;
129 /* First, list all matched authors in date order. */
130 for (i = 0; i < thread->matched_authors_array->len; i++) {
131 author = (char *) g_ptr_array_index (thread->matched_authors_array, i);
133 thread->authors = talloc_asprintf (thread, "%s, %s",
137 thread->authors = author;
140 /* Next, append any non-matched authors that haven't already appeared. */
141 for (i = 0; i < thread->authors_array->len; i++) {
142 author = (char *) g_ptr_array_index (thread->authors_array, i);
143 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
146 if (first_non_matched_author) {
147 thread->authors = talloc_asprintf (thread, "%s| %s",
151 thread->authors = talloc_asprintf (thread, "%s, %s",
156 first_non_matched_author = 0;
159 g_ptr_array_free (thread->authors_array, TRUE);
160 thread->authors_array = NULL;
161 g_ptr_array_free (thread->matched_authors_array, TRUE);
162 thread->matched_authors_array = NULL;
165 /* clean up the ugly "Lastname, Firstname" format that some mail systems
166 * (most notably, Exchange) are creating to be "Firstname Lastname"
167 * To make sure that we don't change other potential situations where a
168 * comma is in the name, we check that we match one of these patterns
169 * "Last, First" <first.last@company.com>
170 * "Last, First MI" <first.mi.last@company.com>
173 _thread_cleanup_author (notmuch_thread_t *thread,
174 const char *author, const char *from)
176 char *clean_author,*test_author;
183 clean_author = talloc_strdup(thread, author);
184 if (clean_author == NULL)
186 /* check if there's a comma in the name and that there's a
187 * component of the name behind it (so the name doesn't end with
188 * the comma - in which case the string that strchr finds is just
189 * one character long ",\0").
190 * Otherwise just return the copy of the original author name that
192 comma = strchr(author,',');
193 if (comma && strlen(comma) > 1) {
194 /* let's assemble what we think is the correct name */
195 lname = comma - author;
197 /* Skip all the spaces after the comma */
198 fname = strlen(author) - lname - 1;
200 while (*comma == ' ') {
204 strncpy(clean_author, comma, fname);
206 *(clean_author+fname) = ' ';
207 strncpy(clean_author + fname + 1, author, lname);
208 *(clean_author+fname+1+lname) = '\0';
209 /* make a temporary copy and see if it matches the email */
210 test_author = talloc_strdup(thread,clean_author);
212 blank=strchr(test_author,' ');
213 while (blank != NULL) {
215 blank=strchr(test_author,' ');
217 if (strcasestr(from, test_author) == NULL)
218 /* we didn't identify this as part of the email address
219 * so let's punt and return the original author */
220 strcpy (clean_author, author);
225 /* Add 'message' as a message that belongs to 'thread'.
227 * The 'thread' will talloc_steal the 'message' and hold onto a
231 _thread_add_message (notmuch_thread_t *thread,
232 notmuch_message_t *message,
233 notmuch_string_list_t *exclude_terms,
234 notmuch_exclude_t omit_exclude)
236 notmuch_tags_t *tags;
238 InternetAddressList *list = NULL;
239 InternetAddress *address;
240 const char *from, *author;
242 notmuch_bool_t message_excluded = FALSE;
244 if (omit_exclude != NOTMUCH_EXCLUDE_FALSE) {
245 for (tags = notmuch_message_get_tags (message);
246 notmuch_tags_valid (tags);
247 notmuch_tags_move_to_next (tags))
249 tag = notmuch_tags_get (tags);
250 /* Is message excluded? */
251 for (notmuch_string_node_t *term = exclude_terms->head;
255 /* Check for an empty string, and then ignore initial 'K'. */
256 if (*(term->string) && strcmp(tag, (term->string + 1)) == 0) {
257 message_excluded = TRUE;
264 if (message_excluded && omit_exclude == NOTMUCH_EXCLUDE_ALL)
267 _notmuch_message_list_add_message (thread->message_list,
268 talloc_steal (thread, message));
269 thread->total_messages++;
270 thread->total_files += notmuch_message_count_files (message);
272 g_hash_table_insert (thread->message_hash,
273 xstrdup (notmuch_message_get_message_id (message)),
276 from = notmuch_message_get_header (message, "from");
278 list = internet_address_list_parse_string (from);
281 address = internet_address_list_get_address (list, 0);
283 author = internet_address_get_name (address);
284 /* We treat quoted empty names as if they were empty. */
285 if (author == NULL || author[0] == '\0') {
286 InternetAddressMailbox *mailbox;
287 mailbox = INTERNET_ADDRESS_MAILBOX (address);
288 author = internet_address_mailbox_get_addr (mailbox);
290 clean_author = _thread_cleanup_author (thread, author, from);
291 _thread_add_author (thread, clean_author);
292 _notmuch_message_set_author (message, clean_author);
294 g_object_unref (G_OBJECT (list));
297 if (! thread->subject) {
299 subject = notmuch_message_get_header (message, "subject");
300 thread->subject = talloc_strdup (thread, subject ? subject : "");
303 for (tags = notmuch_message_get_tags (message);
304 notmuch_tags_valid (tags);
305 notmuch_tags_move_to_next (tags))
307 tag = notmuch_tags_get (tags);
308 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
311 /* Mark excluded messages. */
312 if (message_excluded)
313 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);
317 _thread_set_subject_from_message (notmuch_thread_t *thread,
318 notmuch_message_t *message)
321 const char *cleaned_subject;
323 subject = notmuch_message_get_header (message, "subject");
327 if ((strncasecmp (subject, "Re: ", 4) == 0) ||
328 (strncasecmp (subject, "Aw: ", 4) == 0) ||
329 (strncasecmp (subject, "Vs: ", 4) == 0) ||
330 (strncasecmp (subject, "Sv: ", 4) == 0)) {
332 cleaned_subject = talloc_strndup (thread,
334 strlen(subject) - 4);
336 cleaned_subject = talloc_strdup (thread, subject);
339 if (! EMPTY_STRING(cleaned_subject)) {
341 talloc_free (thread->subject);
343 thread->subject = talloc_strdup (thread, cleaned_subject);
347 /* Add a message to this thread which is known to match the original
348 * search specification. The 'sort' parameter controls whether the
349 * oldest or newest matching subject is applied to the thread as a
352 _thread_add_matched_message (notmuch_thread_t *thread,
353 notmuch_message_t *message,
357 notmuch_message_t *hashed_message;
359 date = notmuch_message_get_date (message);
361 if (date < thread->oldest || ! thread->matched_messages) {
362 thread->oldest = date;
363 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
364 _thread_set_subject_from_message (thread, message);
367 if (date > thread->newest || ! thread->matched_messages) {
368 thread->newest = date;
369 const char *cur_subject = notmuch_thread_get_subject(thread);
370 if (sort != NOTMUCH_SORT_OLDEST_FIRST || EMPTY_STRING(cur_subject))
371 _thread_set_subject_from_message (thread, message);
374 if (!notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED))
375 thread->matched_messages++;
377 if (g_hash_table_lookup_extended (thread->message_hash,
378 notmuch_message_get_message_id (message), NULL,
379 (void **) &hashed_message)) {
380 notmuch_message_set_flag (hashed_message,
381 NOTMUCH_MESSAGE_FLAG_MATCH, 1);
384 _thread_add_matched_author (thread, _notmuch_message_get_author (hashed_message));
388 _resolve_thread_relationships (notmuch_thread_t *thread)
390 notmuch_message_node_t *node;
391 notmuch_message_t *message, *parent;
392 const char *in_reply_to;
394 for (node = thread->message_list->head; node; node = node->next) {
395 message = node->message;
396 in_reply_to = _notmuch_message_get_in_reply_to (message);
397 if (in_reply_to && strlen (in_reply_to) &&
398 g_hash_table_lookup_extended (thread->message_hash,
401 _notmuch_message_add_reply (parent, message);
403 _notmuch_message_list_add_message (thread->toplevel_list, message);
406 /* XXX: After scanning through the entire list looking for parents
407 * via "In-Reply-To", we should do a second pass that looks at the
408 * list of messages IDs in the "References" header instead. (And
409 * for this the parent would be the "deepest" message of all the
410 * messages found in the "References" list.)
412 * Doing this will allow messages and sub-threads to be positioned
413 * correctly in the thread even when an intermediate message is
414 * missing from the thread.
418 /* Create a new notmuch_thread_t object by finding the thread
419 * containing the message with the given doc ID, treating any messages
420 * contained in match_set as "matched". Remove all messages in the
421 * thread from match_set.
423 * Creating the thread will perform a database search to get all
424 * messages belonging to the thread and will get the first subject
425 * line, the total count of messages, and all authors in the thread.
426 * Each message in the thread is checked against match_set to allow
427 * for a separate count of matched messages, and to allow a viewer to
428 * display these messages differently.
430 * Here, 'ctx' is talloc context for the resulting thread object.
432 * This function returns NULL in the case of any error.
435 _notmuch_thread_create (void *ctx,
436 notmuch_database_t *notmuch,
437 unsigned int seed_doc_id,
438 notmuch_doc_id_set_t *match_set,
439 notmuch_string_list_t *exclude_terms,
440 notmuch_exclude_t omit_excluded,
443 void *local = talloc_new (ctx);
444 notmuch_thread_t *thread = NULL;
445 notmuch_message_t *seed_message;
446 const char *thread_id;
447 char *thread_id_query_string;
448 notmuch_query_t *thread_id_query;
450 notmuch_messages_t *messages;
451 notmuch_message_t *message;
452 notmuch_status_t status;
454 seed_message = _notmuch_message_create (local, notmuch, seed_doc_id, NULL);
456 INTERNAL_ERROR ("Thread seed message %u does not exist", seed_doc_id);
458 thread_id = notmuch_message_get_thread_id (seed_message);
459 thread_id_query_string = talloc_asprintf (local, "thread:%s", thread_id);
460 if (unlikely (thread_id_query_string == NULL))
463 thread_id_query = talloc_steal (
464 local, notmuch_query_create (notmuch, thread_id_query_string));
465 if (unlikely (thread_id_query == NULL))
468 thread = talloc (local, notmuch_thread_t);
469 if (unlikely (thread == NULL))
472 talloc_set_destructor (thread, _notmuch_thread_destructor);
474 thread->notmuch = notmuch;
475 thread->thread_id = talloc_strdup (thread, thread_id);
476 thread->subject = NULL;
477 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
479 thread->authors_array = g_ptr_array_new ();
480 thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
483 thread->matched_authors_array = g_ptr_array_new ();
484 thread->authors = NULL;
485 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
488 thread->message_list = _notmuch_message_list_create (thread);
489 thread->toplevel_list = _notmuch_message_list_create (thread);
490 if (unlikely (thread->message_list == NULL ||
491 thread->toplevel_list == NULL)) {
496 thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
499 thread->total_messages = 0;
500 thread->total_files = 0;
501 thread->matched_messages = 0;
505 /* We use oldest-first order unconditionally here to obtain the
506 * proper author ordering for the thread. The 'sort' parameter
507 * passed to this function is used only to indicate whether the
508 * oldest or newest subject is desired. */
509 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
511 status = notmuch_query_search_messages (thread_id_query, &messages);
516 notmuch_messages_valid (messages);
517 notmuch_messages_move_to_next (messages))
521 message = notmuch_messages_get (messages);
522 doc_id = _notmuch_message_get_doc_id (message);
523 if (doc_id == seed_doc_id)
524 message = seed_message;
526 _thread_add_message (thread, message, exclude_terms, omit_excluded);
528 if ( _notmuch_doc_id_set_contains (match_set, doc_id)) {
529 _notmuch_doc_id_set_remove (match_set, doc_id);
530 _thread_add_matched_message (thread, message, sort);
533 _notmuch_message_close (message);
536 _resolve_thread_authors_string (thread);
538 _resolve_thread_relationships (thread);
540 /* Commit to returning thread. */
541 (void) talloc_steal (ctx, thread);
549 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
551 return _notmuch_messages_create (thread->toplevel_list);
555 notmuch_thread_get_messages (notmuch_thread_t *thread)
557 return _notmuch_messages_create (thread->message_list);
561 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
563 return thread->thread_id;
567 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
569 return thread->total_messages;
573 notmuch_thread_get_total_files (notmuch_thread_t *thread)
575 return thread->total_files;
579 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
581 return thread->matched_messages;
585 notmuch_thread_get_authors (notmuch_thread_t *thread)
587 return thread->authors;
591 notmuch_thread_get_subject (notmuch_thread_t *thread)
593 return thread->subject;
597 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
599 return thread->oldest;
603 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
605 return thread->newest;
609 notmuch_thread_get_tags (notmuch_thread_t *thread)
611 notmuch_string_list_t *tags;
614 tags = _notmuch_string_list_create (thread);
615 if (unlikely (tags == NULL))
618 keys = g_hash_table_get_keys (thread->tags);
620 for (l = keys; l; l = l->next)
621 _notmuch_string_list_append (tags, (char *) l->data);
625 _notmuch_string_list_sort (tags);
627 return _notmuch_tags_create (thread, tags);
631 notmuch_thread_destroy (notmuch_thread_t *thread)
633 talloc_free (thread);