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 _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;
52 /* last view of database the struct is synced with */
53 unsigned long last_view;
56 Xapian::termcount termpos;
59 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
61 struct maildir_flag_tag {
64 notmuch_bool_t inverse;
67 /* ASCII ordered table of Maildir flags and associated tags */
68 static struct maildir_flag_tag flag2tag[] = {
69 { 'D', "draft", FALSE},
70 { 'F', "flagged", FALSE},
71 { 'P', "passed", FALSE},
72 { 'R', "replied", FALSE},
73 { 'S', "unread", TRUE }
76 /* We end up having to call the destructor explicitly because we had
77 * to use "placement new" in order to initialize C++ objects within a
78 * block that we allocated with talloc. So C++ is making talloc
79 * slightly less simple to use, (we wouldn't need
80 * talloc_set_destructor at all otherwise).
83 _notmuch_message_destructor (notmuch_message_t *message)
85 message->doc.~Document ();
90 static notmuch_message_t *
91 _notmuch_message_create_for_document (const void *talloc_owner,
92 notmuch_database_t *notmuch,
95 notmuch_private_status_t *status)
97 notmuch_message_t *message;
100 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
102 message = talloc (talloc_owner, notmuch_message_t);
103 if (unlikely (message == NULL)) {
105 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
109 message->notmuch = notmuch;
110 message->doc_id = doc_id;
114 message->lazy_flags = 0;
116 /* the message is initially not synchronized with Xapian */
117 message->last_view = 0;
119 /* Each of these will be lazily created as needed. */
120 message->message_id = NULL;
121 message->thread_id = NULL;
122 message->in_reply_to = NULL;
123 message->tag_list = NULL;
124 message->filename_term_list = NULL;
125 message->filename_list = NULL;
126 message->message_file = NULL;
127 message->author = NULL;
128 message->property_term_list = NULL;
129 message->property_map = NULL;
131 message->replies = _notmuch_message_list_create (message);
132 if (unlikely (message->replies == NULL)) {
134 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
138 /* This is C++'s creepy "placement new", which is really just an
139 * ugly way to call a constructor for a pre-allocated object. So
140 * it's really not an error to not be checking for OUT_OF_MEMORY
141 * here, since this "new" isn't actually allocating memory. This
142 * is language-design comedy of the wrong kind. */
144 new (&message->doc) Xapian::Document;
146 talloc_set_destructor (message, _notmuch_message_destructor);
149 message->termpos = 0;
154 /* Create a new notmuch_message_t object for an existing document in
157 * Here, 'talloc owner' is an optional talloc context to which the new
158 * message will belong. This allows for the caller to not bother
159 * calling notmuch_message_destroy on the message, and know that all
160 * memory will be reclaimed when 'talloc_owner' is freed. The caller
161 * still can call notmuch_message_destroy when finished with the
162 * message if desired.
164 * The 'talloc_owner' argument can also be NULL, in which case the
165 * caller *is* responsible for calling notmuch_message_destroy.
167 * If no document exists in the database with document ID of 'doc_id'
168 * then this function returns NULL and optionally sets *status to
169 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
171 * This function can also fail to due lack of available memory,
172 * returning NULL and optionally setting *status to
173 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
175 * The caller can pass NULL for status if uninterested in
176 * distinguishing these two cases.
179 _notmuch_message_create (const void *talloc_owner,
180 notmuch_database_t *notmuch,
182 notmuch_private_status_t *status)
184 Xapian::Document doc;
187 doc = notmuch->xapian_db->get_document (doc_id);
188 } catch (const Xapian::DocNotFoundError &error) {
190 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
194 return _notmuch_message_create_for_document (talloc_owner, notmuch,
195 doc_id, doc, status);
198 /* Create a new notmuch_message_t object for a specific message ID,
199 * (which may or may not already exist in the database).
201 * The 'notmuch' database will be the talloc owner of the returned
204 * This function returns a valid notmuch_message_t whether or not
205 * there is already a document in the database with the given message
206 * ID. These two cases can be distinguished by the value of *status:
209 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
211 * There is already a document with message ID 'message_id' in the
212 * database. The returned message can be used to query/modify the
213 * document. The message may be a ghost message.
215 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
217 * No document with 'message_id' exists in the database. The
218 * returned message contains a newly created document (not yet
219 * added to the database) and a document ID that is known not to
220 * exist in the database. This message is "blank"; that is, it
221 * contains only a message ID and no other metadata. The caller
222 * can modify the message, and a call to _notmuch_message_sync
223 * will add the document to the database.
225 * If an error occurs, this function will return NULL and *status
226 * will be set as appropriate. (The status pointer argument must
230 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
231 const char *message_id,
232 notmuch_private_status_t *status_ret)
234 notmuch_message_t *message;
235 Xapian::Document doc;
239 *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
243 return talloc_steal (notmuch, message);
244 else if (*status_ret)
247 /* If the message ID is too long, substitute its sha1 instead. */
248 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
249 message_id = _notmuch_message_id_compressed (message, message_id);
251 term = talloc_asprintf (NULL, "%s%s",
252 _find_prefix ("id"), message_id);
254 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
258 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
259 INTERNAL_ERROR ("Failure to ensure database is writable.");
262 doc.add_term (term, 0);
265 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
267 doc_id = _notmuch_database_generate_doc_id (notmuch);
268 } catch (const Xapian::Error &error) {
269 _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred creating message: %s\n",
270 error.get_msg().c_str());
271 notmuch->exception_reported = TRUE;
272 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
276 message = _notmuch_message_create_for_document (notmuch, notmuch,
277 doc_id, doc, status_ret);
279 /* We want to inform the caller that we had to create a new
281 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
282 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
288 _notmuch_message_get_term (notmuch_message_t *message,
289 Xapian::TermIterator &i, Xapian::TermIterator &end,
292 int prefix_len = strlen (prefix);
300 const std::string &term = *i;
301 if (strncmp (term.c_str(), prefix, prefix_len))
304 value = talloc_strdup (message, term.c_str() + prefix_len);
306 #if DEBUG_DATABASE_SANITY
309 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
310 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
311 message->doc_id, prefix, value,
312 (*i).c_str () + prefix_len);
320 _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
322 Xapian::TermIterator i, end;
324 if (field && (message->last_view >= message->notmuch->view))
327 const char *thread_prefix = _find_prefix ("thread"),
328 *tag_prefix = _find_prefix ("tag"),
329 *id_prefix = _find_prefix ("id"),
330 *type_prefix = _find_prefix ("type"),
331 *filename_prefix = _find_prefix ("file-direntry"),
332 *property_prefix = _find_prefix ("property"),
333 *replyto_prefix = _find_prefix ("replyto");
335 /* We do this all in a single pass because Xapian decompresses the
336 * term list every time you iterate over it. Thus, while this is
337 * slightly more costly than looking up individual fields if only
338 * one field of the message object is actually used, it's a huge
339 * win as more fields are used. */
340 for (int count=0; count < 3; count++) {
342 i = message->doc.termlist_begin ();
343 end = message->doc.termlist_end ();
346 if (!message->thread_id)
348 _notmuch_message_get_term (message, i, end, thread_prefix);
351 assert (strcmp (thread_prefix, tag_prefix) < 0);
352 if (!message->tag_list) {
354 _notmuch_database_get_terms_with_prefix (message, i, end,
356 _notmuch_string_list_sort (message->tag_list);
360 assert (strcmp (tag_prefix, id_prefix) < 0);
361 if (!message->message_id)
362 message->message_id =
363 _notmuch_message_get_term (message, i, end, id_prefix);
365 /* Get document type */
366 assert (strcmp (id_prefix, type_prefix) < 0);
367 if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {
368 i.skip_to (type_prefix);
369 /* "T" is the prefix "type" fields. See
370 * BOOLEAN_PREFIX_INTERNAL. */
372 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
373 else if (*i == "Tghost")
374 NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
376 INTERNAL_ERROR ("Message without type term");
377 NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
380 /* Get filename list. Here we get only the terms. We lazily
381 * expand them to full file names when needed in
382 * _notmuch_message_ensure_filename_list. */
383 assert (strcmp (type_prefix, filename_prefix) < 0);
384 if (!message->filename_term_list && !message->filename_list)
385 message->filename_term_list =
386 _notmuch_database_get_terms_with_prefix (message, i, end,
390 /* Get property terms. Mimic the setup with filenames above */
391 assert (strcmp (filename_prefix, property_prefix) < 0);
392 if (!message->property_map && !message->property_term_list)
393 message->property_term_list =
394 _notmuch_database_get_terms_with_prefix (message, i, end,
398 assert (strcmp (property_prefix, replyto_prefix) < 0);
399 if (!message->in_reply_to)
400 message->in_reply_to =
401 _notmuch_message_get_term (message, i, end, replyto_prefix);
404 /* It's perfectly valid for a message to have no In-Reply-To
405 * header. For these cases, we return an empty string. */
406 if (!message->in_reply_to)
407 message->in_reply_to = talloc_strdup (message, "");
409 /* all the way without an exception */
411 } catch (const Xapian::DatabaseModifiedError &error) {
412 notmuch_status_t status = _notmuch_database_reopen (message->notmuch);
413 if (status != NOTMUCH_STATUS_SUCCESS)
414 INTERNAL_ERROR ("unhandled error from notmuch_database_reopen: %s\n",
415 notmuch_status_to_string (status));
416 } catch (const Xapian::Error &error) {
417 INTERNAL_ERROR ("A Xapian exception occurred fetching message metadata: %s\n",
418 error.get_msg().c_str());
421 message->last_view = message->notmuch->view;
425 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
426 const char *prefix_name)
428 if (strcmp ("thread", prefix_name) == 0) {
429 talloc_free (message->thread_id);
430 message->thread_id = NULL;
433 if (strcmp ("tag", prefix_name) == 0) {
434 talloc_unlink (message, message->tag_list);
435 message->tag_list = NULL;
438 if (strcmp ("type", prefix_name) == 0) {
439 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
440 NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
443 if (strcmp ("file-direntry", prefix_name) == 0) {
444 talloc_free (message->filename_term_list);
445 talloc_free (message->filename_list);
446 message->filename_term_list = message->filename_list = NULL;
449 if (strcmp ("property", prefix_name) == 0) {
451 if (message->property_term_list)
452 talloc_free (message->property_term_list);
453 message->property_term_list = NULL;
455 if (message->property_map)
456 talloc_unlink (message, message->property_map);
458 message->property_map = NULL;
461 if (strcmp ("replyto", prefix_name) == 0) {
462 talloc_free (message->in_reply_to);
463 message->in_reply_to = NULL;
468 _notmuch_message_get_doc_id (notmuch_message_t *message)
470 return message->doc_id;
474 notmuch_message_get_message_id (notmuch_message_t *message)
476 _notmuch_message_ensure_metadata (message, message->message_id);
477 if (!message->message_id)
478 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
480 return message->message_id;
484 _notmuch_message_ensure_message_file (notmuch_message_t *message)
486 const char *filename;
488 if (message->message_file)
491 filename = notmuch_message_get_filename (message);
492 if (unlikely (filename == NULL))
495 message->message_file = _notmuch_message_file_open_ctx (
496 _notmuch_message_database (message), message, filename);
500 notmuch_message_get_header (notmuch_message_t *message, const char *header)
502 Xapian::valueno slot = Xapian::BAD_VALUENO;
504 /* Fetch header from the appropriate xapian value field if
506 if (strcasecmp (header, "from") == 0)
507 slot = NOTMUCH_VALUE_FROM;
508 else if (strcasecmp (header, "subject") == 0)
509 slot = NOTMUCH_VALUE_SUBJECT;
510 else if (strcasecmp (header, "message-id") == 0)
511 slot = NOTMUCH_VALUE_MESSAGE_ID;
513 if (slot != Xapian::BAD_VALUENO) {
515 std::string value = message->doc.get_value (slot);
517 /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then
518 * empty values indicate empty headers. If we don't, then
519 * it could just mean we didn't record the header. */
520 if ((message->notmuch->features &
521 NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) ||
523 return talloc_strdup (message, value.c_str ());
525 } catch (Xapian::Error &error) {
526 _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading header: %s\n",
527 error.get_msg().c_str());
528 message->notmuch->exception_reported = TRUE;
533 /* Otherwise fall back to parsing the file */
534 _notmuch_message_ensure_message_file (message);
535 if (message->message_file == NULL)
538 return _notmuch_message_file_get_header (message->message_file, header);
541 /* Return the message ID from the In-Reply-To header of 'message'.
543 * Returns an empty string ("") if 'message' has no In-Reply-To
546 * Returns NULL if any error occurs.
549 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
551 _notmuch_message_ensure_metadata (message, message->in_reply_to);
552 return message->in_reply_to;
556 notmuch_message_get_thread_id (notmuch_message_t *message)
558 _notmuch_message_ensure_metadata (message, message->thread_id);
559 if (!message->thread_id)
560 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
562 return message->thread_id;
566 _notmuch_message_add_reply (notmuch_message_t *message,
567 notmuch_message_t *reply)
569 _notmuch_message_list_add_message (message->replies, reply);
573 notmuch_message_get_replies (notmuch_message_t *message)
575 return _notmuch_messages_create (message->replies);
579 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
581 Xapian::TermIterator i;
582 size_t prefix_len = strlen (prefix);
585 i = message->doc.termlist_begin ();
588 /* Terminate loop when no terms remain with desired prefix. */
589 if (i == message->doc.termlist_end () ||
590 strncmp ((*i).c_str (), prefix, prefix_len))
594 message->doc.remove_term ((*i));
595 message->modified = TRUE;
596 } catch (const Xapian::InvalidArgumentError) {
597 /* Ignore failure to remove non-existent term. */
602 /* Return true if p points at "new" or "cur". */
603 static bool is_maildir (const char *p)
605 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
608 /* Add "folder:" term for directory. */
609 static notmuch_status_t
610 _notmuch_message_add_folder_terms (notmuch_message_t *message,
611 const char *directory)
615 folder = talloc_strdup (NULL, directory);
617 return NOTMUCH_STATUS_OUT_OF_MEMORY;
620 * If the message file is in a leaf directory named "new" or
621 * "cur", presume maildir and index the parent directory. Thus a
622 * "folder:" prefix search matches messages in the specified
623 * maildir folder, i.e. in the specified directory and its "new"
624 * and "cur" subdirectories.
626 * Note that this means the "folder:" prefix can't be used for
627 * distinguishing between message files in "new" or "cur". The
628 * "path:" prefix needs to be used for that.
630 * Note the deliberate difference to _filename_is_in_maildir(). We
631 * don't want to index different things depending on the existence
632 * or non-existence of all maildir sibling directories "new",
633 * "cur", and "tmp". Doing so would be surprising, and difficult
634 * for the user to fix in case all subdirectories were not in
635 * place during indexing.
637 last = strrchr (folder, '/');
639 if (is_maildir (last + 1))
641 } else if (is_maildir (folder)) {
645 _notmuch_message_add_term (message, "folder", folder);
647 talloc_free (folder);
649 return NOTMUCH_STATUS_SUCCESS;
652 #define RECURSIVE_SUFFIX "/**"
654 /* Add "path:" terms for directory. */
655 static notmuch_status_t
656 _notmuch_message_add_path_terms (notmuch_message_t *message,
657 const char *directory)
659 /* Add exact "path:" term. */
660 _notmuch_message_add_term (message, "path", directory);
662 if (strlen (directory)) {
665 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
667 return NOTMUCH_STATUS_OUT_OF_MEMORY;
669 /* Add recursive "path:" terms for directory and all parents. */
670 for (p = path + strlen (path) - 1; p > path; p--) {
672 strcpy (p, RECURSIVE_SUFFIX);
673 _notmuch_message_add_term (message, "path", path);
680 /* Recursive all-matching path:** for consistency. */
681 _notmuch_message_add_term (message, "path", "**");
683 return NOTMUCH_STATUS_SUCCESS;
686 /* Add directory based terms for all filenames of the message. */
687 static notmuch_status_t
688 _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
690 const char *direntry_prefix = _find_prefix ("file-direntry");
691 int direntry_prefix_len = strlen (direntry_prefix);
692 Xapian::TermIterator i = message->doc.termlist_begin ();
693 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
695 for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {
696 unsigned int directory_id;
697 const char *direntry, *directory;
699 const std::string &term = *i;
701 /* Terminate loop at first term without desired prefix. */
702 if (strncmp (term.c_str (), direntry_prefix, direntry_prefix_len))
705 /* Indicate that there are filenames remaining. */
706 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
708 direntry = term.c_str ();
709 direntry += direntry_prefix_len;
711 directory_id = strtol (direntry, &colon, 10);
713 if (colon == NULL || *colon != ':')
714 INTERNAL_ERROR ("malformed direntry");
716 directory = _notmuch_database_get_directory_path (ctx,
720 _notmuch_message_add_folder_terms (message, directory);
721 _notmuch_message_add_path_terms (message, directory);
727 /* Add an additional 'filename' for 'message'.
729 * This change will not be reflected in the database until the next
730 * call to _notmuch_message_sync. */
732 _notmuch_message_add_filename (notmuch_message_t *message,
733 const char *filename)
735 const char *relative, *directory;
736 notmuch_status_t status;
737 void *local = talloc_new (message);
740 if (filename == NULL)
741 INTERNAL_ERROR ("Message filename cannot be NULL.");
743 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
744 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
745 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
747 relative = _notmuch_database_relative_path (message->notmuch, filename);
749 status = _notmuch_database_split_path (local, relative, &directory, NULL);
753 status = _notmuch_database_filename_to_direntry (
754 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
758 /* New file-direntry allows navigating to this message with
759 * notmuch_directory_get_child_files() . */
760 _notmuch_message_add_term (message, "file-direntry", direntry);
762 _notmuch_message_add_folder_terms (message, directory);
763 _notmuch_message_add_path_terms (message, directory);
767 return NOTMUCH_STATUS_SUCCESS;
770 /* Remove a particular 'filename' from 'message'.
772 * This change will not be reflected in the database until the next
773 * call to _notmuch_message_sync.
775 * If this message still has other filenames, returns
776 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
778 * Note: This function does not remove a document from the database,
779 * even if the specified filename is the only filename for this
780 * message. For that functionality, see
781 * notmuch_database_remove_message. */
783 _notmuch_message_remove_filename (notmuch_message_t *message,
784 const char *filename)
786 void *local = talloc_new (message);
788 notmuch_private_status_t private_status;
789 notmuch_status_t status;
791 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
792 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
793 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
795 status = _notmuch_database_filename_to_direntry (
796 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
797 if (status || !direntry)
800 /* Unlink this file from its parent directory. */
801 private_status = _notmuch_message_remove_term (message,
802 "file-direntry", direntry);
803 status = COERCE_STATUS (private_status,
804 "Unexpected error from _notmuch_message_remove_term");
808 /* Re-synchronize "folder:" and "path:" terms for this message. */
810 /* Remove all "folder:" terms. */
811 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
813 /* Remove all "path:" terms. */
814 _notmuch_message_remove_terms (message, _find_prefix ("path"));
816 /* Add back terms for all remaining filenames of the message. */
817 status = _notmuch_message_add_directory_terms (local, message);
824 /* Upgrade the "folder:" prefix from V1 to V2. */
825 #define FOLDER_PREFIX_V1 "XFOLDER"
826 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
828 _notmuch_message_upgrade_folder (notmuch_message_t *message)
830 /* Remove all old "folder:" terms. */
831 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
833 /* Remove all old "folder:" stemmed terms. */
834 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
836 /* Add new boolean "folder:" and "path:" terms. */
837 _notmuch_message_add_directory_terms (message, message);
841 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
843 return talloc_strdup (message, message->doc.get_data ().c_str ());
847 _notmuch_message_clear_data (notmuch_message_t *message)
849 message->doc.set_data ("");
850 message->modified = TRUE;
854 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
856 notmuch_string_node_t *node;
858 if (message->filename_list)
861 _notmuch_message_ensure_metadata (message, message->filename_term_list);
863 message->filename_list = _notmuch_string_list_create (message);
864 node = message->filename_term_list->head;
867 /* A message document created by an old version of notmuch
868 * (prior to rename support) will have the filename in the
869 * data of the document rather than as a file-direntry term.
871 * It would be nice to do the upgrade of the document directly
872 * here, but the database is likely open in read-only mode. */
874 std::string datastr = message->doc.get_data ();
875 const char *data = datastr.c_str ();
878 INTERNAL_ERROR ("message with no filename");
880 _notmuch_string_list_append (message->filename_list, data);
885 for (; node; node = node->next) {
886 void *local = talloc_new (message);
887 const char *db_path, *directory, *basename, *filename;
888 char *colon, *direntry = NULL;
889 unsigned int directory_id;
891 direntry = node->string;
893 directory_id = strtol (direntry, &colon, 10);
895 if (colon == NULL || *colon != ':')
896 INTERNAL_ERROR ("malformed direntry");
898 basename = colon + 1;
902 db_path = notmuch_database_get_path (message->notmuch);
904 directory = _notmuch_database_get_directory_path (local,
908 if (strlen (directory))
909 filename = talloc_asprintf (message, "%s/%s/%s",
910 db_path, directory, basename);
912 filename = talloc_asprintf (message, "%s/%s",
915 _notmuch_string_list_append (message->filename_list, filename);
920 talloc_free (message->filename_term_list);
921 message->filename_term_list = NULL;
925 notmuch_message_get_filename (notmuch_message_t *message)
927 _notmuch_message_ensure_filename_list (message);
929 if (message->filename_list == NULL)
932 if (message->filename_list->head == NULL ||
933 message->filename_list->head->string == NULL)
935 INTERNAL_ERROR ("message with no filename");
938 return message->filename_list->head->string;
941 notmuch_filenames_t *
942 notmuch_message_get_filenames (notmuch_message_t *message)
944 _notmuch_message_ensure_filename_list (message);
946 return _notmuch_filenames_create (message, message->filename_list);
950 notmuch_message_get_flag (notmuch_message_t *message,
951 notmuch_message_flag_t flag)
953 if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
954 ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
955 _notmuch_message_ensure_metadata (message, NULL);
957 return NOTMUCH_TEST_BIT (message->flags, flag);
961 notmuch_message_set_flag (notmuch_message_t *message,
962 notmuch_message_flag_t flag, notmuch_bool_t enable)
965 NOTMUCH_SET_BIT (&message->flags, flag);
967 NOTMUCH_CLEAR_BIT (&message->flags, flag);
968 NOTMUCH_SET_BIT (&message->lazy_flags, flag);
972 notmuch_message_get_date (notmuch_message_t *message)
977 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
978 } catch (Xapian::Error &error) {
979 _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading date: %s\n",
980 error.get_msg().c_str());
981 message->notmuch->exception_reported = TRUE;
986 /* sortable_unserialise is undefined on empty string */
988 return Xapian::sortable_unserialise (value);
992 notmuch_message_get_tags (notmuch_message_t *message)
994 notmuch_tags_t *tags;
996 _notmuch_message_ensure_metadata (message, message->tag_list);
998 tags = _notmuch_tags_create (message, message->tag_list);
999 /* _notmuch_tags_create steals the reference to the tag_list, but
1000 * in this case it's still used by the message, so we add an
1001 * *additional* talloc reference to the list. As a result, it's
1002 * possible to modify the message tags (which talloc_unlink's the
1003 * current list from the message) while still iterating because
1004 * the iterator will keep the current list alive. */
1005 if (!talloc_reference (message, message->tag_list))
1012 _notmuch_message_get_author (notmuch_message_t *message)
1014 return message->author;
1018 _notmuch_message_set_author (notmuch_message_t *message,
1021 if (message->author)
1022 talloc_free(message->author);
1023 message->author = talloc_strdup(message, author);
1028 _notmuch_message_set_header_values (notmuch_message_t *message,
1031 const char *subject)
1035 /* GMime really doesn't want to see a NULL date, so protect its
1037 if (date == NULL || *date == '\0') {
1040 time_value = g_mime_utils_header_decode_date_unix (date);
1042 * Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=779923
1048 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
1049 Xapian::sortable_serialise (time_value));
1050 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
1051 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1052 message->modified = TRUE;
1055 /* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD. The caller
1056 * must call _notmuch_message_sync. */
1058 _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
1060 /* _notmuch_message_sync will update the last modification
1061 * revision; we just have to ask it to. */
1062 message->modified = TRUE;
1065 /* Synchronize changes made to message->doc out into the database. */
1067 _notmuch_message_sync (notmuch_message_t *message)
1069 Xapian::WritableDatabase *db;
1071 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
1074 if (! message->modified)
1077 /* Update the last modification of this message. */
1078 if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)
1079 /* sortable_serialise gives a reasonably compact encoding,
1080 * which directly translates to reduced IO when scanning the
1081 * value stream. Since it's built for doubles, we only get 53
1082 * effective bits, but that's still enough for the database to
1083 * last a few centuries at 1 million revisions per second. */
1084 message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,
1085 Xapian::sortable_serialise (
1086 _notmuch_database_new_revision (
1087 message->notmuch)));
1089 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
1090 db->replace_document (message->doc_id, message->doc);
1091 message->modified = FALSE;
1094 /* Delete a message document from the database, leaving a ghost
1095 * message in its place */
1097 _notmuch_message_delete (notmuch_message_t *message)
1099 notmuch_status_t status;
1100 Xapian::WritableDatabase *db;
1101 const char *mid, *tid, *query_string;
1102 notmuch_message_t *ghost;
1103 notmuch_private_status_t private_status;
1104 notmuch_database_t *notmuch;
1105 notmuch_query_t *query;
1106 unsigned int count = 0;
1107 notmuch_bool_t is_ghost;
1109 mid = notmuch_message_get_message_id (message);
1110 tid = notmuch_message_get_thread_id (message);
1111 notmuch = message->notmuch;
1113 status = _notmuch_database_ensure_writable (message->notmuch);
1117 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1118 db->delete_document (message->doc_id);
1120 /* if this was a ghost to begin with, we are done */
1121 private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
1123 return COERCE_STATUS (private_status,
1124 "Error trying to determine whether message was a ghost");
1126 return NOTMUCH_STATUS_SUCCESS;
1128 query_string = talloc_asprintf (message, "thread:%s", tid);
1129 query = notmuch_query_create (notmuch, query_string);
1131 return NOTMUCH_STATUS_OUT_OF_MEMORY;
1132 status = notmuch_query_count_messages (query, &count);
1134 notmuch_query_destroy (query);
1139 /* reintroduce a ghost in its place because there are still
1140 * other active messages in this thread: */
1141 ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);
1142 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1143 private_status = _notmuch_message_initialize_ghost (ghost, tid);
1144 if (! private_status)
1145 _notmuch_message_sync (ghost);
1146 } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1147 /* this is deeply weird, and we should not have gotten
1148 into this state. is there a better error message to
1150 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1153 notmuch_message_destroy (ghost);
1154 status = COERCE_STATUS (private_status, "Error converting to ghost message");
1156 /* the thread is empty; drop all ghost messages from it */
1157 notmuch_messages_t *messages;
1158 status = _notmuch_query_search_documents (query,
1161 if (status == NOTMUCH_STATUS_SUCCESS) {
1162 notmuch_status_t last_error = NOTMUCH_STATUS_SUCCESS;
1163 while (notmuch_messages_valid (messages)) {
1164 message = notmuch_messages_get (messages);
1165 status = _notmuch_message_delete (message);
1166 if (status) /* we'll report the last failure we see;
1167 * if there is more than one failure, we
1168 * forget about previous ones */
1169 last_error = status;
1170 notmuch_message_destroy (message);
1171 notmuch_messages_move_to_next (messages);
1173 status = last_error;
1176 notmuch_query_destroy (query);
1180 /* Transform a blank message into a ghost message. The caller must
1181 * _notmuch_message_sync the message. */
1182 notmuch_private_status_t
1183 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1184 const char *thread_id)
1186 notmuch_private_status_t status;
1188 status = _notmuch_message_add_term (message, "type", "ghost");
1191 status = _notmuch_message_add_term (message, "thread", thread_id);
1195 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1198 /* Ensure that 'message' is not holding any file object open. Future
1199 * calls to various functions will still automatically open the
1200 * message file as needed.
1203 _notmuch_message_close (notmuch_message_t *message)
1205 if (message->message_file) {
1206 _notmuch_message_file_close (message->message_file);
1207 message->message_file = NULL;
1211 /* Add a name:value term to 'message', (the actual term will be
1212 * encoded by prefixing the value with a short prefix). See
1213 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1214 * names to prefix values.
1216 * This change will not be reflected in the database until the next
1217 * call to _notmuch_message_sync. */
1218 notmuch_private_status_t
1219 _notmuch_message_add_term (notmuch_message_t *message,
1220 const char *prefix_name,
1227 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1229 term = talloc_asprintf (message, "%s%s",
1230 _find_prefix (prefix_name), value);
1232 if (strlen (term) > NOTMUCH_TERM_MAX)
1233 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1235 message->doc.add_term (term, 0);
1236 message->modified = TRUE;
1240 _notmuch_message_invalidate_metadata (message, prefix_name);
1242 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1245 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1246 * term will be added both prefixed (if prefix_name is not NULL) and
1247 * also non-prefixed). */
1248 notmuch_private_status_t
1249 _notmuch_message_gen_terms (notmuch_message_t *message,
1250 const char *prefix_name,
1253 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1256 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1258 term_gen->set_document (message->doc);
1261 const char *prefix = _find_prefix (prefix_name);
1263 term_gen->set_termpos (message->termpos);
1264 term_gen->index_text (text, 1, prefix);
1265 /* Create a gap between this an the next terms so they don't
1266 * appear to be a phrase. */
1267 message->termpos = term_gen->get_termpos () + 100;
1269 _notmuch_message_invalidate_metadata (message, prefix_name);
1272 term_gen->set_termpos (message->termpos);
1273 term_gen->index_text (text);
1274 /* Create a term gap, as above. */
1275 message->termpos = term_gen->get_termpos () + 100;
1277 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1280 /* Remove a name:value term from 'message', (the actual term will be
1281 * encoded by prefixing the value with a short prefix). See
1282 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1283 * names to prefix values.
1285 * This change will not be reflected in the database until the next
1286 * call to _notmuch_message_sync. */
1287 notmuch_private_status_t
1288 _notmuch_message_remove_term (notmuch_message_t *message,
1289 const char *prefix_name,
1295 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1297 term = talloc_asprintf (message, "%s%s",
1298 _find_prefix (prefix_name), value);
1300 if (strlen (term) > NOTMUCH_TERM_MAX)
1301 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1304 message->doc.remove_term (term);
1305 message->modified = TRUE;
1306 } catch (const Xapian::InvalidArgumentError) {
1307 /* We'll let the philosophers try to wrestle with the
1308 * question of whether failing to remove that which was not
1309 * there in the first place is failure. For us, we'll silently
1310 * consider it all good. */
1315 _notmuch_message_invalidate_metadata (message, prefix_name);
1317 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1320 notmuch_private_status_t
1321 _notmuch_message_has_term (notmuch_message_t *message,
1322 const char *prefix_name,
1324 notmuch_bool_t *result)
1327 notmuch_bool_t out = FALSE;
1328 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
1331 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1333 term = talloc_asprintf (message, "%s%s",
1334 _find_prefix (prefix_name), value);
1336 if (strlen (term) > NOTMUCH_TERM_MAX)
1337 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1340 /* Look for the exact term */
1341 Xapian::TermIterator i = message->doc.termlist_begin ();
1343 if (i != message->doc.termlist_end () &&
1344 !strcmp ((*i).c_str (), term))
1346 } catch (Xapian::Error &error) {
1347 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1356 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1358 notmuch_private_status_t private_status;
1359 notmuch_status_t status;
1361 status = _notmuch_database_ensure_writable (message->notmuch);
1366 return NOTMUCH_STATUS_NULL_POINTER;
1368 if (strlen (tag) > NOTMUCH_TAG_MAX)
1369 return NOTMUCH_STATUS_TAG_TOO_LONG;
1371 private_status = _notmuch_message_add_term (message, "tag", tag);
1372 if (private_status) {
1373 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
1377 if (! message->frozen)
1378 _notmuch_message_sync (message);
1380 return NOTMUCH_STATUS_SUCCESS;
1384 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1386 notmuch_private_status_t private_status;
1387 notmuch_status_t status;
1389 status = _notmuch_database_ensure_writable (message->notmuch);
1394 return NOTMUCH_STATUS_NULL_POINTER;
1396 if (strlen (tag) > NOTMUCH_TAG_MAX)
1397 return NOTMUCH_STATUS_TAG_TOO_LONG;
1399 private_status = _notmuch_message_remove_term (message, "tag", tag);
1400 if (private_status) {
1401 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1405 if (! message->frozen)
1406 _notmuch_message_sync (message);
1408 return NOTMUCH_STATUS_SUCCESS;
1411 /* Is the given filename within a maildir directory?
1413 * Specifically, is the final directory component of 'filename' either
1414 * "cur" or "new". If so, return a pointer to that final directory
1415 * component within 'filename'. If not, return NULL.
1417 * A non-NULL return value is guaranteed to be a valid string pointer
1418 * pointing to the characters "new/" or "cur/", (but not
1422 _filename_is_in_maildir (const char *filename)
1424 const char *slash, *dir = NULL;
1426 /* Find the last '/' separating directory from filename. */
1427 slash = strrchr (filename, '/');
1431 /* Jump back 4 characters to where the previous '/' will be if the
1432 * directory is named "cur" or "new". */
1433 if (slash - filename < 4)
1443 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1444 STRNCMP_LITERAL (dir, "new/") == 0)
1453 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1456 notmuch_status_t status;
1457 notmuch_filenames_t *filenames;
1458 const char *filename, *dir;
1459 char *combined_flags = talloc_strdup (message, "");
1461 int seen_maildir_info = 0;
1463 for (filenames = notmuch_message_get_filenames (message);
1464 notmuch_filenames_valid (filenames);
1465 notmuch_filenames_move_to_next (filenames))
1467 filename = notmuch_filenames_get (filenames);
1468 dir = _filename_is_in_maildir (filename);
1473 flags = strstr (filename, ":2,");
1475 seen_maildir_info = 1;
1477 combined_flags = talloc_strdup_append (combined_flags, flags);
1478 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1479 /* Messages are delivered to new/ with no "info" part, but
1480 * they effectively have default maildir flags. According
1481 * to the spec, we should ignore the info part for
1482 * messages in new/, but some MUAs (mutt) can set maildir
1483 * flags on messages in new/, so we're liberal in what we
1485 seen_maildir_info = 1;
1489 /* If none of the filenames have any maildir info field (not even
1490 * an empty info with no flags set) then there's no information to
1491 * go on, so do nothing. */
1492 if (! seen_maildir_info)
1493 return NOTMUCH_STATUS_SUCCESS;
1495 status = notmuch_message_freeze (message);
1499 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1500 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
1502 flag2tag[i].inverse)
1504 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1506 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1511 status = notmuch_message_thaw (message);
1513 talloc_free (combined_flags);
1518 /* From the set of tags on 'message' and the flag2tag table, compute a
1519 * set of maildir-flag actions to be taken, (flags that should be
1520 * either set or cleared).
1522 * The result is returned as two talloced strings: to_set, and to_clear
1525 _get_maildir_flag_actions (notmuch_message_t *message,
1527 char **to_clear_ret)
1529 char *to_set, *to_clear;
1530 notmuch_tags_t *tags;
1534 to_set = talloc_strdup (message, "");
1535 to_clear = talloc_strdup (message, "");
1537 /* First, find flags for all set tags. */
1538 for (tags = notmuch_message_get_tags (message);
1539 notmuch_tags_valid (tags);
1540 notmuch_tags_move_to_next (tags))
1542 tag = notmuch_tags_get (tags);
1544 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1545 if (strcmp (tag, flag2tag[i].tag) == 0) {
1546 if (flag2tag[i].inverse)
1547 to_clear = talloc_asprintf_append (to_clear,
1551 to_set = talloc_asprintf_append (to_set,
1558 /* Then, find the flags for all tags not present. */
1559 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1560 if (flag2tag[i].inverse) {
1561 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1562 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1564 if (strchr (to_set, flag2tag[i].flag) == NULL)
1565 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1569 *to_set_ret = to_set;
1570 *to_clear_ret = to_clear;
1573 /* Given 'filename' and a set of maildir flags to set and to clear,
1574 * compute the new maildir filename.
1576 * If the existing filename is in the directory "new", the new
1577 * filename will be in the directory "cur", except for the case when
1578 * no flags are changed and the existing filename does not contain
1579 * maildir info (starting with ",2:").
1581 * After a sequence of ":2," in the filename, any subsequent
1582 * single-character flags will be added or removed according to the
1583 * characters in flags_to_set and flags_to_clear. Any existing flags
1584 * not mentioned in either string will remain. The final list of flags
1585 * will be in ASCII order.
1587 * If the original flags seem invalid, (repeated characters or
1588 * non-ASCII ordering of flags), this function will return NULL
1589 * (meaning that renaming would not be safe and should not occur).
1592 _new_maildir_filename (void *ctx,
1593 const char *filename,
1594 const char *flags_to_set,
1595 const char *flags_to_clear)
1597 const char *info, *flags;
1598 unsigned int flag, last_flag;
1599 char *filename_new, *dir;
1601 int flags_in_map = 0;
1602 notmuch_bool_t flags_changed = FALSE;
1606 memset (flag_map, 0, sizeof (flag_map));
1608 info = strstr (filename, ":2,");
1611 info = filename + strlen(filename);
1613 /* Loop through existing flags in filename. */
1614 for (flags = info + 3, last_flag = 0;
1616 last_flag = flag, flags++)
1620 /* Original flags not in ASCII order. Abort. */
1621 if (flag < last_flag)
1624 /* Non-ASCII flag. Abort. */
1625 if (flag > sizeof(flag_map) - 1)
1628 /* Repeated flag value. Abort. */
1637 /* Then set and clear our flags from tags. */
1638 for (flags = flags_to_set; *flags; flags++) {
1640 if (flag_map[flag] == 0) {
1643 flags_changed = TRUE;
1647 for (flags = flags_to_clear; *flags; flags++) {
1649 if (flag_map[flag]) {
1652 flags_changed = TRUE;
1656 /* Messages in new/ without maildir info can be kept in new/ if no
1657 * flags have changed. */
1658 dir = (char *) _filename_is_in_maildir (filename);
1659 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && !*info && !flags_changed)
1660 return talloc_strdup (ctx, filename);
1662 filename_new = (char *) talloc_size (ctx,
1664 strlen (":2,") + flags_in_map + 1);
1665 if (unlikely (filename_new == NULL))
1668 strncpy (filename_new, filename, info - filename);
1669 filename_new[info - filename] = '\0';
1671 strcat (filename_new, ":2,");
1673 s = filename_new + strlen (filename_new);
1674 for (i = 0; i < sizeof (flag_map); i++)
1683 /* If message is in new/ move it under cur/. */
1684 dir = (char *) _filename_is_in_maildir (filename_new);
1685 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1686 memcpy (dir, "cur/", 4);
1688 return filename_new;
1692 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1694 notmuch_filenames_t *filenames;
1695 const char *filename;
1697 char *to_set, *to_clear;
1698 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1700 _get_maildir_flag_actions (message, &to_set, &to_clear);
1702 for (filenames = notmuch_message_get_filenames (message);
1703 notmuch_filenames_valid (filenames);
1704 notmuch_filenames_move_to_next (filenames))
1706 filename = notmuch_filenames_get (filenames);
1708 if (! _filename_is_in_maildir (filename))
1711 filename_new = _new_maildir_filename (message, filename,
1713 if (filename_new == NULL)
1716 if (strcmp (filename, filename_new)) {
1718 notmuch_status_t new_status;
1720 err = rename (filename, filename_new);
1724 new_status = _notmuch_message_remove_filename (message,
1726 /* Hold on to only the first error. */
1727 if (! status && new_status
1728 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1729 status = new_status;
1733 new_status = _notmuch_message_add_filename (message,
1735 /* Hold on to only the first error. */
1736 if (! status && new_status) {
1737 status = new_status;
1741 _notmuch_message_sync (message);
1744 talloc_free (filename_new);
1747 talloc_free (to_set);
1748 talloc_free (to_clear);
1754 notmuch_message_remove_all_tags (notmuch_message_t *message)
1756 notmuch_private_status_t private_status;
1757 notmuch_status_t status;
1758 notmuch_tags_t *tags;
1761 status = _notmuch_database_ensure_writable (message->notmuch);
1765 for (tags = notmuch_message_get_tags (message);
1766 notmuch_tags_valid (tags);
1767 notmuch_tags_move_to_next (tags))
1769 tag = notmuch_tags_get (tags);
1771 private_status = _notmuch_message_remove_term (message, "tag", tag);
1772 if (private_status) {
1773 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1778 if (! message->frozen)
1779 _notmuch_message_sync (message);
1782 return NOTMUCH_STATUS_SUCCESS;
1786 notmuch_message_freeze (notmuch_message_t *message)
1788 notmuch_status_t status;
1790 status = _notmuch_database_ensure_writable (message->notmuch);
1796 return NOTMUCH_STATUS_SUCCESS;
1800 notmuch_message_thaw (notmuch_message_t *message)
1802 notmuch_status_t status;
1804 status = _notmuch_database_ensure_writable (message->notmuch);
1808 if (message->frozen > 0) {
1810 if (message->frozen == 0)
1811 _notmuch_message_sync (message);
1812 return NOTMUCH_STATUS_SUCCESS;
1814 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1819 notmuch_message_destroy (notmuch_message_t *message)
1821 talloc_free (message);
1824 notmuch_database_t *
1825 _notmuch_message_database (notmuch_message_t *message)
1827 return message->notmuch;
1831 _notmuch_message_ensure_property_map (notmuch_message_t *message)
1833 notmuch_string_node_t *node;
1835 if (message->property_map)
1838 _notmuch_message_ensure_metadata (message, message->property_term_list);
1840 message->property_map = _notmuch_string_map_create (message);
1842 for (node = message->property_term_list->head; node; node = node->next) {
1846 value = strchr(node->string, '=');
1848 INTERNAL_ERROR ("malformed property term");
1854 _notmuch_string_map_append (message->property_map, key, value);
1858 talloc_free (message->property_term_list);
1859 message->property_term_list = NULL;
1862 notmuch_string_map_t *
1863 _notmuch_message_property_map (notmuch_message_t *message)
1865 _notmuch_message_ensure_property_map (message);
1867 return message->property_map;
1871 _notmuch_message_frozen (notmuch_message_t *message)
1873 return message->frozen;