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