]> git.notmuchmail.org Git - notmuch/blob - lib/thread.cc
Import notmuch_0.27.orig.tar.gz
[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 https://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 #define EMPTY_STRING(s) ((s)[0] == '\0')
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     GPtrArray *matched_authors_array;
37     char *authors;
38     GHashTable *tags;
39
40     /* All messages, oldest first. */
41     notmuch_message_list_t *message_list;
42     /* Top-level messages, oldest first. */
43     notmuch_message_list_t *toplevel_list;
44
45     GHashTable *message_hash;
46     int total_messages;
47     int total_files;
48     int matched_messages;
49     time_t oldest;
50     time_t newest;
51 };
52
53 static int
54 _notmuch_thread_destructor (notmuch_thread_t *thread)
55 {
56     g_hash_table_unref (thread->authors_hash);
57     g_hash_table_unref (thread->matched_authors_hash);
58     g_hash_table_unref (thread->tags);
59     g_hash_table_unref (thread->message_hash);
60
61     if (thread->authors_array) {
62         g_ptr_array_free (thread->authors_array, true);
63         thread->authors_array = NULL;
64     }
65
66     if (thread->matched_authors_array) {
67         g_ptr_array_free (thread->matched_authors_array, true);
68         thread->matched_authors_array = NULL;
69     }
70
71     return 0;
72 }
73
74 /* Add each author of the thread to the thread's authors_hash and to
75  * the thread's authors_array. */
76 static void
77 _thread_add_author (notmuch_thread_t *thread,
78                     const char *author)
79 {
80     char *author_copy;
81
82     if (author == NULL)
83         return;
84
85     if (g_hash_table_lookup_extended (thread->authors_hash,
86                                       author, NULL, NULL))
87         return;
88
89     author_copy = talloc_strdup (thread, author);
90
91     g_hash_table_insert (thread->authors_hash, author_copy, NULL);
92
93     g_ptr_array_add (thread->authors_array, author_copy);
94 }
95
96 /* Add each matched author of the thread to the thread's
97  * matched_authors_hash and to the thread's matched_authors_array. */
98 static void
99 _thread_add_matched_author (notmuch_thread_t *thread,
100                             const char *author)
101 {
102     char *author_copy;
103
104     if (author == NULL)
105         return;
106
107     if (g_hash_table_lookup_extended (thread->matched_authors_hash,
108                                       author, NULL, NULL))
109         return;
110
111     author_copy = talloc_strdup (thread, author);
112
113     g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
114
115     g_ptr_array_add (thread->matched_authors_array, author_copy);
116 }
117
118 /* Construct an authors string from matched_authors_array and
119  * authors_array. The string contains matched authors first, then
120  * non-matched authors (with the two groups separated by '|'). Within
121  * each group, authors are listed in date order. */
122 static void
123 _resolve_thread_authors_string (notmuch_thread_t *thread)
124 {
125     unsigned int i;
126     char *author;
127     int first_non_matched_author = 1;
128
129     /* First, list all matched authors in date order. */
130     for (i = 0; i < thread->matched_authors_array->len; i++) {
131         author = (char *) g_ptr_array_index (thread->matched_authors_array, i);
132         if (thread->authors)
133             thread->authors = talloc_asprintf (thread, "%s, %s",
134                                                thread->authors,
135                                                author);
136         else
137             thread->authors = author;
138     }
139
140     /* Next, append any non-matched authors that haven't already appeared. */
141     for (i = 0; i < thread->authors_array->len; i++) {
142         author = (char *) g_ptr_array_index (thread->authors_array, i);
143         if (g_hash_table_lookup_extended (thread->matched_authors_hash,
144                                           author, NULL, NULL))
145             continue;
146         if (first_non_matched_author) {
147             thread->authors = talloc_asprintf (thread, "%s| %s",
148                                                thread->authors,
149                                                author);
150         } else {
151             thread->authors = talloc_asprintf (thread, "%s, %s",
152                                                thread->authors,
153                                                author);
154         }
155
156         first_non_matched_author = 0;
157     }
158
159     g_ptr_array_free (thread->authors_array, true);
160     thread->authors_array = NULL;
161     g_ptr_array_free (thread->matched_authors_array, true);
162     thread->matched_authors_array = NULL;
163
164     if (!thread->authors)
165         thread->authors = talloc_strdup(thread, "");
166 }
167
168 /* clean up the ugly "Lastname, Firstname" format that some mail systems
169  * (most notably, Exchange) are creating to be "Firstname Lastname"
170  * To make sure that we don't change other potential situations where a
171  * comma is in the name, we check that we match one of these patterns
172  * "Last, First" <first.last@company.com>
173  * "Last, First MI" <first.mi.last@company.com>
174  */
175 static char *
176 _thread_cleanup_author (notmuch_thread_t *thread,
177                         const char *author, const char *from)
178 {
179     char *clean_author,*test_author;
180     const char *comma;
181     char *blank;
182     int fname,lname;
183
184     if (author == NULL)
185         return NULL;
186     clean_author = talloc_strdup(thread, author);
187     if (clean_author == NULL)
188         return NULL;
189     /* check if there's a comma in the name and that there's a
190      * component of the name behind it (so the name doesn't end with
191      * the comma - in which case the string that strchr finds is just
192      * one character long ",\0").
193      * Otherwise just return the copy of the original author name that
194      * we just made*/
195     comma = strchr(author,',');
196     if (comma && strlen(comma) > 1) {
197         /* let's assemble what we think is the correct name */
198         lname = comma - author;
199
200         /* Skip all the spaces after the comma */
201         fname = strlen(author) - lname - 1;
202         comma += 1;
203         while (*comma == ' ') {
204             fname -= 1;
205             comma += 1;
206         }
207         strncpy(clean_author, comma, fname);
208
209         *(clean_author+fname) = ' ';
210         strncpy(clean_author + fname + 1, author, lname);
211         *(clean_author+fname+1+lname) = '\0';
212         /* make a temporary copy and see if it matches the email */
213         test_author = talloc_strdup(thread,clean_author);
214
215         blank=strchr(test_author,' ');
216         while (blank != NULL) {
217             *blank = '.';
218             blank=strchr(test_author,' ');
219         }
220         if (strcasestr(from, test_author) == NULL)
221             /* we didn't identify this as part of the email address
222             * so let's punt and return the original author */
223             strcpy (clean_author, author);
224     }
225     return clean_author;
226 }
227
228 /* Add 'message' as a message that belongs to 'thread'.
229  *
230  * The 'thread' will talloc_steal the 'message' and hold onto a
231  * reference to it.
232  */
233 static void
234 _thread_add_message (notmuch_thread_t *thread,
235                      notmuch_message_t *message,
236                      notmuch_string_list_t *exclude_terms,
237                      notmuch_exclude_t omit_exclude)
238 {
239     notmuch_tags_t *tags;
240     const char *tag;
241     InternetAddressList *list = NULL;
242     InternetAddress *address;
243     const char *from, *author;
244     char *clean_author;
245     bool message_excluded = false;
246
247     if (omit_exclude != NOTMUCH_EXCLUDE_FALSE) {
248         for (tags = notmuch_message_get_tags (message);
249              notmuch_tags_valid (tags);
250              notmuch_tags_move_to_next (tags))
251         {
252             tag = notmuch_tags_get (tags);
253             /* Is message excluded? */
254             for (notmuch_string_node_t *term = exclude_terms->head;
255                  term != NULL;
256                  term = term->next)
257             {
258                 /* Check for an empty string, and then ignore initial 'K'. */
259                 if (*(term->string) && strcmp(tag, (term->string + 1)) == 0) {
260                     message_excluded = true;
261                     break;
262                 }
263             }
264         }
265     }
266
267     if (message_excluded && omit_exclude == NOTMUCH_EXCLUDE_ALL)
268         return;
269
270     _notmuch_message_list_add_message (thread->message_list,
271                                        talloc_steal (thread, message));
272     thread->total_messages++;
273     thread->total_files += notmuch_message_count_files (message);
274
275     g_hash_table_insert (thread->message_hash,
276                          xstrdup (notmuch_message_get_message_id (message)),
277                          message);
278
279     from = notmuch_message_get_header (message, "from");
280     if (from)
281         list = internet_address_list_parse_string (from);
282
283     if (list) {
284         address = internet_address_list_get_address (list, 0);
285         if (address) {
286             author = internet_address_get_name (address);
287             /* We treat quoted empty names as if they were empty. */
288             if (author == NULL || author[0] == '\0') {
289                 InternetAddressMailbox *mailbox;
290                 mailbox = INTERNET_ADDRESS_MAILBOX (address);
291                 author = internet_address_mailbox_get_addr (mailbox);
292             }
293             clean_author = _thread_cleanup_author (thread, author, from);
294             _thread_add_author (thread, clean_author);
295             _notmuch_message_set_author (message, clean_author);
296         }
297         g_object_unref (G_OBJECT (list));
298     }
299
300     if (! thread->subject) {
301         const char *subject;
302         subject = notmuch_message_get_header (message, "subject");
303         thread->subject = talloc_strdup (thread, subject ? subject : "");
304     }
305
306     for (tags = notmuch_message_get_tags (message);
307          notmuch_tags_valid (tags);
308          notmuch_tags_move_to_next (tags))
309     {
310         tag = notmuch_tags_get (tags);
311         g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
312     }
313
314     /* Mark excluded messages. */
315     if (message_excluded)
316         notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
317 }
318
319 static void
320 _thread_set_subject_from_message (notmuch_thread_t *thread,
321                                   notmuch_message_t *message)
322 {
323     const char *subject;
324     const char *cleaned_subject;
325
326     subject = notmuch_message_get_header (message, "subject");
327     if (! subject)
328         return;
329
330     if ((strncasecmp (subject, "Re: ", 4) == 0) ||
331         (strncasecmp (subject, "Aw: ", 4) == 0) ||
332         (strncasecmp (subject, "Vs: ", 4) == 0) ||
333         (strncasecmp (subject, "Sv: ", 4) == 0)) {
334
335         cleaned_subject = talloc_strndup (thread,
336                                           subject + 4,
337                                           strlen(subject) - 4);
338     } else {
339         cleaned_subject = talloc_strdup (thread, subject);
340     }
341
342     if (! EMPTY_STRING(cleaned_subject)) {
343         if (thread->subject)
344             talloc_free (thread->subject);
345
346         thread->subject = talloc_strdup (thread, cleaned_subject);
347     }
348 }
349
350 /* Add a message to this thread which is known to match the original
351  * search specification. The 'sort' parameter controls whether the
352  * oldest or newest matching subject is applied to the thread as a
353  * whole. */
354 static void
355 _thread_add_matched_message (notmuch_thread_t *thread,
356                              notmuch_message_t *message,
357                              notmuch_sort_t sort)
358 {
359     time_t date;
360     notmuch_message_t *hashed_message;
361
362     date = notmuch_message_get_date (message);
363
364     if (date < thread->oldest || ! thread->matched_messages) {
365         thread->oldest = date;
366         if (sort == NOTMUCH_SORT_OLDEST_FIRST)
367             _thread_set_subject_from_message (thread, message);
368     }
369
370     if (date > thread->newest || ! thread->matched_messages) {
371         thread->newest = date;
372         const char *cur_subject = notmuch_thread_get_subject(thread);
373         if (sort != NOTMUCH_SORT_OLDEST_FIRST || EMPTY_STRING(cur_subject))
374             _thread_set_subject_from_message (thread, message);
375     }
376
377     if (!notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED))
378         thread->matched_messages++;
379
380     if (g_hash_table_lookup_extended (thread->message_hash,
381                             notmuch_message_get_message_id (message), NULL,
382                             (void **) &hashed_message)) {
383         notmuch_message_set_flag (hashed_message,
384                                   NOTMUCH_MESSAGE_FLAG_MATCH, 1);
385     }
386
387     _thread_add_matched_author (thread, _notmuch_message_get_author (hashed_message));
388 }
389
390 static void
391 _resolve_thread_relationships (notmuch_thread_t *thread)
392 {
393     notmuch_message_node_t *node, *first_node;
394     notmuch_message_t *message, *parent;
395     const char *in_reply_to;
396
397     first_node = thread->message_list->head;
398     if (! first_node)
399         return;
400
401     for (node = first_node->next; node; node = node->next) {
402         message = node->message;
403         in_reply_to = _notmuch_message_get_in_reply_to (message);
404         if (in_reply_to && strlen (in_reply_to) &&
405             g_hash_table_lookup_extended (thread->message_hash,
406                                           in_reply_to, NULL,
407                                           (void **) &parent))
408             _notmuch_message_add_reply (parent, message);
409         else
410             _notmuch_message_list_add_message (thread->toplevel_list, message);
411     }
412
413     /*
414      * if we reach the end of the list without finding a top-level
415      * message, that means the thread is a cycle (or set of cycles)
416      * and any message can be considered top-level.  Choose the oldest
417      * message, which happens to be first in our list.
418      */
419     if (first_node) {
420         message = first_node->message;
421         in_reply_to = _notmuch_message_get_in_reply_to (message);
422         if (thread->toplevel_list->head &&
423             in_reply_to && strlen (in_reply_to) &&
424             g_hash_table_lookup_extended (thread->message_hash,
425                                           in_reply_to, NULL,
426                                           (void **) &parent))
427             _notmuch_message_add_reply (parent, message);
428         else
429             _notmuch_message_list_add_message (thread->toplevel_list, message);
430     }
431
432     /* XXX: After scanning through the entire list looking for parents
433      * via "In-Reply-To", we should do a second pass that looks at the
434      * list of messages IDs in the "References" header instead. (And
435      * for this the parent would be the "deepest" message of all the
436      * messages found in the "References" list.)
437      *
438      * Doing this will allow messages and sub-threads to be positioned
439      * correctly in the thread even when an intermediate message is
440      * missing from the thread.
441      */
442 }
443
444 /* Create a new notmuch_thread_t object by finding the thread
445  * containing the message with the given doc ID, treating any messages
446  * contained in match_set as "matched".  Remove all messages in the
447  * thread from match_set.
448  *
449  * Creating the thread will perform a database search to get all
450  * messages belonging to the thread and will get the first subject
451  * line, the total count of messages, and all authors in the thread.
452  * Each message in the thread is checked against match_set to allow
453  * for a separate count of matched messages, and to allow a viewer to
454  * display these messages differently.
455  *
456  * Here, 'ctx' is talloc context for the resulting thread object.
457  *
458  * This function returns NULL in the case of any error.
459  */
460 notmuch_thread_t *
461 _notmuch_thread_create (void *ctx,
462                         notmuch_database_t *notmuch,
463                         unsigned int seed_doc_id,
464                         notmuch_doc_id_set_t *match_set,
465                         notmuch_string_list_t *exclude_terms,
466                         notmuch_exclude_t omit_excluded,
467                         notmuch_sort_t sort)
468 {
469     void *local = talloc_new (ctx);
470     notmuch_thread_t *thread = NULL;
471     notmuch_message_t *seed_message;
472     const char *thread_id;
473     char *thread_id_query_string;
474     notmuch_query_t *thread_id_query;
475
476     notmuch_messages_t *messages;
477     notmuch_message_t *message;
478     notmuch_status_t status;
479
480     seed_message = _notmuch_message_create (local, notmuch, seed_doc_id, NULL);
481     if (! seed_message)
482         INTERNAL_ERROR ("Thread seed message %u does not exist", seed_doc_id);
483
484     thread_id = notmuch_message_get_thread_id (seed_message);
485     thread_id_query_string = talloc_asprintf (local, "thread:%s", thread_id);
486     if (unlikely (thread_id_query_string == NULL))
487         goto DONE;
488
489     thread_id_query = talloc_steal (
490         local, notmuch_query_create (notmuch, thread_id_query_string));
491     if (unlikely (thread_id_query == NULL))
492         goto DONE;
493
494     thread = talloc (local, notmuch_thread_t);
495     if (unlikely (thread == NULL))
496         goto DONE;
497
498     talloc_set_destructor (thread, _notmuch_thread_destructor);
499
500     thread->notmuch = notmuch;
501     thread->thread_id = talloc_strdup (thread, thread_id);
502     thread->subject = NULL;
503     thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
504                                                   NULL, NULL);
505     thread->authors_array = g_ptr_array_new ();
506     thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
507                                                           g_str_equal,
508                                                           NULL, NULL);
509     thread->matched_authors_array = g_ptr_array_new ();
510     thread->authors = NULL;
511     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
512                                           free, NULL);
513
514     thread->message_list = _notmuch_message_list_create (thread);
515     thread->toplevel_list = _notmuch_message_list_create (thread);
516     if (unlikely (thread->message_list == NULL ||
517                   thread->toplevel_list == NULL)) {
518         thread = NULL;
519         goto DONE;
520     }
521
522     thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
523                                                   free, NULL);
524
525     thread->total_messages = 0;
526     thread->total_files = 0;
527     thread->matched_messages = 0;
528     thread->oldest = 0;
529     thread->newest = 0;
530
531     /* We use oldest-first order unconditionally here to obtain the
532      * proper author ordering for the thread. The 'sort' parameter
533      * passed to this function is used only to indicate whether the
534      * oldest or newest subject is desired. */
535     notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
536
537     status = notmuch_query_search_messages (thread_id_query, &messages);
538     if (status)
539         goto DONE;
540
541     for (;
542          notmuch_messages_valid (messages);
543          notmuch_messages_move_to_next (messages))
544     {
545         unsigned int doc_id;
546
547         message = notmuch_messages_get (messages);
548         doc_id = _notmuch_message_get_doc_id (message);
549         if (doc_id == seed_doc_id)
550             message = seed_message;
551
552         _thread_add_message (thread, message, exclude_terms, omit_excluded);
553
554         if ( _notmuch_doc_id_set_contains (match_set, doc_id)) {
555             _notmuch_doc_id_set_remove (match_set, doc_id);
556             _thread_add_matched_message (thread, message, sort);
557         }
558
559         _notmuch_message_close (message);
560     }
561
562     _resolve_thread_authors_string (thread);
563
564     _resolve_thread_relationships (thread);
565
566     /* Commit to returning thread. */
567     (void) talloc_steal (ctx, thread);
568
569   DONE:
570     talloc_free (local);
571     return thread;
572 }
573
574 notmuch_messages_t *
575 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
576 {
577     return _notmuch_messages_create (thread->toplevel_list);
578 }
579
580 notmuch_messages_t *
581 notmuch_thread_get_messages (notmuch_thread_t *thread)
582 {
583     return _notmuch_messages_create (thread->message_list);
584 }
585
586 const char *
587 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
588 {
589     return thread->thread_id;
590 }
591
592 int
593 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
594 {
595     return thread->total_messages;
596 }
597
598 int
599 notmuch_thread_get_total_files (notmuch_thread_t *thread)
600 {
601     return thread->total_files;
602 }
603
604 int
605 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
606 {
607     return thread->matched_messages;
608 }
609
610 const char *
611 notmuch_thread_get_authors (notmuch_thread_t *thread)
612 {
613     return thread->authors;
614 }
615
616 const char *
617 notmuch_thread_get_subject (notmuch_thread_t *thread)
618 {
619     return thread->subject;
620 }
621
622 time_t
623 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
624 {
625     return thread->oldest;
626 }
627
628 time_t
629 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
630 {
631     return thread->newest;
632 }
633
634 notmuch_tags_t *
635 notmuch_thread_get_tags (notmuch_thread_t *thread)
636 {
637     notmuch_string_list_t *tags;
638     GList *keys, *l;
639
640     tags = _notmuch_string_list_create (thread);
641     if (unlikely (tags == NULL))
642         return NULL;
643
644     keys = g_hash_table_get_keys (thread->tags);
645
646     for (l = keys; l; l = l->next)
647         _notmuch_string_list_append (tags, (char *) l->data);
648
649     g_list_free (keys);
650
651     _notmuch_string_list_sort (tags);
652
653     return _notmuch_tags_create (thread, tags);
654 }
655
656 void
657 notmuch_thread_destroy (notmuch_thread_t *thread)
658 {
659     talloc_free (thread);
660 }