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 int has_non_matched_authors;
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;
62 /* Add each author of the thread to the thread's authors_hash and the
63 * thread's authors_array. */
65 _thread_add_author (notmuch_thread_t *thread,
73 if (g_hash_table_lookup_extended (thread->authors_hash,
77 author_copy = talloc_strdup (thread, author);
79 g_hash_table_insert (thread->authors_hash, author_copy, NULL);
81 g_ptr_array_add (thread->authors_array, author_copy);
84 /* Add each matched author of the thread to the thread's
85 * matched_authors_hash and to the thread's authors string. */
87 _thread_add_matched_author (notmuch_thread_t *thread,
95 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
99 author_copy = talloc_strdup (thread, author);
101 g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
104 thread->authors = talloc_asprintf (thread, "%s, %s",
108 thread->authors = author_copy;
111 /* Copy any authors from the authors array that were not also matched
112 * authors onto the end of the thread's authors string.
113 * We use a '|' to separate matched and unmatched authors. */
115 _complete_thread_authors (notmuch_thread_t *thread)
120 for (i = 0; i < thread->authors_array->len; i++) {
121 author = (char *) g_ptr_array_index (thread->authors_array, i);
122 if (g_hash_table_lookup_extended (thread->matched_authors_hash,
125 if (thread->has_non_matched_authors) {
126 thread->authors = talloc_asprintf (thread, "%s, %s",
130 thread->authors = talloc_asprintf (thread, "%s| %s",
133 thread->has_non_matched_authors = 1;
137 g_ptr_array_free (thread->authors_array, TRUE);
138 thread->authors_array = NULL;
141 /* clean up the ugly "Lastname, Firstname" format that some mail systems
142 * (most notably, Exchange) are creating to be "Firstname Lastname"
143 * To make sure that we don't change other potential situations where a
144 * comma is in the name, we check that we match one of these patterns
145 * "Last, First" <first.last@company.com>
146 * "Last, First MI" <first.mi.last@company.com>
149 _thread_cleanup_author (notmuch_thread_t *thread,
150 const char *author, const char *from)
152 char *clean_author,*test_author;
159 clean_author = talloc_strdup(thread, author);
160 if (clean_author == NULL)
162 /* check if there's a comma in the name and that there's a
163 * component of the name behind it (so the name doesn't end with
164 * the comma - in which case the string that strchr finds is just
165 * one character long ",\0").
166 * Otherwise just return the copy of the original author name that
168 comma = strchr(author,',');
169 if (comma && strlen(comma) > 1) {
170 /* let's assemble what we think is the correct name */
171 lname = comma - author;
172 fname = strlen(author) - lname - 2;
173 strncpy(clean_author, comma + 2, fname);
174 *(clean_author+fname) = ' ';
175 strncpy(clean_author + fname + 1, author, lname);
176 *(clean_author+fname+1+lname) = '\0';
177 /* make a temporary copy and see if it matches the email */
178 test_author = talloc_strdup(thread,clean_author);
180 blank=strchr(test_author,' ');
181 while (blank != NULL) {
183 blank=strchr(test_author,' ');
185 if (strcasestr(from, test_author) == NULL)
186 /* we didn't identify this as part of the email address
187 * so let's punt and return the original author */
188 strcpy (clean_author, author);
193 /* Add 'message' as a message that belongs to 'thread'.
195 * The 'thread' will talloc_steal the 'message' and hold onto a
199 _thread_add_message (notmuch_thread_t *thread,
200 notmuch_message_t *message)
202 notmuch_tags_t *tags;
204 InternetAddressList *list = NULL;
205 InternetAddress *address;
206 const char *from, *author;
209 _notmuch_message_list_add_message (thread->message_list,
210 talloc_steal (thread, message));
211 thread->total_messages++;
213 g_hash_table_insert (thread->message_hash,
214 xstrdup (notmuch_message_get_message_id (message)),
217 from = notmuch_message_get_header (message, "from");
219 list = internet_address_list_parse_string (from);
222 address = internet_address_list_get_address (list, 0);
224 author = internet_address_get_name (address);
225 if (author == NULL) {
226 InternetAddressMailbox *mailbox;
227 mailbox = INTERNET_ADDRESS_MAILBOX (address);
228 author = internet_address_mailbox_get_addr (mailbox);
230 clean_author = _thread_cleanup_author (thread, author, from);
231 _thread_add_author (thread, clean_author);
232 notmuch_message_set_author (message, clean_author);
234 g_object_unref (G_OBJECT (list));
237 if (! thread->subject) {
239 subject = notmuch_message_get_header (message, "subject");
240 thread->subject = talloc_strdup (thread, subject ? subject : "");
243 for (tags = notmuch_message_get_tags (message);
244 notmuch_tags_valid (tags);
245 notmuch_tags_move_to_next (tags))
247 tag = notmuch_tags_get (tags);
248 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
253 _thread_set_subject_from_message (notmuch_thread_t *thread,
254 notmuch_message_t *message)
257 const char *cleaned_subject;
259 subject = notmuch_message_get_header (message, "subject");
263 if ((strncasecmp (subject, "Re: ", 4) == 0) ||
264 (strncasecmp (subject, "Aw: ", 4) == 0) ||
265 (strncasecmp (subject, "Vs: ", 4) == 0) ||
266 (strncasecmp (subject, "Sv: ", 4) == 0)) {
268 cleaned_subject = talloc_strndup (thread,
270 strlen(subject) - 4);
272 cleaned_subject = talloc_strdup (thread, subject);
276 talloc_free (thread->subject);
278 thread->subject = talloc_strdup (thread, cleaned_subject);
282 _thread_add_matched_message (notmuch_thread_t *thread,
283 notmuch_message_t *message,
287 notmuch_message_t *hashed_message;
289 date = notmuch_message_get_date (message);
291 if (date < thread->oldest || ! thread->matched_messages)
292 thread->oldest = date;
294 if (date > thread->newest || ! thread->matched_messages)
295 thread->newest = date;
297 thread->matched_messages++;
299 if (g_hash_table_lookup_extended (thread->message_hash,
300 notmuch_message_get_message_id (message), NULL,
301 (void **) &hashed_message)) {
302 notmuch_message_set_flag (hashed_message,
303 NOTMUCH_MESSAGE_FLAG_MATCH, 1);
306 _thread_add_matched_author (thread, notmuch_message_get_author (hashed_message));
308 if ((sort == NOTMUCH_SORT_OLDEST_FIRST && date <= thread->newest) ||
309 (sort != NOTMUCH_SORT_OLDEST_FIRST && date == thread->newest))
311 _thread_set_subject_from_message (thread, message);
316 _resolve_thread_relationships (unused (notmuch_thread_t *thread))
318 notmuch_message_node_t **prev, *node;
319 notmuch_message_t *message, *parent;
320 const char *in_reply_to;
322 prev = &thread->message_list->head;
323 while ((node = *prev)) {
324 message = node->message;
325 in_reply_to = _notmuch_message_get_in_reply_to (message);
326 if (in_reply_to && strlen (in_reply_to) &&
327 g_hash_table_lookup_extended (thread->message_hash,
332 if (thread->message_list->tail == &node->next)
333 thread->message_list->tail = prev;
335 _notmuch_message_add_reply (parent, node);
337 prev = &((*prev)->next);
341 /* XXX: After scanning through the entire list looking for parents
342 * via "In-Reply-To", we should do a second pass that looks at the
343 * list of messages IDs in the "References" header instead. (And
344 * for this the parent would be the "deepest" message of all the
345 * messages found in the "References" list.)
347 * Doing this will allow messages and sub-threads to be positioned
348 * correctly in the thread even when an intermediate message is
349 * missing from the thread.
353 /* Create a new notmuch_thread_t object for the given thread ID,
354 * treating any messages matching 'query_string' as "matched".
356 * Creating the thread will trigger two database searches. The first
357 * is for all messages belonging to the thread, (to get the first
358 * subject line, the total count of messages, and all authors). The
359 * second search is for all messages that are in the thread and that
360 * also match the given query_string. This is to allow for a separate
361 * count of matched messages, and to allow a viewer to display these
362 * messages differently.
364 * Here, 'ctx' is talloc context for the resulting thread object.
366 * This function returns NULL in the case of any error.
369 _notmuch_thread_create (void *ctx,
370 notmuch_database_t *notmuch,
371 const char *thread_id,
372 const char *query_string,
375 notmuch_thread_t *thread;
376 const char *thread_id_query_string;
377 notmuch_query_t *thread_id_query;
379 notmuch_messages_t *messages;
380 notmuch_message_t *message;
381 notmuch_bool_t matched_is_subset_of_thread;
383 thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
384 if (unlikely (query_string == NULL))
387 /* Under normal circumstances we need to do two database
388 * queries. One is for the thread itself (thread_id_query_string)
389 * and the second is to determine which messages in that thread
390 * match the original query (matched_query_string).
392 * But under two circumstances, we use only the
393 * thread_id_query_string:
395 * 1. If the original query_string *is* just the thread
398 * 2. If the original query_string matches all messages ("" or
401 * In either of these cases, we can be more efficient by running
402 * just the thread_id query (since we know all messages in the
403 * thread will match the query_string).
405 * Beyond the performance advantage, in the second case, it's
406 * important to not try to create a concatenated query because our
407 * parser handles "" and "*" as special cases and will not do the
408 * right thing with a query string of "* and thread:<foo>".
410 matched_is_subset_of_thread = 1;
411 if (strcmp (query_string, thread_id_query_string) == 0 ||
412 strcmp (query_string, "") == 0 ||
413 strcmp (query_string, "*") == 0)
415 matched_is_subset_of_thread = 0;
418 thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
419 if (unlikely (thread_id_query == NULL))
422 thread = talloc (ctx, notmuch_thread_t);
423 if (unlikely (thread == NULL))
426 talloc_set_destructor (thread, _notmuch_thread_destructor);
428 thread->notmuch = notmuch;
429 thread->thread_id = talloc_strdup (thread, thread_id);
430 thread->subject = NULL;
431 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
433 thread->authors_array = g_ptr_array_new ();
434 thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
437 thread->authors = NULL;
438 thread->has_non_matched_authors = 0;
439 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
442 thread->message_list = _notmuch_message_list_create (thread);
443 if (unlikely (thread->message_list == NULL))
446 thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
449 thread->total_messages = 0;
450 thread->matched_messages = 0;
454 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
456 for (messages = notmuch_query_search_messages (thread_id_query);
457 notmuch_messages_valid (messages);
458 notmuch_messages_move_to_next (messages))
460 message = notmuch_messages_get (messages);
462 _thread_add_message (thread, message);
464 if (! matched_is_subset_of_thread)
465 _thread_add_matched_message (thread, message, sort);
467 _notmuch_message_close (message);
470 notmuch_query_destroy (thread_id_query);
472 if (matched_is_subset_of_thread)
474 const char *matched_query_string;
475 notmuch_query_t *matched_query;
477 matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
478 thread_id_query_string,
480 if (unlikely (matched_query_string == NULL))
483 matched_query = notmuch_query_create (notmuch, matched_query_string);
484 if (unlikely (matched_query == NULL))
487 for (messages = notmuch_query_search_messages (matched_query);
488 notmuch_messages_valid (messages);
489 notmuch_messages_move_to_next (messages))
491 message = notmuch_messages_get (messages);
492 _thread_add_matched_message (thread, message, sort);
493 _notmuch_message_close (message);
496 notmuch_query_destroy (matched_query);
499 _complete_thread_authors (thread);
501 _resolve_thread_relationships (thread);
507 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
509 return _notmuch_messages_create (thread->message_list);
513 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
515 return thread->thread_id;
519 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
521 return thread->total_messages;
525 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
527 return thread->matched_messages;
531 notmuch_thread_get_authors (notmuch_thread_t *thread)
533 return thread->authors;
537 notmuch_thread_get_subject (notmuch_thread_t *thread)
539 return thread->subject;
543 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
545 return thread->oldest;
549 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
551 return thread->newest;
555 notmuch_thread_get_tags (notmuch_thread_t *thread)
557 notmuch_tags_t *tags;
560 tags = _notmuch_tags_create (thread);
561 if (unlikely (tags == NULL))
564 keys = g_hash_table_get_keys (thread->tags);
566 for (l = keys; l; l = l->next)
567 _notmuch_tags_add_tag (tags, (char *) l->data);
571 _notmuch_tags_prepare_iterator (tags);
577 notmuch_thread_destroy (notmuch_thread_t *thread)
579 talloc_free (thread);