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 http://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 struct visible _notmuch_thread {
28 notmuch_database_t *notmuch;
31 GHashTable *authors_hash;
32 GPtrArray *authors_array;
33 GHashTable *matched_authors_hash;
34 GPtrArray *matched_authors_array;
38 /* All messages, oldest first. */
39 notmuch_message_list_t *message_list;
40 /* Top-level messages, oldest first. */
41 notmuch_message_list_t *toplevel_list;
43 GHashTable *message_hash;
51 _notmuch_thread_destructor (notmuch_thread_t *thread)
53 g_hash_table_unref (thread->authors_hash);
54 g_hash_table_unref (thread->matched_authors_hash);
55 g_hash_table_unref (thread->tags);
56 g_hash_table_unref (thread->message_hash);
58 if (thread->authors_array) {
59 g_ptr_array_free (thread->authors_array, TRUE);
60 thread->authors_array = NULL;
63 if (thread->matched_authors_array) {
64 g_ptr_array_free (thread->matched_authors_array, TRUE);
65 thread->matched_authors_array = NULL;
71 /* Add each author of the thread to the thread's authors_hash and to
72 * the thread's authors_array. */
74 _thread_add_author (notmuch_thread_t *thread,
82 if (g_hash_table_lookup_extended (thread->authors_hash,
86 author_copy = talloc_strdup (thread, author);
88 g_hash_table_insert (thread->authors_hash, author_copy, NULL);
90 g_ptr_array_add (thread->authors_array, author_copy);
93 /* Add each matched author of the thread to the thread's
94 * matched_authors_hash and to the thread's matched_authors_array. */
96 _thread_add_matched_author (notmuch_thread_t *thread,
104 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
108 author_copy = talloc_strdup (thread, author);
110 g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
112 g_ptr_array_add (thread->matched_authors_array, author_copy);
115 /* Construct an authors string from matched_authors_array and
116 * authors_array. The string contains matched authors first, then
117 * non-matched authors (with the two groups separated by '|'). Within
118 * each group, authors are listed in date order. */
120 _resolve_thread_authors_string (notmuch_thread_t *thread)
124 int first_non_matched_author = 1;
126 /* First, list all matched authors in date order. */
127 for (i = 0; i < thread->matched_authors_array->len; i++) {
128 author = (char *) g_ptr_array_index (thread->matched_authors_array, i);
130 thread->authors = talloc_asprintf (thread, "%s, %s",
134 thread->authors = author;
137 /* Next, append any non-matched authors that haven't already appeared. */
138 for (i = 0; i < thread->authors_array->len; i++) {
139 author = (char *) g_ptr_array_index (thread->authors_array, i);
140 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
143 if (first_non_matched_author) {
144 thread->authors = talloc_asprintf (thread, "%s| %s",
148 thread->authors = talloc_asprintf (thread, "%s, %s",
153 first_non_matched_author = 0;
156 g_ptr_array_free (thread->authors_array, TRUE);
157 thread->authors_array = NULL;
158 g_ptr_array_free (thread->matched_authors_array, TRUE);
159 thread->matched_authors_array = NULL;
162 /* clean up the ugly "Lastname, Firstname" format that some mail systems
163 * (most notably, Exchange) are creating to be "Firstname Lastname"
164 * To make sure that we don't change other potential situations where a
165 * comma is in the name, we check that we match one of these patterns
166 * "Last, First" <first.last@company.com>
167 * "Last, First MI" <first.mi.last@company.com>
170 _thread_cleanup_author (notmuch_thread_t *thread,
171 const char *author, const char *from)
173 char *clean_author,*test_author;
180 clean_author = talloc_strdup(thread, author);
181 if (clean_author == NULL)
183 /* check if there's a comma in the name and that there's a
184 * component of the name behind it (so the name doesn't end with
185 * the comma - in which case the string that strchr finds is just
186 * one character long ",\0").
187 * Otherwise just return the copy of the original author name that
189 comma = strchr(author,',');
190 if (comma && strlen(comma) > 1) {
191 /* let's assemble what we think is the correct name */
192 lname = comma - author;
194 /* Skip all the spaces after the comma */
195 fname = strlen(author) - lname - 1;
197 while (*comma == ' ') {
201 strncpy(clean_author, comma, fname);
203 *(clean_author+fname) = ' ';
204 strncpy(clean_author + fname + 1, author, lname);
205 *(clean_author+fname+1+lname) = '\0';
206 /* make a temporary copy and see if it matches the email */
207 test_author = talloc_strdup(thread,clean_author);
209 blank=strchr(test_author,' ');
210 while (blank != NULL) {
212 blank=strchr(test_author,' ');
214 if (strcasestr(from, test_author) == NULL)
215 /* we didn't identify this as part of the email address
216 * so let's punt and return the original author */
217 strcpy (clean_author, author);
222 /* Add 'message' as a message that belongs to 'thread'.
224 * The 'thread' will talloc_steal the 'message' and hold onto a
228 _thread_add_message (notmuch_thread_t *thread,
229 notmuch_message_t *message,
230 notmuch_string_list_t *exclude_terms,
231 notmuch_exclude_t omit_exclude)
233 notmuch_tags_t *tags;
235 InternetAddressList *list = NULL;
236 InternetAddress *address;
237 const char *from, *author;
239 notmuch_bool_t message_excluded = FALSE;
241 if (omit_exclude != NOTMUCH_EXCLUDE_FALSE) {
242 for (tags = notmuch_message_get_tags (message);
243 notmuch_tags_valid (tags);
244 notmuch_tags_move_to_next (tags))
246 tag = notmuch_tags_get (tags);
247 /* Is message excluded? */
248 for (notmuch_string_node_t *term = exclude_terms->head;
252 /* We ignore initial 'K'. */
253 if (strcmp(tag, (term->string + 1)) == 0) {
254 message_excluded = TRUE;
261 if (message_excluded && omit_exclude == NOTMUCH_EXCLUDE_ALL)
264 _notmuch_message_list_add_message (thread->message_list,
265 talloc_steal (thread, message));
266 thread->total_messages++;
268 g_hash_table_insert (thread->message_hash,
269 xstrdup (notmuch_message_get_message_id (message)),
272 from = notmuch_message_get_header (message, "from");
274 list = internet_address_list_parse_string (from);
277 address = internet_address_list_get_address (list, 0);
279 author = internet_address_get_name (address);
280 /* We treat quoted empty names as if they were empty. */
281 if (author == NULL || author[0] == '\0') {
282 InternetAddressMailbox *mailbox;
283 mailbox = INTERNET_ADDRESS_MAILBOX (address);
284 author = internet_address_mailbox_get_addr (mailbox);
286 clean_author = _thread_cleanup_author (thread, author, from);
287 _thread_add_author (thread, clean_author);
288 _notmuch_message_set_author (message, clean_author);
290 g_object_unref (G_OBJECT (list));
293 if (! thread->subject) {
295 subject = notmuch_message_get_header (message, "subject");
296 thread->subject = talloc_strdup (thread, subject ? subject : "");
299 for (tags = notmuch_message_get_tags (message);
300 notmuch_tags_valid (tags);
301 notmuch_tags_move_to_next (tags))
303 tag = notmuch_tags_get (tags);
304 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
307 /* Mark excluded messages. */
308 if (message_excluded)
309 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);
313 _thread_set_subject_from_message (notmuch_thread_t *thread,
314 notmuch_message_t *message)
317 const char *cleaned_subject;
319 subject = notmuch_message_get_header (message, "subject");
323 if ((strncasecmp (subject, "Re: ", 4) == 0) ||
324 (strncasecmp (subject, "Aw: ", 4) == 0) ||
325 (strncasecmp (subject, "Vs: ", 4) == 0) ||
326 (strncasecmp (subject, "Sv: ", 4) == 0)) {
328 cleaned_subject = talloc_strndup (thread,
330 strlen(subject) - 4);
332 cleaned_subject = talloc_strdup (thread, subject);
336 talloc_free (thread->subject);
338 thread->subject = talloc_strdup (thread, cleaned_subject);
341 /* Add a message to this thread which is known to match the original
342 * search specification. The 'sort' parameter controls whether the
343 * oldest or newest matching subject is applied to the thread as a
346 _thread_add_matched_message (notmuch_thread_t *thread,
347 notmuch_message_t *message,
351 notmuch_message_t *hashed_message;
353 date = notmuch_message_get_date (message);
355 if (date < thread->oldest || ! thread->matched_messages) {
356 thread->oldest = date;
357 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
358 _thread_set_subject_from_message (thread, message);
361 if (date > thread->newest || ! thread->matched_messages) {
362 thread->newest = date;
363 if (sort != NOTMUCH_SORT_OLDEST_FIRST)
364 _thread_set_subject_from_message (thread, message);
367 if (!notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED))
368 thread->matched_messages++;
370 if (g_hash_table_lookup_extended (thread->message_hash,
371 notmuch_message_get_message_id (message), NULL,
372 (void **) &hashed_message)) {
373 notmuch_message_set_flag (hashed_message,
374 NOTMUCH_MESSAGE_FLAG_MATCH, 1);
377 _thread_add_matched_author (thread, _notmuch_message_get_author (hashed_message));
381 _resolve_thread_relationships (notmuch_thread_t *thread)
383 notmuch_message_node_t *node;
384 notmuch_message_t *message, *parent;
385 const char *in_reply_to;
387 for (node = thread->message_list->head; node; node = node->next) {
388 message = node->message;
389 in_reply_to = _notmuch_message_get_in_reply_to (message);
390 if (in_reply_to && strlen (in_reply_to) &&
391 g_hash_table_lookup_extended (thread->message_hash,
394 _notmuch_message_add_reply (parent, message);
396 _notmuch_message_list_add_message (thread->toplevel_list, message);
399 /* XXX: After scanning through the entire list looking for parents
400 * via "In-Reply-To", we should do a second pass that looks at the
401 * list of messages IDs in the "References" header instead. (And
402 * for this the parent would be the "deepest" message of all the
403 * messages found in the "References" list.)
405 * Doing this will allow messages and sub-threads to be positioned
406 * correctly in the thread even when an intermediate message is
407 * missing from the thread.
411 /* Create a new notmuch_thread_t object by finding the thread
412 * containing the message with the given doc ID, treating any messages
413 * contained in match_set as "matched". Remove all messages in the
414 * thread from match_set.
416 * Creating the thread will perform a database search to get all
417 * messages belonging to the thread and will get the first subject
418 * line, the total count of messages, and all authors in the thread.
419 * Each message in the thread is checked against match_set to allow
420 * for a separate count of matched messages, and to allow a viewer to
421 * display these messages differently.
423 * Here, 'ctx' is talloc context for the resulting thread object.
425 * This function returns NULL in the case of any error.
428 _notmuch_thread_create (void *ctx,
429 notmuch_database_t *notmuch,
430 unsigned int seed_doc_id,
431 notmuch_doc_id_set_t *match_set,
432 notmuch_string_list_t *exclude_terms,
433 notmuch_exclude_t omit_excluded,
436 void *local = talloc_new (ctx);
437 notmuch_thread_t *thread = NULL;
438 notmuch_message_t *seed_message;
439 const char *thread_id;
440 char *thread_id_query_string;
441 notmuch_query_t *thread_id_query;
443 notmuch_messages_t *messages;
444 notmuch_message_t *message;
446 seed_message = _notmuch_message_create (local, notmuch, seed_doc_id, NULL);
448 INTERNAL_ERROR ("Thread seed message %u does not exist", seed_doc_id);
450 thread_id = notmuch_message_get_thread_id (seed_message);
451 thread_id_query_string = talloc_asprintf (local, "thread:%s", thread_id);
452 if (unlikely (thread_id_query_string == NULL))
455 thread_id_query = talloc_steal (
456 local, notmuch_query_create (notmuch, thread_id_query_string));
457 if (unlikely (thread_id_query == NULL))
460 thread = talloc (local, notmuch_thread_t);
461 if (unlikely (thread == NULL))
464 talloc_set_destructor (thread, _notmuch_thread_destructor);
466 thread->notmuch = notmuch;
467 thread->thread_id = talloc_strdup (thread, thread_id);
468 thread->subject = NULL;
469 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
471 thread->authors_array = g_ptr_array_new ();
472 thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
475 thread->matched_authors_array = g_ptr_array_new ();
476 thread->authors = NULL;
477 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
480 thread->message_list = _notmuch_message_list_create (thread);
481 thread->toplevel_list = _notmuch_message_list_create (thread);
482 if (unlikely (thread->message_list == NULL ||
483 thread->toplevel_list == NULL)) {
488 thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
491 thread->total_messages = 0;
492 thread->matched_messages = 0;
496 /* We use oldest-first order unconditionally here to obtain the
497 * proper author ordering for the thread. The 'sort' parameter
498 * passed to this function is used only to indicate whether the
499 * oldest or newest subject is desired. */
500 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
502 for (messages = notmuch_query_search_messages (thread_id_query);
503 notmuch_messages_valid (messages);
504 notmuch_messages_move_to_next (messages))
508 message = notmuch_messages_get (messages);
509 doc_id = _notmuch_message_get_doc_id (message);
510 if (doc_id == seed_doc_id)
511 message = seed_message;
513 _thread_add_message (thread, message, exclude_terms, omit_excluded);
515 if ( _notmuch_doc_id_set_contains (match_set, doc_id)) {
516 _notmuch_doc_id_set_remove (match_set, doc_id);
517 _thread_add_matched_message (thread, message, sort);
520 _notmuch_message_close (message);
523 _resolve_thread_authors_string (thread);
525 _resolve_thread_relationships (thread);
527 /* Commit to returning thread. */
528 (void) talloc_steal (ctx, thread);
536 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
538 return _notmuch_messages_create (thread->toplevel_list);
542 notmuch_thread_get_messages (notmuch_thread_t *thread)
544 return _notmuch_messages_create (thread->message_list);
548 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
550 return thread->thread_id;
554 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
556 return thread->total_messages;
560 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
562 return thread->matched_messages;
566 notmuch_thread_get_authors (notmuch_thread_t *thread)
568 return thread->authors;
572 notmuch_thread_get_subject (notmuch_thread_t *thread)
574 return thread->subject;
578 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
580 return thread->oldest;
584 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
586 return thread->newest;
590 notmuch_thread_get_tags (notmuch_thread_t *thread)
592 notmuch_string_list_t *tags;
595 tags = _notmuch_string_list_create (thread);
596 if (unlikely (tags == NULL))
599 keys = g_hash_table_get_keys (thread->tags);
601 for (l = keys; l; l = l->next)
602 _notmuch_string_list_append (tags, (char *) l->data);
606 _notmuch_string_list_sort (tags);
608 return _notmuch_tags_create (thread, tags);
612 notmuch_thread_destroy (notmuch_thread_t *thread)
614 talloc_free (thread);