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;
35 char *nonmatched_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->tags);
51 g_hash_table_unref (thread->message_hash);
57 _thread_add_author (notmuch_thread_t *thread,
63 if (g_hash_table_lookup_extended (thread->authors_hash,
67 g_hash_table_insert (thread->authors_hash, xstrdup (author), NULL);
70 thread->authors = talloc_asprintf (thread, "%s, %s",
74 thread->authors = talloc_strdup (thread, author);
78 * move authors of matched messages in the thread to
79 * the front of the authors list, but keep them in
80 * existing order within their group
83 _thread_move_matched_author (notmuch_thread_t *thread,
88 char *last_pipe,*next_pipe;
89 int idx,nm_start,author_len,authors_len;
91 if (thread->authors == NULL || author == NULL)
93 if (thread->nonmatched_authors == NULL)
94 thread->nonmatched_authors = thread->authors;
95 author_len = strlen(author);
96 authors_len = strlen(thread->authors);
97 if (author_len == authors_len) {
99 thread->nonmatched_authors += author_len;
102 current_author = strcasestr(thread->authors, author);
103 if (current_author == NULL)
105 /* how far inside the nonmatched authors is our author? */
106 idx = current_author - thread->nonmatched_authors;
108 /* already among matched authors */
111 /* are there any authors in the list after our author? */
112 if (thread->nonmatched_authors + author_len < thread->authors + authors_len) {
113 /* we have to make changes, so let's get a temp copy */
114 authors_copy = talloc_strdup(thread,thread->authors);
115 /* nm_start is the offset into where the non-matched authors start */
116 nm_start = thread->nonmatched_authors - thread->authors;
117 /* copy this author and add the "| " - the if clause above tells us there's more */
118 strncpy(thread->nonmatched_authors,author,author_len);
119 strncpy(thread->nonmatched_authors+author_len,"| ",2);
120 thread->nonmatched_authors += author_len+2;
122 /* we are actually moving authors around, not just changing the separator
123 * first copy the authors that came BEFORE our author */
124 strncpy(thread->nonmatched_authors, authors_copy+nm_start, idx-2);
125 /* finally, if there are authors AFTER our author, copy those */
126 if(author_len+nm_start+idx < authors_len) {
127 strncpy(thread->nonmatched_authors + idx - 2,", ",2);
128 strncpy(thread->nonmatched_authors + idx, authors_copy+nm_start + idx + author_len + 2,
129 authors_len - (nm_start + idx + author_len + 2));
132 /* finally let's make sure there's just one '|' in the authors string */
133 last_pipe = strchr(thread->authors,'|');
135 next_pipe = strchr(last_pipe+1,'|');
138 last_pipe = next_pipe;
141 thread->nonmatched_authors += author_len;
142 /* so now all authors are matched - let's remove the '|' */
143 last_pipe = strchr(thread->authors,'|');
150 /* Add 'message' as a message that belongs to 'thread'.
152 * The 'thread' will talloc_steal the 'message' and hold onto a
156 _thread_add_message (notmuch_thread_t *thread,
157 notmuch_message_t *message)
159 notmuch_tags_t *tags;
161 InternetAddressList *list = NULL;
162 InternetAddress *address;
163 const char *from, *author;
165 _notmuch_message_list_add_message (thread->message_list,
166 talloc_steal (thread, message));
167 thread->total_messages++;
169 g_hash_table_insert (thread->message_hash,
170 xstrdup (notmuch_message_get_message_id (message)),
173 from = notmuch_message_get_header (message, "from");
175 list = internet_address_list_parse_string (from);
178 address = internet_address_list_get_address (list, 0);
180 author = internet_address_get_name (address);
181 if (author == NULL) {
182 InternetAddressMailbox *mailbox;
183 mailbox = INTERNET_ADDRESS_MAILBOX (address);
184 author = internet_address_mailbox_get_addr (mailbox);
186 _thread_add_author (thread, author);
187 notmuch_message_set_author (message, author);
189 g_object_unref (G_OBJECT (list));
192 if (! thread->subject) {
194 subject = notmuch_message_get_header (message, "subject");
195 thread->subject = talloc_strdup (thread, subject ? subject : "");
198 for (tags = notmuch_message_get_tags (message);
199 notmuch_tags_valid (tags);
200 notmuch_tags_move_to_next (tags))
202 tag = notmuch_tags_get (tags);
203 g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
208 _thread_set_subject_from_message (notmuch_thread_t *thread,
209 notmuch_message_t *message)
212 const char *cleaned_subject;
214 subject = notmuch_message_get_header (message, "subject");
218 if ((strncasecmp (subject, "Re: ", 4) == 0) ||
219 (strncasecmp (subject, "Aw: ", 4) == 0) ||
220 (strncasecmp (subject, "Vs: ", 4) == 0) ||
221 (strncasecmp (subject, "Sv: ", 4) == 0)) {
223 cleaned_subject = talloc_strndup (thread,
225 strlen(subject) - 4);
227 cleaned_subject = talloc_strdup (thread, subject);
231 talloc_free (thread->subject);
233 thread->subject = talloc_strdup (thread, cleaned_subject);
237 _thread_add_matched_message (notmuch_thread_t *thread,
238 notmuch_message_t *message,
242 notmuch_message_t *hashed_message;
244 date = notmuch_message_get_date (message);
246 if (date < thread->oldest || ! thread->matched_messages)
247 thread->oldest = date;
249 if (date > thread->newest || ! thread->matched_messages)
250 thread->newest = date;
252 thread->matched_messages++;
254 if (g_hash_table_lookup_extended (thread->message_hash,
255 notmuch_message_get_message_id (message), NULL,
256 (void **) &hashed_message)) {
257 notmuch_message_set_flag (hashed_message,
258 NOTMUCH_MESSAGE_FLAG_MATCH, 1);
260 _thread_move_matched_author (thread,notmuch_message_get_author(hashed_message));
262 if ((sort == NOTMUCH_SORT_OLDEST_FIRST && date <= thread->newest) ||
263 (sort != NOTMUCH_SORT_OLDEST_FIRST && date == thread->newest))
265 _thread_set_subject_from_message (thread, message);
270 _resolve_thread_relationships (unused (notmuch_thread_t *thread))
272 notmuch_message_node_t **prev, *node;
273 notmuch_message_t *message, *parent;
274 const char *in_reply_to;
276 prev = &thread->message_list->head;
277 while ((node = *prev)) {
278 message = node->message;
279 in_reply_to = _notmuch_message_get_in_reply_to (message);
280 if (in_reply_to && strlen (in_reply_to) &&
281 g_hash_table_lookup_extended (thread->message_hash,
286 if (thread->message_list->tail == &node->next)
287 thread->message_list->tail = prev;
289 _notmuch_message_add_reply (parent, node);
291 prev = &((*prev)->next);
295 /* XXX: After scanning through the entire list looking for parents
296 * via "In-Reply-To", we should do a second pass that looks at the
297 * list of messages IDs in the "References" header instead. (And
298 * for this the parent would be the "deepest" message of all the
299 * messages found in the "References" list.)
301 * Doing this will allow messages and sub-threads to be positioned
302 * correctly in the thread even when an intermediate message is
303 * missing from the thread.
307 /* Create a new notmuch_thread_t object for the given thread ID,
308 * treating any messages matching 'query_string' as "matched".
310 * Creating the thread will trigger two database searches. The first
311 * is for all messages belonging to the thread, (to get the first
312 * subject line, the total count of messages, and all authors). The
313 * second search is for all messages that are in the thread and that
314 * also match the given query_string. This is to allow for a separate
315 * count of matched messages, and to allow a viewer to display these
316 * messages differently.
318 * Here, 'ctx' is talloc context for the resulting thread object.
320 * This function returns NULL in the case of any error.
323 _notmuch_thread_create (void *ctx,
324 notmuch_database_t *notmuch,
325 const char *thread_id,
326 const char *query_string,
329 notmuch_thread_t *thread;
330 const char *thread_id_query_string;
331 notmuch_query_t *thread_id_query;
333 notmuch_messages_t *messages;
334 notmuch_message_t *message;
335 notmuch_bool_t matched_is_subset_of_thread;
337 thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
338 if (unlikely (query_string == NULL))
341 /* Under normal circumstances we need to do two database
342 * queries. One is for the thread itself (thread_id_query_string)
343 * and the second is to determine which messages in that thread
344 * match the original query (matched_query_string).
346 * But under two circumstances, we use only the
347 * thread_id_query_string:
349 * 1. If the original query_string *is* just the thread
352 * 2. If the original query_string matches all messages ("" or
355 * In either of these cases, we can be more efficient by running
356 * just the thread_id query (since we know all messages in the
357 * thread will match the query_string).
359 * Beyond the performance advantage, in the second case, it's
360 * important to not try to create a concatenated query because our
361 * parser handles "" and "*" as special cases and will not do the
362 * right thing with a query string of "* and thread:<foo>".
364 matched_is_subset_of_thread = 1;
365 if (strcmp (query_string, thread_id_query_string) == 0 ||
366 strcmp (query_string, "") == 0 ||
367 strcmp (query_string, "*") == 0)
369 matched_is_subset_of_thread = 0;
372 thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
373 if (unlikely (thread_id_query == NULL))
376 thread = talloc (ctx, notmuch_thread_t);
377 if (unlikely (thread == NULL))
380 talloc_set_destructor (thread, _notmuch_thread_destructor);
382 thread->notmuch = notmuch;
383 thread->thread_id = talloc_strdup (thread, thread_id);
384 thread->subject = NULL;
385 thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
387 thread->authors = NULL;
388 thread->nonmatched_authors = NULL;
389 thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
392 thread->message_list = _notmuch_message_list_create (thread);
393 if (unlikely (thread->message_list == NULL))
396 thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
399 thread->total_messages = 0;
400 thread->matched_messages = 0;
404 notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
406 for (messages = notmuch_query_search_messages (thread_id_query);
407 notmuch_messages_valid (messages);
408 notmuch_messages_move_to_next (messages))
410 message = notmuch_messages_get (messages);
412 _thread_add_message (thread, message);
414 if (! matched_is_subset_of_thread)
415 _thread_add_matched_message (thread, message, sort);
417 _notmuch_message_close (message);
420 notmuch_query_destroy (thread_id_query);
422 if (matched_is_subset_of_thread)
424 const char *matched_query_string;
425 notmuch_query_t *matched_query;
427 matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
428 thread_id_query_string,
430 if (unlikely (matched_query_string == NULL))
433 matched_query = notmuch_query_create (notmuch, matched_query_string);
434 if (unlikely (matched_query == NULL))
437 for (messages = notmuch_query_search_messages (matched_query);
438 notmuch_messages_valid (messages);
439 notmuch_messages_move_to_next (messages))
441 message = notmuch_messages_get (messages);
442 _thread_add_matched_message (thread, message, sort);
443 _notmuch_message_close (message);
446 notmuch_query_destroy (matched_query);
449 _resolve_thread_relationships (thread);
455 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
457 return _notmuch_messages_create (thread->message_list);
461 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
463 return thread->thread_id;
467 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
469 return thread->total_messages;
473 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
475 return thread->matched_messages;
479 notmuch_thread_get_authors (notmuch_thread_t *thread)
481 return thread->authors;
485 notmuch_thread_get_subject (notmuch_thread_t *thread)
487 return thread->subject;
491 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
493 return thread->oldest;
497 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
499 return thread->newest;
503 notmuch_thread_get_tags (notmuch_thread_t *thread)
505 notmuch_tags_t *tags;
508 tags = _notmuch_tags_create (thread);
509 if (unlikely (tags == NULL))
512 keys = g_hash_table_get_keys (thread->tags);
514 for (l = keys; l; l = l->next)
515 _notmuch_tags_add_tag (tags, (char *) l->data);
519 _notmuch_tags_prepare_iterator (tags);
525 notmuch_thread_destroy (notmuch_thread_t *thread)
527 talloc_free (thread);