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 _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 notmuch_message_list_t *message_list;
39 GHashTable *message_hash;
47 _notmuch_thread_destructor (notmuch_thread_t *thread)
49 g_hash_table_unref (thread->authors_hash);
50 g_hash_table_unref (thread->matched_authors_hash);
51 g_hash_table_unref (thread->tags);
52 g_hash_table_unref (thread->message_hash);
54 if (thread->authors_array) {
55 g_ptr_array_free (thread->authors_array, TRUE);
56 thread->authors_array = NULL;
59 if (thread->matched_authors_array) {
60 g_ptr_array_free (thread->matched_authors_array, TRUE);
61 thread->matched_authors_array = NULL;
67 /* Add each author of the thread to the thread's authors_hash and to
68 * the thread's authors_array. */
70 _thread_add_author (notmuch_thread_t *thread,
78 if (g_hash_table_lookup_extended (thread->authors_hash,
82 author_copy = talloc_strdup (thread, author);
84 g_hash_table_insert (thread->authors_hash, author_copy, NULL);
86 g_ptr_array_add (thread->authors_array, author_copy);
89 /* Add each matched author of the thread to the thread's
90 * matched_authors_hash and to the thread's matched_authors_array. */
92 _thread_add_matched_author (notmuch_thread_t *thread,
100 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
104 author_copy = talloc_strdup (thread, author);
106 g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
108 g_ptr_array_add (thread->matched_authors_array, author_copy);
111 /* Construct an authors string from matched_authors_array and
112 * authors_array. The string contains matched authors first, then
113 * non-matched authors (with the two groups separated by '|'). Within
114 * each group, authors are listed in date order. */
116 _resolve_thread_authors_string (notmuch_thread_t *thread)
120 int first_non_matched_author = 1;
122 /* First, list all matched authors in date order. */
123 for (i = 0; i < thread->matched_authors_array->len; i++) {
124 author = (char *) g_ptr_array_index (thread->matched_authors_array, i);
126 thread->authors = talloc_asprintf (thread, "%s, %s",
130 thread->authors = author;
133 /* Next, append any non-matched authors that haven't already appeared. */
134 for (i = 0; i < thread->authors_array->len; i++) {
135 author = (char *) g_ptr_array_index (thread->authors_array, i);
136 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
139 if (first_non_matched_author) {
140 thread->authors = talloc_asprintf (thread, "%s| %s",
144 thread->authors = talloc_asprintf (thread, "%s, %s",
149 first_non_matched_author = 0;
152 g_ptr_array_free (thread->authors_array, TRUE);
153 thread->authors_array = NULL;
154 g_ptr_array_free (thread->matched_authors_array, TRUE);
155 thread->matched_authors_array = NULL;
158 /* clean up the ugly "Lastname, Firstname" format that some mail systems
159 * (most notably, Exchange) are creating to be "Firstname Lastname"
160 * To make sure that we don't change other potential situations where a
161 * comma is in the name, we check that we match one of these patterns
162 * "Last, First" <first.last@company.com>
163 * "Last, First MI" <first.mi.last@company.com>
166 _thread_cleanup_author (notmuch_thread_t *thread,
167 const char *author, const char *from)
169 char *clean_author,*test_author;
176 clean_author = talloc_strdup(thread, author);
177 if (clean_author == NULL)
179 /* check if there's a comma in the name and that there's a
180 * component of the name behind it (so the name doesn't end with
181 * the comma - in which case the string that strchr finds is just
182 * one character long ",\0").
183 * Otherwise just return the copy of the original author name that
185 comma = strchr(author,',');
186 if (comma && strlen(comma) > 1) {
187 /* let's assemble what we think is the correct name */
188 lname = comma - author;
189 fname = strlen(author) - lname - 2;
190 strncpy(clean_author, comma + 2, fname);
191 *(clean_author+fname) = ' ';
192 strncpy(clean_author + fname + 1, author, lname);
193 *(clean_author+fname+1+lname) = '\0';
194 /* make a temporary copy and see if it matches the email */
195 test_author = talloc_strdup(thread,clean_author);
197 blank=strchr(test_author,' ');
198 while (blank != NULL) {
200 blank=strchr(test_author,' ');
202 if (strcasestr(from, test_author) == NULL)
203 /* we didn't identify this as part of the email address
204 * so let's punt and return the original author */
205 strcpy (clean_author, author);
210 /* Add 'message' as a message that belongs to 'thread'.
212 * The 'thread' will talloc_steal the 'message' and hold onto a
216 _thread_add_message (notmuch_thread_t *thread,
217 notmuch_message_t *message)
219 notmuch_tags_t *tags;
221 InternetAddressList *list = NULL;
222 InternetAddress *address;
223 const char *from, *author;
226 _notmuch_message_list_add_message (thread->message_list,
227 talloc_steal (thread, message));
228 thread->total_messages++;
230 g_hash_table_insert (thread->message_hash,
231 xstrdup (notmuch_message_get_message_id (message)),
234 from = notmuch_message_get_header (message, "from");
236 list = internet_address_list_parse_string (from);
239 address = internet_address_list_get_address (list, 0);
241 author = internet_address_get_name (address);
242 if (author == NULL) {
243 InternetAddressMailbox *mailbox;
244 mailbox = INTERNET_ADDRESS_MAILBOX (address);
245 author = internet_address_mailbox_get_addr (mailbox);
247 clean_author = _thread_cleanup_author (thread, author, from);
248 _thread_add_author (thread, clean_author);
249 notmuch_message_set_author (message, clean_author);
251 g_object_unref (G_OBJECT (list));
254 if (! thread->subject) {
256 subject = notmuch_message_get_header (message, "subject");
257 thread->subject = talloc_strdup (thread, subject ? subject : "");
260 for (tags = notmuch_message_get_tags (message);
261 notmuch_tags_valid (tags);
262 notmuch_tags_move_to_next (tags))
264 tag = notmuch_tags_get (tags);
265 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
270 _thread_set_subject_from_message (notmuch_thread_t *thread,
271 notmuch_message_t *message)
274 const char *cleaned_subject;
276 subject = notmuch_message_get_header (message, "subject");
280 if ((strncasecmp (subject, "Re: ", 4) == 0) ||
281 (strncasecmp (subject, "Aw: ", 4) == 0) ||
282 (strncasecmp (subject, "Vs: ", 4) == 0) ||
283 (strncasecmp (subject, "Sv: ", 4) == 0)) {
285 cleaned_subject = talloc_strndup (thread,
287 strlen(subject) - 4);
289 cleaned_subject = talloc_strdup (thread, subject);
293 talloc_free (thread->subject);
295 thread->subject = talloc_strdup (thread, cleaned_subject);
298 /* Add a message to this thread which is known to match the original
299 * search specification. The 'sort' parameter controls whether the
300 * oldest or newest matching subject is applied to the thread as a
303 _thread_add_matched_message (notmuch_thread_t *thread,
304 notmuch_message_t *message,
308 notmuch_message_t *hashed_message;
310 date = notmuch_message_get_date (message);
312 if (date < thread->oldest || ! thread->matched_messages) {
313 thread->oldest = date;
314 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
315 _thread_set_subject_from_message (thread, message);
318 if (date > thread->newest || ! thread->matched_messages) {
319 thread->newest = date;
320 if (sort != NOTMUCH_SORT_OLDEST_FIRST)
321 _thread_set_subject_from_message (thread, message);
324 thread->matched_messages++;
326 if (g_hash_table_lookup_extended (thread->message_hash,
327 notmuch_message_get_message_id (message), NULL,
328 (void **) &hashed_message)) {
329 notmuch_message_set_flag (hashed_message,
330 NOTMUCH_MESSAGE_FLAG_MATCH, 1);
333 _thread_add_matched_author (thread, notmuch_message_get_author (hashed_message));
337 _resolve_thread_relationships (unused (notmuch_thread_t *thread))
339 notmuch_message_node_t **prev, *node;
340 notmuch_message_t *message, *parent;
341 const char *in_reply_to;
343 prev = &thread->message_list->head;
344 while ((node = *prev)) {
345 message = node->message;
346 in_reply_to = _notmuch_message_get_in_reply_to (message);
347 if (in_reply_to && strlen (in_reply_to) &&
348 g_hash_table_lookup_extended (thread->message_hash,
353 if (thread->message_list->tail == &node->next)
354 thread->message_list->tail = prev;
356 _notmuch_message_add_reply (parent, node);
358 prev = &((*prev)->next);
362 /* XXX: After scanning through the entire list looking for parents
363 * via "In-Reply-To", we should do a second pass that looks at the
364 * list of messages IDs in the "References" header instead. (And
365 * for this the parent would be the "deepest" message of all the
366 * messages found in the "References" list.)
368 * Doing this will allow messages and sub-threads to be positioned
369 * correctly in the thread even when an intermediate message is
370 * missing from the thread.
374 /* Create a new notmuch_thread_t object for the given thread ID,
375 * treating any messages matching 'query_string' as "matched".
377 * Creating the thread will trigger two database searches. The first
378 * is for all messages belonging to the thread, (to get the first
379 * subject line, the total count of messages, and all authors). The
380 * second search is for all messages that are in the thread and that
381 * also match the given query_string. This is to allow for a separate
382 * count of matched messages, and to allow a viewer to display these
383 * messages differently.
385 * Here, 'ctx' is talloc context for the resulting thread object.
387 * This function returns NULL in the case of any error.
390 _notmuch_thread_create (void *ctx,
391 notmuch_database_t *notmuch,
392 const char *thread_id,
393 const char *query_string,
396 notmuch_thread_t *thread;
397 const char *thread_id_query_string;
398 notmuch_query_t *thread_id_query;
400 notmuch_messages_t *messages;
401 notmuch_message_t *message;
402 notmuch_bool_t matched_is_subset_of_thread;
404 thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
405 if (unlikely (query_string == NULL))
408 /* Under normal circumstances we need to do two database
409 * queries. One is for the thread itself (thread_id_query_string)
410 * and the second is to determine which messages in that thread
411 * match the original query (matched_query_string).
413 * But under two circumstances, we use only the
414 * thread_id_query_string:
416 * 1. If the original query_string *is* just the thread
419 * 2. If the original query_string matches all messages ("" or
422 * In either of these cases, we can be more efficient by running
423 * just the thread_id query (since we know all messages in the
424 * thread will match the query_string).
426 * Beyond the performance advantage, in the second case, it's
427 * important to not try to create a concatenated query because our
428 * parser handles "" and "*" as special cases and will not do the
429 * right thing with a query string of "* and thread:<foo>".
431 matched_is_subset_of_thread = 1;
432 if (strcmp (query_string, thread_id_query_string) == 0 ||
433 strcmp (query_string, "") == 0 ||
434 strcmp (query_string, "*") == 0)
436 matched_is_subset_of_thread = 0;
439 thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
440 if (unlikely (thread_id_query == NULL))
443 thread = talloc (ctx, notmuch_thread_t);
444 if (unlikely (thread == NULL))
447 talloc_set_destructor (thread, _notmuch_thread_destructor);
449 thread->notmuch = notmuch;
450 thread->thread_id = talloc_strdup (thread, thread_id);
451 thread->subject = NULL;
452 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
454 thread->authors_array = g_ptr_array_new ();
455 thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
458 thread->matched_authors_array = g_ptr_array_new ();
459 thread->authors = NULL;
460 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
463 thread->message_list = _notmuch_message_list_create (thread);
464 if (unlikely (thread->message_list == NULL))
467 thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
470 thread->total_messages = 0;
471 thread->matched_messages = 0;
475 /* We use oldest-first order unconditionally here to obtain the
476 * proper author ordering for the thread. The 'sort' parameter
477 * passed to this function is used only to indicate whether the
478 * oldest or newest subject is desired. */
479 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
481 for (messages = notmuch_query_search_messages (thread_id_query);
482 notmuch_messages_valid (messages);
483 notmuch_messages_move_to_next (messages))
485 message = notmuch_messages_get (messages);
487 _thread_add_message (thread, message);
489 if (! matched_is_subset_of_thread)
490 _thread_add_matched_message (thread, message, sort);
492 _notmuch_message_close (message);
495 notmuch_query_destroy (thread_id_query);
497 if (matched_is_subset_of_thread)
499 const char *matched_query_string;
500 notmuch_query_t *matched_query;
502 matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
503 thread_id_query_string,
505 if (unlikely (matched_query_string == NULL))
508 matched_query = notmuch_query_create (notmuch, matched_query_string);
509 if (unlikely (matched_query == NULL))
512 /* As above, we use oldest-first order unconditionally here. */
513 notmuch_query_set_sort (matched_query, NOTMUCH_SORT_OLDEST_FIRST);
515 for (messages = notmuch_query_search_messages (matched_query);
516 notmuch_messages_valid (messages);
517 notmuch_messages_move_to_next (messages))
519 message = notmuch_messages_get (messages);
520 _thread_add_matched_message (thread, message, sort);
521 _notmuch_message_close (message);
524 notmuch_query_destroy (matched_query);
527 _resolve_thread_authors_string (thread);
529 _resolve_thread_relationships (thread);
535 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
537 return _notmuch_messages_create (thread->message_list);
541 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
543 return thread->thread_id;
547 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
549 return thread->total_messages;
553 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
555 return thread->matched_messages;
559 notmuch_thread_get_authors (notmuch_thread_t *thread)
561 return thread->authors;
565 notmuch_thread_get_subject (notmuch_thread_t *thread)
567 return thread->subject;
571 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
573 return thread->oldest;
577 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
579 return thread->newest;
583 notmuch_thread_get_tags (notmuch_thread_t *thread)
585 notmuch_tags_t *tags;
588 tags = _notmuch_tags_create (thread);
589 if (unlikely (tags == NULL))
592 keys = g_hash_table_get_keys (thread->tags);
594 for (l = keys; l; l = l->next)
595 _notmuch_tags_add_tag (tags, (char *) l->data);
599 _notmuch_tags_prepare_iterator (tags);
605 notmuch_thread_destroy (notmuch_thread_t *thread)
607 talloc_free (thread);