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"
23 #include "message-private.h"
27 #include <gmime/gmime.h>
29 struct visible _notmuch_message {
30 notmuch_database_t *notmuch;
36 notmuch_string_list_t *tag_list;
37 notmuch_string_list_t *filename_term_list;
38 notmuch_string_list_t *filename_list;
40 notmuch_message_file_t *message_file;
41 notmuch_string_list_t *property_term_list;
42 notmuch_string_map_t *property_map;
43 notmuch_message_list_t *replies;
45 /* For flags that are initialized on-demand, lazy_flags indicates
46 * if each flag has been initialized. */
47 unsigned long lazy_flags;
49 /* Message document modified since last sync */
50 notmuch_bool_t modified;
53 Xapian::termcount termpos;
56 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
58 struct maildir_flag_tag {
61 notmuch_bool_t inverse;
64 /* ASCII ordered table of Maildir flags and associated tags */
65 static struct maildir_flag_tag flag2tag[] = {
66 { 'D', "draft", FALSE},
67 { 'F', "flagged", FALSE},
68 { 'P', "passed", FALSE},
69 { 'R', "replied", FALSE},
70 { 'S', "unread", TRUE }
73 /* We end up having to call the destructor explicitly because we had
74 * to use "placement new" in order to initialize C++ objects within a
75 * block that we allocated with talloc. So C++ is making talloc
76 * slightly less simple to use, (we wouldn't need
77 * talloc_set_destructor at all otherwise).
80 _notmuch_message_destructor (notmuch_message_t *message)
82 message->doc.~Document ();
87 static notmuch_message_t *
88 _notmuch_message_create_for_document (const void *talloc_owner,
89 notmuch_database_t *notmuch,
92 notmuch_private_status_t *status)
94 notmuch_message_t *message;
97 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
99 message = talloc (talloc_owner, notmuch_message_t);
100 if (unlikely (message == NULL)) {
102 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
106 message->notmuch = notmuch;
107 message->doc_id = doc_id;
111 message->lazy_flags = 0;
113 /* Each of these will be lazily created as needed. */
114 message->message_id = NULL;
115 message->thread_id = NULL;
116 message->in_reply_to = NULL;
117 message->tag_list = NULL;
118 message->filename_term_list = NULL;
119 message->filename_list = NULL;
120 message->message_file = NULL;
121 message->author = NULL;
122 message->property_term_list = NULL;
123 message->property_map = NULL;
125 message->replies = _notmuch_message_list_create (message);
126 if (unlikely (message->replies == NULL)) {
128 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
132 /* This is C++'s creepy "placement new", which is really just an
133 * ugly way to call a constructor for a pre-allocated object. So
134 * it's really not an error to not be checking for OUT_OF_MEMORY
135 * here, since this "new" isn't actually allocating memory. This
136 * is language-design comedy of the wrong kind. */
138 new (&message->doc) Xapian::Document;
140 talloc_set_destructor (message, _notmuch_message_destructor);
143 message->termpos = 0;
148 /* Create a new notmuch_message_t object for an existing document in
151 * Here, 'talloc owner' is an optional talloc context to which the new
152 * message will belong. This allows for the caller to not bother
153 * calling notmuch_message_destroy on the message, and know that all
154 * memory will be reclaimed when 'talloc_owner' is freed. The caller
155 * still can call notmuch_message_destroy when finished with the
156 * message if desired.
158 * The 'talloc_owner' argument can also be NULL, in which case the
159 * caller *is* responsible for calling notmuch_message_destroy.
161 * If no document exists in the database with document ID of 'doc_id'
162 * then this function returns NULL and optionally sets *status to
163 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
165 * This function can also fail to due lack of available memory,
166 * returning NULL and optionally setting *status to
167 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
169 * The caller can pass NULL for status if uninterested in
170 * distinguishing these two cases.
173 _notmuch_message_create (const void *talloc_owner,
174 notmuch_database_t *notmuch,
176 notmuch_private_status_t *status)
178 Xapian::Document doc;
181 doc = notmuch->xapian_db->get_document (doc_id);
182 } catch (const Xapian::DocNotFoundError &error) {
184 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
188 return _notmuch_message_create_for_document (talloc_owner, notmuch,
189 doc_id, doc, status);
192 /* Create a new notmuch_message_t object for a specific message ID,
193 * (which may or may not already exist in the database).
195 * The 'notmuch' database will be the talloc owner of the returned
198 * This function returns a valid notmuch_message_t whether or not
199 * there is already a document in the database with the given message
200 * ID. These two cases can be distinguished by the value of *status:
203 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
205 * There is already a document with message ID 'message_id' in the
206 * database. The returned message can be used to query/modify the
207 * document. The message may be a ghost message.
209 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
211 * No document with 'message_id' exists in the database. The
212 * returned message contains a newly created document (not yet
213 * added to the database) and a document ID that is known not to
214 * exist in the database. This message is "blank"; that is, it
215 * contains only a message ID and no other metadata. The caller
216 * can modify the message, and a call to _notmuch_message_sync
217 * will add the document to the database.
219 * If an error occurs, this function will return NULL and *status
220 * will be set as appropriate. (The status pointer argument must
224 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
225 const char *message_id,
226 notmuch_private_status_t *status_ret)
228 notmuch_message_t *message;
229 Xapian::Document doc;
233 *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
237 return talloc_steal (notmuch, message);
238 else if (*status_ret)
241 /* If the message ID is too long, substitute its sha1 instead. */
242 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
243 message_id = _notmuch_message_id_compressed (message, message_id);
245 term = talloc_asprintf (NULL, "%s%s",
246 _find_prefix ("id"), message_id);
248 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
252 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
253 INTERNAL_ERROR ("Failure to ensure database is writable.");
256 doc.add_term (term, 0);
259 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
261 doc_id = _notmuch_database_generate_doc_id (notmuch);
262 } catch (const Xapian::Error &error) {
263 _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred creating message: %s\n",
264 error.get_msg().c_str());
265 notmuch->exception_reported = TRUE;
266 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
270 message = _notmuch_message_create_for_document (notmuch, notmuch,
271 doc_id, doc, status_ret);
273 /* We want to inform the caller that we had to create a new
275 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
276 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
282 _notmuch_message_get_term (notmuch_message_t *message,
283 Xapian::TermIterator &i, Xapian::TermIterator &end,
286 int prefix_len = strlen (prefix);
294 const std::string &term = *i;
295 if (strncmp (term.c_str(), prefix, prefix_len))
298 value = talloc_strdup (message, term.c_str() + prefix_len);
300 #if DEBUG_DATABASE_SANITY
303 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
304 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
305 message->doc_id, prefix, value,
306 (*i).c_str () + prefix_len);
314 _notmuch_message_ensure_metadata (notmuch_message_t *message)
316 Xapian::TermIterator i, end;
317 const char *thread_prefix = _find_prefix ("thread"),
318 *tag_prefix = _find_prefix ("tag"),
319 *id_prefix = _find_prefix ("id"),
320 *type_prefix = _find_prefix ("type"),
321 *filename_prefix = _find_prefix ("file-direntry"),
322 *property_prefix = _find_prefix ("property"),
323 *replyto_prefix = _find_prefix ("replyto");
325 /* We do this all in a single pass because Xapian decompresses the
326 * term list every time you iterate over it. Thus, while this is
327 * slightly more costly than looking up individual fields if only
328 * one field of the message object is actually used, it's a huge
329 * win as more fields are used. */
331 i = message->doc.termlist_begin ();
332 end = message->doc.termlist_end ();
335 if (!message->thread_id)
337 _notmuch_message_get_term (message, i, end, thread_prefix);
340 assert (strcmp (thread_prefix, tag_prefix) < 0);
341 if (!message->tag_list) {
343 _notmuch_database_get_terms_with_prefix (message, i, end,
345 _notmuch_string_list_sort (message->tag_list);
349 assert (strcmp (tag_prefix, id_prefix) < 0);
350 if (!message->message_id)
351 message->message_id =
352 _notmuch_message_get_term (message, i, end, id_prefix);
354 /* Get document type */
355 assert (strcmp (id_prefix, type_prefix) < 0);
356 if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {
357 i.skip_to (type_prefix);
358 /* "T" is the prefix "type" fields. See
359 * BOOLEAN_PREFIX_INTERNAL. */
361 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
362 else if (*i == "Tghost")
363 NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
365 INTERNAL_ERROR ("Message without type term");
366 NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
369 /* Get filename list. Here we get only the terms. We lazily
370 * expand them to full file names when needed in
371 * _notmuch_message_ensure_filename_list. */
372 assert (strcmp (type_prefix, filename_prefix) < 0);
373 if (!message->filename_term_list && !message->filename_list)
374 message->filename_term_list =
375 _notmuch_database_get_terms_with_prefix (message, i, end,
379 /* Get property terms. Mimic the setup with filenames above */
380 assert (strcmp (filename_prefix, property_prefix) < 0);
381 if (!message->property_map && !message->property_term_list)
382 message->property_term_list =
383 _notmuch_database_get_terms_with_prefix (message, i, end,
387 assert (strcmp (property_prefix, replyto_prefix) < 0);
388 if (!message->in_reply_to)
389 message->in_reply_to =
390 _notmuch_message_get_term (message, i, end, replyto_prefix);
393 /* It's perfectly valid for a message to have no In-Reply-To
394 * header. For these cases, we return an empty string. */
395 if (!message->in_reply_to)
396 message->in_reply_to = talloc_strdup (message, "");
400 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
401 const char *prefix_name)
403 if (strcmp ("thread", prefix_name) == 0) {
404 talloc_free (message->thread_id);
405 message->thread_id = NULL;
408 if (strcmp ("tag", prefix_name) == 0) {
409 talloc_unlink (message, message->tag_list);
410 message->tag_list = NULL;
413 if (strcmp ("type", prefix_name) == 0) {
414 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
415 NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
418 if (strcmp ("file-direntry", prefix_name) == 0) {
419 talloc_free (message->filename_term_list);
420 talloc_free (message->filename_list);
421 message->filename_term_list = message->filename_list = NULL;
424 if (strcmp ("property", prefix_name) == 0) {
426 if (message->property_term_list)
427 talloc_free (message->property_term_list);
428 message->property_term_list = NULL;
430 if (message->property_map)
431 talloc_unlink (message, message->property_map);
433 message->property_map = NULL;
436 if (strcmp ("replyto", prefix_name) == 0) {
437 talloc_free (message->in_reply_to);
438 message->in_reply_to = NULL;
443 _notmuch_message_get_doc_id (notmuch_message_t *message)
445 return message->doc_id;
449 notmuch_message_get_message_id (notmuch_message_t *message)
451 if (!message->message_id)
452 _notmuch_message_ensure_metadata (message);
453 if (!message->message_id)
454 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
456 return message->message_id;
460 _notmuch_message_ensure_message_file (notmuch_message_t *message)
462 const char *filename;
464 if (message->message_file)
467 filename = notmuch_message_get_filename (message);
468 if (unlikely (filename == NULL))
471 message->message_file = _notmuch_message_file_open_ctx (
472 _notmuch_message_database (message), message, filename);
476 notmuch_message_get_header (notmuch_message_t *message, const char *header)
478 Xapian::valueno slot = Xapian::BAD_VALUENO;
480 /* Fetch header from the appropriate xapian value field if
482 if (strcasecmp (header, "from") == 0)
483 slot = NOTMUCH_VALUE_FROM;
484 else if (strcasecmp (header, "subject") == 0)
485 slot = NOTMUCH_VALUE_SUBJECT;
486 else if (strcasecmp (header, "message-id") == 0)
487 slot = NOTMUCH_VALUE_MESSAGE_ID;
489 if (slot != Xapian::BAD_VALUENO) {
491 std::string value = message->doc.get_value (slot);
493 /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then
494 * empty values indicate empty headers. If we don't, then
495 * it could just mean we didn't record the header. */
496 if ((message->notmuch->features &
497 NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) ||
499 return talloc_strdup (message, value.c_str ());
501 } catch (Xapian::Error &error) {
502 _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading header: %s\n",
503 error.get_msg().c_str());
504 message->notmuch->exception_reported = TRUE;
509 /* Otherwise fall back to parsing the file */
510 _notmuch_message_ensure_message_file (message);
511 if (message->message_file == NULL)
514 return _notmuch_message_file_get_header (message->message_file, header);
517 /* Return the message ID from the In-Reply-To header of 'message'.
519 * Returns an empty string ("") if 'message' has no In-Reply-To
522 * Returns NULL if any error occurs.
525 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
527 if (!message->in_reply_to)
528 _notmuch_message_ensure_metadata (message);
529 return message->in_reply_to;
533 notmuch_message_get_thread_id (notmuch_message_t *message)
535 if (!message->thread_id)
536 _notmuch_message_ensure_metadata (message);
537 if (!message->thread_id)
538 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
540 return message->thread_id;
544 _notmuch_message_add_reply (notmuch_message_t *message,
545 notmuch_message_t *reply)
547 _notmuch_message_list_add_message (message->replies, reply);
551 notmuch_message_get_replies (notmuch_message_t *message)
553 return _notmuch_messages_create (message->replies);
557 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
559 Xapian::TermIterator i;
560 size_t prefix_len = strlen (prefix);
563 i = message->doc.termlist_begin ();
566 /* Terminate loop when no terms remain with desired prefix. */
567 if (i == message->doc.termlist_end () ||
568 strncmp ((*i).c_str (), prefix, prefix_len))
572 message->doc.remove_term ((*i));
573 message->modified = TRUE;
574 } catch (const Xapian::InvalidArgumentError) {
575 /* Ignore failure to remove non-existent term. */
580 /* Return true if p points at "new" or "cur". */
581 static bool is_maildir (const char *p)
583 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
586 /* Add "folder:" term for directory. */
587 static notmuch_status_t
588 _notmuch_message_add_folder_terms (notmuch_message_t *message,
589 const char *directory)
593 folder = talloc_strdup (NULL, directory);
595 return NOTMUCH_STATUS_OUT_OF_MEMORY;
598 * If the message file is in a leaf directory named "new" or
599 * "cur", presume maildir and index the parent directory. Thus a
600 * "folder:" prefix search matches messages in the specified
601 * maildir folder, i.e. in the specified directory and its "new"
602 * and "cur" subdirectories.
604 * Note that this means the "folder:" prefix can't be used for
605 * distinguishing between message files in "new" or "cur". The
606 * "path:" prefix needs to be used for that.
608 * Note the deliberate difference to _filename_is_in_maildir(). We
609 * don't want to index different things depending on the existence
610 * or non-existence of all maildir sibling directories "new",
611 * "cur", and "tmp". Doing so would be surprising, and difficult
612 * for the user to fix in case all subdirectories were not in
613 * place during indexing.
615 last = strrchr (folder, '/');
617 if (is_maildir (last + 1))
619 } else if (is_maildir (folder)) {
623 _notmuch_message_add_term (message, "folder", folder);
625 talloc_free (folder);
627 return NOTMUCH_STATUS_SUCCESS;
630 #define RECURSIVE_SUFFIX "/**"
632 /* Add "path:" terms for directory. */
633 static notmuch_status_t
634 _notmuch_message_add_path_terms (notmuch_message_t *message,
635 const char *directory)
637 /* Add exact "path:" term. */
638 _notmuch_message_add_term (message, "path", directory);
640 if (strlen (directory)) {
643 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
645 return NOTMUCH_STATUS_OUT_OF_MEMORY;
647 /* Add recursive "path:" terms for directory and all parents. */
648 for (p = path + strlen (path) - 1; p > path; p--) {
650 strcpy (p, RECURSIVE_SUFFIX);
651 _notmuch_message_add_term (message, "path", path);
658 /* Recursive all-matching path:** for consistency. */
659 _notmuch_message_add_term (message, "path", "**");
661 return NOTMUCH_STATUS_SUCCESS;
664 /* Add directory based terms for all filenames of the message. */
665 static notmuch_status_t
666 _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
668 const char *direntry_prefix = _find_prefix ("file-direntry");
669 int direntry_prefix_len = strlen (direntry_prefix);
670 Xapian::TermIterator i = message->doc.termlist_begin ();
671 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
673 for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {
674 unsigned int directory_id;
675 const char *direntry, *directory;
677 const std::string &term = *i;
679 /* Terminate loop at first term without desired prefix. */
680 if (strncmp (term.c_str (), direntry_prefix, direntry_prefix_len))
683 /* Indicate that there are filenames remaining. */
684 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
686 direntry = term.c_str ();
687 direntry += direntry_prefix_len;
689 directory_id = strtol (direntry, &colon, 10);
691 if (colon == NULL || *colon != ':')
692 INTERNAL_ERROR ("malformed direntry");
694 directory = _notmuch_database_get_directory_path (ctx,
698 _notmuch_message_add_folder_terms (message, directory);
699 _notmuch_message_add_path_terms (message, directory);
705 /* Add an additional 'filename' for 'message'.
707 * This change will not be reflected in the database until the next
708 * call to _notmuch_message_sync. */
710 _notmuch_message_add_filename (notmuch_message_t *message,
711 const char *filename)
713 const char *relative, *directory;
714 notmuch_status_t status;
715 void *local = talloc_new (message);
718 if (filename == NULL)
719 INTERNAL_ERROR ("Message filename cannot be NULL.");
721 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
722 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
723 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
725 relative = _notmuch_database_relative_path (message->notmuch, filename);
727 status = _notmuch_database_split_path (local, relative, &directory, NULL);
731 status = _notmuch_database_filename_to_direntry (
732 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
736 /* New file-direntry allows navigating to this message with
737 * notmuch_directory_get_child_files() . */
738 _notmuch_message_add_term (message, "file-direntry", direntry);
740 _notmuch_message_add_folder_terms (message, directory);
741 _notmuch_message_add_path_terms (message, directory);
745 return NOTMUCH_STATUS_SUCCESS;
748 /* Remove a particular 'filename' from 'message'.
750 * This change will not be reflected in the database until the next
751 * call to _notmuch_message_sync.
753 * If this message still has other filenames, returns
754 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
756 * Note: This function does not remove a document from the database,
757 * even if the specified filename is the only filename for this
758 * message. For that functionality, see
759 * notmuch_database_remove_message. */
761 _notmuch_message_remove_filename (notmuch_message_t *message,
762 const char *filename)
764 void *local = talloc_new (message);
766 notmuch_private_status_t private_status;
767 notmuch_status_t status;
769 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
770 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
771 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
773 status = _notmuch_database_filename_to_direntry (
774 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
775 if (status || !direntry)
778 /* Unlink this file from its parent directory. */
779 private_status = _notmuch_message_remove_term (message,
780 "file-direntry", direntry);
781 status = COERCE_STATUS (private_status,
782 "Unexpected error from _notmuch_message_remove_term");
786 /* Re-synchronize "folder:" and "path:" terms for this message. */
788 /* Remove all "folder:" terms. */
789 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
791 /* Remove all "path:" terms. */
792 _notmuch_message_remove_terms (message, _find_prefix ("path"));
794 /* Add back terms for all remaining filenames of the message. */
795 status = _notmuch_message_add_directory_terms (local, message);
802 /* Upgrade the "folder:" prefix from V1 to V2. */
803 #define FOLDER_PREFIX_V1 "XFOLDER"
804 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
806 _notmuch_message_upgrade_folder (notmuch_message_t *message)
808 /* Remove all old "folder:" terms. */
809 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
811 /* Remove all old "folder:" stemmed terms. */
812 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
814 /* Add new boolean "folder:" and "path:" terms. */
815 _notmuch_message_add_directory_terms (message, message);
819 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
821 return talloc_strdup (message, message->doc.get_data ().c_str ());
825 _notmuch_message_clear_data (notmuch_message_t *message)
827 message->doc.set_data ("");
828 message->modified = TRUE;
832 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
834 notmuch_string_node_t *node;
836 if (message->filename_list)
839 if (!message->filename_term_list)
840 _notmuch_message_ensure_metadata (message);
842 message->filename_list = _notmuch_string_list_create (message);
843 node = message->filename_term_list->head;
846 /* A message document created by an old version of notmuch
847 * (prior to rename support) will have the filename in the
848 * data of the document rather than as a file-direntry term.
850 * It would be nice to do the upgrade of the document directly
851 * here, but the database is likely open in read-only mode. */
854 data = message->doc.get_data ().c_str ();
857 INTERNAL_ERROR ("message with no filename");
859 _notmuch_string_list_append (message->filename_list, data);
864 for (; node; node = node->next) {
865 void *local = talloc_new (message);
866 const char *db_path, *directory, *basename, *filename;
867 char *colon, *direntry = NULL;
868 unsigned int directory_id;
870 direntry = node->string;
872 directory_id = strtol (direntry, &colon, 10);
874 if (colon == NULL || *colon != ':')
875 INTERNAL_ERROR ("malformed direntry");
877 basename = colon + 1;
881 db_path = notmuch_database_get_path (message->notmuch);
883 directory = _notmuch_database_get_directory_path (local,
887 if (strlen (directory))
888 filename = talloc_asprintf (message, "%s/%s/%s",
889 db_path, directory, basename);
891 filename = talloc_asprintf (message, "%s/%s",
894 _notmuch_string_list_append (message->filename_list, filename);
899 talloc_free (message->filename_term_list);
900 message->filename_term_list = NULL;
904 notmuch_message_get_filename (notmuch_message_t *message)
906 _notmuch_message_ensure_filename_list (message);
908 if (message->filename_list == NULL)
911 if (message->filename_list->head == NULL ||
912 message->filename_list->head->string == NULL)
914 INTERNAL_ERROR ("message with no filename");
917 return message->filename_list->head->string;
920 notmuch_filenames_t *
921 notmuch_message_get_filenames (notmuch_message_t *message)
923 _notmuch_message_ensure_filename_list (message);
925 return _notmuch_filenames_create (message, message->filename_list);
929 notmuch_message_get_flag (notmuch_message_t *message,
930 notmuch_message_flag_t flag)
932 if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
933 ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
934 _notmuch_message_ensure_metadata (message);
936 return NOTMUCH_TEST_BIT (message->flags, flag);
940 notmuch_message_set_flag (notmuch_message_t *message,
941 notmuch_message_flag_t flag, notmuch_bool_t enable)
944 NOTMUCH_SET_BIT (&message->flags, flag);
946 NOTMUCH_CLEAR_BIT (&message->flags, flag);
947 NOTMUCH_SET_BIT (&message->lazy_flags, flag);
951 notmuch_message_get_date (notmuch_message_t *message)
956 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
957 } catch (Xapian::Error &error) {
958 _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading date: %s\n",
959 error.get_msg().c_str());
960 message->notmuch->exception_reported = TRUE;
965 /* sortable_unserialise is undefined on empty string */
967 return Xapian::sortable_unserialise (value);
971 notmuch_message_get_tags (notmuch_message_t *message)
973 notmuch_tags_t *tags;
975 if (!message->tag_list)
976 _notmuch_message_ensure_metadata (message);
978 tags = _notmuch_tags_create (message, message->tag_list);
979 /* _notmuch_tags_create steals the reference to the tag_list, but
980 * in this case it's still used by the message, so we add an
981 * *additional* talloc reference to the list. As a result, it's
982 * possible to modify the message tags (which talloc_unlink's the
983 * current list from the message) while still iterating because
984 * the iterator will keep the current list alive. */
985 if (!talloc_reference (message, message->tag_list))
992 _notmuch_message_get_author (notmuch_message_t *message)
994 return message->author;
998 _notmuch_message_set_author (notmuch_message_t *message,
1001 if (message->author)
1002 talloc_free(message->author);
1003 message->author = talloc_strdup(message, author);
1008 _notmuch_message_set_header_values (notmuch_message_t *message,
1011 const char *subject)
1015 /* GMime really doesn't want to see a NULL date, so protect its
1017 if (date == NULL || *date == '\0')
1020 time_value = g_mime_utils_header_decode_date (date, NULL);
1022 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
1023 Xapian::sortable_serialise (time_value));
1024 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
1025 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1026 message->modified = TRUE;
1029 /* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD. The caller
1030 * must call _notmuch_message_sync. */
1032 _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
1034 /* _notmuch_message_sync will update the last modification
1035 * revision; we just have to ask it to. */
1036 message->modified = TRUE;
1039 /* Synchronize changes made to message->doc out into the database. */
1041 _notmuch_message_sync (notmuch_message_t *message)
1043 Xapian::WritableDatabase *db;
1045 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
1048 if (! message->modified)
1051 /* Update the last modification of this message. */
1052 if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)
1053 /* sortable_serialise gives a reasonably compact encoding,
1054 * which directly translates to reduced IO when scanning the
1055 * value stream. Since it's built for doubles, we only get 53
1056 * effective bits, but that's still enough for the database to
1057 * last a few centuries at 1 million revisions per second. */
1058 message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,
1059 Xapian::sortable_serialise (
1060 _notmuch_database_new_revision (
1061 message->notmuch)));
1063 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
1064 db->replace_document (message->doc_id, message->doc);
1065 message->modified = FALSE;
1068 /* Delete a message document from the database, leaving a ghost
1069 * message in its place */
1071 _notmuch_message_delete (notmuch_message_t *message)
1073 notmuch_status_t status;
1074 Xapian::WritableDatabase *db;
1075 const char *mid, *tid, *query_string;
1076 notmuch_message_t *ghost;
1077 notmuch_private_status_t private_status;
1078 notmuch_database_t *notmuch;
1079 notmuch_query_t *query;
1080 unsigned int count = 0;
1081 notmuch_bool_t is_ghost;
1083 mid = notmuch_message_get_message_id (message);
1084 tid = notmuch_message_get_thread_id (message);
1085 notmuch = message->notmuch;
1087 status = _notmuch_database_ensure_writable (message->notmuch);
1091 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1092 db->delete_document (message->doc_id);
1094 /* if this was a ghost to begin with, we are done */
1095 private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
1097 return COERCE_STATUS (private_status,
1098 "Error trying to determine whether message was a ghost");
1100 return NOTMUCH_STATUS_SUCCESS;
1102 query_string = talloc_asprintf (message, "thread:%s", tid);
1103 query = notmuch_query_create (notmuch, query_string);
1105 return NOTMUCH_STATUS_OUT_OF_MEMORY;
1106 status = notmuch_query_count_messages_st (query, &count);
1108 notmuch_query_destroy (query);
1113 /* reintroduce a ghost in its place because there are still
1114 * other active messages in this thread: */
1115 ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);
1116 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1117 private_status = _notmuch_message_initialize_ghost (ghost, tid);
1118 if (! private_status)
1119 _notmuch_message_sync (ghost);
1120 } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1121 /* this is deeply weird, and we should not have gotten
1122 into this state. is there a better error message to
1124 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1127 notmuch_message_destroy (ghost);
1128 status = COERCE_STATUS (private_status, "Error converting to ghost message");
1130 /* the thread is empty; drop all ghost messages from it */
1131 notmuch_messages_t *messages;
1132 status = _notmuch_query_search_documents (query,
1135 if (status == NOTMUCH_STATUS_SUCCESS) {
1136 notmuch_status_t last_error = NOTMUCH_STATUS_SUCCESS;
1137 while (notmuch_messages_valid (messages)) {
1138 message = notmuch_messages_get (messages);
1139 status = _notmuch_message_delete (message);
1140 if (status) /* we'll report the last failure we see;
1141 * if there is more than one failure, we
1142 * forget about previous ones */
1143 last_error = status;
1144 notmuch_message_destroy (message);
1145 notmuch_messages_move_to_next (messages);
1147 status = last_error;
1150 notmuch_query_destroy (query);
1154 /* Transform a blank message into a ghost message. The caller must
1155 * _notmuch_message_sync the message. */
1156 notmuch_private_status_t
1157 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1158 const char *thread_id)
1160 notmuch_private_status_t status;
1162 status = _notmuch_message_add_term (message, "type", "ghost");
1165 status = _notmuch_message_add_term (message, "thread", thread_id);
1169 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1172 /* Ensure that 'message' is not holding any file object open. Future
1173 * calls to various functions will still automatically open the
1174 * message file as needed.
1177 _notmuch_message_close (notmuch_message_t *message)
1179 if (message->message_file) {
1180 _notmuch_message_file_close (message->message_file);
1181 message->message_file = NULL;
1185 /* Add a name:value term to 'message', (the actual term will be
1186 * encoded by prefixing the value with a short prefix). See
1187 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1188 * names to prefix values.
1190 * This change will not be reflected in the database until the next
1191 * call to _notmuch_message_sync. */
1192 notmuch_private_status_t
1193 _notmuch_message_add_term (notmuch_message_t *message,
1194 const char *prefix_name,
1201 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1203 term = talloc_asprintf (message, "%s%s",
1204 _find_prefix (prefix_name), value);
1206 if (strlen (term) > NOTMUCH_TERM_MAX)
1207 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1209 message->doc.add_term (term, 0);
1210 message->modified = TRUE;
1214 _notmuch_message_invalidate_metadata (message, prefix_name);
1216 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1219 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1220 * term will be added both prefixed (if prefix_name is not NULL) and
1221 * also non-prefixed). */
1222 notmuch_private_status_t
1223 _notmuch_message_gen_terms (notmuch_message_t *message,
1224 const char *prefix_name,
1227 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1230 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1232 term_gen->set_document (message->doc);
1235 const char *prefix = _find_prefix (prefix_name);
1237 term_gen->set_termpos (message->termpos);
1238 term_gen->index_text (text, 1, prefix);
1239 /* Create a gap between this an the next terms so they don't
1240 * appear to be a phrase. */
1241 message->termpos = term_gen->get_termpos () + 100;
1243 _notmuch_message_invalidate_metadata (message, prefix_name);
1246 term_gen->set_termpos (message->termpos);
1247 term_gen->index_text (text);
1248 /* Create a term gap, as above. */
1249 message->termpos = term_gen->get_termpos () + 100;
1251 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1254 /* Remove a name:value term from 'message', (the actual term will be
1255 * encoded by prefixing the value with a short prefix). See
1256 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1257 * names to prefix values.
1259 * This change will not be reflected in the database until the next
1260 * call to _notmuch_message_sync. */
1261 notmuch_private_status_t
1262 _notmuch_message_remove_term (notmuch_message_t *message,
1263 const char *prefix_name,
1269 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1271 term = talloc_asprintf (message, "%s%s",
1272 _find_prefix (prefix_name), value);
1274 if (strlen (term) > NOTMUCH_TERM_MAX)
1275 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1278 message->doc.remove_term (term);
1279 message->modified = TRUE;
1280 } catch (const Xapian::InvalidArgumentError) {
1281 /* We'll let the philosophers try to wrestle with the
1282 * question of whether failing to remove that which was not
1283 * there in the first place is failure. For us, we'll silently
1284 * consider it all good. */
1289 _notmuch_message_invalidate_metadata (message, prefix_name);
1291 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1294 notmuch_private_status_t
1295 _notmuch_message_has_term (notmuch_message_t *message,
1296 const char *prefix_name,
1298 notmuch_bool_t *result)
1301 notmuch_bool_t out = FALSE;
1302 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
1305 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1307 term = talloc_asprintf (message, "%s%s",
1308 _find_prefix (prefix_name), value);
1310 if (strlen (term) > NOTMUCH_TERM_MAX)
1311 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1314 /* Look for the exact term */
1315 Xapian::TermIterator i = message->doc.termlist_begin ();
1317 if (i != message->doc.termlist_end () &&
1318 !strcmp ((*i).c_str (), term))
1320 } catch (Xapian::Error &error) {
1321 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1330 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1332 notmuch_private_status_t private_status;
1333 notmuch_status_t status;
1335 status = _notmuch_database_ensure_writable (message->notmuch);
1340 return NOTMUCH_STATUS_NULL_POINTER;
1342 if (strlen (tag) > NOTMUCH_TAG_MAX)
1343 return NOTMUCH_STATUS_TAG_TOO_LONG;
1345 private_status = _notmuch_message_add_term (message, "tag", tag);
1346 if (private_status) {
1347 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
1351 if (! message->frozen)
1352 _notmuch_message_sync (message);
1354 return NOTMUCH_STATUS_SUCCESS;
1358 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1360 notmuch_private_status_t private_status;
1361 notmuch_status_t status;
1363 status = _notmuch_database_ensure_writable (message->notmuch);
1368 return NOTMUCH_STATUS_NULL_POINTER;
1370 if (strlen (tag) > NOTMUCH_TAG_MAX)
1371 return NOTMUCH_STATUS_TAG_TOO_LONG;
1373 private_status = _notmuch_message_remove_term (message, "tag", tag);
1374 if (private_status) {
1375 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1379 if (! message->frozen)
1380 _notmuch_message_sync (message);
1382 return NOTMUCH_STATUS_SUCCESS;
1385 /* Is the given filename within a maildir directory?
1387 * Specifically, is the final directory component of 'filename' either
1388 * "cur" or "new". If so, return a pointer to that final directory
1389 * component within 'filename'. If not, return NULL.
1391 * A non-NULL return value is guaranteed to be a valid string pointer
1392 * pointing to the characters "new/" or "cur/", (but not
1396 _filename_is_in_maildir (const char *filename)
1398 const char *slash, *dir = NULL;
1400 /* Find the last '/' separating directory from filename. */
1401 slash = strrchr (filename, '/');
1405 /* Jump back 4 characters to where the previous '/' will be if the
1406 * directory is named "cur" or "new". */
1407 if (slash - filename < 4)
1417 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1418 STRNCMP_LITERAL (dir, "new/") == 0)
1427 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1430 notmuch_status_t status;
1431 notmuch_filenames_t *filenames;
1432 const char *filename, *dir;
1433 char *combined_flags = talloc_strdup (message, "");
1435 int seen_maildir_info = 0;
1437 for (filenames = notmuch_message_get_filenames (message);
1438 notmuch_filenames_valid (filenames);
1439 notmuch_filenames_move_to_next (filenames))
1441 filename = notmuch_filenames_get (filenames);
1442 dir = _filename_is_in_maildir (filename);
1447 flags = strstr (filename, ":2,");
1449 seen_maildir_info = 1;
1451 combined_flags = talloc_strdup_append (combined_flags, flags);
1452 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1453 /* Messages are delivered to new/ with no "info" part, but
1454 * they effectively have default maildir flags. According
1455 * to the spec, we should ignore the info part for
1456 * messages in new/, but some MUAs (mutt) can set maildir
1457 * flags on messages in new/, so we're liberal in what we
1459 seen_maildir_info = 1;
1463 /* If none of the filenames have any maildir info field (not even
1464 * an empty info with no flags set) then there's no information to
1465 * go on, so do nothing. */
1466 if (! seen_maildir_info)
1467 return NOTMUCH_STATUS_SUCCESS;
1469 status = notmuch_message_freeze (message);
1473 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1474 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
1476 flag2tag[i].inverse)
1478 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1480 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1485 status = notmuch_message_thaw (message);
1487 talloc_free (combined_flags);
1492 /* From the set of tags on 'message' and the flag2tag table, compute a
1493 * set of maildir-flag actions to be taken, (flags that should be
1494 * either set or cleared).
1496 * The result is returned as two talloced strings: to_set, and to_clear
1499 _get_maildir_flag_actions (notmuch_message_t *message,
1501 char **to_clear_ret)
1503 char *to_set, *to_clear;
1504 notmuch_tags_t *tags;
1508 to_set = talloc_strdup (message, "");
1509 to_clear = talloc_strdup (message, "");
1511 /* First, find flags for all set tags. */
1512 for (tags = notmuch_message_get_tags (message);
1513 notmuch_tags_valid (tags);
1514 notmuch_tags_move_to_next (tags))
1516 tag = notmuch_tags_get (tags);
1518 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1519 if (strcmp (tag, flag2tag[i].tag) == 0) {
1520 if (flag2tag[i].inverse)
1521 to_clear = talloc_asprintf_append (to_clear,
1525 to_set = talloc_asprintf_append (to_set,
1532 /* Then, find the flags for all tags not present. */
1533 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1534 if (flag2tag[i].inverse) {
1535 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1536 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1538 if (strchr (to_set, flag2tag[i].flag) == NULL)
1539 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1543 *to_set_ret = to_set;
1544 *to_clear_ret = to_clear;
1547 /* Given 'filename' and a set of maildir flags to set and to clear,
1548 * compute the new maildir filename.
1550 * If the existing filename is in the directory "new", the new
1551 * filename will be in the directory "cur", except for the case when
1552 * no flags are changed and the existing filename does not contain
1553 * maildir info (starting with ",2:").
1555 * After a sequence of ":2," in the filename, any subsequent
1556 * single-character flags will be added or removed according to the
1557 * characters in flags_to_set and flags_to_clear. Any existing flags
1558 * not mentioned in either string will remain. The final list of flags
1559 * will be in ASCII order.
1561 * If the original flags seem invalid, (repeated characters or
1562 * non-ASCII ordering of flags), this function will return NULL
1563 * (meaning that renaming would not be safe and should not occur).
1566 _new_maildir_filename (void *ctx,
1567 const char *filename,
1568 const char *flags_to_set,
1569 const char *flags_to_clear)
1571 const char *info, *flags;
1572 unsigned int flag, last_flag;
1573 char *filename_new, *dir;
1575 int flags_in_map = 0;
1576 notmuch_bool_t flags_changed = FALSE;
1580 memset (flag_map, 0, sizeof (flag_map));
1582 info = strstr (filename, ":2,");
1585 info = filename + strlen(filename);
1587 /* Loop through existing flags in filename. */
1588 for (flags = info + 3, last_flag = 0;
1590 last_flag = flag, flags++)
1594 /* Original flags not in ASCII order. Abort. */
1595 if (flag < last_flag)
1598 /* Non-ASCII flag. Abort. */
1599 if (flag > sizeof(flag_map) - 1)
1602 /* Repeated flag value. Abort. */
1611 /* Then set and clear our flags from tags. */
1612 for (flags = flags_to_set; *flags; flags++) {
1614 if (flag_map[flag] == 0) {
1617 flags_changed = TRUE;
1621 for (flags = flags_to_clear; *flags; flags++) {
1623 if (flag_map[flag]) {
1626 flags_changed = TRUE;
1630 /* Messages in new/ without maildir info can be kept in new/ if no
1631 * flags have changed. */
1632 dir = (char *) _filename_is_in_maildir (filename);
1633 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && !*info && !flags_changed)
1634 return talloc_strdup (ctx, filename);
1636 filename_new = (char *) talloc_size (ctx,
1638 strlen (":2,") + flags_in_map + 1);
1639 if (unlikely (filename_new == NULL))
1642 strncpy (filename_new, filename, info - filename);
1643 filename_new[info - filename] = '\0';
1645 strcat (filename_new, ":2,");
1647 s = filename_new + strlen (filename_new);
1648 for (i = 0; i < sizeof (flag_map); i++)
1657 /* If message is in new/ move it under cur/. */
1658 dir = (char *) _filename_is_in_maildir (filename_new);
1659 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1660 memcpy (dir, "cur/", 4);
1662 return filename_new;
1666 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1668 notmuch_filenames_t *filenames;
1669 const char *filename;
1671 char *to_set, *to_clear;
1672 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1674 _get_maildir_flag_actions (message, &to_set, &to_clear);
1676 for (filenames = notmuch_message_get_filenames (message);
1677 notmuch_filenames_valid (filenames);
1678 notmuch_filenames_move_to_next (filenames))
1680 filename = notmuch_filenames_get (filenames);
1682 if (! _filename_is_in_maildir (filename))
1685 filename_new = _new_maildir_filename (message, filename,
1687 if (filename_new == NULL)
1690 if (strcmp (filename, filename_new)) {
1692 notmuch_status_t new_status;
1694 err = rename (filename, filename_new);
1698 new_status = _notmuch_message_remove_filename (message,
1700 /* Hold on to only the first error. */
1701 if (! status && new_status
1702 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1703 status = new_status;
1707 new_status = _notmuch_message_add_filename (message,
1709 /* Hold on to only the first error. */
1710 if (! status && new_status) {
1711 status = new_status;
1715 _notmuch_message_sync (message);
1718 talloc_free (filename_new);
1721 talloc_free (to_set);
1722 talloc_free (to_clear);
1728 notmuch_message_remove_all_tags (notmuch_message_t *message)
1730 notmuch_private_status_t private_status;
1731 notmuch_status_t status;
1732 notmuch_tags_t *tags;
1735 status = _notmuch_database_ensure_writable (message->notmuch);
1739 for (tags = notmuch_message_get_tags (message);
1740 notmuch_tags_valid (tags);
1741 notmuch_tags_move_to_next (tags))
1743 tag = notmuch_tags_get (tags);
1745 private_status = _notmuch_message_remove_term (message, "tag", tag);
1746 if (private_status) {
1747 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1752 if (! message->frozen)
1753 _notmuch_message_sync (message);
1756 return NOTMUCH_STATUS_SUCCESS;
1760 notmuch_message_freeze (notmuch_message_t *message)
1762 notmuch_status_t status;
1764 status = _notmuch_database_ensure_writable (message->notmuch);
1770 return NOTMUCH_STATUS_SUCCESS;
1774 notmuch_message_thaw (notmuch_message_t *message)
1776 notmuch_status_t status;
1778 status = _notmuch_database_ensure_writable (message->notmuch);
1782 if (message->frozen > 0) {
1784 if (message->frozen == 0)
1785 _notmuch_message_sync (message);
1786 return NOTMUCH_STATUS_SUCCESS;
1788 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1793 notmuch_message_destroy (notmuch_message_t *message)
1795 talloc_free (message);
1798 notmuch_database_t *
1799 _notmuch_message_database (notmuch_message_t *message)
1801 return message->notmuch;
1805 _notmuch_message_ensure_property_map (notmuch_message_t *message)
1807 notmuch_string_node_t *node;
1809 if (message->property_map)
1812 if (!message->property_term_list)
1813 _notmuch_message_ensure_metadata (message);
1815 message->property_map = _notmuch_string_map_create (message);
1817 for (node = message->property_term_list->head; node; node = node->next) {
1821 value = index(node->string, '=');
1823 INTERNAL_ERROR ("malformed property term");
1829 _notmuch_string_map_append (message->property_map, key, value);
1833 talloc_free (message->property_term_list);
1834 message->property_term_list = NULL;
1837 notmuch_string_map_t *
1838 _notmuch_message_property_map (notmuch_message_t *message)
1840 _notmuch_message_ensure_property_map (message);
1842 return message->property_map;
1846 _notmuch_message_frozen (notmuch_message_t *message)
1848 return message->frozen;