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