]> git.notmuchmail.org Git - notmuch/blob - message.cc
Add an INTERNAL_ERROR macro and use it for all internal errors.
[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.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 *filename;
31     Xapian::Document doc;
32 };
33
34 typedef struct _notmuch_terms {
35     char prefix_char;
36     Xapian::TermIterator iterator;
37     Xapian::TermIterator iterator_end;
38 } notmuch_terms_t;
39
40 struct _notmuch_tags {
41     notmuch_terms_t terms;
42 };
43
44 struct _notmuch_thread_ids {
45     notmuch_terms_t terms;
46 };
47
48 /* "128 bits of thread-id ought to be enough for anybody" */
49 #define NOTMUCH_THREAD_ID_BITS   128
50 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
51 typedef struct _thread_id {
52     char str[NOTMUCH_THREAD_ID_DIGITS + 1];
53 } thread_id_t;
54
55 /* We end up having to call the destructor explicitly because we had
56  * to use "placement new" in order to initialize C++ objects within a
57  * block that we allocated with talloc. So C++ is making talloc
58  * slightly less simple to use, (we wouldn't need
59  * talloc_set_destructor at all otherwise).
60  */
61 static int
62 _notmuch_message_destructor (notmuch_message_t *message)
63 {
64     message->doc.~Document ();
65
66     return 0;
67 }
68
69 /* Create a new notmuch_message_t object for an existing document in
70  * the database.
71  *
72  * Here, 'talloc owner' is an optional talloc context to which the new
73  * message will belong. This allows for the caller to not bother
74  * calling notmuch_message_destroy on the message, and no that all
75  * memory will be reclaimed with 'talloc_owner' is free. The caller
76  * still can call notmuch_message_destroy when finished with the
77  * message if desired.
78  *
79  * The 'talloc_owner' argument can also be NULL, in which case the
80  * caller *is* responsible for calling notmuch_message_destroy.
81  *
82  * If no document exists in the database with document ID of 'doc_id'
83  * then this function returns NULL and sets *status to
84  * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
85  *
86  * This function can also fail to due lack of available memory,
87  * returning NULL and optionally setting *status to
88  * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY. Caller can pass NULL
89  * for status if uninterested in distinguishing these two cases.
90  */
91 notmuch_message_t *
92 _notmuch_message_create (const void *talloc_owner,
93                          notmuch_database_t *notmuch,
94                          unsigned int doc_id,
95                          notmuch_private_status_t *status)
96 {
97     notmuch_message_t *message;
98
99     if (status)
100         *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
101
102     message = talloc (talloc_owner, notmuch_message_t);
103     if (unlikely (message == NULL)) {
104         if (status)
105             *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
106         return NULL;
107     }
108
109     message->notmuch = notmuch;
110     message->doc_id = doc_id;
111     message->message_id = NULL; /* lazily created */
112     message->filename = NULL; /* lazily created */
113
114     /* This is C++'s creepy "placement new", which is really just an
115      * ugly way to call a constructor for a pre-allocated object. So
116      * it's really not an error to not be checking for OUT_OF_MEMORY
117      * here, since this "new" isn't actually allocating memory. This
118      * is language-design comedy of the wrong kind. */
119
120     new (&message->doc) Xapian::Document;
121
122     talloc_set_destructor (message, _notmuch_message_destructor);
123
124     try {
125         message->doc = notmuch->xapian_db->get_document (doc_id);
126     } catch (const Xapian::DocNotFoundError &error) {
127         talloc_free (message);
128         if (status)
129             *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
130         return NULL;
131     }
132
133     return message;
134 }
135
136 /* Create a new notmuch_message_t object for a specific message ID,
137  * (which may or may not already exist in the databas).
138  *
139  * Here, 'talloc owner' is an optional talloc context to which the new
140  * message will belong. This allows for the caller to not bother
141  * calling notmuch_message_destroy on the message, and no that all
142  * memory will be reclaimed with 'talloc_owner' is free. The caller
143  * still can call notmuch_message_destroy when finished with the
144  * message if desired.
145  *
146  * The 'talloc_owner' argument can also be NULL, in which case the
147  * caller *is* responsible for calling notmuch_message_destroy.
148  *
149  * If there is already a document with message ID 'message_id' in the
150  * database, then the returned message can be used to query/modify the
151  * document. Otherwise, a new document will be inserted into the
152  * database before this function returns.
153  *
154  * If an error occurs, this function will return NULL and *status
155  * will be set as appropriate. (The status pointer argument must
156  * not be NULL.)
157  */
158 notmuch_message_t *
159 _notmuch_message_create_for_message_id (const void *talloc_owner,
160                                         notmuch_database_t *notmuch,
161                                         const char *message_id,
162                                         notmuch_status_t *status)
163 {
164     notmuch_private_status_t private_status;
165     notmuch_message_t *message;
166     Xapian::Document doc;
167     unsigned int doc_id;
168     char *term;
169
170     *status = NOTMUCH_STATUS_SUCCESS;
171
172     message = notmuch_database_find_message (notmuch, message_id);
173     if (message)
174         return talloc_steal (talloc_owner, message);
175
176     term = talloc_asprintf (NULL, "%s%s",
177                             _find_prefix ("id"), message_id);
178     if (term == NULL) {
179         *status = NOTMUCH_STATUS_OUT_OF_MEMORY;
180         return NULL;
181     }
182
183     try {
184         doc.add_term (term);
185         talloc_free (term);
186
187         doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
188
189         doc_id = notmuch->xapian_db->add_document (doc);
190     } catch (const Xapian::Error &error) {
191         *status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
192         return NULL;
193     }
194
195     message = _notmuch_message_create (talloc_owner, notmuch,
196                                        doc_id, &private_status);
197
198     if (private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)
199     {
200         INTERNAL_ERROR ("Failed to find document immediately after adding it.\n");
201     }
202
203     *status = (notmuch_status_t) private_status;
204
205     return message;
206 }
207
208 const char *
209 notmuch_message_get_message_id (notmuch_message_t *message)
210 {
211     Xapian::TermIterator i;
212
213     if (message->message_id)
214         return message->message_id;
215
216     i = message->doc.termlist_begin ();
217     i.skip_to (_find_prefix ("id"));
218
219     if (i == message->doc.termlist_end ()) {
220         INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
221                         message->doc_id);
222     }
223
224     message->message_id = talloc_strdup (message, (*i).c_str () + 1);
225     return message->message_id;
226 }
227
228 /* Set the filename for 'message' to 'filename'.
229  *
230  * XXX: We should still figure out what we want to do for multiple
231  * files with identical message IDs. We will probably want to store a
232  * list of filenames here, (so that this will be "add_filename"
233  * instead of "set_filename"). Which would make this very similar to
234  * add_thread_ids.
235  *
236  * This change will not be reflected in the database until the next
237  * call to _notmuch_message_set_sync. */
238 void
239 _notmuch_message_set_filename (notmuch_message_t *message,
240                                const char *filename)
241 {
242     if (message->filename)
243         talloc_free (message->filename);
244     message->doc.set_data (filename);
245 }
246
247 const char *
248 notmuch_message_get_filename (notmuch_message_t *message)
249 {
250     std::string filename_str;
251
252     if (message->filename)
253         return message->filename;
254
255     filename_str = message->doc.get_data ();
256     message->filename = talloc_strdup (message, filename_str.c_str ());
257
258     return message->filename;
259 }
260
261 /* We end up having to call the destructors explicitly because we had
262  * to use "placement new" in order to initialize C++ objects within a
263  * block that we allocated with talloc. So C++ is making talloc
264  * slightly less simple to use, (we wouldn't need
265  * talloc_set_destructor at all otherwise).
266  */
267 static int
268 _notmuch_terms_destructor (notmuch_terms_t *terms)
269 {
270     terms->iterator.~TermIterator ();
271     terms->iterator_end.~TermIterator ();
272
273     return 0;
274 }
275
276 notmuch_terms_t *
277 _notmuch_terms_create (void *ctx,
278                        Xapian::Document doc,
279                        const char *prefix_name)
280 {
281     notmuch_terms_t *terms;
282     const char *prefix = _find_prefix (prefix_name);
283
284     /* Currently, notmuch_terms_t is written with the assumption that
285      * any prefix its derivatives use will be only a single
286      * character. */
287     assert (strlen (prefix) == 1);
288
289     terms = talloc (ctx, notmuch_terms_t);
290     if (unlikely (terms == NULL))
291         return NULL;
292
293     terms->prefix_char = *prefix;
294     new (&terms->iterator) Xapian::TermIterator;
295     new (&terms->iterator_end) Xapian::TermIterator;
296
297     talloc_set_destructor (terms, _notmuch_terms_destructor);
298
299     terms->iterator = doc.termlist_begin ();
300     terms->iterator.skip_to (prefix);
301     terms->iterator_end = doc.termlist_end ();
302
303     return terms;
304 }
305
306 /* The assertion is to ensure that 'type' is a derivative of
307  * notmuch_terms_t in that it contains a notmuch_terms_t as its first
308  * member. We do this by name of 'terms' as opposed to type, because
309  * that's as clever as I've been so far. */
310 #define _notmuch_terms_create_type(ctx, doc, prefix_name, type) \
311     (COMPILE_TIME_ASSERT(offsetof(type, terms) == 0),           \
312      (type *) _notmuch_terms_create (ctx, doc, prefix_name))
313
314 notmuch_tags_t *
315 notmuch_message_get_tags (notmuch_message_t *message)
316 {
317     return _notmuch_terms_create_type (message, message->doc, "tag",
318                                        notmuch_tags_t);
319 }
320
321 notmuch_thread_ids_t *
322 notmuch_message_get_thread_ids (notmuch_message_t *message)
323 {
324     return _notmuch_terms_create_type (message, message->doc, "thread",
325                                        notmuch_thread_ids_t);
326 }
327
328 void
329 _notmuch_message_set_date (notmuch_message_t *message,
330                            const char *date)
331 {
332     time_t time_value;
333
334     time_value = notmuch_parse_date (date, NULL);
335
336     message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
337                             Xapian::sortable_serialise (time_value));
338 }
339
340 void
341 _notmuch_message_add_thread_id (notmuch_message_t *message,
342                                 const char *thread_id)
343 {
344     _notmuch_message_add_term (message, "thread", thread_id);
345 }
346
347 static void
348 thread_id_generate (thread_id_t *thread_id)
349 {
350     static int seeded = 0;
351     FILE *dev_random;
352     uint32_t value;
353     char *s;
354     int i;
355
356     if (! seeded) {
357         dev_random = fopen ("/dev/random", "r");
358         if (dev_random == NULL) {
359             srand (time (NULL));
360         } else {
361             fread ((void *) &value, sizeof (value), 1, dev_random);
362             srand (value);
363             fclose (dev_random);
364         }
365         seeded = 1;
366     }
367
368     s = thread_id->str;
369     for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
370         value = rand ();
371         sprintf (s, "%08x", value);
372         s += 8;
373     }
374 }
375
376 void
377 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
378 {
379     /* If not part of any existing thread, generate a new thread_id. */
380     thread_id_t thread_id;
381
382     thread_id_generate (&thread_id);
383     _notmuch_message_add_term (message, "thread", thread_id.str);
384 }
385
386 /* Synchronize changes made to message->doc out into the database. */
387 void
388 _notmuch_message_sync (notmuch_message_t *message)
389 {
390     Xapian::WritableDatabase *db = message->notmuch->xapian_db;
391
392     db->replace_document (message->doc_id, message->doc);
393 }
394
395 /* Add a name:value term to 'message', (the actual term will be
396  * encoded by prefixing the value with a short prefix). See
397  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
398  * names to prefix values.
399  *
400  * This change will not be reflected in the database until the next
401  * call to _notmuch_message_set_sync. */
402 notmuch_private_status_t
403 _notmuch_message_add_term (notmuch_message_t *message,
404                            const char *prefix_name,
405                            const char *value)
406 {
407
408     char *term;
409
410     if (value == NULL)
411         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
412
413     term = talloc_asprintf (message, "%s%s",
414                             _find_prefix (prefix_name), value);
415
416     if (strlen (term) > NOTMUCH_TERM_MAX)
417         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
418
419     message->doc.add_term (term);
420
421     talloc_free (term);
422
423     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
424 }
425
426 /* Remove a name:value term from 'message', (the actual term will be
427  * encoded by prefixing the value with a short prefix). See
428  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
429  * names to prefix values.
430  *
431  * This change will not be reflected in the database until the next
432  * call to _notmuch_message_set_sync. */
433 notmuch_private_status_t
434 _notmuch_message_remove_term (notmuch_message_t *message,
435                               const char *prefix_name,
436                               const char *value)
437 {
438     char *term;
439
440     if (value == NULL)
441         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
442
443     term = talloc_asprintf (message, "%s%s",
444                             _find_prefix (prefix_name), value);
445
446     if (strlen (term) > NOTMUCH_TERM_MAX)
447         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
448
449     message->doc.remove_term (term);
450
451     talloc_free (term);
452
453     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
454 }
455
456 notmuch_status_t
457 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
458 {
459     notmuch_private_status_t status;
460
461     if (tag == NULL)
462         return NOTMUCH_STATUS_NULL_POINTER;
463
464     if (strlen (tag) > NOTMUCH_TAG_MAX)
465         return NOTMUCH_STATUS_TAG_TOO_LONG;
466
467     status = _notmuch_message_add_term (message, "tag", tag);
468     if (status) {
469         INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
470                         status);
471     }
472
473     _notmuch_message_sync (message);
474
475     return NOTMUCH_STATUS_SUCCESS;
476 }
477
478 notmuch_status_t
479 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
480 {
481     notmuch_private_status_t status;
482
483     if (tag == NULL)
484         return NOTMUCH_STATUS_NULL_POINTER;
485
486     if (strlen (tag) > NOTMUCH_TAG_MAX)
487         return NOTMUCH_STATUS_TAG_TOO_LONG;
488
489     status = _notmuch_message_remove_term (message, "tag", tag);
490     if (status) {
491         INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
492                         status);
493     }
494
495     _notmuch_message_sync (message);
496
497     return NOTMUCH_STATUS_SUCCESS;
498 }
499
500 void
501 notmuch_message_destroy (notmuch_message_t *message)
502 {
503     talloc_free (message);
504 }
505
506 notmuch_bool_t
507 _notmuch_terms_has_more (notmuch_terms_t *terms)
508 {
509     std::string s;
510
511     if (terms->iterator == terms->iterator_end)
512         return FALSE;
513
514     s = *terms->iterator;
515     if (! s.empty () && s[0] == terms->prefix_char)
516         return TRUE;
517     else
518         return FALSE;
519 }
520
521 const char *
522 _notmuch_terms_get (notmuch_terms_t *terms)
523 {
524     return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
525 }
526
527 void
528 _notmuch_terms_advance (notmuch_terms_t *terms)
529 {
530     terms->iterator++;
531 }
532
533 void
534 _notmuch_terms_destroy (notmuch_terms_t *terms)
535 {
536     talloc_free (terms);
537 }
538
539 notmuch_bool_t
540 notmuch_tags_has_more (notmuch_tags_t *tags)
541 {
542     return _notmuch_terms_has_more (&tags->terms);
543 }
544
545 const char *
546 notmuch_tags_get (notmuch_tags_t *tags)
547 {
548     return _notmuch_terms_get (&tags->terms);
549 }
550
551 void
552 notmuch_tags_advance (notmuch_tags_t *tags)
553 {
554     return _notmuch_terms_advance (&tags->terms);
555 }
556
557 void
558 notmuch_tags_destroy (notmuch_tags_t *tags)
559 {
560     return _notmuch_terms_destroy (&tags->terms);
561 }
562
563 notmuch_bool_t
564 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
565 {
566     return _notmuch_terms_has_more (&thread_ids->terms);
567 }
568
569 const char *
570 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
571 {
572     return _notmuch_terms_get (&thread_ids->terms);
573 }
574
575 void
576 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
577 {
578     return _notmuch_terms_advance (&thread_ids->terms);
579 }
580
581 void
582 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
583 {
584     return _notmuch_terms_destroy (&thread_ids->terms);
585 }