]> git.notmuchmail.org Git - notmuch/blob - message.cc
Hide away the details of the implementation of notmuch_tags_t.
[notmuch] / message.cc
1 /* message.cc - Results of message-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-cxx.h"
22 #include "database-private.h"
23
24 #include <xapian.h>
25
26 struct _notmuch_message {
27     notmuch_database_t *notmuch;
28     Xapian::docid doc_id;
29     char *message_id;
30     char *thread_id;
31     char *filename;
32     Xapian::Document doc;
33 };
34
35
36 /* "128 bits of thread-id ought to be enough for anybody" */
37 #define NOTMUCH_THREAD_ID_BITS   128
38 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
39 typedef struct _thread_id {
40     char str[NOTMUCH_THREAD_ID_DIGITS + 1];
41 } thread_id_t;
42
43 /* We end up having to call the destructor explicitly because we had
44  * to use "placement new" in order to initialize C++ objects within a
45  * block that we allocated with talloc. So C++ is making talloc
46  * slightly less simple to use, (we wouldn't need
47  * talloc_set_destructor at all otherwise).
48  */
49 static int
50 _notmuch_message_destructor (notmuch_message_t *message)
51 {
52     message->doc.~Document ();
53
54     return 0;
55 }
56
57 /* Create a new notmuch_message_t object for an existing document in
58  * the database.
59  *
60  * Here, 'talloc owner' is an optional talloc context to which the new
61  * message will belong. This allows for the caller to not bother
62  * calling notmuch_message_destroy on the message, and no that all
63  * memory will be reclaimed with 'talloc_owner' is free. The caller
64  * still can call notmuch_message_destroy when finished with the
65  * message if desired.
66  *
67  * The 'talloc_owner' argument can also be NULL, in which case the
68  * caller *is* responsible for calling notmuch_message_destroy.
69  *
70  * If no document exists in the database with document ID of 'doc_id'
71  * then this function returns NULL and optionally sets *status to
72  * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
73  *
74  * This function can also fail to due lack of available memory,
75  * returning NULL and optionally setting *status to
76  * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
77  *
78  * The caller can pass NULL for status if uninterested in
79  * distinguishing these two cases.
80  */
81 notmuch_message_t *
82 _notmuch_message_create (const void *talloc_owner,
83                          notmuch_database_t *notmuch,
84                          unsigned int doc_id,
85                          notmuch_private_status_t *status)
86 {
87     notmuch_message_t *message;
88
89     if (status)
90         *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
91
92     message = talloc (talloc_owner, notmuch_message_t);
93     if (unlikely (message == NULL)) {
94         if (status)
95             *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
96         return NULL;
97     }
98
99     message->notmuch = notmuch;
100     message->doc_id = doc_id;
101     message->message_id = NULL; /* lazily created */
102     message->thread_id = NULL; /* lazily created */
103     message->filename = NULL; /* lazily created */
104
105     /* This is C++'s creepy "placement new", which is really just an
106      * ugly way to call a constructor for a pre-allocated object. So
107      * it's really not an error to not be checking for OUT_OF_MEMORY
108      * here, since this "new" isn't actually allocating memory. This
109      * is language-design comedy of the wrong kind. */
110
111     new (&message->doc) Xapian::Document;
112
113     talloc_set_destructor (message, _notmuch_message_destructor);
114
115     try {
116         message->doc = notmuch->xapian_db->get_document (doc_id);
117     } catch (const Xapian::DocNotFoundError &error) {
118         talloc_free (message);
119         if (status)
120             *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
121         return NULL;
122     }
123
124     return message;
125 }
126
127 /* Create a new notmuch_message_t object for a specific message ID,
128  * (which may or may not already exist in the databas).
129  *
130  * Here, 'talloc owner' is an optional talloc context to which the new
131  * message will belong. This allows for the caller to not bother
132  * calling notmuch_message_destroy on the message, and no that all
133  * memory will be reclaimed with 'talloc_owner' is free. The caller
134  * still can call notmuch_message_destroy when finished with the
135  * message if desired.
136  *
137  * The 'talloc_owner' argument can also be NULL, in which case the
138  * caller *is* responsible for calling notmuch_message_destroy.
139  *
140  * If there is already a document with message ID 'message_id' in the
141  * database, then the returned message can be used to query/modify the
142  * document. Otherwise, a new document will be inserted into the
143  * database before this function returns.
144  *
145  * If an error occurs, this function will return NULL and *status
146  * will be set as appropriate. (The status pointer argument must
147  * not be NULL.)
148  */
149 notmuch_message_t *
150 _notmuch_message_create_for_message_id (const void *talloc_owner,
151                                         notmuch_database_t *notmuch,
152                                         const char *message_id,
153                                         notmuch_status_t *status)
154 {
155     notmuch_private_status_t private_status;
156     notmuch_message_t *message;
157     Xapian::Document doc;
158     unsigned int doc_id;
159     char *term;
160
161     *status = NOTMUCH_STATUS_SUCCESS;
162
163     message = notmuch_database_find_message (notmuch, message_id);
164     if (message)
165         return talloc_steal (talloc_owner, message);
166
167     term = talloc_asprintf (NULL, "%s%s",
168                             _find_prefix ("id"), message_id);
169     if (term == NULL) {
170         *status = NOTMUCH_STATUS_OUT_OF_MEMORY;
171         return NULL;
172     }
173
174     try {
175         doc.add_term (term);
176         talloc_free (term);
177
178         doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
179
180         doc_id = notmuch->xapian_db->add_document (doc);
181     } catch (const Xapian::Error &error) {
182         *status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
183         return NULL;
184     }
185
186     message = _notmuch_message_create (talloc_owner, notmuch,
187                                        doc_id, &private_status);
188
189     *status = COERCE_STATUS (private_status,
190                              "Failed to find dcocument after inserting it.");
191
192     return message;
193 }
194
195 const char *
196 notmuch_message_get_message_id (notmuch_message_t *message)
197 {
198     Xapian::TermIterator i;
199
200     if (message->message_id)
201         return message->message_id;
202
203     i = message->doc.termlist_begin ();
204     i.skip_to (_find_prefix ("id"));
205
206     if (i == message->doc.termlist_end ())
207         INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
208                         message->doc_id);
209
210     message->message_id = talloc_strdup (message, (*i).c_str () + 1);
211
212 #if DEBUG_DATABASE_SANITY
213     i++;
214
215     if (i != message->doc.termlist_end () &&
216         strncmp ((*i).c_str (), _find_prefix ("id"),
217                  strlen (_find_prefix ("id"))) == 0)
218     {
219         INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
220                         message->doc_id);
221     }
222 #endif
223
224     return message->message_id;
225 }
226
227 const char *
228 notmuch_message_get_thread_id (notmuch_message_t *message)
229 {
230     Xapian::TermIterator i;
231
232     if (message->thread_id)
233         return message->thread_id;
234
235     i = message->doc.termlist_begin ();
236     i.skip_to (_find_prefix ("thread"));
237
238     if (i == message->doc.termlist_end ())
239         INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
240                         message->doc_id);
241
242     message->thread_id = talloc_strdup (message, (*i).c_str () + 1);
243
244 #if DEBUG_DATABASE_SANITY
245     i++;
246
247     if (i != message->doc.termlist_end () &&
248         strncmp ((*i).c_str (), _find_prefix ("thread"),
249                  strlen (_find_prefix ("thread"))) == 0)
250     {
251         INTERNAL_ERROR ("Message with document ID of %d has duplicate thread IDs.\n",
252                         message->doc_id);
253     }
254 #endif
255
256     return message->thread_id;
257 }
258
259 /* Set the filename for 'message' to 'filename'.
260  *
261  * XXX: We should still figure out if we think it's important to store
262  * multiple filenames for email messages with identical message IDs.
263  *
264  * This change will not be reflected in the database until the next
265  * call to _notmuch_message_set_sync. */
266 void
267 _notmuch_message_set_filename (notmuch_message_t *message,
268                                const char *filename)
269 {
270     if (message->filename)
271         talloc_free (message->filename);
272     message->doc.set_data (filename);
273 }
274
275 const char *
276 notmuch_message_get_filename (notmuch_message_t *message)
277 {
278     std::string filename_str;
279
280     if (message->filename)
281         return message->filename;
282
283     filename_str = message->doc.get_data ();
284     message->filename = talloc_strdup (message, filename_str.c_str ());
285
286     return message->filename;
287 }
288
289 notmuch_tags_t *
290 notmuch_message_get_tags (notmuch_message_t *message)
291 {
292     return _notmuch_tags_create_terms (message, message->doc, "tag");
293 }
294
295 void
296 _notmuch_message_set_date (notmuch_message_t *message,
297                            const char *date)
298 {
299     time_t time_value;
300
301     time_value = notmuch_parse_date (date, NULL);
302
303     message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
304                             Xapian::sortable_serialise (time_value));
305 }
306
307 static void
308 thread_id_generate (thread_id_t *thread_id)
309 {
310     static int seeded = 0;
311     FILE *dev_random;
312     uint32_t value;
313     char *s;
314     int i;
315
316     if (! seeded) {
317         dev_random = fopen ("/dev/random", "r");
318         if (dev_random == NULL) {
319             srand (time (NULL));
320         } else {
321             fread ((void *) &value, sizeof (value), 1, dev_random);
322             srand (value);
323             fclose (dev_random);
324         }
325         seeded = 1;
326     }
327
328     s = thread_id->str;
329     for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
330         value = rand ();
331         sprintf (s, "%08x", value);
332         s += 8;
333     }
334 }
335
336 void
337 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
338 {
339     /* If not part of any existing thread, generate a new thread_id. */
340     thread_id_t thread_id;
341
342     thread_id_generate (&thread_id);
343     _notmuch_message_add_term (message, "thread", thread_id.str);
344 }
345
346 /* Synchronize changes made to message->doc out into the database. */
347 void
348 _notmuch_message_sync (notmuch_message_t *message)
349 {
350     Xapian::WritableDatabase *db = message->notmuch->xapian_db;
351
352     db->replace_document (message->doc_id, message->doc);
353 }
354
355 /* Add a name:value term to 'message', (the actual term will be
356  * encoded by prefixing the value with a short prefix). See
357  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
358  * names to prefix values.
359  *
360  * This change will not be reflected in the database until the next
361  * call to _notmuch_message_set_sync. */
362 notmuch_private_status_t
363 _notmuch_message_add_term (notmuch_message_t *message,
364                            const char *prefix_name,
365                            const char *value)
366 {
367
368     char *term;
369
370     if (value == NULL)
371         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
372
373     term = talloc_asprintf (message, "%s%s",
374                             _find_prefix (prefix_name), value);
375
376     if (strlen (term) > NOTMUCH_TERM_MAX)
377         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
378
379     message->doc.add_term (term);
380
381     talloc_free (term);
382
383     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
384 }
385
386 /* Remove a name:value term from 'message', (the actual term will be
387  * encoded by prefixing the value with a short prefix). See
388  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
389  * names to prefix values.
390  *
391  * This change will not be reflected in the database until the next
392  * call to _notmuch_message_set_sync. */
393 notmuch_private_status_t
394 _notmuch_message_remove_term (notmuch_message_t *message,
395                               const char *prefix_name,
396                               const char *value)
397 {
398     char *term;
399
400     if (value == NULL)
401         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
402
403     term = talloc_asprintf (message, "%s%s",
404                             _find_prefix (prefix_name), value);
405
406     if (strlen (term) > NOTMUCH_TERM_MAX)
407         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
408
409     message->doc.remove_term (term);
410
411     talloc_free (term);
412
413     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
414 }
415
416 notmuch_status_t
417 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
418 {
419     notmuch_private_status_t status;
420
421     if (tag == NULL)
422         return NOTMUCH_STATUS_NULL_POINTER;
423
424     if (strlen (tag) > NOTMUCH_TAG_MAX)
425         return NOTMUCH_STATUS_TAG_TOO_LONG;
426
427     status = _notmuch_message_add_term (message, "tag", tag);
428     if (status) {
429         INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
430                         status);
431     }
432
433     _notmuch_message_sync (message);
434
435     return NOTMUCH_STATUS_SUCCESS;
436 }
437
438 notmuch_status_t
439 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
440 {
441     notmuch_private_status_t status;
442
443     if (tag == NULL)
444         return NOTMUCH_STATUS_NULL_POINTER;
445
446     if (strlen (tag) > NOTMUCH_TAG_MAX)
447         return NOTMUCH_STATUS_TAG_TOO_LONG;
448
449     status = _notmuch_message_remove_term (message, "tag", tag);
450     if (status) {
451         INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
452                         status);
453     }
454
455     _notmuch_message_sync (message);
456
457     return NOTMUCH_STATUS_SUCCESS;
458 }
459
460 void
461 notmuch_message_destroy (notmuch_message_t *message)
462 {
463     talloc_free (message);
464 }