]> git.notmuchmail.org Git - notmuch/blob - lib/thread.cc
notmuch_tags_advance: Make safe against excessive calls.
[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     int total_messages;
38     int matched_messages;
39     time_t oldest;
40     time_t newest;
41 };
42
43 static int
44 _notmuch_thread_destructor (notmuch_thread_t *thread)
45 {
46     g_hash_table_unref (thread->authors_hash);
47     g_hash_table_unref (thread->tags);
48
49     return 0;
50 }
51
52 static void
53 _thread_add_author (notmuch_thread_t *thread,
54                     const char *author)
55 {
56     if (author == NULL)
57         return;
58
59     if (g_hash_table_lookup_extended (thread->authors_hash,
60                                       author, NULL, NULL))
61         return;
62
63     g_hash_table_insert (thread->authors_hash, xstrdup (author), NULL);
64
65     if (thread->authors)
66         thread->authors = talloc_asprintf (thread, "%s, %s",
67                                            thread->authors,
68                                            author);
69     else
70         thread->authors = talloc_strdup (thread, author);
71 }
72
73 static void
74 _thread_add_message (notmuch_thread_t *thread,
75                      notmuch_message_t *message)
76 {
77     notmuch_tags_t *tags;
78     const char *tag;
79     time_t date;
80     InternetAddressList *list;
81     InternetAddress *address;
82     const char *from, *author;
83
84     from = notmuch_message_get_header (message, "from");
85     list = internet_address_list_parse_string (from);
86     if (list) {
87         address = internet_address_list_get_address (list, 0);
88         if (address) {
89             author = internet_address_get_name (address);
90             if (author == NULL) {
91                 InternetAddressMailbox *mailbox;
92                 mailbox = INTERNET_ADDRESS_MAILBOX (address);
93                 author = internet_address_mailbox_get_addr (mailbox);
94             }
95             _thread_add_author (thread, author);
96         }
97         g_object_unref (G_OBJECT (list));
98     }
99
100     if (! thread->subject) {
101         const char *subject;
102         subject = notmuch_message_get_header (message, "subject");
103         thread->subject = talloc_strdup (thread, subject);
104     }
105
106     for (tags = notmuch_message_get_tags (message);
107          notmuch_tags_has_more (tags);
108          notmuch_tags_advance (tags))
109     {
110         tag = notmuch_tags_get (tags);
111         g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
112     }
113
114     thread->total_messages++;
115 }
116
117 static void
118 _thread_add_matched_message (notmuch_thread_t *thread,
119                              notmuch_message_t *message)
120 {
121     time_t date;
122
123     date = notmuch_message_get_date (message);
124
125     if (date < thread->oldest || ! thread->matched_messages)
126         thread->oldest = date;
127
128     if (date > thread->newest || ! thread->matched_messages)
129         thread->newest = date;
130
131     thread->matched_messages++;
132 }
133
134 /* Create a new notmuch_thread_t object for the given thread ID,
135  * treating any messages matching 'query_string' as "matched".
136  *
137  * Creating the thread will trigger two database searches. The first
138  * is for all messages belonging to the thread, (to get the first
139  * subject line, the total count of messages, and all authors). The
140  * second search is for all messages that are in the thread and that
141  * also match the given query_string. This is to allow for a separate
142  * count of matched messages, and to allow a viewer to diplay these
143  * messages differently.
144  *
145  * Here, 'ctx' is talloc context for the resulting thread object.
146  *
147  * This function returns NULL in the case of any error.
148  */
149 notmuch_thread_t *
150 _notmuch_thread_create (void *ctx,
151                         notmuch_database_t *notmuch,
152                         const char *thread_id,
153                         const char *query_string)
154 {
155     notmuch_thread_t *thread;
156     const char *thread_id_query_string, *matched_query_string;
157     notmuch_query_t *thread_id_query, *matched_query;
158     notmuch_messages_t *messages;
159
160     thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
161     if (unlikely (query_string == NULL))
162         return NULL;
163
164     matched_query_string = talloc_asprintf (ctx, "%s AND (%s)",
165                                             thread_id_query_string,
166                                             query_string);
167     if (unlikely (matched_query_string == NULL))
168         return NULL;
169
170     thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
171     if (unlikely (thread_id_query == NULL))
172         return NULL;
173
174     matched_query = notmuch_query_create (notmuch, matched_query_string);
175     if (unlikely (thread_id_query == NULL))
176         return NULL;
177
178     thread = talloc (ctx, notmuch_thread_t);
179     if (unlikely (thread == NULL))
180         return NULL;
181
182     talloc_set_destructor (thread, _notmuch_thread_destructor);
183
184     thread->notmuch = notmuch;
185     thread->thread_id = talloc_strdup (thread, thread_id);
186     thread->subject = NULL;
187     thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
188                                                   free, NULL);
189     thread->authors = NULL;
190     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
191                                           free, NULL);
192
193     thread->total_messages = 0;
194     thread->matched_messages = 0;
195     thread->oldest = 0;
196     thread->newest = 0;
197
198     notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_DATE);
199
200     for (messages = notmuch_query_search_messages (thread_id_query, 0, -1);
201          notmuch_messages_has_more (messages);
202          notmuch_messages_advance (messages))
203     {
204         _thread_add_message (thread, notmuch_messages_get (messages));
205     }
206
207     notmuch_query_destroy (thread_id_query);
208
209     for (messages = notmuch_query_search_messages (matched_query, 0, -1);
210          notmuch_messages_has_more (messages);
211          notmuch_messages_advance (messages))
212     {
213         _thread_add_matched_message (thread, notmuch_messages_get (messages));
214     }
215
216     notmuch_query_destroy (matched_query);
217
218     return thread;
219 }
220
221 const char *
222 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
223 {
224     return thread->thread_id;
225 }
226
227 int
228 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
229 {
230     return thread->total_messages;
231 }
232
233 int
234 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
235 {
236     return thread->matched_messages;
237 }
238
239 const char *
240 notmuch_thread_get_authors (notmuch_thread_t *thread)
241 {
242     return thread->authors;
243 }
244
245 const char *
246 notmuch_thread_get_subject (notmuch_thread_t *thread)
247 {
248     return thread->subject;
249 }
250
251 time_t
252 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
253 {
254     return thread->oldest;
255 }
256
257 time_t
258 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
259 {
260     return thread->newest;
261 }
262
263 notmuch_tags_t *
264 notmuch_thread_get_tags (notmuch_thread_t *thread)
265 {
266     notmuch_tags_t *tags;
267     GList *keys, *l;
268
269     tags = _notmuch_tags_create (thread);
270     if (unlikely (tags == NULL))
271         return NULL;
272
273     keys = g_hash_table_get_keys (thread->tags);
274
275     for (l = keys; l; l = l->next)
276         _notmuch_tags_add_tag (tags, (char *) l->data);
277
278     g_list_free (keys);
279
280     _notmuch_tags_prepare_iterator (tags);
281
282     return tags;
283 }
284
285 void
286 notmuch_thread_destroy (notmuch_thread_t *thread)
287 {
288     talloc_free (thread);
289 }