]> git.notmuchmail.org Git - notmuch/blob - lib/thread.cc
7f15586286d9441e1199c6f63c1867944b4b645b
[notmuch] / lib / thread.cc
1 /* thread.cc - Results of thread-based searches from a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22 #include "database-private.h"
23
24 #include <gmime/gmime.h>
25 #include <glib.h> /* GHashTable */
26
27 struct _notmuch_thread {
28     notmuch_database_t *notmuch;
29     char *thread_id;
30     char *subject;
31     GHashTable *authors_hash;
32     GPtrArray *authors_array;
33     GHashTable *matched_authors_hash;
34     int has_non_matched_authors;
35     char *authors;
36     GHashTable *tags;
37
38     notmuch_message_list_t *message_list;
39     GHashTable *message_hash;
40     int total_messages;
41     int matched_messages;
42     time_t oldest;
43     time_t newest;
44 };
45
46 static int
47 _notmuch_thread_destructor (notmuch_thread_t *thread)
48 {
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);
53
54     if (thread->authors_array) {
55         g_ptr_array_free (thread->authors_array, TRUE);
56         thread->authors_array = NULL;
57     }
58
59     return 0;
60 }
61
62 /* Add each author of the thread to the thread's authors_hash and the
63  * thread's authors_array. */
64 static void
65 _thread_add_author (notmuch_thread_t *thread,
66                     const char *author)
67 {
68     char *author_copy;
69
70     if (author == NULL)
71         return;
72
73     if (g_hash_table_lookup_extended (thread->authors_hash,
74                                       author, NULL, NULL))
75         return;
76
77     author_copy = talloc_strdup (thread, author);
78
79     g_hash_table_insert (thread->authors_hash, author_copy, NULL);
80
81     g_ptr_array_add (thread->authors_array, author_copy);
82 }
83
84 /* Add each matched author of the thread to the thread's
85  * matched_authors_hash and to the thread's authors string. */
86 static void
87 _thread_add_matched_author (notmuch_thread_t *thread,
88                             const char *author)
89 {
90     char *author_copy;
91
92     if (author == NULL)
93         return;
94
95     if (g_hash_table_lookup_extended (thread->matched_authors_hash,
96                                       author, NULL, NULL))
97         return;
98
99     author_copy = talloc_strdup (thread, author);
100
101     g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
102
103     if (thread->authors)
104         thread->authors = talloc_asprintf (thread, "%s, %s",
105                                            thread->authors,
106                                            author);
107     else
108         thread->authors = author_copy;
109 }
110
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. */
114 static void
115 _complete_thread_authors (notmuch_thread_t *thread)
116 {
117     unsigned int i;
118     char *author;
119
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,
123                                           author, NULL, NULL))
124             continue;
125         if (thread->has_non_matched_authors) {
126             thread->authors = talloc_asprintf (thread, "%s, %s",
127                                                thread->authors,
128                                                author);
129         } else {
130             thread->authors = talloc_asprintf (thread, "%s| %s",
131                                                thread->authors,
132                                                author);
133             thread->has_non_matched_authors = 1;
134         }
135     }
136
137     g_ptr_array_free (thread->authors_array, TRUE);
138     thread->authors_array = NULL;
139 }
140
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>
147  */
148 static char *
149 _thread_cleanup_author (notmuch_thread_t *thread,
150                         const char *author, const char *from)
151 {
152     char *clean_author,*test_author;
153     const char *comma;
154     char *blank;
155     int fname,lname;
156
157     if (author == NULL)
158         return NULL;
159     clean_author = talloc_strdup(thread, author);
160     if (clean_author == NULL)
161         return 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
167      * we just made*/
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);
179
180         blank=strchr(test_author,' ');
181         while (blank != NULL) {
182             *blank = '.';
183             blank=strchr(test_author,' ');
184         }
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);
189     }
190     return clean_author;
191 }
192
193 /* Add 'message' as a message that belongs to 'thread'.
194  *
195  * The 'thread' will talloc_steal the 'message' and hold onto a
196  * reference to it.
197  */
198 static void
199 _thread_add_message (notmuch_thread_t *thread,
200                      notmuch_message_t *message)
201 {
202     notmuch_tags_t *tags;
203     const char *tag;
204     InternetAddressList *list = NULL;
205     InternetAddress *address;
206     const char *from, *author;
207     char *clean_author;
208
209     _notmuch_message_list_add_message (thread->message_list,
210                                        talloc_steal (thread, message));
211     thread->total_messages++;
212
213     g_hash_table_insert (thread->message_hash,
214                          xstrdup (notmuch_message_get_message_id (message)),
215                          message);
216
217     from = notmuch_message_get_header (message, "from");
218     if (from)
219         list = internet_address_list_parse_string (from);
220
221     if (list) {
222         address = internet_address_list_get_address (list, 0);
223         if (address) {
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);
229             }
230             clean_author = _thread_cleanup_author (thread, author, from);
231             _thread_add_author (thread, clean_author);
232             notmuch_message_set_author (message, clean_author);
233         }
234         g_object_unref (G_OBJECT (list));
235     }
236
237     if (! thread->subject) {
238         const char *subject;
239         subject = notmuch_message_get_header (message, "subject");
240         thread->subject = talloc_strdup (thread, subject ? subject : "");
241     }
242
243     for (tags = notmuch_message_get_tags (message);
244          notmuch_tags_valid (tags);
245          notmuch_tags_move_to_next (tags))
246     {
247         tag = notmuch_tags_get (tags);
248         g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
249     }
250 }
251
252 static void
253 _thread_set_subject_from_message (notmuch_thread_t *thread,
254                                   notmuch_message_t *message)
255 {
256     const char *subject;
257     const char *cleaned_subject;
258
259     subject = notmuch_message_get_header (message, "subject");
260     if (! subject)
261         return;
262
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)) {
267
268         cleaned_subject = talloc_strndup (thread,
269                                           subject + 4,
270                                           strlen(subject) - 4);
271     } else {
272         cleaned_subject = talloc_strdup (thread, subject);
273     }
274
275     if (thread->subject)
276         talloc_free (thread->subject);
277
278     thread->subject = talloc_strdup (thread, cleaned_subject);
279 }
280
281 static void
282 _thread_add_matched_message (notmuch_thread_t *thread,
283                              notmuch_message_t *message,
284                              notmuch_sort_t sort)
285 {
286     time_t date;
287     notmuch_message_t *hashed_message;
288
289     date = notmuch_message_get_date (message);
290
291     if (date < thread->oldest || ! thread->matched_messages)
292         thread->oldest = date;
293
294     if (date > thread->newest || ! thread->matched_messages)
295         thread->newest = date;
296
297     thread->matched_messages++;
298
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);
304     }
305
306     _thread_add_matched_author (thread, notmuch_message_get_author (hashed_message));
307
308     if ((sort == NOTMUCH_SORT_OLDEST_FIRST && date <= thread->newest) ||
309         (sort != NOTMUCH_SORT_OLDEST_FIRST && date == thread->newest))
310     {
311         _thread_set_subject_from_message (thread, message);
312     }
313 }
314
315 static void
316 _resolve_thread_relationships (unused (notmuch_thread_t *thread))
317 {
318     notmuch_message_node_t **prev, *node;
319     notmuch_message_t *message, *parent;
320     const char *in_reply_to;
321
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,
328                                           in_reply_to, NULL,
329                                           (void **) &parent))
330         {
331             *prev = node->next;
332             if (thread->message_list->tail == &node->next)
333                 thread->message_list->tail = prev;
334             node->next = NULL;
335             _notmuch_message_add_reply (parent, node);
336         } else {
337             prev = &((*prev)->next);
338         }
339     }
340
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.)
346      *
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.
350      */
351 }
352
353 /* Create a new notmuch_thread_t object for the given thread ID,
354  * treating any messages matching 'query_string' as "matched".
355  *
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.
363  *
364  * Here, 'ctx' is talloc context for the resulting thread object.
365  *
366  * This function returns NULL in the case of any error.
367  */
368 notmuch_thread_t *
369 _notmuch_thread_create (void *ctx,
370                         notmuch_database_t *notmuch,
371                         const char *thread_id,
372                         const char *query_string,
373                         notmuch_sort_t sort)
374 {
375     notmuch_thread_t *thread;
376     const char *thread_id_query_string;
377     notmuch_query_t *thread_id_query;
378
379     notmuch_messages_t *messages;
380     notmuch_message_t *message;
381     notmuch_bool_t matched_is_subset_of_thread;
382
383     thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
384     if (unlikely (query_string == NULL))
385         return NULL;
386
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).
391      *
392      * But under two circumstances, we use only the
393      * thread_id_query_string:
394      *
395      *  1. If the original query_string *is* just the thread
396      *     specification.
397      *
398      *  2. If the original query_string matches all messages ("" or
399      *     "*").
400      *
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).
404      *
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>".
409      **/
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)
414     {
415         matched_is_subset_of_thread = 0;
416     }
417
418     thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
419     if (unlikely (thread_id_query == NULL))
420         return NULL;
421
422     thread = talloc (ctx, notmuch_thread_t);
423     if (unlikely (thread == NULL))
424         return NULL;
425
426     talloc_set_destructor (thread, _notmuch_thread_destructor);
427
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,
432                                                   NULL, NULL);
433     thread->authors_array = g_ptr_array_new ();
434     thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
435                                                           g_str_equal,
436                                                           NULL, NULL);
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,
440                                           free, NULL);
441
442     thread->message_list = _notmuch_message_list_create (thread);
443     if (unlikely (thread->message_list == NULL))
444         return NULL;
445
446     thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
447                                                   free, NULL);
448
449     thread->total_messages = 0;
450     thread->matched_messages = 0;
451     thread->oldest = 0;
452     thread->newest = 0;
453
454     notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
455
456     for (messages = notmuch_query_search_messages (thread_id_query);
457          notmuch_messages_valid (messages);
458          notmuch_messages_move_to_next (messages))
459     {
460         message = notmuch_messages_get (messages);
461
462         _thread_add_message (thread, message);
463
464         if (! matched_is_subset_of_thread)
465             _thread_add_matched_message (thread, message, sort);
466
467         _notmuch_message_close (message);
468     }
469
470     notmuch_query_destroy (thread_id_query);
471
472     if (matched_is_subset_of_thread)
473     {
474         const char *matched_query_string;
475         notmuch_query_t *matched_query;
476
477         matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
478                                                 thread_id_query_string,
479                                                 query_string);
480         if (unlikely (matched_query_string == NULL))
481             return NULL;
482
483         matched_query = notmuch_query_create (notmuch, matched_query_string);
484         if (unlikely (matched_query == NULL))
485             return NULL;
486
487         for (messages = notmuch_query_search_messages (matched_query);
488              notmuch_messages_valid (messages);
489              notmuch_messages_move_to_next (messages))
490         {
491             message = notmuch_messages_get (messages);
492             _thread_add_matched_message (thread, message, sort);
493             _notmuch_message_close (message);
494         }
495
496         notmuch_query_destroy (matched_query);
497     }
498
499     _complete_thread_authors (thread);
500
501     _resolve_thread_relationships (thread);
502
503     return thread;
504 }
505
506 notmuch_messages_t *
507 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
508 {
509     return _notmuch_messages_create (thread->message_list);
510 }
511
512 const char *
513 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
514 {
515     return thread->thread_id;
516 }
517
518 int
519 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
520 {
521     return thread->total_messages;
522 }
523
524 int
525 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
526 {
527     return thread->matched_messages;
528 }
529
530 const char *
531 notmuch_thread_get_authors (notmuch_thread_t *thread)
532 {
533     return thread->authors;
534 }
535
536 const char *
537 notmuch_thread_get_subject (notmuch_thread_t *thread)
538 {
539     return thread->subject;
540 }
541
542 time_t
543 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
544 {
545     return thread->oldest;
546 }
547
548 time_t
549 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
550 {
551     return thread->newest;
552 }
553
554 notmuch_tags_t *
555 notmuch_thread_get_tags (notmuch_thread_t *thread)
556 {
557     notmuch_tags_t *tags;
558     GList *keys, *l;
559
560     tags = _notmuch_tags_create (thread);
561     if (unlikely (tags == NULL))
562         return NULL;
563
564     keys = g_hash_table_get_keys (thread->tags);
565
566     for (l = keys; l; l = l->next)
567         _notmuch_tags_add_tag (tags, (char *) l->data);
568
569     g_list_free (keys);
570
571     _notmuch_tags_prepare_iterator (tags);
572
573     return tags;
574 }
575
576 void
577 notmuch_thread_destroy (notmuch_thread_t *thread)
578 {
579     talloc_free (thread);
580 }