1 /* message.cc - Results of message-based searches from a notmuch database
3 * Copyright © 2009 Carl Worth
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
26 #include <gmime/gmime.h>
28 struct visible _notmuch_message {
29 notmuch_database_t *notmuch;
35 notmuch_string_list_t *tag_list;
36 notmuch_string_list_t *filename_term_list;
37 notmuch_string_list_t *filename_list;
39 notmuch_message_file_t *message_file;
40 notmuch_string_list_t *property_term_list;
41 notmuch_string_map_t *property_map;
42 notmuch_message_list_t *replies;
44 /* For flags that are initialized on-demand, lazy_flags indicates
45 * if each flag has been initialized. */
46 unsigned long lazy_flags;
48 /* Message document modified since last sync */
49 notmuch_bool_t modified;
52 Xapian::termcount termpos;
55 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
57 struct maildir_flag_tag {
60 notmuch_bool_t inverse;
63 /* ASCII ordered table of Maildir flags and associated tags */
64 static struct maildir_flag_tag flag2tag[] = {
65 { 'D', "draft", FALSE},
66 { 'F', "flagged", FALSE},
67 { 'P', "passed", FALSE},
68 { 'R', "replied", FALSE},
69 { 'S', "unread", TRUE }
72 /* We end up having to call the destructor explicitly because we had
73 * to use "placement new" in order to initialize C++ objects within a
74 * block that we allocated with talloc. So C++ is making talloc
75 * slightly less simple to use, (we wouldn't need
76 * talloc_set_destructor at all otherwise).
79 _notmuch_message_destructor (notmuch_message_t *message)
81 message->doc.~Document ();
86 static notmuch_message_t *
87 _notmuch_message_create_for_document (const void *talloc_owner,
88 notmuch_database_t *notmuch,
91 notmuch_private_status_t *status)
93 notmuch_message_t *message;
96 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
98 message = talloc (talloc_owner, notmuch_message_t);
99 if (unlikely (message == NULL)) {
101 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
105 message->notmuch = notmuch;
106 message->doc_id = doc_id;
110 message->lazy_flags = 0;
112 /* Each of these will be lazily created as needed. */
113 message->message_id = NULL;
114 message->thread_id = NULL;
115 message->in_reply_to = NULL;
116 message->tag_list = NULL;
117 message->filename_term_list = NULL;
118 message->filename_list = NULL;
119 message->message_file = NULL;
120 message->author = NULL;
121 message->property_term_list = NULL;
122 message->property_map = NULL;
124 message->replies = _notmuch_message_list_create (message);
125 if (unlikely (message->replies == NULL)) {
127 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
131 /* This is C++'s creepy "placement new", which is really just an
132 * ugly way to call a constructor for a pre-allocated object. So
133 * it's really not an error to not be checking for OUT_OF_MEMORY
134 * here, since this "new" isn't actually allocating memory. This
135 * is language-design comedy of the wrong kind. */
137 new (&message->doc) Xapian::Document;
139 talloc_set_destructor (message, _notmuch_message_destructor);
142 message->termpos = 0;
147 /* Create a new notmuch_message_t object for an existing document in
150 * Here, 'talloc owner' is an optional talloc context to which the new
151 * message will belong. This allows for the caller to not bother
152 * calling notmuch_message_destroy on the message, and know that all
153 * memory will be reclaimed when 'talloc_owner' is freed. The caller
154 * still can call notmuch_message_destroy when finished with the
155 * message if desired.
157 * The 'talloc_owner' argument can also be NULL, in which case the
158 * caller *is* responsible for calling notmuch_message_destroy.
160 * If no document exists in the database with document ID of 'doc_id'
161 * then this function returns NULL and optionally sets *status to
162 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
164 * This function can also fail to due lack of available memory,
165 * returning NULL and optionally setting *status to
166 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
168 * The caller can pass NULL for status if uninterested in
169 * distinguishing these two cases.
172 _notmuch_message_create (const void *talloc_owner,
173 notmuch_database_t *notmuch,
175 notmuch_private_status_t *status)
177 Xapian::Document doc;
180 doc = notmuch->xapian_db->get_document (doc_id);
181 } catch (const Xapian::DocNotFoundError &error) {
183 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
187 return _notmuch_message_create_for_document (talloc_owner, notmuch,
188 doc_id, doc, status);
191 /* Create a new notmuch_message_t object for a specific message ID,
192 * (which may or may not already exist in the database).
194 * The 'notmuch' database will be the talloc owner of the returned
197 * This function returns a valid notmuch_message_t whether or not
198 * there is already a document in the database with the given message
199 * ID. These two cases can be distinguished by the value of *status:
202 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
204 * There is already a document with message ID 'message_id' in the
205 * database. The returned message can be used to query/modify the
206 * document. The message may be a ghost message.
208 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
210 * No document with 'message_id' exists in the database. The
211 * returned message contains a newly created document (not yet
212 * added to the database) and a document ID that is known not to
213 * exist in the database. This message is "blank"; that is, it
214 * contains only a message ID and no other metadata. The caller
215 * can modify the message, and a call to _notmuch_message_sync
216 * will add the document to the database.
218 * If an error occurs, this function will return NULL and *status
219 * will be set as appropriate. (The status pointer argument must
223 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
224 const char *message_id,
225 notmuch_private_status_t *status_ret)
227 notmuch_message_t *message;
228 Xapian::Document doc;
232 *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
236 return talloc_steal (notmuch, message);
237 else if (*status_ret)
240 /* If the message ID is too long, substitute its sha1 instead. */
241 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
242 message_id = _notmuch_message_id_compressed (message, message_id);
244 term = talloc_asprintf (NULL, "%s%s",
245 _find_prefix ("id"), message_id);
247 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
251 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
252 INTERNAL_ERROR ("Failure to ensure database is writable.");
255 doc.add_term (term, 0);
258 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
260 doc_id = _notmuch_database_generate_doc_id (notmuch);
261 } catch (const Xapian::Error &error) {
262 _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred creating message: %s\n",
263 error.get_msg().c_str());
264 notmuch->exception_reported = TRUE;
265 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
269 message = _notmuch_message_create_for_document (notmuch, notmuch,
270 doc_id, doc, status_ret);
272 /* We want to inform the caller that we had to create a new
274 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
275 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
281 _notmuch_message_get_term (notmuch_message_t *message,
282 Xapian::TermIterator &i, Xapian::TermIterator &end,
285 int prefix_len = strlen (prefix);
293 const std::string &term = *i;
294 if (strncmp (term.c_str(), prefix, prefix_len))
297 value = talloc_strdup (message, term.c_str() + prefix_len);
299 #if DEBUG_DATABASE_SANITY
302 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
303 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
304 message->doc_id, prefix, value,
305 (*i).c_str () + prefix_len);
313 _notmuch_message_ensure_metadata (notmuch_message_t *message)
315 Xapian::TermIterator i, end;
316 const char *thread_prefix = _find_prefix ("thread"),
317 *tag_prefix = _find_prefix ("tag"),
318 *id_prefix = _find_prefix ("id"),
319 *type_prefix = _find_prefix ("type"),
320 *filename_prefix = _find_prefix ("file-direntry"),
321 *property_prefix = _find_prefix ("property"),
322 *replyto_prefix = _find_prefix ("replyto");
324 /* We do this all in a single pass because Xapian decompresses the
325 * term list every time you iterate over it. Thus, while this is
326 * slightly more costly than looking up individual fields if only
327 * one field of the message object is actually used, it's a huge
328 * win as more fields are used. */
330 i = message->doc.termlist_begin ();
331 end = message->doc.termlist_end ();
334 if (!message->thread_id)
336 _notmuch_message_get_term (message, i, end, thread_prefix);
339 assert (strcmp (thread_prefix, tag_prefix) < 0);
340 if (!message->tag_list) {
342 _notmuch_database_get_terms_with_prefix (message, i, end,
344 _notmuch_string_list_sort (message->tag_list);
348 assert (strcmp (tag_prefix, id_prefix) < 0);
349 if (!message->message_id)
350 message->message_id =
351 _notmuch_message_get_term (message, i, end, id_prefix);
353 /* Get document type */
354 assert (strcmp (id_prefix, type_prefix) < 0);
355 if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {
356 i.skip_to (type_prefix);
357 /* "T" is the prefix "type" fields. See
358 * BOOLEAN_PREFIX_INTERNAL. */
360 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
361 else if (*i == "Tghost")
362 NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
364 INTERNAL_ERROR ("Message without type term");
365 NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
368 /* Get filename list. Here we get only the terms. We lazily
369 * expand them to full file names when needed in
370 * _notmuch_message_ensure_filename_list. */
371 assert (strcmp (type_prefix, filename_prefix) < 0);
372 if (!message->filename_term_list && !message->filename_list)
373 message->filename_term_list =
374 _notmuch_database_get_terms_with_prefix (message, i, end,
378 /* Get property terms. Mimic the setup with filenames above */
379 assert (strcmp (filename_prefix, property_prefix) < 0);
380 if (!message->property_map && !message->property_term_list)
381 message->property_term_list =
382 _notmuch_database_get_terms_with_prefix (message, i, end,
386 assert (strcmp (property_prefix, replyto_prefix) < 0);
387 if (!message->in_reply_to)
388 message->in_reply_to =
389 _notmuch_message_get_term (message, i, end, replyto_prefix);
392 /* It's perfectly valid for a message to have no In-Reply-To
393 * header. For these cases, we return an empty string. */
394 if (!message->in_reply_to)
395 message->in_reply_to = talloc_strdup (message, "");
399 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
400 const char *prefix_name)
402 if (strcmp ("thread", prefix_name) == 0) {
403 talloc_free (message->thread_id);
404 message->thread_id = NULL;
407 if (strcmp ("tag", prefix_name) == 0) {
408 talloc_unlink (message, message->tag_list);
409 message->tag_list = NULL;
412 if (strcmp ("type", prefix_name) == 0) {
413 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
414 NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
417 if (strcmp ("file-direntry", prefix_name) == 0) {
418 talloc_free (message->filename_term_list);
419 talloc_free (message->filename_list);
420 message->filename_term_list = message->filename_list = NULL;
423 if (strcmp ("property", prefix_name) == 0) {
425 if (message->property_term_list)
426 talloc_free (message->property_term_list);
427 message->property_term_list = NULL;
429 if (message->property_map)
430 talloc_unlink (message, message->property_map);
432 message->property_map = NULL;
435 if (strcmp ("replyto", prefix_name) == 0) {
436 talloc_free (message->in_reply_to);
437 message->in_reply_to = NULL;
442 _notmuch_message_get_doc_id (notmuch_message_t *message)
444 return message->doc_id;
448 notmuch_message_get_message_id (notmuch_message_t *message)
450 if (!message->message_id)
451 _notmuch_message_ensure_metadata (message);
452 if (!message->message_id)
453 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
455 return message->message_id;
459 _notmuch_message_ensure_message_file (notmuch_message_t *message)
461 const char *filename;
463 if (message->message_file)
466 filename = notmuch_message_get_filename (message);
467 if (unlikely (filename == NULL))
470 message->message_file = _notmuch_message_file_open_ctx (
471 _notmuch_message_database (message), message, filename);
475 notmuch_message_get_header (notmuch_message_t *message, const char *header)
477 Xapian::valueno slot = Xapian::BAD_VALUENO;
479 /* Fetch header from the appropriate xapian value field if
481 if (strcasecmp (header, "from") == 0)
482 slot = NOTMUCH_VALUE_FROM;
483 else if (strcasecmp (header, "subject") == 0)
484 slot = NOTMUCH_VALUE_SUBJECT;
485 else if (strcasecmp (header, "message-id") == 0)
486 slot = NOTMUCH_VALUE_MESSAGE_ID;
488 if (slot != Xapian::BAD_VALUENO) {
490 std::string value = message->doc.get_value (slot);
492 /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then
493 * empty values indicate empty headers. If we don't, then
494 * it could just mean we didn't record the header. */
495 if ((message->notmuch->features &
496 NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) ||
498 return talloc_strdup (message, value.c_str ());
500 } catch (Xapian::Error &error) {
501 _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading header: %s\n",
502 error.get_msg().c_str());
503 message->notmuch->exception_reported = TRUE;
508 /* Otherwise fall back to parsing the file */
509 _notmuch_message_ensure_message_file (message);
510 if (message->message_file == NULL)
513 return _notmuch_message_file_get_header (message->message_file, header);
516 /* Return the message ID from the In-Reply-To header of 'message'.
518 * Returns an empty string ("") if 'message' has no In-Reply-To
521 * Returns NULL if any error occurs.
524 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
526 if (!message->in_reply_to)
527 _notmuch_message_ensure_metadata (message);
528 return message->in_reply_to;
532 notmuch_message_get_thread_id (notmuch_message_t *message)
534 if (!message->thread_id)
535 _notmuch_message_ensure_metadata (message);
536 if (!message->thread_id)
537 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
539 return message->thread_id;
543 _notmuch_message_add_reply (notmuch_message_t *message,
544 notmuch_message_t *reply)
546 _notmuch_message_list_add_message (message->replies, reply);
550 notmuch_message_get_replies (notmuch_message_t *message)
552 return _notmuch_messages_create (message->replies);
556 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
558 Xapian::TermIterator i;
559 size_t prefix_len = strlen (prefix);
562 i = message->doc.termlist_begin ();
565 /* Terminate loop when no terms remain with desired prefix. */
566 if (i == message->doc.termlist_end () ||
567 strncmp ((*i).c_str (), prefix, prefix_len))
571 message->doc.remove_term ((*i));
572 message->modified = TRUE;
573 } catch (const Xapian::InvalidArgumentError) {
574 /* Ignore failure to remove non-existent term. */
579 /* Return true if p points at "new" or "cur". */
580 static bool is_maildir (const char *p)
582 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
585 /* Add "folder:" term for directory. */
586 static notmuch_status_t
587 _notmuch_message_add_folder_terms (notmuch_message_t *message,
588 const char *directory)
592 folder = talloc_strdup (NULL, directory);
594 return NOTMUCH_STATUS_OUT_OF_MEMORY;
597 * If the message file is in a leaf directory named "new" or
598 * "cur", presume maildir and index the parent directory. Thus a
599 * "folder:" prefix search matches messages in the specified
600 * maildir folder, i.e. in the specified directory and its "new"
601 * and "cur" subdirectories.
603 * Note that this means the "folder:" prefix can't be used for
604 * distinguishing between message files in "new" or "cur". The
605 * "path:" prefix needs to be used for that.
607 * Note the deliberate difference to _filename_is_in_maildir(). We
608 * don't want to index different things depending on the existence
609 * or non-existence of all maildir sibling directories "new",
610 * "cur", and "tmp". Doing so would be surprising, and difficult
611 * for the user to fix in case all subdirectories were not in
612 * place during indexing.
614 last = strrchr (folder, '/');
616 if (is_maildir (last + 1))
618 } else if (is_maildir (folder)) {
622 _notmuch_message_add_term (message, "folder", folder);
624 talloc_free (folder);
626 return NOTMUCH_STATUS_SUCCESS;
629 #define RECURSIVE_SUFFIX "/**"
631 /* Add "path:" terms for directory. */
632 static notmuch_status_t
633 _notmuch_message_add_path_terms (notmuch_message_t *message,
634 const char *directory)
636 /* Add exact "path:" term. */
637 _notmuch_message_add_term (message, "path", directory);
639 if (strlen (directory)) {
642 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
644 return NOTMUCH_STATUS_OUT_OF_MEMORY;
646 /* Add recursive "path:" terms for directory and all parents. */
647 for (p = path + strlen (path) - 1; p > path; p--) {
649 strcpy (p, RECURSIVE_SUFFIX);
650 _notmuch_message_add_term (message, "path", path);
657 /* Recursive all-matching path:** for consistency. */
658 _notmuch_message_add_term (message, "path", "**");
660 return NOTMUCH_STATUS_SUCCESS;
663 /* Add directory based terms for all filenames of the message. */
664 static notmuch_status_t
665 _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
667 const char *direntry_prefix = _find_prefix ("file-direntry");
668 int direntry_prefix_len = strlen (direntry_prefix);
669 Xapian::TermIterator i = message->doc.termlist_begin ();
670 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
672 for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {
673 unsigned int directory_id;
674 const char *direntry, *directory;
676 const std::string &term = *i;
678 /* Terminate loop at first term without desired prefix. */
679 if (strncmp (term.c_str (), direntry_prefix, direntry_prefix_len))
682 /* Indicate that there are filenames remaining. */
683 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
685 direntry = term.c_str ();
686 direntry += direntry_prefix_len;
688 directory_id = strtol (direntry, &colon, 10);
690 if (colon == NULL || *colon != ':')
691 INTERNAL_ERROR ("malformed direntry");
693 directory = _notmuch_database_get_directory_path (ctx,
697 _notmuch_message_add_folder_terms (message, directory);
698 _notmuch_message_add_path_terms (message, directory);
704 /* Add an additional 'filename' for 'message'.
706 * This change will not be reflected in the database until the next
707 * call to _notmuch_message_sync. */
709 _notmuch_message_add_filename (notmuch_message_t *message,
710 const char *filename)
712 const char *relative, *directory;
713 notmuch_status_t status;
714 void *local = talloc_new (message);
717 if (filename == NULL)
718 INTERNAL_ERROR ("Message filename cannot be NULL.");
720 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
721 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
722 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
724 relative = _notmuch_database_relative_path (message->notmuch, filename);
726 status = _notmuch_database_split_path (local, relative, &directory, NULL);
730 status = _notmuch_database_filename_to_direntry (
731 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
735 /* New file-direntry allows navigating to this message with
736 * notmuch_directory_get_child_files() . */
737 _notmuch_message_add_term (message, "file-direntry", direntry);
739 _notmuch_message_add_folder_terms (message, directory);
740 _notmuch_message_add_path_terms (message, directory);
744 return NOTMUCH_STATUS_SUCCESS;
747 /* Remove a particular 'filename' from 'message'.
749 * This change will not be reflected in the database until the next
750 * call to _notmuch_message_sync.
752 * If this message still has other filenames, returns
753 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
755 * Note: This function does not remove a document from the database,
756 * even if the specified filename is the only filename for this
757 * message. For that functionality, see
758 * notmuch_database_remove_message. */
760 _notmuch_message_remove_filename (notmuch_message_t *message,
761 const char *filename)
763 void *local = talloc_new (message);
765 notmuch_private_status_t private_status;
766 notmuch_status_t status;
768 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
769 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
770 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
772 status = _notmuch_database_filename_to_direntry (
773 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
774 if (status || !direntry)
777 /* Unlink this file from its parent directory. */
778 private_status = _notmuch_message_remove_term (message,
779 "file-direntry", direntry);
780 status = COERCE_STATUS (private_status,
781 "Unexpected error from _notmuch_message_remove_term");
785 /* Re-synchronize "folder:" and "path:" terms for this message. */
787 /* Remove all "folder:" terms. */
788 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
790 /* Remove all "path:" terms. */
791 _notmuch_message_remove_terms (message, _find_prefix ("path"));
793 /* Add back terms for all remaining filenames of the message. */
794 status = _notmuch_message_add_directory_terms (local, message);
801 /* Upgrade the "folder:" prefix from V1 to V2. */
802 #define FOLDER_PREFIX_V1 "XFOLDER"
803 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
805 _notmuch_message_upgrade_folder (notmuch_message_t *message)
807 /* Remove all old "folder:" terms. */
808 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
810 /* Remove all old "folder:" stemmed terms. */
811 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
813 /* Add new boolean "folder:" and "path:" terms. */
814 _notmuch_message_add_directory_terms (message, message);
818 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
820 return talloc_strdup (message, message->doc.get_data ().c_str ());
824 _notmuch_message_clear_data (notmuch_message_t *message)
826 message->doc.set_data ("");
827 message->modified = TRUE;
831 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
833 notmuch_string_node_t *node;
835 if (message->filename_list)
838 if (!message->filename_term_list)
839 _notmuch_message_ensure_metadata (message);
841 message->filename_list = _notmuch_string_list_create (message);
842 node = message->filename_term_list->head;
845 /* A message document created by an old version of notmuch
846 * (prior to rename support) will have the filename in the
847 * data of the document rather than as a file-direntry term.
849 * It would be nice to do the upgrade of the document directly
850 * here, but the database is likely open in read-only mode. */
853 data = message->doc.get_data ().c_str ();
856 INTERNAL_ERROR ("message with no filename");
858 _notmuch_string_list_append (message->filename_list, data);
863 for (; node; node = node->next) {
864 void *local = talloc_new (message);
865 const char *db_path, *directory, *basename, *filename;
866 char *colon, *direntry = NULL;
867 unsigned int directory_id;
869 direntry = node->string;
871 directory_id = strtol (direntry, &colon, 10);
873 if (colon == NULL || *colon != ':')
874 INTERNAL_ERROR ("malformed direntry");
876 basename = colon + 1;
880 db_path = notmuch_database_get_path (message->notmuch);
882 directory = _notmuch_database_get_directory_path (local,
886 if (strlen (directory))
887 filename = talloc_asprintf (message, "%s/%s/%s",
888 db_path, directory, basename);
890 filename = talloc_asprintf (message, "%s/%s",
893 _notmuch_string_list_append (message->filename_list, filename);
898 talloc_free (message->filename_term_list);
899 message->filename_term_list = NULL;
903 notmuch_message_get_filename (notmuch_message_t *message)
905 _notmuch_message_ensure_filename_list (message);
907 if (message->filename_list == NULL)
910 if (message->filename_list->head == NULL ||
911 message->filename_list->head->string == NULL)
913 INTERNAL_ERROR ("message with no filename");
916 return message->filename_list->head->string;
919 notmuch_filenames_t *
920 notmuch_message_get_filenames (notmuch_message_t *message)
922 _notmuch_message_ensure_filename_list (message);
924 return _notmuch_filenames_create (message, message->filename_list);
928 notmuch_message_get_flag (notmuch_message_t *message,
929 notmuch_message_flag_t flag)
931 if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
932 ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
933 _notmuch_message_ensure_metadata (message);
935 return NOTMUCH_TEST_BIT (message->flags, flag);
939 notmuch_message_set_flag (notmuch_message_t *message,
940 notmuch_message_flag_t flag, notmuch_bool_t enable)
943 NOTMUCH_SET_BIT (&message->flags, flag);
945 NOTMUCH_CLEAR_BIT (&message->flags, flag);
946 NOTMUCH_SET_BIT (&message->lazy_flags, flag);
950 notmuch_message_get_date (notmuch_message_t *message)
955 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
956 } catch (Xapian::Error &error) {
957 _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading date: %s\n",
958 error.get_msg().c_str());
959 message->notmuch->exception_reported = TRUE;
964 /* sortable_unserialise is undefined on empty string */
966 return Xapian::sortable_unserialise (value);
970 notmuch_message_get_tags (notmuch_message_t *message)
972 notmuch_tags_t *tags;
974 if (!message->tag_list)
975 _notmuch_message_ensure_metadata (message);
977 tags = _notmuch_tags_create (message, message->tag_list);
978 /* _notmuch_tags_create steals the reference to the tag_list, but
979 * in this case it's still used by the message, so we add an
980 * *additional* talloc reference to the list. As a result, it's
981 * possible to modify the message tags (which talloc_unlink's the
982 * current list from the message) while still iterating because
983 * the iterator will keep the current list alive. */
984 if (!talloc_reference (message, message->tag_list))
991 _notmuch_message_get_author (notmuch_message_t *message)
993 return message->author;
997 _notmuch_message_set_author (notmuch_message_t *message,
1000 if (message->author)
1001 talloc_free(message->author);
1002 message->author = talloc_strdup(message, author);
1007 _notmuch_message_set_header_values (notmuch_message_t *message,
1010 const char *subject)
1014 /* GMime really doesn't want to see a NULL date, so protect its
1016 if (date == NULL || *date == '\0')
1019 time_value = g_mime_utils_header_decode_date (date, NULL);
1021 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
1022 Xapian::sortable_serialise (time_value));
1023 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
1024 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1025 message->modified = TRUE;
1028 /* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD. The caller
1029 * must call _notmuch_message_sync. */
1031 _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
1033 /* _notmuch_message_sync will update the last modification
1034 * revision; we just have to ask it to. */
1035 message->modified = TRUE;
1038 /* Synchronize changes made to message->doc out into the database. */
1040 _notmuch_message_sync (notmuch_message_t *message)
1042 Xapian::WritableDatabase *db;
1044 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
1047 if (! message->modified)
1050 /* Update the last modification of this message. */
1051 if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)
1052 /* sortable_serialise gives a reasonably compact encoding,
1053 * which directly translates to reduced IO when scanning the
1054 * value stream. Since it's built for doubles, we only get 53
1055 * effective bits, but that's still enough for the database to
1056 * last a few centuries at 1 million revisions per second. */
1057 message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,
1058 Xapian::sortable_serialise (
1059 _notmuch_database_new_revision (
1060 message->notmuch)));
1062 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
1063 db->replace_document (message->doc_id, message->doc);
1064 message->modified = FALSE;
1067 /* Delete a message document from the database, leaving a ghost
1068 * message in its place */
1070 _notmuch_message_delete (notmuch_message_t *message)
1072 notmuch_status_t status;
1073 Xapian::WritableDatabase *db;
1074 const char *mid, *tid, *query_string;
1075 notmuch_message_t *ghost;
1076 notmuch_private_status_t private_status;
1077 notmuch_database_t *notmuch;
1078 notmuch_query_t *query;
1079 unsigned int count = 0;
1080 notmuch_bool_t is_ghost;
1082 mid = notmuch_message_get_message_id (message);
1083 tid = notmuch_message_get_thread_id (message);
1084 notmuch = message->notmuch;
1086 status = _notmuch_database_ensure_writable (message->notmuch);
1090 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1091 db->delete_document (message->doc_id);
1093 /* if this was a ghost to begin with, we are done */
1094 private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
1096 return COERCE_STATUS (private_status,
1097 "Error trying to determine whether message was a ghost");
1099 return NOTMUCH_STATUS_SUCCESS;
1101 query_string = talloc_asprintf (message, "thread:%s", tid);
1102 query = notmuch_query_create (notmuch, query_string);
1104 return NOTMUCH_STATUS_OUT_OF_MEMORY;
1105 status = notmuch_query_count_messages_st (query, &count);
1107 notmuch_query_destroy (query);
1112 /* reintroduce a ghost in its place because there are still
1113 * other active messages in this thread: */
1114 ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);
1115 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1116 private_status = _notmuch_message_initialize_ghost (ghost, tid);
1117 if (! private_status)
1118 _notmuch_message_sync (ghost);
1119 } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1120 /* this is deeply weird, and we should not have gotten
1121 into this state. is there a better error message to
1123 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1126 notmuch_message_destroy (ghost);
1127 status = COERCE_STATUS (private_status, "Error converting to ghost message");
1129 /* the thread is empty; drop all ghost messages from it */
1130 notmuch_messages_t *messages;
1131 status = _notmuch_query_search_documents (query,
1134 if (status == NOTMUCH_STATUS_SUCCESS) {
1135 notmuch_status_t last_error = NOTMUCH_STATUS_SUCCESS;
1136 while (notmuch_messages_valid (messages)) {
1137 message = notmuch_messages_get (messages);
1138 status = _notmuch_message_delete (message);
1139 if (status) /* we'll report the last failure we see;
1140 * if there is more than one failure, we
1141 * forget about previous ones */
1142 last_error = status;
1143 notmuch_message_destroy (message);
1144 notmuch_messages_move_to_next (messages);
1146 status = last_error;
1149 notmuch_query_destroy (query);
1153 /* Transform a blank message into a ghost message. The caller must
1154 * _notmuch_message_sync the message. */
1155 notmuch_private_status_t
1156 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1157 const char *thread_id)
1159 notmuch_private_status_t status;
1161 status = _notmuch_message_add_term (message, "type", "ghost");
1164 status = _notmuch_message_add_term (message, "thread", thread_id);
1168 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1171 /* Ensure that 'message' is not holding any file object open. Future
1172 * calls to various functions will still automatically open the
1173 * message file as needed.
1176 _notmuch_message_close (notmuch_message_t *message)
1178 if (message->message_file) {
1179 _notmuch_message_file_close (message->message_file);
1180 message->message_file = NULL;
1184 /* Add a name:value term to 'message', (the actual term will be
1185 * encoded by prefixing the value with a short prefix). See
1186 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1187 * names to prefix values.
1189 * This change will not be reflected in the database until the next
1190 * call to _notmuch_message_sync. */
1191 notmuch_private_status_t
1192 _notmuch_message_add_term (notmuch_message_t *message,
1193 const char *prefix_name,
1200 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1202 term = talloc_asprintf (message, "%s%s",
1203 _find_prefix (prefix_name), value);
1205 if (strlen (term) > NOTMUCH_TERM_MAX)
1206 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1208 message->doc.add_term (term, 0);
1209 message->modified = TRUE;
1213 _notmuch_message_invalidate_metadata (message, prefix_name);
1215 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1218 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1219 * term will be added both prefixed (if prefix_name is not NULL) and
1220 * also non-prefixed). */
1221 notmuch_private_status_t
1222 _notmuch_message_gen_terms (notmuch_message_t *message,
1223 const char *prefix_name,
1226 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1229 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1231 term_gen->set_document (message->doc);
1234 const char *prefix = _find_prefix (prefix_name);
1236 term_gen->set_termpos (message->termpos);
1237 term_gen->index_text (text, 1, prefix);
1238 /* Create a gap between this an the next terms so they don't
1239 * appear to be a phrase. */
1240 message->termpos = term_gen->get_termpos () + 100;
1242 _notmuch_message_invalidate_metadata (message, prefix_name);
1245 term_gen->set_termpos (message->termpos);
1246 term_gen->index_text (text);
1247 /* Create a term gap, as above. */
1248 message->termpos = term_gen->get_termpos () + 100;
1250 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1253 /* Remove a name:value term from 'message', (the actual term will be
1254 * encoded by prefixing the value with a short prefix). See
1255 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1256 * names to prefix values.
1258 * This change will not be reflected in the database until the next
1259 * call to _notmuch_message_sync. */
1260 notmuch_private_status_t
1261 _notmuch_message_remove_term (notmuch_message_t *message,
1262 const char *prefix_name,
1268 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1270 term = talloc_asprintf (message, "%s%s",
1271 _find_prefix (prefix_name), value);
1273 if (strlen (term) > NOTMUCH_TERM_MAX)
1274 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1277 message->doc.remove_term (term);
1278 message->modified = TRUE;
1279 } catch (const Xapian::InvalidArgumentError) {
1280 /* We'll let the philosophers try to wrestle with the
1281 * question of whether failing to remove that which was not
1282 * there in the first place is failure. For us, we'll silently
1283 * consider it all good. */
1288 _notmuch_message_invalidate_metadata (message, prefix_name);
1290 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1293 notmuch_private_status_t
1294 _notmuch_message_has_term (notmuch_message_t *message,
1295 const char *prefix_name,
1297 notmuch_bool_t *result)
1300 notmuch_bool_t out = FALSE;
1301 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
1304 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1306 term = talloc_asprintf (message, "%s%s",
1307 _find_prefix (prefix_name), value);
1309 if (strlen (term) > NOTMUCH_TERM_MAX)
1310 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1313 /* Look for the exact term */
1314 Xapian::TermIterator i = message->doc.termlist_begin ();
1316 if (i != message->doc.termlist_end () &&
1317 !strcmp ((*i).c_str (), term))
1319 } catch (Xapian::Error &error) {
1320 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1329 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1331 notmuch_private_status_t private_status;
1332 notmuch_status_t status;
1334 status = _notmuch_database_ensure_writable (message->notmuch);
1339 return NOTMUCH_STATUS_NULL_POINTER;
1341 if (strlen (tag) > NOTMUCH_TAG_MAX)
1342 return NOTMUCH_STATUS_TAG_TOO_LONG;
1344 private_status = _notmuch_message_add_term (message, "tag", tag);
1345 if (private_status) {
1346 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
1350 if (! message->frozen)
1351 _notmuch_message_sync (message);
1353 return NOTMUCH_STATUS_SUCCESS;
1357 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1359 notmuch_private_status_t private_status;
1360 notmuch_status_t status;
1362 status = _notmuch_database_ensure_writable (message->notmuch);
1367 return NOTMUCH_STATUS_NULL_POINTER;
1369 if (strlen (tag) > NOTMUCH_TAG_MAX)
1370 return NOTMUCH_STATUS_TAG_TOO_LONG;
1372 private_status = _notmuch_message_remove_term (message, "tag", tag);
1373 if (private_status) {
1374 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1378 if (! message->frozen)
1379 _notmuch_message_sync (message);
1381 return NOTMUCH_STATUS_SUCCESS;
1384 /* Is the given filename within a maildir directory?
1386 * Specifically, is the final directory component of 'filename' either
1387 * "cur" or "new". If so, return a pointer to that final directory
1388 * component within 'filename'. If not, return NULL.
1390 * A non-NULL return value is guaranteed to be a valid string pointer
1391 * pointing to the characters "new/" or "cur/", (but not
1395 _filename_is_in_maildir (const char *filename)
1397 const char *slash, *dir = NULL;
1399 /* Find the last '/' separating directory from filename. */
1400 slash = strrchr (filename, '/');
1404 /* Jump back 4 characters to where the previous '/' will be if the
1405 * directory is named "cur" or "new". */
1406 if (slash - filename < 4)
1416 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1417 STRNCMP_LITERAL (dir, "new/") == 0)
1426 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1429 notmuch_status_t status;
1430 notmuch_filenames_t *filenames;
1431 const char *filename, *dir;
1432 char *combined_flags = talloc_strdup (message, "");
1434 int seen_maildir_info = 0;
1436 for (filenames = notmuch_message_get_filenames (message);
1437 notmuch_filenames_valid (filenames);
1438 notmuch_filenames_move_to_next (filenames))
1440 filename = notmuch_filenames_get (filenames);
1441 dir = _filename_is_in_maildir (filename);
1446 flags = strstr (filename, ":2,");
1448 seen_maildir_info = 1;
1450 combined_flags = talloc_strdup_append (combined_flags, flags);
1451 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1452 /* Messages are delivered to new/ with no "info" part, but
1453 * they effectively have default maildir flags. According
1454 * to the spec, we should ignore the info part for
1455 * messages in new/, but some MUAs (mutt) can set maildir
1456 * flags on messages in new/, so we're liberal in what we
1458 seen_maildir_info = 1;
1462 /* If none of the filenames have any maildir info field (not even
1463 * an empty info with no flags set) then there's no information to
1464 * go on, so do nothing. */
1465 if (! seen_maildir_info)
1466 return NOTMUCH_STATUS_SUCCESS;
1468 status = notmuch_message_freeze (message);
1472 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1473 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
1475 flag2tag[i].inverse)
1477 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1479 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1484 status = notmuch_message_thaw (message);
1486 talloc_free (combined_flags);
1491 /* From the set of tags on 'message' and the flag2tag table, compute a
1492 * set of maildir-flag actions to be taken, (flags that should be
1493 * either set or cleared).
1495 * The result is returned as two talloced strings: to_set, and to_clear
1498 _get_maildir_flag_actions (notmuch_message_t *message,
1500 char **to_clear_ret)
1502 char *to_set, *to_clear;
1503 notmuch_tags_t *tags;
1507 to_set = talloc_strdup (message, "");
1508 to_clear = talloc_strdup (message, "");
1510 /* First, find flags for all set tags. */
1511 for (tags = notmuch_message_get_tags (message);
1512 notmuch_tags_valid (tags);
1513 notmuch_tags_move_to_next (tags))
1515 tag = notmuch_tags_get (tags);
1517 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1518 if (strcmp (tag, flag2tag[i].tag) == 0) {
1519 if (flag2tag[i].inverse)
1520 to_clear = talloc_asprintf_append (to_clear,
1524 to_set = talloc_asprintf_append (to_set,
1531 /* Then, find the flags for all tags not present. */
1532 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1533 if (flag2tag[i].inverse) {
1534 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1535 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1537 if (strchr (to_set, flag2tag[i].flag) == NULL)
1538 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1542 *to_set_ret = to_set;
1543 *to_clear_ret = to_clear;
1546 /* Given 'filename' and a set of maildir flags to set and to clear,
1547 * compute the new maildir filename.
1549 * If the existing filename is in the directory "new", the new
1550 * filename will be in the directory "cur", except for the case when
1551 * no flags are changed and the existing filename does not contain
1552 * maildir info (starting with ",2:").
1554 * After a sequence of ":2," in the filename, any subsequent
1555 * single-character flags will be added or removed according to the
1556 * characters in flags_to_set and flags_to_clear. Any existing flags
1557 * not mentioned in either string will remain. The final list of flags
1558 * will be in ASCII order.
1560 * If the original flags seem invalid, (repeated characters or
1561 * non-ASCII ordering of flags), this function will return NULL
1562 * (meaning that renaming would not be safe and should not occur).
1565 _new_maildir_filename (void *ctx,
1566 const char *filename,
1567 const char *flags_to_set,
1568 const char *flags_to_clear)
1570 const char *info, *flags;
1571 unsigned int flag, last_flag;
1572 char *filename_new, *dir;
1574 int flags_in_map = 0;
1575 notmuch_bool_t flags_changed = FALSE;
1579 memset (flag_map, 0, sizeof (flag_map));
1581 info = strstr (filename, ":2,");
1584 info = filename + strlen(filename);
1586 /* Loop through existing flags in filename. */
1587 for (flags = info + 3, last_flag = 0;
1589 last_flag = flag, flags++)
1593 /* Original flags not in ASCII order. Abort. */
1594 if (flag < last_flag)
1597 /* Non-ASCII flag. Abort. */
1598 if (flag > sizeof(flag_map) - 1)
1601 /* Repeated flag value. Abort. */
1610 /* Then set and clear our flags from tags. */
1611 for (flags = flags_to_set; *flags; flags++) {
1613 if (flag_map[flag] == 0) {
1616 flags_changed = TRUE;
1620 for (flags = flags_to_clear; *flags; flags++) {
1622 if (flag_map[flag]) {
1625 flags_changed = TRUE;
1629 /* Messages in new/ without maildir info can be kept in new/ if no
1630 * flags have changed. */
1631 dir = (char *) _filename_is_in_maildir (filename);
1632 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && !*info && !flags_changed)
1633 return talloc_strdup (ctx, filename);
1635 filename_new = (char *) talloc_size (ctx,
1637 strlen (":2,") + flags_in_map + 1);
1638 if (unlikely (filename_new == NULL))
1641 strncpy (filename_new, filename, info - filename);
1642 filename_new[info - filename] = '\0';
1644 strcat (filename_new, ":2,");
1646 s = filename_new + strlen (filename_new);
1647 for (i = 0; i < sizeof (flag_map); i++)
1656 /* If message is in new/ move it under cur/. */
1657 dir = (char *) _filename_is_in_maildir (filename_new);
1658 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1659 memcpy (dir, "cur/", 4);
1661 return filename_new;
1665 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1667 notmuch_filenames_t *filenames;
1668 const char *filename;
1670 char *to_set, *to_clear;
1671 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1673 _get_maildir_flag_actions (message, &to_set, &to_clear);
1675 for (filenames = notmuch_message_get_filenames (message);
1676 notmuch_filenames_valid (filenames);
1677 notmuch_filenames_move_to_next (filenames))
1679 filename = notmuch_filenames_get (filenames);
1681 if (! _filename_is_in_maildir (filename))
1684 filename_new = _new_maildir_filename (message, filename,
1686 if (filename_new == NULL)
1689 if (strcmp (filename, filename_new)) {
1691 notmuch_status_t new_status;
1693 err = rename (filename, filename_new);
1697 new_status = _notmuch_message_remove_filename (message,
1699 /* Hold on to only the first error. */
1700 if (! status && new_status
1701 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1702 status = new_status;
1706 new_status = _notmuch_message_add_filename (message,
1708 /* Hold on to only the first error. */
1709 if (! status && new_status) {
1710 status = new_status;
1714 _notmuch_message_sync (message);
1717 talloc_free (filename_new);
1720 talloc_free (to_set);
1721 talloc_free (to_clear);
1727 notmuch_message_remove_all_tags (notmuch_message_t *message)
1729 notmuch_private_status_t private_status;
1730 notmuch_status_t status;
1731 notmuch_tags_t *tags;
1734 status = _notmuch_database_ensure_writable (message->notmuch);
1738 for (tags = notmuch_message_get_tags (message);
1739 notmuch_tags_valid (tags);
1740 notmuch_tags_move_to_next (tags))
1742 tag = notmuch_tags_get (tags);
1744 private_status = _notmuch_message_remove_term (message, "tag", tag);
1745 if (private_status) {
1746 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1751 if (! message->frozen)
1752 _notmuch_message_sync (message);
1755 return NOTMUCH_STATUS_SUCCESS;
1759 notmuch_message_freeze (notmuch_message_t *message)
1761 notmuch_status_t status;
1763 status = _notmuch_database_ensure_writable (message->notmuch);
1769 return NOTMUCH_STATUS_SUCCESS;
1773 notmuch_message_thaw (notmuch_message_t *message)
1775 notmuch_status_t status;
1777 status = _notmuch_database_ensure_writable (message->notmuch);
1781 if (message->frozen > 0) {
1783 if (message->frozen == 0)
1784 _notmuch_message_sync (message);
1785 return NOTMUCH_STATUS_SUCCESS;
1787 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1792 notmuch_message_destroy (notmuch_message_t *message)
1794 talloc_free (message);
1797 notmuch_database_t *
1798 _notmuch_message_database (notmuch_message_t *message)
1800 return message->notmuch;