]> git.notmuchmail.org Git - notmuch/blob - lib/thread.cc
c29140397feb78e81cbb284e8ae52df75e3df22f
[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 <xapian.h>
25
26 #include <gmime/gmime.h>
27 #include <glib.h> /* GHashTable */
28
29 struct _notmuch_thread {
30     notmuch_database_t *notmuch;
31     char *thread_id;
32     char *subject;
33     GHashTable *authors_hash;
34     GPtrArray *authors_array;
35     GHashTable *matched_authors_hash;
36     int has_non_matched_authors;
37     char *authors;
38     GHashTable *tags;
39
40     notmuch_message_list_t *message_list;
41     GHashTable *message_hash;
42     int total_messages;
43     int matched_messages;
44     time_t oldest;
45     time_t newest;
46 };
47
48 static int
49 _notmuch_thread_destructor (notmuch_thread_t *thread)
50 {
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);
55
56     if (thread->authors_array) {
57         g_ptr_array_free (thread->authors_array, TRUE);
58         thread->authors_array = NULL;
59     }
60
61     return 0;
62 }
63
64 /* Add each author of the thread to the thread's authors_hash and the
65  * thread's authors_array. */
66 static void
67 _thread_add_author (notmuch_thread_t *thread,
68                     const char *author)
69 {
70     char *author_copy;
71
72     if (author == NULL)
73         return;
74
75     if (g_hash_table_lookup_extended (thread->authors_hash,
76                                       author, NULL, NULL))
77         return;
78
79     author_copy = talloc_strdup (thread, author);
80
81     g_hash_table_insert (thread->authors_hash, author_copy, NULL);
82
83     g_ptr_array_add (thread->authors_array, author_copy);
84 }
85
86 /* Add each matched author of the thread to the thread's
87  * matched_authors_hash and to the thread's authors string. */
88 static void
89 _thread_add_matched_author (notmuch_thread_t *thread,
90                             const char *author)
91 {
92     char *author_copy;
93
94     if (author == NULL)
95         return;
96
97     if (g_hash_table_lookup_extended (thread->matched_authors_hash,
98                                       author, NULL, NULL))
99         return;
100
101     author_copy = talloc_strdup (thread, author);
102
103     g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
104
105     if (thread->authors)
106         thread->authors = talloc_asprintf (thread, "%s, %s",
107                                            thread->authors,
108                                            author);
109     else
110         thread->authors = author_copy;
111 }
112
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. */
116 static void
117 _complete_thread_authors (notmuch_thread_t *thread)
118 {
119     unsigned int i;
120     char *author;
121
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,
125                                           author, NULL, NULL))
126             continue;
127         if (thread->has_non_matched_authors) {
128             thread->authors = talloc_asprintf (thread, "%s, %s",
129                                                thread->authors,
130                                                author);
131         } else {
132             thread->authors = talloc_asprintf (thread, "%s| %s",
133                                                thread->authors,
134                                                author);
135             thread->has_non_matched_authors = 1;
136         }
137     }
138
139     g_ptr_array_free (thread->authors_array, TRUE);
140     thread->authors_array = NULL;
141 }
142
143 /* clean up the ugly "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>
149  */
150 static char *
151 _thread_cleanup_author (notmuch_thread_t *thread,
152                         const char *author, const char *from)
153 {
154     char *clean_author,*test_author;
155     const char *comma;
156     char *blank;
157     int fname,lname;
158
159     if (author == NULL)
160         return NULL;
161     clean_author = talloc_strdup(thread, author);
162     if (clean_author == NULL)
163         return NULL;
164     /* check if there's a comma in the name and that there's a
165      * component of the name behind it (so the name doesn't end with
166      * the comma - in which case the string that strchr finds is just
167      * one character long ",\0").
168      * Otherwise just return the copy of the original author name that
169      * we just made*/
170     comma = strchr(author,',');
171     if (comma && strlen(comma) > 1) {
172         /* let's assemble what we think is the correct name */
173         lname = comma - author;
174         fname = strlen(author) - lname - 2;
175         strncpy(clean_author, comma + 2, fname);
176         *(clean_author+fname) = ' ';
177         strncpy(clean_author + fname + 1, author, lname);
178         *(clean_author+fname+1+lname) = '\0';
179         /* make a temporary copy and see if it matches the email */
180         test_author = talloc_strdup(thread,clean_author);
181
182         blank=strchr(test_author,' ');
183         while (blank != NULL) {
184             *blank = '.';
185             blank=strchr(test_author,' ');
186         }
187         if (strcasestr(from, test_author) == NULL)
188             /* we didn't identify this as part of the email address
189             * so let's punt and return the original author */
190             strcpy (clean_author, author);
191     }
192     return clean_author;
193 }
194
195 /* Add 'message' as a message that belongs to 'thread'.
196  *
197  * The 'thread' will talloc_steal the 'message' and hold onto a
198  * reference to it.
199  */
200 static void
201 _thread_add_message (notmuch_thread_t *thread,
202                      notmuch_message_t *message)
203 {
204     notmuch_tags_t *tags;
205     const char *tag;
206     InternetAddressList *list = NULL;
207     InternetAddress *address;
208     const char *from, *author;
209     char *clean_author;
210
211     _notmuch_message_list_add_message (thread->message_list,
212                                        talloc_steal (thread, message));
213     thread->total_messages++;
214
215     g_hash_table_insert (thread->message_hash,
216                          xstrdup (notmuch_message_get_message_id (message)),
217                          message);
218
219     from = notmuch_message_get_header (message, "from");
220     if (from)
221         list = internet_address_list_parse_string (from);
222
223     if (list) {
224         address = internet_address_list_get_address (list, 0);
225         if (address) {
226             author = internet_address_get_name (address);
227             if (author == NULL) {
228                 InternetAddressMailbox *mailbox;
229                 mailbox = INTERNET_ADDRESS_MAILBOX (address);
230                 author = internet_address_mailbox_get_addr (mailbox);
231             }
232             clean_author = _thread_cleanup_author (thread, author, from);
233             _thread_add_author (thread, clean_author);
234             notmuch_message_set_author (message, clean_author);
235         }
236         g_object_unref (G_OBJECT (list));
237     }
238
239     if (! thread->subject) {
240         const char *subject;
241         subject = notmuch_message_get_header (message, "subject");
242         thread->subject = talloc_strdup (thread, subject ? subject : "");
243     }
244
245     for (tags = notmuch_message_get_tags (message);
246          notmuch_tags_valid (tags);
247          notmuch_tags_move_to_next (tags))
248     {
249         tag = notmuch_tags_get (tags);
250         g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
251     }
252 }
253
254 static void
255 _thread_set_subject_from_message (notmuch_thread_t *thread,
256                                   notmuch_message_t *message)
257 {
258     const char *subject;
259     const char *cleaned_subject;
260
261     subject = notmuch_message_get_header (message, "subject");
262     if (! subject)
263         return;
264
265     if ((strncasecmp (subject, "Re: ", 4) == 0) ||
266         (strncasecmp (subject, "Aw: ", 4) == 0) ||
267         (strncasecmp (subject, "Vs: ", 4) == 0) ||
268         (strncasecmp (subject, "Sv: ", 4) == 0)) {
269
270         cleaned_subject = talloc_strndup (thread,
271                                           subject + 4,
272                                           strlen(subject) - 4);
273     } else {
274         cleaned_subject = talloc_strdup (thread, subject);
275     }
276
277     if (thread->subject)
278         talloc_free (thread->subject);
279
280     thread->subject = talloc_strdup (thread, cleaned_subject);
281 }
282
283 static void
284 _thread_add_matched_message (notmuch_thread_t *thread,
285                              notmuch_message_t *message,
286                              notmuch_sort_t sort)
287 {
288     time_t date;
289     notmuch_message_t *hashed_message;
290
291     date = notmuch_message_get_date (message);
292
293     if (date < thread->oldest || ! thread->matched_messages)
294         thread->oldest = date;
295
296     if (date > thread->newest || ! thread->matched_messages)
297         thread->newest = date;
298
299     thread->matched_messages++;
300
301     if (g_hash_table_lookup_extended (thread->message_hash,
302                             notmuch_message_get_message_id (message), NULL,
303                             (void **) &hashed_message)) {
304         notmuch_message_set_flag (hashed_message,
305                                   NOTMUCH_MESSAGE_FLAG_MATCH, 1);
306     }
307
308     _thread_add_matched_author (thread, notmuch_message_get_author (hashed_message));
309
310     if ((sort == NOTMUCH_SORT_OLDEST_FIRST && date <= thread->newest) ||
311         (sort != NOTMUCH_SORT_OLDEST_FIRST && date == thread->newest))
312     {
313         _thread_set_subject_from_message (thread, message);
314     }
315 }
316
317 static void
318 _resolve_thread_relationships (unused (notmuch_thread_t *thread))
319 {
320     notmuch_message_node_t **prev, *node;
321     notmuch_message_t *message, *parent;
322     const char *in_reply_to;
323
324     prev = &thread->message_list->head;
325     while ((node = *prev)) {
326         message = node->message;
327         in_reply_to = _notmuch_message_get_in_reply_to (message);
328         if (in_reply_to && strlen (in_reply_to) &&
329             g_hash_table_lookup_extended (thread->message_hash,
330                                           in_reply_to, NULL,
331                                           (void **) &parent))
332         {
333             *prev = node->next;
334             if (thread->message_list->tail == &node->next)
335                 thread->message_list->tail = prev;
336             node->next = NULL;
337             _notmuch_message_add_reply (parent, node);
338         } else {
339             prev = &((*prev)->next);
340         }
341     }
342
343     /* XXX: After scanning through the entire list looking for parents
344      * via "In-Reply-To", we should do a second pass that looks at the
345      * list of messages IDs in the "References" header instead. (And
346      * for this the parent would be the "deepest" message of all the
347      * messages found in the "References" list.)
348      *
349      * Doing this will allow messages and sub-threads to be positioned
350      * correctly in the thread even when an intermediate message is
351      * missing from the thread.
352      */
353 }
354
355 /* Create a new notmuch_thread_t object for the given thread ID,
356  * treating any messages matching 'query_string' as "matched".
357  *
358  * Creating the thread will trigger two database searches. The first
359  * is for all messages belonging to the thread, (to get the first
360  * subject line, the total count of messages, and all authors). The
361  * second search is for all messages that are in the thread and that
362  * also match the given query_string. This is to allow for a separate
363  * count of matched messages, and to allow a viewer to display these
364  * messages differently.
365  *
366  * Here, 'ctx' is talloc context for the resulting thread object.
367  *
368  * This function returns NULL in the case of any error.
369  */
370 notmuch_thread_t *
371 _notmuch_thread_create (void *ctx,
372                         notmuch_database_t *notmuch,
373                         const char *thread_id,
374                         const char *query_string,
375                         notmuch_sort_t sort)
376 {
377     notmuch_thread_t *thread;
378     const char *thread_id_query_string;
379     notmuch_query_t *thread_id_query;
380
381     notmuch_messages_t *messages;
382     notmuch_message_t *message;
383     notmuch_bool_t matched_is_subset_of_thread;
384
385     thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
386     if (unlikely (query_string == NULL))
387         return NULL;
388
389     /* Under normal circumstances we need to do two database
390      * queries. One is for the thread itself (thread_id_query_string)
391      * and the second is to determine which messages in that thread
392      * match the original query (matched_query_string).
393      *
394      * But under two circumstances, we use only the
395      * thread_id_query_string:
396      *
397      *  1. If the original query_string *is* just the thread
398      *     specification.
399      *
400      *  2. If the original query_string matches all messages ("" or
401      *     "*").
402      *
403      * In either of these cases, we can be more efficient by running
404      * just the thread_id query (since we know all messages in the
405      * thread will match the query_string).
406      *
407      * Beyond the performance advantage, in the second case, it's
408      * important to not try to create a concatenated query because our
409      * parser handles "" and "*" as special cases and will not do the
410      * right thing with a query string of "* and thread:<foo>".
411      **/
412     matched_is_subset_of_thread = 1;
413     if (strcmp (query_string, thread_id_query_string) == 0 ||
414         strcmp (query_string, "") == 0 ||
415         strcmp (query_string, "*") == 0)
416     {
417         matched_is_subset_of_thread = 0;
418     }
419
420     thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
421     if (unlikely (thread_id_query == NULL))
422         return NULL;
423
424     thread = talloc (ctx, notmuch_thread_t);
425     if (unlikely (thread == NULL))
426         return NULL;
427
428     talloc_set_destructor (thread, _notmuch_thread_destructor);
429
430     thread->notmuch = notmuch;
431     thread->thread_id = talloc_strdup (thread, thread_id);
432     thread->subject = NULL;
433     thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
434                                                   NULL, NULL);
435     thread->authors_array = g_ptr_array_new ();
436     thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
437                                                           g_str_equal,
438                                                           NULL, NULL);
439     thread->authors = NULL;
440     thread->has_non_matched_authors = 0;
441     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
442                                           free, NULL);
443
444     thread->message_list = _notmuch_message_list_create (thread);
445     if (unlikely (thread->message_list == NULL))
446         return NULL;
447
448     thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
449                                                   free, NULL);
450
451     thread->total_messages = 0;
452     thread->matched_messages = 0;
453     thread->oldest = 0;
454     thread->newest = 0;
455
456     notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
457
458     for (messages = notmuch_query_search_messages (thread_id_query);
459          notmuch_messages_valid (messages);
460          notmuch_messages_move_to_next (messages))
461     {
462         message = notmuch_messages_get (messages);
463
464         _thread_add_message (thread, message);
465
466         if (! matched_is_subset_of_thread)
467             _thread_add_matched_message (thread, message, sort);
468
469         _notmuch_message_close (message);
470     }
471
472     notmuch_query_destroy (thread_id_query);
473
474     if (matched_is_subset_of_thread)
475     {
476         const char *matched_query_string;
477         notmuch_query_t *matched_query;
478
479         matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
480                                                 thread_id_query_string,
481                                                 query_string);
482         if (unlikely (matched_query_string == NULL))
483             return NULL;
484
485         matched_query = notmuch_query_create (notmuch, matched_query_string);
486         if (unlikely (matched_query == NULL))
487             return NULL;
488
489         for (messages = notmuch_query_search_messages (matched_query);
490              notmuch_messages_valid (messages);
491              notmuch_messages_move_to_next (messages))
492         {
493             message = notmuch_messages_get (messages);
494             _thread_add_matched_message (thread, message, sort);
495             _notmuch_message_close (message);
496         }
497
498         notmuch_query_destroy (matched_query);
499     }
500
501     _complete_thread_authors (thread);
502
503     _resolve_thread_relationships (thread);
504
505     return thread;
506 }
507
508 notmuch_messages_t *
509 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
510 {
511     return _notmuch_messages_create (thread->message_list);
512 }
513
514 const char *
515 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
516 {
517     return thread->thread_id;
518 }
519
520 int
521 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
522 {
523     return thread->total_messages;
524 }
525
526 int
527 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
528 {
529     return thread->matched_messages;
530 }
531
532 const char *
533 notmuch_thread_get_authors (notmuch_thread_t *thread)
534 {
535     return thread->authors;
536 }
537
538 const char *
539 notmuch_thread_get_subject (notmuch_thread_t *thread)
540 {
541     return thread->subject;
542 }
543
544 time_t
545 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
546 {
547     return thread->oldest;
548 }
549
550 time_t
551 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
552 {
553     return thread->newest;
554 }
555
556 notmuch_tags_t *
557 notmuch_thread_get_tags (notmuch_thread_t *thread)
558 {
559     notmuch_tags_t *tags;
560     GList *keys, *l;
561
562     tags = _notmuch_tags_create (thread);
563     if (unlikely (tags == NULL))
564         return NULL;
565
566     keys = g_hash_table_get_keys (thread->tags);
567
568     for (l = keys; l; l = l->next)
569         _notmuch_tags_add_tag (tags, (char *) l->data);
570
571     g_list_free (keys);
572
573     _notmuch_tags_prepare_iterator (tags);
574
575     return tags;
576 }
577
578 void
579 notmuch_thread_destroy (notmuch_thread_t *thread)
580 {
581     talloc_free (thread);
582 }