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 },
59 { 'T', "deleted", false},
62 /* We end up having to call the destructor explicitly because we had
63 * to use "placement new" in order to initialize C++ objects within a
64 * block that we allocated with talloc. So C++ is making talloc
65 * slightly less simple to use, (we wouldn't need
66 * talloc_set_destructor at all otherwise).
69 _notmuch_message_destructor (notmuch_message_t *message)
71 message->doc.~Document ();
76 static notmuch_message_t *
77 _notmuch_message_create_for_document (const void *talloc_owner,
78 notmuch_database_t *notmuch,
81 notmuch_private_status_t *status)
83 notmuch_message_t *message;
86 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
88 message = talloc (talloc_owner, notmuch_message_t);
89 if (unlikely (message == NULL)) {
91 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
95 message->notmuch = notmuch;
96 message->doc_id = doc_id;
101 /* Each of these will be lazily created as needed. */
102 message->message_id = NULL;
103 message->thread_id = NULL;
104 message->in_reply_to = NULL;
105 message->filename = NULL;
106 message->message_file = NULL;
107 message->author = NULL;
109 message->replies = _notmuch_message_list_create (message);
110 if (unlikely (message->replies == NULL)) {
112 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
116 /* This is C++'s creepy "placement new", which is really just an
117 * ugly way to call a constructor for a pre-allocated object. So
118 * it's really not an error to not be checking for OUT_OF_MEMORY
119 * here, since this "new" isn't actually allocating memory. This
120 * is language-design comedy of the wrong kind. */
122 new (&message->doc) Xapian::Document;
124 talloc_set_destructor (message, _notmuch_message_destructor);
131 /* Create a new notmuch_message_t object for an existing document in
134 * Here, 'talloc owner' is an optional talloc context to which the new
135 * message will belong. This allows for the caller to not bother
136 * calling notmuch_message_destroy on the message, and no that all
137 * memory will be reclaimed with 'talloc_owner' is free. The caller
138 * still can call notmuch_message_destroy when finished with the
139 * message if desired.
141 * The 'talloc_owner' argument can also be NULL, in which case the
142 * caller *is* responsible for calling notmuch_message_destroy.
144 * If no document exists in the database with document ID of 'doc_id'
145 * then this function returns NULL and optionally sets *status to
146 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
148 * This function can also fail to due lack of available memory,
149 * returning NULL and optionally setting *status to
150 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
152 * The caller can pass NULL for status if uninterested in
153 * distinguishing these two cases.
156 _notmuch_message_create (const void *talloc_owner,
157 notmuch_database_t *notmuch,
159 notmuch_private_status_t *status)
161 Xapian::Document doc;
164 doc = notmuch->xapian_db->get_document (doc_id);
165 } catch (const Xapian::DocNotFoundError &error) {
167 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
171 return _notmuch_message_create_for_document (talloc_owner, notmuch,
172 doc_id, doc, status);
175 /* Create a new notmuch_message_t object for a specific message ID,
176 * (which may or may not already exist in the database).
178 * The 'notmuch' database will be the talloc owner of the returned
181 * This function returns a valid notmuch_message_t whether or not
182 * there is already a document in the database with the given message
183 * ID. These two cases can be distinguished by the value of *status:
186 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
188 * There is already a document with message ID 'message_id' in the
189 * database. The returned message can be used to query/modify the
191 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
193 * No document with 'message_id' exists in the database. The
194 * returned message contains a newly created document (not yet
195 * added to the database) and a document ID that is known not to
196 * exist in the database. The caller can modify the message, and a
197 * call to _notmuch_message_sync will add * the document to the
200 * If an error occurs, this function will return NULL and *status
201 * will be set as appropriate. (The status pointer argument must
205 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
206 const char *message_id,
207 notmuch_private_status_t *status_ret)
209 notmuch_message_t *message;
210 Xapian::Document doc;
211 Xapian::WritableDatabase *db;
215 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
217 message = notmuch_database_find_message (notmuch, message_id);
219 return talloc_steal (notmuch, message);
221 term = talloc_asprintf (NULL, "%s%s",
222 _find_prefix ("id"), message_id);
224 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
228 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
229 INTERNAL_ERROR ("Failure to ensure database is writable.");
231 db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
233 doc.add_term (term, 0);
236 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
238 doc_id = _notmuch_database_generate_doc_id (notmuch);
239 } catch (const Xapian::Error &error) {
240 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
241 error.get_msg().c_str());
242 notmuch->exception_reported = TRUE;
243 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
247 message = _notmuch_message_create_for_document (notmuch, notmuch,
248 doc_id, doc, status_ret);
250 /* We want to inform the caller that we had to create a new
252 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
253 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
259 notmuch_message_get_message_id (notmuch_message_t *message)
261 Xapian::TermIterator i;
263 if (message->message_id)
264 return message->message_id;
266 i = message->doc.termlist_begin ();
267 i.skip_to (_find_prefix ("id"));
269 if (i == message->doc.termlist_end ())
270 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
273 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
275 #if DEBUG_DATABASE_SANITY
278 if (i != message->doc.termlist_end () &&
279 strncmp ((*i).c_str (), _find_prefix ("id"),
280 strlen (_find_prefix ("id"))) == 0)
282 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
287 return message->message_id;
291 _notmuch_message_ensure_message_file (notmuch_message_t *message)
293 const char *filename;
295 if (message->message_file)
298 filename = notmuch_message_get_filename (message);
299 if (unlikely (filename == NULL))
302 message->message_file = _notmuch_message_file_open_ctx (message, filename);
306 notmuch_message_get_header (notmuch_message_t *message, const char *header)
308 _notmuch_message_ensure_message_file (message);
309 if (message->message_file == NULL)
312 return notmuch_message_file_get_header (message->message_file, header);
315 /* Return the message ID from the In-Reply-To header of 'message'.
317 * Returns an empty string ("") if 'message' has no In-Reply-To
320 * Returns NULL if any error occurs.
323 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
325 const char *prefix = _find_prefix ("replyto");
326 int prefix_len = strlen (prefix);
327 Xapian::TermIterator i;
328 std::string in_reply_to;
330 if (message->in_reply_to)
331 return message->in_reply_to;
333 i = message->doc.termlist_begin ();
336 if (i != message->doc.termlist_end ())
339 /* It's perfectly valid for a message to have no In-Reply-To
340 * header. For these cases, we return an empty string. */
341 if (i == message->doc.termlist_end () ||
342 strncmp (in_reply_to.c_str (), prefix, prefix_len))
344 message->in_reply_to = talloc_strdup (message, "");
345 return message->in_reply_to;
348 message->in_reply_to = talloc_strdup (message,
349 in_reply_to.c_str () + prefix_len);
351 #if DEBUG_DATABASE_SANITY
356 if (i != message->doc.termlist_end () &&
357 strncmp ((*i).c_str (), prefix, prefix_len) == 0)
359 INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
360 notmuch_message_get_message_id (message),
361 message->in_reply_to,
362 (*i).c_str () + prefix_len);
366 return message->in_reply_to;
370 notmuch_message_get_thread_id (notmuch_message_t *message)
372 const char *prefix = _find_prefix ("thread");
373 Xapian::TermIterator i;
376 /* This code is written with the assumption that "thread" has a
377 * single-character prefix. */
378 assert (strlen (prefix) == 1);
380 if (message->thread_id)
381 return message->thread_id;
383 i = message->doc.termlist_begin ();
386 if (i != message->doc.termlist_end ())
389 if (i == message->doc.termlist_end () || id[0] != *prefix)
390 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
393 message->thread_id = talloc_strdup (message, id.c_str () + 1);
395 #if DEBUG_DATABASE_SANITY
399 if (i != message->doc.termlist_end () && id[0] == *prefix)
401 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
402 notmuch_message_get_message_id (message),
408 return message->thread_id;
412 _notmuch_message_add_reply (notmuch_message_t *message,
413 notmuch_message_node_t *reply)
415 _notmuch_message_list_append (message->replies, reply);
419 notmuch_message_get_replies (notmuch_message_t *message)
421 return _notmuch_messages_create (message->replies);
424 /* Add an additional 'filename' for 'message'.
426 * This change will not be reflected in the database until the next
427 * call to _notmuch_message_sync. */
429 _notmuch_message_add_filename (notmuch_message_t *message,
430 const char *filename)
432 notmuch_status_t status;
433 void *local = talloc_new (message);
436 if (message->filename) {
437 talloc_free (message->filename);
438 message->filename = NULL;
441 if (filename == NULL)
442 INTERNAL_ERROR ("Message filename cannot be NULL.");
444 status = _notmuch_database_filename_to_direntry (local,
446 filename, &direntry);
450 _notmuch_message_add_term (message, "file-direntry", direntry);
454 return NOTMUCH_STATUS_SUCCESS;
458 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
460 return talloc_strdup (message, message->doc.get_data ().c_str ());
464 _notmuch_message_clear_data (notmuch_message_t *message)
466 message->doc.set_data ("");
470 notmuch_message_get_filename (notmuch_message_t *message)
472 const char *prefix = _find_prefix ("file-direntry");
473 int prefix_len = strlen (prefix);
474 Xapian::TermIterator i;
475 char *colon, *direntry = NULL;
476 const char *db_path, *directory, *basename;
477 unsigned int directory_id;
478 void *local = talloc_new (message);
480 if (message->filename)
481 return message->filename;
483 i = message->doc.termlist_begin ();
486 if (i != message->doc.termlist_end ())
487 direntry = talloc_strdup (local, (*i).c_str ());
489 if (i == message->doc.termlist_end () ||
490 strncmp (direntry, prefix, prefix_len))
492 /* A message document created by an old version of notmuch
493 * (prior to rename support) will have the filename in the
494 * data of the document rather than as a file-direntry term.
496 * It would be nice to do the upgrade of the document directly
497 * here, but the database is likely open in read-only mode. */
500 data = message->doc.get_data ().c_str ();
503 INTERNAL_ERROR ("message with no filename");
505 message->filename = talloc_strdup (message, data);
507 return message->filename;
510 direntry += prefix_len;
512 directory_id = strtol (direntry, &colon, 10);
514 if (colon == NULL || *colon != ':')
515 INTERNAL_ERROR ("malformed direntry");
517 basename = colon + 1;
521 db_path = notmuch_database_get_path (message->notmuch);
523 directory = _notmuch_database_get_directory_path (local,
527 if (strlen (directory))
528 message->filename = talloc_asprintf (message, "%s/%s/%s",
529 db_path, directory, basename);
531 message->filename = talloc_asprintf (message, "%s/%s",
533 talloc_free ((void *) directory);
537 return message->filename;
541 notmuch_message_get_flag (notmuch_message_t *message,
542 notmuch_message_flag_t flag)
544 return message->flags & (1 << flag);
548 notmuch_message_set_flag (notmuch_message_t *message,
549 notmuch_message_flag_t flag, notmuch_bool_t enable)
552 message->flags |= (1 << flag);
554 message->flags &= ~(1 << flag);
558 notmuch_message_get_date (notmuch_message_t *message)
563 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
564 } catch (Xapian::Error &error) {
565 INTERNAL_ERROR ("Failed to read timestamp value from document.");
569 return Xapian::sortable_unserialise (value);
573 notmuch_message_get_tags (notmuch_message_t *message)
575 Xapian::TermIterator i, end;
576 i = message->doc.termlist_begin();
577 end = message->doc.termlist_end();
578 return _notmuch_convert_tags(message, i, end);
582 notmuch_message_get_author (notmuch_message_t *message)
584 return message->author;
588 notmuch_message_set_author (notmuch_message_t *message,
592 talloc_free(message->author);
593 message->author = talloc_strdup(message, author);
598 _notmuch_message_set_date (notmuch_message_t *message,
603 /* GMime really doesn't want to see a NULL date, so protect its
605 if (date == NULL || *date == '\0')
608 time_value = g_mime_utils_header_decode_date (date, NULL);
610 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
611 Xapian::sortable_serialise (time_value));
614 static notmuch_private_status_t
615 _notmuch_message_tags_to_maildir (notmuch_message_t *message);
617 /* Synchronize changes made to message->doc out into the database. */
619 _notmuch_message_sync (notmuch_message_t *message)
621 Xapian::WritableDatabase *db;
622 notmuch_private_status_t status;
624 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
627 if (message->notmuch->maildir_sync &&
628 !notmuch_message_get_flag(message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID)) {
629 status = _notmuch_message_tags_to_maildir (message);
630 if (status != NOTMUCH_PRIVATE_STATUS_SUCCESS) {
631 fprintf (stderr, "Error: Cannot sync tags to maildir (%s)\n",
632 notmuch_status_to_string ((notmuch_status_t)status));
633 /* Exit to avoid unsynchronized mailstore. */
637 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
638 db->replace_document (message->doc_id, message->doc);
641 /* Ensure that 'message' is not holding any file object open. Future
642 * calls to various functions will still automatically open the
643 * message file as needed.
646 _notmuch_message_close (notmuch_message_t *message)
648 if (message->message_file) {
649 notmuch_message_file_close (message->message_file);
650 message->message_file = NULL;
654 /* Add a name:value term to 'message', (the actual term will be
655 * encoded by prefixing the value with a short prefix). See
656 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
657 * names to prefix values.
659 * This change will not be reflected in the database until the next
660 * call to _notmuch_message_sync. */
661 notmuch_private_status_t
662 _notmuch_message_add_term (notmuch_message_t *message,
663 const char *prefix_name,
670 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
672 term = talloc_asprintf (message, "%s%s",
673 _find_prefix (prefix_name), value);
675 if (strlen (term) > NOTMUCH_TERM_MAX)
676 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
678 message->doc.add_term (term, 0);
682 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
685 /* Parse 'text' and add a term to 'message' for each parsed word. Each
686 * term will be added both prefixed (if prefix_name is not NULL) and
687 * also unprefixed). */
688 notmuch_private_status_t
689 _notmuch_message_gen_terms (notmuch_message_t *message,
690 const char *prefix_name,
693 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
696 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
698 term_gen->set_document (message->doc);
701 const char *prefix = _find_prefix (prefix_name);
703 term_gen->index_text (text, 1, prefix);
706 term_gen->index_text (text);
708 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
711 /* Remove a name:value term from 'message', (the actual term will be
712 * encoded by prefixing the value with a short prefix). See
713 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
714 * names to prefix values.
716 * This change will not be reflected in the database until the next
717 * call to _notmuch_message_sync. */
718 notmuch_private_status_t
719 _notmuch_message_remove_term (notmuch_message_t *message,
720 const char *prefix_name,
726 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
728 term = talloc_asprintf (message, "%s%s",
729 _find_prefix (prefix_name), value);
731 if (strlen (term) > NOTMUCH_TERM_MAX)
732 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
735 message->doc.remove_term (term);
736 } catch (const Xapian::InvalidArgumentError) {
737 /* We'll let the philosopher's try to wrestle with the
738 * question of whether failing to remove that which was not
739 * there in the first place is failure. For us, we'll silently
740 * consider it all good. */
745 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
748 /* Change the message filename stored in the database.
750 * This change will not be reflected in the database until the next
751 * call to _notmuch_message_sync.
753 notmuch_private_status_t
754 _notmuch_message_rename (notmuch_message_t *message,
755 const char *new_filename)
757 void *local = talloc_new (message);
759 Xapian::PostingIterator i, end;
760 Xapian::Document document;
761 notmuch_private_status_t pstatus;
762 notmuch_status_t status;
763 const char *old_filename;
765 old_filename = notmuch_message_get_filename(message);
766 old_filename = talloc_reference(local, old_filename);
767 if (unlikely(!old_filename))
768 return NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
770 status = _notmuch_message_add_filename (message, new_filename);
772 return (notmuch_private_status_t)status;
774 status = _notmuch_database_filename_to_direntry (local, message->notmuch,
775 old_filename, &direntry);
777 return (notmuch_private_status_t)status;
779 pstatus = _notmuch_message_remove_term (message, "file-direntry", direntry);
787 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
789 notmuch_private_status_t private_status;
790 notmuch_status_t status;
792 status = _notmuch_database_ensure_writable (message->notmuch);
797 return NOTMUCH_STATUS_NULL_POINTER;
799 if (strlen (tag) > NOTMUCH_TAG_MAX)
800 return NOTMUCH_STATUS_TAG_TOO_LONG;
802 private_status = _notmuch_message_add_term (message, "tag", tag);
803 if (private_status) {
804 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
808 if (! message->frozen)
809 _notmuch_message_sync (message);
811 return NOTMUCH_STATUS_SUCCESS;
815 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
817 notmuch_private_status_t private_status;
818 notmuch_status_t status;
820 status = _notmuch_database_ensure_writable (message->notmuch);
825 return NOTMUCH_STATUS_NULL_POINTER;
827 if (strlen (tag) > NOTMUCH_TAG_MAX)
828 return NOTMUCH_STATUS_TAG_TOO_LONG;
830 private_status = _notmuch_message_remove_term (message, "tag", tag);
831 if (private_status) {
832 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
836 if (! message->frozen)
837 _notmuch_message_sync (message);
839 return NOTMUCH_STATUS_SUCCESS;
843 notmuch_message_maildir_to_tags (notmuch_message_t *message, const char *filename)
845 const char *flags, *p;
849 notmuch_status_t status;
851 flags = strstr (filename, ":2,");
853 return NOTMUCH_STATUS_FILE_NOT_EMAIL;
856 /* Check that the letters are valid Maildir flags */
859 for (p=flags; valid && *p; p++) {
875 fprintf (stderr, "Warning: Invalid maildir flags in filename %s\n", filename);
876 return NOTMUCH_STATUS_FILE_NOT_EMAIL;
879 status = notmuch_message_freeze (message);
883 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
884 if ((strchr (flags, flag2tag[i].flag) != NULL) ^ flag2tag[i].inverse) {
885 status = notmuch_message_add_tag (message, flag2tag[i].tag);
887 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
892 status = notmuch_message_thaw (message);
894 /* From now on, we can synchronize the tags from the database to
896 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID, FALSE);
901 maildir_get_new_flags(notmuch_message_t *message, char *flags)
903 notmuch_tags_t *tags;
908 for (i = 0; i < ARRAY_SIZE(flag2tag); i++)
909 flags[i] = flag2tag[i].inverse ? flag2tag[i].flag : '\0';
911 for (tags = notmuch_message_get_tags (message);
912 notmuch_tags_valid (tags);
913 notmuch_tags_move_to_next (tags))
915 tag = notmuch_tags_get (tags);
916 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
917 if (strcmp(tag, flag2tag[i].tag) == 0)
918 flags[i] = flag2tag[i].inverse ? '\0' : flag2tag[i].flag;
923 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
931 maildir_get_subdir (char *filename)
933 char *p, *subdir = NULL;
935 p = filename + strlen (filename) - 1;
936 while (p > filename + 3 && *p != '/')
940 if (subdir > filename && *(subdir - 1) != '/')
946 /* Rename the message file so that maildir flags corresponds to the
947 * tags and, if aplicable, move the message from new/ to cur/. */
948 static notmuch_private_status_t
949 _notmuch_message_tags_to_maildir (notmuch_message_t *message)
951 char flags[ARRAY_SIZE(flag2tag)+1];
952 const char *filename, *p;
953 char *filename_new, *subdir = NULL;
956 maildir_get_new_flags (message, flags);
958 filename = notmuch_message_get_filename (message);
959 /* TODO: Iterate over all file names. */
960 p = strstr(filename, ":2,");
961 if ((p && strcmp (p+3, flags) == 0) ||
962 (!p && flags[0] == '\0')) {
963 // Return if flags are not to be changed - this suppresses
964 // moving the message from new/ to cur/ during initial
966 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
969 p = filename + strlen(filename);
971 filename_new = (char*)talloc_size(message, (p-filename) + 3 + sizeof(flags));
972 if (unlikely (filename_new == NULL))
973 return NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
974 memcpy(filename_new, filename, p-filename);
975 filename_new[p-filename] = '\0';
977 /* If message is in new/ move it under cur/. */
978 subdir = maildir_get_subdir (filename_new);
979 if (subdir && memcmp (subdir, "new/", 4) == 0)
980 memcpy (subdir, "cur/", 4);
982 strcpy (filename_new+(p-filename), ":2,");
983 strcpy (filename_new+(p-filename)+3, flags);
985 if (strcmp (filename, filename_new) != 0) {
986 ret = rename (filename, filename_new);
988 perror (talloc_asprintf (message, "rename of %s to %s failed",
989 filename, filename_new));
992 return _notmuch_message_rename (message, filename_new);
993 /* _notmuch_message_sync is our caller. Do not call it here. */
995 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
999 notmuch_message_remove_all_tags (notmuch_message_t *message)
1001 notmuch_private_status_t private_status;
1002 notmuch_status_t status;
1003 notmuch_tags_t *tags;
1006 status = _notmuch_database_ensure_writable (message->notmuch);
1010 for (tags = notmuch_message_get_tags (message);
1011 notmuch_tags_valid (tags);
1012 notmuch_tags_move_to_next (tags))
1014 tag = notmuch_tags_get (tags);
1016 private_status = _notmuch_message_remove_term (message, "tag", tag);
1017 if (private_status) {
1018 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1023 if (! message->frozen)
1024 _notmuch_message_sync (message);
1026 return NOTMUCH_STATUS_SUCCESS;
1030 notmuch_message_freeze (notmuch_message_t *message)
1032 notmuch_status_t status;
1034 status = _notmuch_database_ensure_writable (message->notmuch);
1040 return NOTMUCH_STATUS_SUCCESS;
1044 notmuch_message_thaw (notmuch_message_t *message)
1046 notmuch_status_t status;
1048 status = _notmuch_database_ensure_writable (message->notmuch);
1052 if (message->frozen > 0) {
1054 if (message->frozen == 0)
1055 _notmuch_message_sync (message);
1056 return NOTMUCH_STATUS_SUCCESS;
1058 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1063 notmuch_message_destroy (notmuch_message_t *message)
1065 talloc_free (message);