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 http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
26 #include <gmime/gmime.h>
28 struct _notmuch_message {
29 notmuch_database_t *notmuch;
37 notmuch_message_file_t *message_file;
38 notmuch_message_list_t *replies;
44 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
46 struct maildir_flag_tag {
52 /* ASCII ordered table of Maildir flags and associated tags */
53 struct maildir_flag_tag flag2tag[] = {
54 { 'D', "draft", false},
55 { 'F', "flagged", false},
56 { 'P', "passed", false},
57 { 'R', "replied", false},
58 { 'S', "unread", true }
61 /* We end up having to call the destructor explicitly because we had
62 * to use "placement new" in order to initialize C++ objects within a
63 * block that we allocated with talloc. So C++ is making talloc
64 * slightly less simple to use, (we wouldn't need
65 * talloc_set_destructor at all otherwise).
68 _notmuch_message_destructor (notmuch_message_t *message)
70 message->doc.~Document ();
75 static notmuch_message_t *
76 _notmuch_message_create_for_document (const void *talloc_owner,
77 notmuch_database_t *notmuch,
80 notmuch_private_status_t *status)
82 notmuch_message_t *message;
85 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
87 message = talloc (talloc_owner, notmuch_message_t);
88 if (unlikely (message == NULL)) {
90 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
94 message->notmuch = notmuch;
95 message->doc_id = doc_id;
100 /* Each of these will be lazily created as needed. */
101 message->message_id = NULL;
102 message->thread_id = NULL;
103 message->in_reply_to = NULL;
104 message->filename = NULL;
105 message->message_file = NULL;
106 message->author = NULL;
108 message->replies = _notmuch_message_list_create (message);
109 if (unlikely (message->replies == NULL)) {
111 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
115 /* This is C++'s creepy "placement new", which is really just an
116 * ugly way to call a constructor for a pre-allocated object. So
117 * it's really not an error to not be checking for OUT_OF_MEMORY
118 * here, since this "new" isn't actually allocating memory. This
119 * is language-design comedy of the wrong kind. */
121 new (&message->doc) Xapian::Document;
123 talloc_set_destructor (message, _notmuch_message_destructor);
130 /* Create a new notmuch_message_t object for an existing document in
133 * Here, 'talloc owner' is an optional talloc context to which the new
134 * message will belong. This allows for the caller to not bother
135 * calling notmuch_message_destroy on the message, and no that all
136 * memory will be reclaimed with 'talloc_owner' is free. The caller
137 * still can call notmuch_message_destroy when finished with the
138 * message if desired.
140 * The 'talloc_owner' argument can also be NULL, in which case the
141 * caller *is* responsible for calling notmuch_message_destroy.
143 * If no document exists in the database with document ID of 'doc_id'
144 * then this function returns NULL and optionally sets *status to
145 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
147 * This function can also fail to due lack of available memory,
148 * returning NULL and optionally setting *status to
149 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
151 * The caller can pass NULL for status if uninterested in
152 * distinguishing these two cases.
155 _notmuch_message_create (const void *talloc_owner,
156 notmuch_database_t *notmuch,
158 notmuch_private_status_t *status)
160 Xapian::Document doc;
163 doc = notmuch->xapian_db->get_document (doc_id);
164 } catch (const Xapian::DocNotFoundError &error) {
166 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
170 return _notmuch_message_create_for_document (talloc_owner, notmuch,
171 doc_id, doc, status);
174 /* Create a new notmuch_message_t object for a specific message ID,
175 * (which may or may not already exist in the database).
177 * The 'notmuch' database will be the talloc owner of the returned
180 * This function returns a valid notmuch_message_t whether or not
181 * there is already a document in the database with the given message
182 * ID. These two cases can be distinguished by the value of *status:
185 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
187 * There is already a document with message ID 'message_id' in the
188 * database. The returned message can be used to query/modify the
190 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
192 * No document with 'message_id' exists in the database. The
193 * returned message contains a newly created document (not yet
194 * added to the database) and a document ID that is known not to
195 * exist in the database. The caller can modify the message, and a
196 * call to _notmuch_message_sync will add * the document to the
199 * If an error occurs, this function will return NULL and *status
200 * will be set as appropriate. (The status pointer argument must
204 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
205 const char *message_id,
206 notmuch_private_status_t *status_ret)
208 notmuch_message_t *message;
209 Xapian::Document doc;
210 Xapian::WritableDatabase *db;
214 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
216 message = notmuch_database_find_message (notmuch, message_id);
218 return talloc_steal (notmuch, message);
220 term = talloc_asprintf (NULL, "%s%s",
221 _find_prefix ("id"), message_id);
223 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
227 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
228 INTERNAL_ERROR ("Failure to ensure database is writable.");
230 db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
232 doc.add_term (term, 0);
235 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
237 doc_id = _notmuch_database_generate_doc_id (notmuch);
238 } catch (const Xapian::Error &error) {
239 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
240 error.get_msg().c_str());
241 notmuch->exception_reported = TRUE;
242 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
246 message = _notmuch_message_create_for_document (notmuch, notmuch,
247 doc_id, doc, status_ret);
249 /* We want to inform the caller that we had to create a new
251 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
252 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
258 notmuch_message_get_message_id (notmuch_message_t *message)
260 Xapian::TermIterator i;
262 if (message->message_id)
263 return message->message_id;
265 i = message->doc.termlist_begin ();
266 i.skip_to (_find_prefix ("id"));
268 if (i == message->doc.termlist_end ())
269 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
272 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
274 #if DEBUG_DATABASE_SANITY
277 if (i != message->doc.termlist_end () &&
278 strncmp ((*i).c_str (), _find_prefix ("id"),
279 strlen (_find_prefix ("id"))) == 0)
281 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
286 return message->message_id;
290 _notmuch_message_ensure_message_file (notmuch_message_t *message)
292 const char *filename;
294 if (message->message_file)
297 filename = notmuch_message_get_filename (message);
298 if (unlikely (filename == NULL))
301 message->message_file = _notmuch_message_file_open_ctx (message, filename);
305 notmuch_message_get_header (notmuch_message_t *message, const char *header)
307 _notmuch_message_ensure_message_file (message);
308 if (message->message_file == NULL)
311 return notmuch_message_file_get_header (message->message_file, header);
314 /* Return the message ID from the In-Reply-To header of 'message'.
316 * Returns an empty string ("") if 'message' has no In-Reply-To
319 * Returns NULL if any error occurs.
322 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
324 const char *prefix = _find_prefix ("replyto");
325 int prefix_len = strlen (prefix);
326 Xapian::TermIterator i;
327 std::string in_reply_to;
329 if (message->in_reply_to)
330 return message->in_reply_to;
332 i = message->doc.termlist_begin ();
335 if (i != message->doc.termlist_end ())
338 /* It's perfectly valid for a message to have no In-Reply-To
339 * header. For these cases, we return an empty string. */
340 if (i == message->doc.termlist_end () ||
341 strncmp (in_reply_to.c_str (), prefix, prefix_len))
343 message->in_reply_to = talloc_strdup (message, "");
344 return message->in_reply_to;
347 message->in_reply_to = talloc_strdup (message,
348 in_reply_to.c_str () + prefix_len);
350 #if DEBUG_DATABASE_SANITY
355 if (i != message->doc.termlist_end () &&
356 strncmp ((*i).c_str (), prefix, prefix_len) == 0)
358 INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
359 notmuch_message_get_message_id (message),
360 message->in_reply_to,
361 (*i).c_str () + prefix_len);
365 return message->in_reply_to;
369 notmuch_message_get_thread_id (notmuch_message_t *message)
371 const char *prefix = _find_prefix ("thread");
372 Xapian::TermIterator i;
375 /* This code is written with the assumption that "thread" has a
376 * single-character prefix. */
377 assert (strlen (prefix) == 1);
379 if (message->thread_id)
380 return message->thread_id;
382 i = message->doc.termlist_begin ();
385 if (i != message->doc.termlist_end ())
388 if (i == message->doc.termlist_end () || id[0] != *prefix)
389 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
392 message->thread_id = talloc_strdup (message, id.c_str () + 1);
394 #if DEBUG_DATABASE_SANITY
398 if (i != message->doc.termlist_end () && id[0] == *prefix)
400 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
401 notmuch_message_get_message_id (message),
407 return message->thread_id;
411 _notmuch_message_add_reply (notmuch_message_t *message,
412 notmuch_message_node_t *reply)
414 _notmuch_message_list_append (message->replies, reply);
418 notmuch_message_get_replies (notmuch_message_t *message)
420 return _notmuch_messages_create (message->replies);
423 /* Add an additional 'filename' for 'message'.
425 * This change will not be reflected in the database until the next
426 * call to _notmuch_message_sync. */
428 _notmuch_message_add_filename (notmuch_message_t *message,
429 const char *filename)
431 notmuch_status_t status;
432 void *local = talloc_new (message);
435 if (message->filename) {
436 talloc_free (message->filename);
437 message->filename = NULL;
440 if (filename == NULL)
441 INTERNAL_ERROR ("Message filename cannot be NULL.");
443 status = _notmuch_database_filename_to_direntry (local,
445 filename, &direntry);
449 _notmuch_message_add_term (message, "file-direntry", direntry);
453 return NOTMUCH_STATUS_SUCCESS;
457 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
459 return talloc_strdup (message, message->doc.get_data ().c_str ());
463 _notmuch_message_clear_data (notmuch_message_t *message)
465 message->doc.set_data ("");
469 notmuch_message_get_filename (notmuch_message_t *message)
471 const char *prefix = _find_prefix ("file-direntry");
472 int prefix_len = strlen (prefix);
473 Xapian::TermIterator i;
474 char *colon, *direntry = NULL;
475 const char *db_path, *directory, *basename;
476 unsigned int directory_id;
477 void *local = talloc_new (message);
479 if (message->filename)
480 return message->filename;
482 i = message->doc.termlist_begin ();
485 if (i != message->doc.termlist_end ())
486 direntry = talloc_strdup (local, (*i).c_str ());
488 if (i == message->doc.termlist_end () ||
489 strncmp (direntry, prefix, prefix_len))
491 /* A message document created by an old version of notmuch
492 * (prior to rename support) will have the filename in the
493 * data of the document rather than as a file-direntry term.
495 * It would be nice to do the upgrade of the document directly
496 * here, but the database is likely open in read-only mode. */
499 data = message->doc.get_data ().c_str ();
502 INTERNAL_ERROR ("message with no filename");
504 message->filename = talloc_strdup (message, data);
506 return message->filename;
509 direntry += prefix_len;
511 directory_id = strtol (direntry, &colon, 10);
513 if (colon == NULL || *colon != ':')
514 INTERNAL_ERROR ("malformed direntry");
516 basename = colon + 1;
520 db_path = notmuch_database_get_path (message->notmuch);
522 directory = _notmuch_database_get_directory_path (local,
526 if (strlen (directory))
527 message->filename = talloc_asprintf (message, "%s/%s/%s",
528 db_path, directory, basename);
530 message->filename = talloc_asprintf (message, "%s/%s",
532 talloc_free ((void *) directory);
536 return message->filename;
540 notmuch_message_get_flag (notmuch_message_t *message,
541 notmuch_message_flag_t flag)
543 return message->flags & (1 << flag);
547 notmuch_message_set_flag (notmuch_message_t *message,
548 notmuch_message_flag_t flag, notmuch_bool_t enable)
551 message->flags |= (1 << flag);
553 message->flags &= ~(1 << flag);
557 notmuch_message_get_date (notmuch_message_t *message)
562 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
563 } catch (Xapian::Error &error) {
564 INTERNAL_ERROR ("Failed to read timestamp value from document.");
568 return Xapian::sortable_unserialise (value);
572 notmuch_message_get_tags (notmuch_message_t *message)
574 Xapian::TermIterator i, end;
575 i = message->doc.termlist_begin();
576 end = message->doc.termlist_end();
577 return _notmuch_convert_tags(message, i, end);
581 notmuch_message_get_author (notmuch_message_t *message)
583 return message->author;
587 notmuch_message_set_author (notmuch_message_t *message,
591 talloc_free(message->author);
592 message->author = talloc_strdup(message, author);
597 _notmuch_message_set_date (notmuch_message_t *message,
602 /* GMime really doesn't want to see a NULL date, so protect its
604 if (date == NULL || *date == '\0')
607 time_value = g_mime_utils_header_decode_date (date, NULL);
609 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
610 Xapian::sortable_serialise (time_value));
613 static notmuch_private_status_t
614 _notmuch_message_tags_to_maildir (notmuch_message_t *message);
616 /* Synchronize changes made to message->doc out into the database. */
618 _notmuch_message_sync (notmuch_message_t *message)
620 Xapian::WritableDatabase *db;
621 notmuch_private_status_t status;
623 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
626 if (message->notmuch->maildir_sync &&
627 !notmuch_message_get_flag(message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID)) {
628 status = _notmuch_message_tags_to_maildir (message);
629 if (status != NOTMUCH_PRIVATE_STATUS_SUCCESS) {
630 fprintf (stderr, "Error: Cannot sync tags to maildir (%s)\n",
631 notmuch_status_to_string ((notmuch_status_t)status));
632 /* Exit to avoid unsynchronized mailstore. */
636 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
637 db->replace_document (message->doc_id, message->doc);
640 /* Ensure that 'message' is not holding any file object open. Future
641 * calls to various functions will still automatically open the
642 * message file as needed.
645 _notmuch_message_close (notmuch_message_t *message)
647 if (message->message_file) {
648 notmuch_message_file_close (message->message_file);
649 message->message_file = NULL;
653 /* Add a name:value term to 'message', (the actual term will be
654 * encoded by prefixing the value with a short prefix). See
655 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
656 * names to prefix values.
658 * This change will not be reflected in the database until the next
659 * call to _notmuch_message_sync. */
660 notmuch_private_status_t
661 _notmuch_message_add_term (notmuch_message_t *message,
662 const char *prefix_name,
669 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
671 term = talloc_asprintf (message, "%s%s",
672 _find_prefix (prefix_name), value);
674 if (strlen (term) > NOTMUCH_TERM_MAX)
675 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
677 message->doc.add_term (term, 0);
681 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
684 /* Parse 'text' and add a term to 'message' for each parsed word. Each
685 * term will be added both prefixed (if prefix_name is not NULL) and
686 * also unprefixed). */
687 notmuch_private_status_t
688 _notmuch_message_gen_terms (notmuch_message_t *message,
689 const char *prefix_name,
692 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
695 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
697 term_gen->set_document (message->doc);
700 const char *prefix = _find_prefix (prefix_name);
702 term_gen->index_text (text, 1, prefix);
705 term_gen->index_text (text);
707 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
710 /* Remove a name:value term from 'message', (the actual term will be
711 * encoded by prefixing the value with a short prefix). See
712 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
713 * names to prefix values.
715 * This change will not be reflected in the database until the next
716 * call to _notmuch_message_sync. */
717 notmuch_private_status_t
718 _notmuch_message_remove_term (notmuch_message_t *message,
719 const char *prefix_name,
725 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
727 term = talloc_asprintf (message, "%s%s",
728 _find_prefix (prefix_name), value);
730 if (strlen (term) > NOTMUCH_TERM_MAX)
731 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
734 message->doc.remove_term (term);
735 } catch (const Xapian::InvalidArgumentError) {
736 /* We'll let the philosopher's try to wrestle with the
737 * question of whether failing to remove that which was not
738 * there in the first place is failure. For us, we'll silently
739 * consider it all good. */
744 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
747 /* Change the message filename stored in the database.
749 * This change will not be reflected in the database until the next
750 * call to _notmuch_message_sync.
752 notmuch_private_status_t
753 _notmuch_message_rename (notmuch_message_t *message,
754 const char *new_filename)
756 void *local = talloc_new (message);
758 Xapian::PostingIterator i, end;
759 Xapian::Document document;
760 notmuch_private_status_t pstatus;
761 notmuch_status_t status;
762 const char *old_filename;
764 old_filename = notmuch_message_get_filename(message);
765 old_filename = talloc_reference(local, old_filename);
766 if (unlikely(!old_filename))
767 return NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
769 status = _notmuch_message_add_filename (message, new_filename);
771 return (notmuch_private_status_t)status;
773 status = _notmuch_database_filename_to_direntry (local, message->notmuch,
774 old_filename, &direntry);
776 return (notmuch_private_status_t)status;
778 pstatus = _notmuch_message_remove_term (message, "file-direntry", direntry);
786 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
788 notmuch_private_status_t private_status;
789 notmuch_status_t status;
791 status = _notmuch_database_ensure_writable (message->notmuch);
796 return NOTMUCH_STATUS_NULL_POINTER;
798 if (strlen (tag) > NOTMUCH_TAG_MAX)
799 return NOTMUCH_STATUS_TAG_TOO_LONG;
801 private_status = _notmuch_message_add_term (message, "tag", tag);
802 if (private_status) {
803 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
807 if (! message->frozen)
808 _notmuch_message_sync (message);
810 return NOTMUCH_STATUS_SUCCESS;
814 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
816 notmuch_private_status_t private_status;
817 notmuch_status_t status;
819 status = _notmuch_database_ensure_writable (message->notmuch);
824 return NOTMUCH_STATUS_NULL_POINTER;
826 if (strlen (tag) > NOTMUCH_TAG_MAX)
827 return NOTMUCH_STATUS_TAG_TOO_LONG;
829 private_status = _notmuch_message_remove_term (message, "tag", tag);
830 if (private_status) {
831 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
835 if (! message->frozen)
836 _notmuch_message_sync (message);
838 return NOTMUCH_STATUS_SUCCESS;
842 notmuch_message_maildir_to_tags (notmuch_message_t *message, const char *filename)
844 const char *flags, *p;
848 notmuch_status_t status;
850 flags = strstr (filename, ":2,");
852 return NOTMUCH_STATUS_FILE_NOT_EMAIL;
855 /* Check that the letters are valid Maildir flags */
858 for (p=flags; valid && *p; p++) {
874 fprintf (stderr, "Warning: Invalid maildir flags in filename %s\n", filename);
875 return NOTMUCH_STATUS_FILE_NOT_EMAIL;
878 status = notmuch_message_freeze (message);
882 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
883 if ((strchr (flags, flag2tag[i].flag) != NULL) ^ flag2tag[i].inverse) {
884 status = notmuch_message_add_tag (message, flag2tag[i].tag);
886 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
891 status = notmuch_message_thaw (message);
893 /* From now on, we can synchronize the tags from the database to
895 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID, FALSE);
900 maildir_get_new_flags(notmuch_message_t *message, char *flags)
902 notmuch_tags_t *tags;
907 for (i = 0; i < ARRAY_SIZE(flag2tag); i++)
908 flags[i] = flag2tag[i].inverse ? flag2tag[i].flag : '\0';
910 for (tags = notmuch_message_get_tags (message);
911 notmuch_tags_valid (tags);
912 notmuch_tags_move_to_next (tags))
914 tag = notmuch_tags_get (tags);
915 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
916 if (strcmp(tag, flag2tag[i].tag) == 0)
917 flags[i] = flag2tag[i].inverse ? '\0' : flag2tag[i].flag;
922 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
930 maildir_get_subdir (char *filename)
932 char *p, *subdir = NULL;
934 p = filename + strlen (filename) - 1;
935 while (p > filename + 3 && *p != '/')
939 if (subdir > filename && *(subdir - 1) != '/')
945 /* Rename the message file so that maildir flags corresponds to the
946 * tags and, if aplicable, move the message from new/ to cur/. */
947 static notmuch_private_status_t
948 _notmuch_message_tags_to_maildir (notmuch_message_t *message)
950 char flags[ARRAY_SIZE(flag2tag)+1];
951 const char *filename, *p;
952 char *filename_new, *subdir = NULL;
955 maildir_get_new_flags (message, flags);
957 filename = notmuch_message_get_filename (message);
958 /* TODO: Iterate over all file names. */
959 p = strstr(filename, ":2,");
960 if ((p && strcmp (p+3, flags) == 0) ||
961 (!p && flags[0] == '\0')) {
962 // Return if flags are not to be changed - this suppresses
963 // moving the message from new/ to cur/ during initial
965 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
968 p = filename + strlen(filename);
970 filename_new = (char*)talloc_size(message, (p-filename) + 3 + sizeof(flags));
971 if (unlikely (filename_new == NULL))
972 return NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
973 memcpy(filename_new, filename, p-filename);
974 filename_new[p-filename] = '\0';
976 /* If message is in new/ move it under cur/. */
977 subdir = maildir_get_subdir (filename_new);
978 if (subdir && memcmp (subdir, "new/", 4) == 0)
979 memcpy (subdir, "cur/", 4);
981 strcpy (filename_new+(p-filename), ":2,");
982 strcpy (filename_new+(p-filename)+3, flags);
984 if (strcmp (filename, filename_new) != 0) {
985 ret = rename (filename, filename_new);
987 perror (talloc_asprintf (message, "rename of %s to %s failed",
988 filename, filename_new));
991 return _notmuch_message_rename (message, filename_new);
992 /* _notmuch_message_sync is our caller. Do not call it here. */
994 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
998 notmuch_message_remove_all_tags (notmuch_message_t *message)
1000 notmuch_private_status_t private_status;
1001 notmuch_status_t status;
1002 notmuch_tags_t *tags;
1005 status = _notmuch_database_ensure_writable (message->notmuch);
1009 for (tags = notmuch_message_get_tags (message);
1010 notmuch_tags_valid (tags);
1011 notmuch_tags_move_to_next (tags))
1013 tag = notmuch_tags_get (tags);
1015 private_status = _notmuch_message_remove_term (message, "tag", tag);
1016 if (private_status) {
1017 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1022 if (! message->frozen)
1023 _notmuch_message_sync (message);
1025 return NOTMUCH_STATUS_SUCCESS;
1029 notmuch_message_freeze (notmuch_message_t *message)
1031 notmuch_status_t status;
1033 status = _notmuch_database_ensure_writable (message->notmuch);
1039 return NOTMUCH_STATUS_SUCCESS;
1043 notmuch_message_thaw (notmuch_message_t *message)
1045 notmuch_status_t status;
1047 status = _notmuch_database_ensure_writable (message->notmuch);
1051 if (message->frozen > 0) {
1053 if (message->frozen == 0)
1054 _notmuch_message_sync (message);
1055 return NOTMUCH_STATUS_SUCCESS;
1057 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1062 notmuch_message_destroy (notmuch_message_t *message)
1064 talloc_free (message);