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