]> git.notmuchmail.org Git - notmuch/blob - lib/message.cc
notmuch.h: Fix a couple of typos in the documentation.
[notmuch] / lib / 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 <stdint.h>
25
26 #include <gmime/gmime.h>
27
28 #include <xapian.h>
29
30 struct _notmuch_message {
31     notmuch_database_t *notmuch;
32     Xapian::docid doc_id;
33     int frozen;
34     char *message_id;
35     char *thread_id;
36     char *in_reply_to;
37     char *filename;
38     notmuch_message_file_t *message_file;
39     notmuch_message_list_t *replies;
40     unsigned long flags;
41
42     Xapian::Document doc;
43 };
44
45 /* "128 bits of thread-id ought to be enough for anybody" */
46 #define NOTMUCH_THREAD_ID_BITS   128
47 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
48 typedef struct _thread_id {
49     char str[NOTMUCH_THREAD_ID_DIGITS + 1];
50 } thread_id_t;
51
52 /* We end up having to call the destructor explicitly because we had
53  * to use "placement new" in order to initialize C++ objects within a
54  * block that we allocated with talloc. So C++ is making talloc
55  * slightly less simple to use, (we wouldn't need
56  * talloc_set_destructor at all otherwise).
57  */
58 static int
59 _notmuch_message_destructor (notmuch_message_t *message)
60 {
61     message->doc.~Document ();
62
63     return 0;
64 }
65
66 /* Create a new notmuch_message_t object for an existing document in
67  * the database.
68  *
69  * Here, 'talloc owner' is an optional talloc context to which the new
70  * message will belong. This allows for the caller to not bother
71  * calling notmuch_message_destroy on the message, and no that all
72  * memory will be reclaimed with 'talloc_owner' is free. The caller
73  * still can call notmuch_message_destroy when finished with the
74  * message if desired.
75  *
76  * The 'talloc_owner' argument can also be NULL, in which case the
77  * caller *is* responsible for calling notmuch_message_destroy.
78  *
79  * If no document exists in the database with document ID of 'doc_id'
80  * then this function returns NULL and optionally sets *status to
81  * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
82  *
83  * This function can also fail to due lack of available memory,
84  * returning NULL and optionally setting *status to
85  * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
86  *
87  * The caller can pass NULL for status if uninterested in
88  * distinguishing these two cases.
89  */
90 notmuch_message_t *
91 _notmuch_message_create (const void *talloc_owner,
92                          notmuch_database_t *notmuch,
93                          unsigned int doc_id,
94                          notmuch_private_status_t *status)
95 {
96     notmuch_message_t *message;
97
98     if (status)
99         *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
100
101     message = talloc (talloc_owner, notmuch_message_t);
102     if (unlikely (message == NULL)) {
103         if (status)
104             *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
105         return NULL;
106     }
107
108     message->notmuch = notmuch;
109     message->doc_id = doc_id;
110
111     message->frozen = 0;
112     message->flags = 0;
113
114     /* Each of these will be lazily created as needed. */
115     message->message_id = NULL;
116     message->thread_id = NULL;
117     message->in_reply_to = NULL;
118     message->filename = NULL;
119     message->message_file = NULL;
120
121     message->replies = _notmuch_message_list_create (message);
122     if (unlikely (message->replies == NULL)) {
123         if (status)
124             *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
125         return NULL;
126     }
127
128     /* This is C++'s creepy "placement new", which is really just an
129      * ugly way to call a constructor for a pre-allocated object. So
130      * it's really not an error to not be checking for OUT_OF_MEMORY
131      * here, since this "new" isn't actually allocating memory. This
132      * is language-design comedy of the wrong kind. */
133
134     new (&message->doc) Xapian::Document;
135
136     talloc_set_destructor (message, _notmuch_message_destructor);
137
138     try {
139         message->doc = notmuch->xapian_db->get_document (doc_id);
140     } catch (const Xapian::DocNotFoundError &error) {
141         talloc_free (message);
142         if (status)
143             *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
144         return NULL;
145     }
146
147     return message;
148 }
149
150 /* Create a new notmuch_message_t object for a specific message ID,
151  * (which may or may not already exist in the database).
152  *
153  * The 'notmuch' database will be the talloc owner of the returned
154  * message.
155  *
156  * If there is already a document with message ID 'message_id' in the
157  * database, then the returned message can be used to query/modify the
158  * document. Otherwise, a new document will be inserted into the
159  * database before this function returns, (and *status will be set
160  * to NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND).
161  *
162  * If an error occurs, this function will return NULL and *status
163  * will be set as appropriate. (The status pointer argument must
164  * not be NULL.)
165  */
166 notmuch_message_t *
167 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
168                                         const char *message_id,
169                                         notmuch_private_status_t *status_ret)
170 {
171     notmuch_message_t *message;
172     Xapian::Document doc;
173     Xapian::WritableDatabase *db;
174     unsigned int doc_id;
175     char *term;
176
177     *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
178
179     message = notmuch_database_find_message (notmuch, message_id);
180     if (message)
181         return talloc_steal (notmuch, message);
182
183     term = talloc_asprintf (NULL, "%s%s",
184                             _find_prefix ("id"), message_id);
185     if (term == NULL) {
186         *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
187         return NULL;
188     }
189
190     if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
191         INTERNAL_ERROR ("Failure to ensure database is writable.");
192
193     db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
194     try {
195         doc.add_term (term, 0);
196         talloc_free (term);
197
198         doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
199
200         doc_id = db->add_document (doc);
201     } catch (const Xapian::Error &error) {
202         fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
203                  error.get_msg().c_str());
204         notmuch->exception_reported = TRUE;
205         *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
206         return NULL;
207     }
208
209     message = _notmuch_message_create (notmuch, notmuch,
210                                        doc_id, status_ret);
211
212     /* We want to inform the caller that we had to create a new
213      * document. */
214     if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
215         *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
216
217     return message;
218 }
219
220 const char *
221 notmuch_message_get_message_id (notmuch_message_t *message)
222 {
223     Xapian::TermIterator i;
224
225     if (message->message_id)
226         return message->message_id;
227
228     i = message->doc.termlist_begin ();
229     i.skip_to (_find_prefix ("id"));
230
231     if (i == message->doc.termlist_end ())
232         INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
233                         message->doc_id);
234
235     message->message_id = talloc_strdup (message, (*i).c_str () + 1);
236
237 #if DEBUG_DATABASE_SANITY
238     i++;
239
240     if (i != message->doc.termlist_end () &&
241         strncmp ((*i).c_str (), _find_prefix ("id"),
242                  strlen (_find_prefix ("id"))) == 0)
243     {
244         INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
245                         message->doc_id);
246     }
247 #endif
248
249     return message->message_id;
250 }
251
252 static void
253 _notmuch_message_ensure_message_file (notmuch_message_t *message)
254 {
255     const char *filename;
256
257     if (message->message_file)
258         return;
259
260     filename = notmuch_message_get_filename (message);
261     if (unlikely (filename == NULL))
262         return;
263
264     message->message_file = _notmuch_message_file_open_ctx (message, filename);
265 }
266
267 const char *
268 notmuch_message_get_header (notmuch_message_t *message, const char *header)
269 {
270     _notmuch_message_ensure_message_file (message);
271     if (message->message_file == NULL)
272         return NULL;
273
274     return notmuch_message_file_get_header (message->message_file, header);
275 }
276
277 /* Return the message ID from the In-Reply-To header of 'message'.
278  *
279  * Returns an empty string ("") if 'message' has no In-Reply-To
280  * header.
281  *
282  * Returns NULL if any error occurs.
283  */
284 const char *
285 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
286 {
287     const char *prefix = _find_prefix ("replyto");
288     int prefix_len = strlen (prefix);
289     Xapian::TermIterator i;
290     std::string in_reply_to;
291
292     if (message->in_reply_to)
293         return message->in_reply_to;
294
295     i = message->doc.termlist_begin ();
296     i.skip_to (prefix);
297
298     if (i != message->doc.termlist_end ())
299         in_reply_to = *i;
300
301     /* It's perfectly valid for a message to have no In-Reply-To
302      * header. For these cases, we return an empty string. */
303     if (i == message->doc.termlist_end () ||
304         strncmp (in_reply_to.c_str (), prefix, prefix_len))
305     {
306         message->in_reply_to = talloc_strdup (message, "");
307         return message->in_reply_to;
308     }
309
310     message->in_reply_to = talloc_strdup (message,
311                                           in_reply_to.c_str () + prefix_len);
312
313 #if DEBUG_DATABASE_SANITY
314     i++;
315
316     in_reply_to = *i;
317
318     if (i != message->doc.termlist_end () &&
319         strncmp ((*i).c_str (), prefix, prefix_len) == 0)
320     {
321        INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
322                         notmuch_message_get_message_id (message),
323                         message->in_reply_to,
324                         (*i).c_str () + prefix_len);
325     }
326 #endif
327
328     return message->in_reply_to;
329 }
330
331 const char *
332 notmuch_message_get_thread_id (notmuch_message_t *message)
333 {
334     const char *prefix = _find_prefix ("thread");
335     Xapian::TermIterator i;
336     std::string id;
337
338     /* This code is written with the assumption that "thread" has a
339      * single-character prefix. */
340     assert (strlen (prefix) == 1);
341
342     if (message->thread_id)
343         return message->thread_id;
344
345     i = message->doc.termlist_begin ();
346     i.skip_to (prefix);
347
348     if (i != message->doc.termlist_end ())
349         id = *i;
350
351     if (i == message->doc.termlist_end () || id[0] != *prefix)
352         INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
353                         message->doc_id);
354
355     message->thread_id = talloc_strdup (message, id.c_str () + 1);
356
357 #if DEBUG_DATABASE_SANITY
358     i++;
359     id = *i;
360
361     if (i != message->doc.termlist_end () && id[0] == *prefix)
362     {
363         INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
364                         notmuch_message_get_message_id (message),
365                         message->thread_id,
366                         id.c_str () + 1);
367     }
368 #endif
369
370     return message->thread_id;
371 }
372
373 void
374 _notmuch_message_add_reply (notmuch_message_t *message,
375                             notmuch_message_node_t *reply)
376 {
377     _notmuch_message_list_append (message->replies, reply);
378 }
379
380 notmuch_messages_t *
381 notmuch_message_get_replies (notmuch_message_t *message)
382 {
383     return _notmuch_messages_create (message->replies);
384 }
385
386 /* Add an additional 'filename' for 'message'.
387  *
388  * This change will not be reflected in the database until the next
389  * call to _notmuch_message_set_sync. */
390 notmuch_status_t
391 _notmuch_message_add_filename (notmuch_message_t *message,
392                                const char *filename)
393 {
394     notmuch_status_t status;
395     void *local = talloc_new (message);
396     char *direntry;
397
398     if (message->filename) {
399         talloc_free (message->filename);
400         message->filename = NULL;
401     }
402
403     if (filename == NULL)
404         INTERNAL_ERROR ("Message filename cannot be NULL.");
405
406     status = _notmuch_database_filename_to_direntry (local,
407                                                      message->notmuch,
408                                                      filename, &direntry);
409     if (status)
410         return status;
411
412     _notmuch_message_add_term (message, "file-direntry", direntry);
413
414     talloc_free (local);
415
416     return NOTMUCH_STATUS_SUCCESS;
417 }
418
419 char *
420 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
421 {
422     return talloc_strdup (message, message->doc.get_data ().c_str ());
423 }
424
425 void
426 _notmuch_message_clear_data (notmuch_message_t *message)
427 {
428     message->doc.set_data ("");
429 }
430
431 const char *
432 notmuch_message_get_filename (notmuch_message_t *message)
433 {
434     const char *prefix = _find_prefix ("file-direntry");
435     int prefix_len = strlen (prefix);
436     Xapian::TermIterator i;
437     char *direntry, *colon;
438     const char *db_path, *directory, *basename;
439     unsigned int directory_id;
440     void *local = talloc_new (message);
441
442     if (message->filename)
443         return message->filename;
444
445     i = message->doc.termlist_begin ();
446     i.skip_to (prefix);
447
448     if (i != message->doc.termlist_end ())
449         direntry = talloc_strdup (local, (*i).c_str ());
450
451     if (i == message->doc.termlist_end () ||
452         strncmp (direntry, prefix, prefix_len))
453     {
454         /* A message document created by an old version of notmuch
455          * (prior to rename support) will have the filename in the
456          * data of the document rather than as a file-direntry term.
457          *
458          * It would be nice to do the upgrade of the document directly
459          * here, but the database is likely open in read-only mode. */
460         const char *data;
461
462         data = message->doc.get_data ().c_str ();
463
464         if (data == NULL)
465             INTERNAL_ERROR ("message with no filename");
466
467         message->filename = talloc_strdup (message, data);
468
469         return message->filename;
470     }
471
472     direntry += prefix_len;
473
474     directory_id = strtol (direntry, &colon, 10);
475
476     if (colon == NULL || *colon != ':')
477         INTERNAL_ERROR ("malformed direntry");
478
479     basename = colon + 1;
480
481     *colon = '\0';
482
483     db_path = notmuch_database_get_path (message->notmuch);
484
485     directory = _notmuch_database_get_directory_path (local,
486                                                       message->notmuch,
487                                                       directory_id);
488
489     if (strlen (directory))
490         message->filename = talloc_asprintf (message, "%s/%s/%s",
491                                              db_path, directory, basename);
492     else
493         message->filename = talloc_asprintf (message, "%s/%s",
494                                              db_path, basename);
495     talloc_free ((void *) directory);
496
497     talloc_free (local);
498
499     return message->filename;
500 }
501
502 notmuch_bool_t
503 notmuch_message_get_flag (notmuch_message_t *message,
504                           notmuch_message_flag_t flag)
505 {
506     return message->flags & (1 << flag);
507 }
508
509 void
510 notmuch_message_set_flag (notmuch_message_t *message,
511                           notmuch_message_flag_t flag, notmuch_bool_t enable)
512 {
513     if (enable)
514         message->flags |= (1 << flag);
515     else
516         message->flags &= ~(1 << flag);
517 }
518
519 time_t
520 notmuch_message_get_date (notmuch_message_t *message)
521 {
522     std::string value;
523
524     try {
525         value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
526     } catch (Xapian::Error &error) {
527         INTERNAL_ERROR ("Failed to read timestamp value from document.");
528         return 0;
529     }
530
531     return Xapian::sortable_unserialise (value);
532 }
533
534 notmuch_tags_t *
535 notmuch_message_get_tags (notmuch_message_t *message)
536 {
537     Xapian::TermIterator i, end;
538     i = message->doc.termlist_begin();
539     end = message->doc.termlist_end();
540     return _notmuch_convert_tags(message, i, end);
541 }
542
543 void
544 _notmuch_message_set_date (notmuch_message_t *message,
545                            const char *date)
546 {
547     time_t time_value;
548
549     /* GMime really doesn't want to see a NULL date, so protect its
550      * sensibilities. */
551     if (date == NULL || *date == '\0')
552         time_value = 0;
553     else
554         time_value = g_mime_utils_header_decode_date (date, NULL);
555
556     message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
557                             Xapian::sortable_serialise (time_value));
558 }
559
560 static void
561 thread_id_generate (thread_id_t *thread_id)
562 {
563     static int seeded = 0;
564     FILE *dev_random;
565     uint32_t value;
566     char *s;
567     int i;
568
569     if (! seeded) {
570         dev_random = fopen ("/dev/random", "r");
571         if (dev_random == NULL) {
572             srand (time (NULL));
573         } else {
574             fread ((void *) &value, sizeof (value), 1, dev_random);
575             srand (value);
576             fclose (dev_random);
577         }
578         seeded = 1;
579     }
580
581     s = thread_id->str;
582     for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
583         value = rand ();
584         sprintf (s, "%08x", value);
585         s += 8;
586     }
587 }
588
589 void
590 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
591 {
592     /* If not part of any existing thread, generate a new thread_id. */
593     thread_id_t thread_id;
594
595     thread_id_generate (&thread_id);
596     _notmuch_message_add_term (message, "thread", thread_id.str);
597 }
598
599 /* Synchronize changes made to message->doc out into the database. */
600 void
601 _notmuch_message_sync (notmuch_message_t *message)
602 {
603     Xapian::WritableDatabase *db;
604
605     if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
606         return;
607
608     db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
609     db->replace_document (message->doc_id, message->doc);
610 }
611
612 /* Ensure that 'message' is not holding any file object open. Future
613  * calls to various functions will still automatically open the
614  * message file as needed.
615  */
616 void
617 _notmuch_message_close (notmuch_message_t *message)
618 {
619     if (message->message_file) {
620         notmuch_message_file_close (message->message_file);
621         message->message_file = NULL;
622     }
623 }
624
625 /* Add a name:value term to 'message', (the actual term will be
626  * encoded by prefixing the value with a short prefix). See
627  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
628  * names to prefix values.
629  *
630  * This change will not be reflected in the database until the next
631  * call to _notmuch_message_set_sync. */
632 notmuch_private_status_t
633 _notmuch_message_add_term (notmuch_message_t *message,
634                            const char *prefix_name,
635                            const char *value)
636 {
637
638     char *term;
639
640     if (value == NULL)
641         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
642
643     term = talloc_asprintf (message, "%s%s",
644                             _find_prefix (prefix_name), value);
645
646     if (strlen (term) > NOTMUCH_TERM_MAX)
647         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
648
649     message->doc.add_term (term, 0);
650
651     talloc_free (term);
652
653     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
654 }
655
656 /* Parse 'text' and add a term to 'message' for each parsed word. Each
657  * term will be added both prefixed (if prefix_name is not NULL) and
658  * also unprefixed). */
659 notmuch_private_status_t
660 _notmuch_message_gen_terms (notmuch_message_t *message,
661                             const char *prefix_name,
662                             const char *text)
663 {
664     Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
665
666     if (text == NULL)
667         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
668
669     term_gen->set_document (message->doc);
670
671     if (prefix_name) {
672         const char *prefix = _find_prefix (prefix_name);
673
674         term_gen->index_text (text, 1, prefix);
675     }
676
677     term_gen->index_text (text);
678
679     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
680 }
681
682 /* Remove a name:value term from 'message', (the actual term will be
683  * encoded by prefixing the value with a short prefix). See
684  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
685  * names to prefix values.
686  *
687  * This change will not be reflected in the database until the next
688  * call to _notmuch_message_set_sync. */
689 notmuch_private_status_t
690 _notmuch_message_remove_term (notmuch_message_t *message,
691                               const char *prefix_name,
692                               const char *value)
693 {
694     char *term;
695
696     if (value == NULL)
697         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
698
699     term = talloc_asprintf (message, "%s%s",
700                             _find_prefix (prefix_name), value);
701
702     if (strlen (term) > NOTMUCH_TERM_MAX)
703         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
704
705     try {
706         message->doc.remove_term (term);
707     } catch (const Xapian::InvalidArgumentError) {
708         /* We'll let the philosopher's try to wrestle with the
709          * question of whether failing to remove that which was not
710          * there in the first place is failure. For us, we'll silently
711          * consider it all good. */
712     }
713
714     talloc_free (term);
715
716     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
717 }
718
719 notmuch_status_t
720 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
721 {
722     notmuch_private_status_t private_status;
723     notmuch_status_t status;
724
725     status = _notmuch_database_ensure_writable (message->notmuch);
726     if (status)
727         return status;
728
729     if (tag == NULL)
730         return NOTMUCH_STATUS_NULL_POINTER;
731
732     if (strlen (tag) > NOTMUCH_TAG_MAX)
733         return NOTMUCH_STATUS_TAG_TOO_LONG;
734
735     private_status = _notmuch_message_add_term (message, "tag", tag);
736     if (private_status) {
737         INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
738                         private_status);
739     }
740
741     if (! message->frozen)
742         _notmuch_message_sync (message);
743
744     return NOTMUCH_STATUS_SUCCESS;
745 }
746
747 notmuch_status_t
748 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
749 {
750     notmuch_private_status_t private_status;
751     notmuch_status_t status;
752
753     status = _notmuch_database_ensure_writable (message->notmuch);
754     if (status)
755         return status;
756
757     if (tag == NULL)
758         return NOTMUCH_STATUS_NULL_POINTER;
759
760     if (strlen (tag) > NOTMUCH_TAG_MAX)
761         return NOTMUCH_STATUS_TAG_TOO_LONG;
762
763     private_status = _notmuch_message_remove_term (message, "tag", tag);
764     if (private_status) {
765         INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
766                         private_status);
767     }
768
769     if (! message->frozen)
770         _notmuch_message_sync (message);
771
772     return NOTMUCH_STATUS_SUCCESS;
773 }
774
775 notmuch_status_t
776 notmuch_message_remove_all_tags (notmuch_message_t *message)
777 {
778     notmuch_private_status_t private_status;
779     notmuch_status_t status;
780     notmuch_tags_t *tags;
781     const char *tag;
782
783     status = _notmuch_database_ensure_writable (message->notmuch);
784     if (status)
785         return status;
786
787     for (tags = notmuch_message_get_tags (message);
788          notmuch_tags_has_more (tags);
789          notmuch_tags_advance (tags))
790     {
791         tag = notmuch_tags_get (tags);
792
793         private_status = _notmuch_message_remove_term (message, "tag", tag);
794         if (private_status) {
795             INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
796                             private_status);
797         }
798     }
799
800     if (! message->frozen)
801         _notmuch_message_sync (message);
802
803     return NOTMUCH_STATUS_SUCCESS;
804 }
805
806 notmuch_status_t
807 notmuch_message_freeze (notmuch_message_t *message)
808 {
809     notmuch_status_t status;
810
811     status = _notmuch_database_ensure_writable (message->notmuch);
812     if (status)
813         return status;
814
815     message->frozen++;
816
817     return NOTMUCH_STATUS_SUCCESS;
818 }
819
820 notmuch_status_t
821 notmuch_message_thaw (notmuch_message_t *message)
822 {
823     notmuch_status_t status;
824
825     status = _notmuch_database_ensure_writable (message->notmuch);
826     if (status)
827         return status;
828
829     if (message->frozen > 0) {
830         message->frozen--;
831         if (message->frozen == 0)
832             _notmuch_message_sync (message);
833         return NOTMUCH_STATUS_SUCCESS;
834     } else {
835         return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
836     }
837 }
838
839 void
840 notmuch_message_destroy (notmuch_message_t *message)
841 {
842     talloc_free (message);
843 }