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"
26 #include <gmime/gmime.h>
27 #include <glib.h> /* GHashTable */
29 struct _notmuch_thread {
30 notmuch_database_t *notmuch;
33 GHashTable *authors_hash;
34 GPtrArray *authors_array;
35 GHashTable *matched_authors_hash;
36 int has_non_matched_authors;
40 notmuch_message_list_t *message_list;
41 GHashTable *message_hash;
49 _notmuch_thread_destructor (notmuch_thread_t *thread)
51 g_hash_table_unref (thread->authors_hash);
52 g_hash_table_unref (thread->matched_authors_hash);
53 g_hash_table_unref (thread->tags);
54 g_hash_table_unref (thread->message_hash);
56 if (thread->authors_array) {
57 g_ptr_array_free (thread->authors_array, TRUE);
58 thread->authors_array = NULL;
64 /* Add each author of the thread to the thread's authors_hash and the
65 * thread's authors_array. */
67 _thread_add_author (notmuch_thread_t *thread,
75 if (g_hash_table_lookup_extended (thread->authors_hash,
79 author_copy = talloc_strdup (thread, author);
81 g_hash_table_insert (thread->authors_hash, author_copy, NULL);
83 g_ptr_array_add (thread->authors_array, author_copy);
86 /* Add each matched author of the thread to the thread's
87 * matched_authors_hash and to the thread's authors string. */
89 _thread_add_matched_author (notmuch_thread_t *thread,
97 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
101 author_copy = talloc_strdup (thread, author);
103 g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
106 thread->authors = talloc_asprintf (thread, "%s, %s",
110 thread->authors = author_copy;
113 /* Copy any authors from the authors array that were not also matched
114 * authors onto the end of the thread's authors string.
115 * We use a '|' to separate matched and unmatched authors. */
117 _complete_thread_authors (notmuch_thread_t *thread)
122 for (i = 0; i < thread->authors_array->len; i++) {
123 author = (char *) g_ptr_array_index (thread->authors_array, i);
124 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
127 if (thread->has_non_matched_authors) {
128 thread->authors = talloc_asprintf (thread, "%s, %s",
132 thread->authors = talloc_asprintf (thread, "%s| %s",
135 thread->has_non_matched_authors = 1;
139 g_ptr_array_free (thread->authors_array, TRUE);
140 thread->authors_array = NULL;
143 /* clean up the uggly "Lastname, Firstname" format that some mail systems
144 * (most notably, Exchange) are creating to be "Firstname Lastname"
145 * To make sure that we don't change other potential situations where a
146 * comma is in the name, we check that we match one of these patterns
147 * "Last, First" <first.last@company.com>
148 * "Last, First MI" <first.mi.last@company.com>
151 _thread_cleanup_author (notmuch_thread_t *thread,
152 const char *author, const char *from)
154 char *clean_author,*test_author;
159 clean_author = talloc_strdup(thread, author);
160 if (clean_author == NULL)
162 comma = strchr(author,',');
164 /* let's assemble what we think is the correct name */
165 lname = comma - author;
166 fname = strlen(author) - lname - 2;
167 strncpy(clean_author, comma + 2, fname);
168 *(clean_author+fname) = ' ';
169 strncpy(clean_author + fname + 1, author, lname);
170 *(clean_author+fname+1+lname) = '\0';
171 /* make a temporary copy and see if it matches the email */
172 test_author = talloc_strdup(thread,clean_author);
174 blank=strchr(test_author,' ');
175 while (blank != NULL) {
177 blank=strchr(test_author,' ');
179 if (strcasestr(from, test_author) == NULL)
180 /* we didn't identify this as part of the email address
181 * so let's punt and return the original author */
182 strcpy (clean_author, author);
188 /* Add 'message' as a message that belongs to 'thread'.
190 * The 'thread' will talloc_steal the 'message' and hold onto a
194 _thread_add_message (notmuch_thread_t *thread,
195 notmuch_message_t *message)
197 notmuch_tags_t *tags;
199 InternetAddressList *list = NULL;
200 InternetAddress *address;
201 const char *from, *author;
204 _notmuch_message_list_add_message (thread->message_list,
205 talloc_steal (thread, message));
206 thread->total_messages++;
208 g_hash_table_insert (thread->message_hash,
209 xstrdup (notmuch_message_get_message_id (message)),
212 from = notmuch_message_get_header (message, "from");
214 list = internet_address_list_parse_string (from);
217 address = internet_address_list_get_address (list, 0);
219 author = internet_address_get_name (address);
220 if (author == NULL) {
221 InternetAddressMailbox *mailbox;
222 mailbox = INTERNET_ADDRESS_MAILBOX (address);
223 author = internet_address_mailbox_get_addr (mailbox);
225 clean_author = _thread_cleanup_author (thread, author, from);
226 _thread_add_author (thread, clean_author);
227 notmuch_message_set_author (message, clean_author);
229 g_object_unref (G_OBJECT (list));
232 if (! thread->subject) {
234 subject = notmuch_message_get_header (message, "subject");
235 thread->subject = talloc_strdup (thread, subject ? subject : "");
238 for (tags = notmuch_message_get_tags (message);
239 notmuch_tags_valid (tags);
240 notmuch_tags_move_to_next (tags))
242 tag = notmuch_tags_get (tags);
243 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
248 _thread_set_subject_from_message (notmuch_thread_t *thread,
249 notmuch_message_t *message)
252 const char *cleaned_subject;
254 subject = notmuch_message_get_header (message, "subject");
258 if ((strncasecmp (subject, "Re: ", 4) == 0) ||
259 (strncasecmp (subject, "Aw: ", 4) == 0) ||
260 (strncasecmp (subject, "Vs: ", 4) == 0) ||
261 (strncasecmp (subject, "Sv: ", 4) == 0)) {
263 cleaned_subject = talloc_strndup (thread,
265 strlen(subject) - 4);
267 cleaned_subject = talloc_strdup (thread, subject);
271 talloc_free (thread->subject);
273 thread->subject = talloc_strdup (thread, cleaned_subject);
277 _thread_add_matched_message (notmuch_thread_t *thread,
278 notmuch_message_t *message,
282 notmuch_message_t *hashed_message;
284 date = notmuch_message_get_date (message);
286 if (date < thread->oldest || ! thread->matched_messages)
287 thread->oldest = date;
289 if (date > thread->newest || ! thread->matched_messages)
290 thread->newest = date;
292 thread->matched_messages++;
294 if (g_hash_table_lookup_extended (thread->message_hash,
295 notmuch_message_get_message_id (message), NULL,
296 (void **) &hashed_message)) {
297 notmuch_message_set_flag (hashed_message,
298 NOTMUCH_MESSAGE_FLAG_MATCH, 1);
301 _thread_add_matched_author (thread, notmuch_message_get_author (hashed_message));
303 if ((sort == NOTMUCH_SORT_OLDEST_FIRST && date <= thread->newest) ||
304 (sort != NOTMUCH_SORT_OLDEST_FIRST && date == thread->newest))
306 _thread_set_subject_from_message (thread, message);
311 _resolve_thread_relationships (unused (notmuch_thread_t *thread))
313 notmuch_message_node_t **prev, *node;
314 notmuch_message_t *message, *parent;
315 const char *in_reply_to;
317 prev = &thread->message_list->head;
318 while ((node = *prev)) {
319 message = node->message;
320 in_reply_to = _notmuch_message_get_in_reply_to (message);
321 if (in_reply_to && strlen (in_reply_to) &&
322 g_hash_table_lookup_extended (thread->message_hash,
327 if (thread->message_list->tail == &node->next)
328 thread->message_list->tail = prev;
330 _notmuch_message_add_reply (parent, node);
332 prev = &((*prev)->next);
336 /* XXX: After scanning through the entire list looking for parents
337 * via "In-Reply-To", we should do a second pass that looks at the
338 * list of messages IDs in the "References" header instead. (And
339 * for this the parent would be the "deepest" message of all the
340 * messages found in the "References" list.)
342 * Doing this will allow messages and sub-threads to be positioned
343 * correctly in the thread even when an intermediate message is
344 * missing from the thread.
348 /* Create a new notmuch_thread_t object for the given thread ID,
349 * treating any messages matching 'query_string' as "matched".
351 * Creating the thread will trigger two database searches. The first
352 * is for all messages belonging to the thread, (to get the first
353 * subject line, the total count of messages, and all authors). The
354 * second search is for all messages that are in the thread and that
355 * also match the given query_string. This is to allow for a separate
356 * count of matched messages, and to allow a viewer to display these
357 * messages differently.
359 * Here, 'ctx' is talloc context for the resulting thread object.
361 * This function returns NULL in the case of any error.
364 _notmuch_thread_create (void *ctx,
365 notmuch_database_t *notmuch,
366 const char *thread_id,
367 const char *query_string,
370 notmuch_thread_t *thread;
371 const char *thread_id_query_string;
372 notmuch_query_t *thread_id_query;
374 notmuch_messages_t *messages;
375 notmuch_message_t *message;
376 notmuch_bool_t matched_is_subset_of_thread;
378 thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
379 if (unlikely (query_string == NULL))
382 /* Under normal circumstances we need to do two database
383 * queries. One is for the thread itself (thread_id_query_string)
384 * and the second is to determine which messages in that thread
385 * match the original query (matched_query_string).
387 * But under two circumstances, we use only the
388 * thread_id_query_string:
390 * 1. If the original query_string *is* just the thread
393 * 2. If the original query_string matches all messages ("" or
396 * In either of these cases, we can be more efficient by running
397 * just the thread_id query (since we know all messages in the
398 * thread will match the query_string).
400 * Beyond the performance advantage, in the second case, it's
401 * important to not try to create a concatenated query because our
402 * parser handles "" and "*" as special cases and will not do the
403 * right thing with a query string of "* and thread:<foo>".
405 matched_is_subset_of_thread = 1;
406 if (strcmp (query_string, thread_id_query_string) == 0 ||
407 strcmp (query_string, "") == 0 ||
408 strcmp (query_string, "*") == 0)
410 matched_is_subset_of_thread = 0;
413 thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
414 if (unlikely (thread_id_query == NULL))
417 thread = talloc (ctx, notmuch_thread_t);
418 if (unlikely (thread == NULL))
421 talloc_set_destructor (thread, _notmuch_thread_destructor);
423 thread->notmuch = notmuch;
424 thread->thread_id = talloc_strdup (thread, thread_id);
425 thread->subject = NULL;
426 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
428 thread->authors_array = g_ptr_array_new ();
429 thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
432 thread->authors = NULL;
433 thread->has_non_matched_authors = 0;
434 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
437 thread->message_list = _notmuch_message_list_create (thread);
438 if (unlikely (thread->message_list == NULL))
441 thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
444 thread->total_messages = 0;
445 thread->matched_messages = 0;
449 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
451 for (messages = notmuch_query_search_messages (thread_id_query);
452 notmuch_messages_valid (messages);
453 notmuch_messages_move_to_next (messages))
455 message = notmuch_messages_get (messages);
457 _thread_add_message (thread, message);
459 if (! matched_is_subset_of_thread)
460 _thread_add_matched_message (thread, message, sort);
462 _notmuch_message_close (message);
465 notmuch_query_destroy (thread_id_query);
467 if (matched_is_subset_of_thread)
469 const char *matched_query_string;
470 notmuch_query_t *matched_query;
472 matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
473 thread_id_query_string,
475 if (unlikely (matched_query_string == NULL))
478 matched_query = notmuch_query_create (notmuch, matched_query_string);
479 if (unlikely (matched_query == NULL))
482 for (messages = notmuch_query_search_messages (matched_query);
483 notmuch_messages_valid (messages);
484 notmuch_messages_move_to_next (messages))
486 message = notmuch_messages_get (messages);
487 _thread_add_matched_message (thread, message, sort);
488 _notmuch_message_close (message);
491 notmuch_query_destroy (matched_query);
494 _complete_thread_authors (thread);
496 _resolve_thread_relationships (thread);
502 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
504 return _notmuch_messages_create (thread->message_list);
508 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
510 return thread->thread_id;
514 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
516 return thread->total_messages;
520 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
522 return thread->matched_messages;
526 notmuch_thread_get_authors (notmuch_thread_t *thread)
528 return thread->authors;
532 notmuch_thread_get_subject (notmuch_thread_t *thread)
534 return thread->subject;
538 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
540 return thread->oldest;
544 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
546 return thread->newest;
550 notmuch_thread_get_tags (notmuch_thread_t *thread)
552 notmuch_tags_t *tags;
555 tags = _notmuch_tags_create (thread);
556 if (unlikely (tags == NULL))
559 keys = g_hash_table_get_keys (thread->tags);
561 for (l = keys; l; l = l->next)
562 _notmuch_tags_add_tag (tags, (char *) l->data);
566 _notmuch_tags_prepare_iterator (tags);
572 notmuch_thread_destroy (notmuch_thread_t *thread)
574 talloc_free (thread);