]> git.notmuchmail.org Git - notmuch/blob - lib/thread.cc
lib: Audit calls to notmuch_message_get_header to handle NULL return
[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     char *authors;
35     GHashTable *tags;
36
37     notmuch_message_list_t *message_list;
38     GHashTable *message_hash;
39     int total_messages;
40     int matched_messages;
41     time_t oldest;
42     time_t newest;
43 };
44
45 static int
46 _notmuch_thread_destructor (notmuch_thread_t *thread)
47 {
48     g_hash_table_unref (thread->authors_hash);
49     g_hash_table_unref (thread->tags);
50     g_hash_table_unref (thread->message_hash);
51
52     return 0;
53 }
54
55 static void
56 _thread_add_author (notmuch_thread_t *thread,
57                     const char *author)
58 {
59     if (author == NULL)
60         return;
61
62     if (g_hash_table_lookup_extended (thread->authors_hash,
63                                       author, NULL, NULL))
64         return;
65
66     g_hash_table_insert (thread->authors_hash, xstrdup (author), NULL);
67
68     if (thread->authors)
69         thread->authors = talloc_asprintf (thread, "%s, %s",
70                                            thread->authors,
71                                            author);
72     else
73         thread->authors = talloc_strdup (thread, author);
74 }
75
76 /* Add 'message' as a message that belongs to 'thread'.
77  *
78  * The 'thread' will talloc_steal the 'message' and hold onto a
79  * reference to it.
80  */
81 static void
82 _thread_add_message (notmuch_thread_t *thread,
83                      notmuch_message_t *message)
84 {
85     notmuch_tags_t *tags;
86     const char *tag;
87     InternetAddressList *list = NULL;
88     InternetAddress *address;
89     const char *from, *author;
90
91     _notmuch_message_list_add_message (thread->message_list,
92                                        talloc_steal (thread, message));
93     thread->total_messages++;
94
95     g_hash_table_insert (thread->message_hash,
96                          xstrdup (notmuch_message_get_message_id (message)),
97                          message);
98
99     from = notmuch_message_get_header (message, "from");
100     if (from)
101         list = internet_address_list_parse_string (from);
102
103     if (list) {
104         address = internet_address_list_get_address (list, 0);
105         if (address) {
106             author = internet_address_get_name (address);
107             if (author == NULL) {
108                 InternetAddressMailbox *mailbox;
109                 mailbox = INTERNET_ADDRESS_MAILBOX (address);
110                 author = internet_address_mailbox_get_addr (mailbox);
111             }
112             _thread_add_author (thread, author);
113         }
114         g_object_unref (G_OBJECT (list));
115     }
116
117     if (! thread->subject) {
118         const char *subject;
119         subject = notmuch_message_get_header (message, "subject");
120         thread->subject = talloc_strdup (thread, subject ? subject : "");
121     }
122
123     for (tags = notmuch_message_get_tags (message);
124          notmuch_tags_valid (tags);
125          notmuch_tags_move_to_next (tags))
126     {
127         tag = notmuch_tags_get (tags);
128         g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
129     }
130 }
131
132 static void
133 _thread_set_subject_from_message (notmuch_thread_t *thread,
134                                   notmuch_message_t *message)
135 {
136     const char *subject;
137     const char *cleaned_subject;
138
139     subject = notmuch_message_get_header (message, "subject");
140     if (! subject)
141         return;
142
143     if ((strncasecmp (subject, "Re: ", 4) == 0) ||
144         (strncasecmp (subject, "Aw: ", 4) == 0) ||
145         (strncasecmp (subject, "Vs: ", 4) == 0) ||
146         (strncasecmp (subject, "Sv: ", 4) == 0)) {
147
148         cleaned_subject = talloc_strndup (thread,
149                                           subject + 4,
150                                           strlen(subject) - 4);
151     } else {
152         cleaned_subject = talloc_strdup (thread, subject);
153     }
154
155     if (thread->subject)
156         talloc_free (thread->subject);
157
158     thread->subject = talloc_strdup (thread, cleaned_subject);
159 }
160
161 static void
162 _thread_add_matched_message (notmuch_thread_t *thread,
163                              notmuch_message_t *message,
164                              notmuch_sort_t sort)
165 {
166     time_t date;
167     notmuch_message_t *hashed_message;
168
169     date = notmuch_message_get_date (message);
170
171     if (date < thread->oldest || ! thread->matched_messages)
172         thread->oldest = date;
173
174     if (date > thread->newest || ! thread->matched_messages)
175         thread->newest = date;
176
177     thread->matched_messages++;
178
179     if (g_hash_table_lookup_extended (thread->message_hash,
180                             notmuch_message_get_message_id (message), NULL,
181                             (void **) &hashed_message)) {
182         notmuch_message_set_flag (hashed_message,
183                                   NOTMUCH_MESSAGE_FLAG_MATCH, 1);
184     }
185
186     if ((sort == NOTMUCH_SORT_OLDEST_FIRST && date <= thread->newest) ||
187         (sort != NOTMUCH_SORT_OLDEST_FIRST && date == thread->newest))
188     {
189         _thread_set_subject_from_message (thread, message);
190     }
191 }
192
193 static void
194 _resolve_thread_relationships (unused (notmuch_thread_t *thread))
195 {
196     notmuch_message_node_t **prev, *node;
197     notmuch_message_t *message, *parent;
198     const char *in_reply_to;
199
200     prev = &thread->message_list->head;
201     while ((node = *prev)) {
202         message = node->message;
203         in_reply_to = _notmuch_message_get_in_reply_to (message);
204         if (in_reply_to && strlen (in_reply_to) &&
205             g_hash_table_lookup_extended (thread->message_hash,
206                                           in_reply_to, NULL,
207                                           (void **) &parent))
208         {
209             *prev = node->next;
210             if (thread->message_list->tail == &node->next)
211                 thread->message_list->tail = prev;
212             node->next = NULL;
213             _notmuch_message_add_reply (parent, node);
214         } else {
215             prev = &((*prev)->next);
216         }
217     }
218
219     /* XXX: After scanning through the entire list looking for parents
220      * via "In-Reply-To", we should do a second pass that looks at the
221      * list of messages IDs in the "References" header instead. (And
222      * for this the parent would be the "deepest" message of all the
223      * messages found in the "References" list.)
224      *
225      * Doing this will allow messages and sub-threads to be positioned
226      * correctly in the thread even when an intermediate message is
227      * missing from the thread.
228      */
229 }
230
231 /* Create a new notmuch_thread_t object for the given thread ID,
232  * treating any messages matching 'query_string' as "matched".
233  *
234  * Creating the thread will trigger two database searches. The first
235  * is for all messages belonging to the thread, (to get the first
236  * subject line, the total count of messages, and all authors). The
237  * second search is for all messages that are in the thread and that
238  * also match the given query_string. This is to allow for a separate
239  * count of matched messages, and to allow a viewer to display these
240  * messages differently.
241  *
242  * Here, 'ctx' is talloc context for the resulting thread object.
243  *
244  * This function returns NULL in the case of any error.
245  */
246 notmuch_thread_t *
247 _notmuch_thread_create (void *ctx,
248                         notmuch_database_t *notmuch,
249                         const char *thread_id,
250                         const char *query_string,
251                         notmuch_sort_t sort)
252 {
253     notmuch_thread_t *thread;
254     const char *thread_id_query_string;
255     notmuch_query_t *thread_id_query;
256
257     notmuch_messages_t *messages;
258     notmuch_message_t *message;
259     notmuch_bool_t matched_is_subset_of_thread;
260
261     thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
262     if (unlikely (query_string == NULL))
263         return NULL;
264
265     /* Under normal circumstances we need to do two database
266      * queries. One is for the thread itself (thread_id_query_string)
267      * and the second is to determine which messages in that thread
268      * match the original query (matched_query_string).
269      *
270      * But under two circumstances, we use only the
271      * thread_id_query_string:
272      *
273      *  1. If the original query_string *is* just the thread
274      *     specification.
275      *
276      *  2. If the original query_string matches all messages ("" or
277      *     "*").
278      *
279      * In either of these cases, we can be more efficient by running
280      * just the thread_id query (since we know all messages in the
281      * thread will match the query_string).
282      *
283      * Beyond the performance advantage, in the second case, it's
284      * important to not try to create a concatenated query because our
285      * parser handles "" and "*" as special cases and will not do the
286      * right thing with a query string of "* and thread:<foo>".
287      **/
288     matched_is_subset_of_thread = 1;
289     if (strcmp (query_string, thread_id_query_string) == 0 ||
290         strcmp (query_string, "") == 0 ||
291         strcmp (query_string, "*") == 0)
292     {
293         matched_is_subset_of_thread = 0;
294     }
295
296     thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
297     if (unlikely (thread_id_query == NULL))
298         return NULL;
299
300     thread = talloc (ctx, notmuch_thread_t);
301     if (unlikely (thread == NULL))
302         return NULL;
303
304     talloc_set_destructor (thread, _notmuch_thread_destructor);
305
306     thread->notmuch = notmuch;
307     thread->thread_id = talloc_strdup (thread, thread_id);
308     thread->subject = NULL;
309     thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
310                                                   free, NULL);
311     thread->authors = NULL;
312     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
313                                           free, NULL);
314
315     thread->message_list = _notmuch_message_list_create (thread);
316     if (unlikely (thread->message_list == NULL))
317         return NULL;
318
319     thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
320                                                   free, NULL);
321
322     thread->total_messages = 0;
323     thread->matched_messages = 0;
324     thread->oldest = 0;
325     thread->newest = 0;
326
327     notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
328
329     for (messages = notmuch_query_search_messages (thread_id_query);
330          notmuch_messages_valid (messages);
331          notmuch_messages_move_to_next (messages))
332     {
333         message = notmuch_messages_get (messages);
334
335         _thread_add_message (thread, message);
336
337         if (! matched_is_subset_of_thread)
338             _thread_add_matched_message (thread, message, sort);
339
340         _notmuch_message_close (message);
341     }
342
343     notmuch_query_destroy (thread_id_query);
344
345     if (matched_is_subset_of_thread)
346     {
347         const char *matched_query_string;
348         notmuch_query_t *matched_query;
349
350         matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
351                                                 thread_id_query_string,
352                                                 query_string);
353         if (unlikely (matched_query_string == NULL))
354             return NULL;
355
356         matched_query = notmuch_query_create (notmuch, matched_query_string);
357         if (unlikely (matched_query == NULL))
358             return NULL;
359
360         for (messages = notmuch_query_search_messages (matched_query);
361              notmuch_messages_valid (messages);
362              notmuch_messages_move_to_next (messages))
363         {
364             message = notmuch_messages_get (messages);
365             _thread_add_matched_message (thread, message, sort);
366             _notmuch_message_close (message);
367         }
368
369         notmuch_query_destroy (matched_query);
370     }
371
372     _resolve_thread_relationships (thread);
373
374     return thread;
375 }
376
377 notmuch_messages_t *
378 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
379 {
380     return _notmuch_messages_create (thread->message_list);
381 }
382
383 const char *
384 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
385 {
386     return thread->thread_id;
387 }
388
389 int
390 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
391 {
392     return thread->total_messages;
393 }
394
395 int
396 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
397 {
398     return thread->matched_messages;
399 }
400
401 const char *
402 notmuch_thread_get_authors (notmuch_thread_t *thread)
403 {
404     return thread->authors;
405 }
406
407 const char *
408 notmuch_thread_get_subject (notmuch_thread_t *thread)
409 {
410     return thread->subject;
411 }
412
413 time_t
414 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
415 {
416     return thread->oldest;
417 }
418
419 time_t
420 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
421 {
422     return thread->newest;
423 }
424
425 notmuch_tags_t *
426 notmuch_thread_get_tags (notmuch_thread_t *thread)
427 {
428     notmuch_tags_t *tags;
429     GList *keys, *l;
430
431     tags = _notmuch_tags_create (thread);
432     if (unlikely (tags == NULL))
433         return NULL;
434
435     keys = g_hash_table_get_keys (thread->tags);
436
437     for (l = keys; l; l = l->next)
438         _notmuch_tags_add_tag (tags, (char *) l->data);
439
440     g_list_free (keys);
441
442     _notmuch_tags_prepare_iterator (tags);
443
444     return tags;
445 }
446
447 void
448 notmuch_thread_destroy (notmuch_thread_t *thread)
449 {
450     talloc_free (thread);
451 }