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;
41 notmuch_message_file_t *message_file;
42 notmuch_string_list_t *property_term_list;
43 notmuch_string_map_t *property_map;
44 notmuch_message_list_t *replies;
46 /* For flags that are initialized on-demand, lazy_flags indicates
47 * if each flag has been initialized. */
48 unsigned long lazy_flags;
50 /* Message document modified since last sync */
53 /* last view of database the struct is synced with */
54 unsigned long last_view;
57 Xapian::termcount termpos;
60 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
62 struct maildir_flag_tag {
68 /* ASCII ordered table of Maildir flags and associated tags */
69 static struct maildir_flag_tag flag2tag[] = {
70 { 'D', "draft", false},
71 { 'F', "flagged", false},
72 { 'P', "passed", false},
73 { 'R', "replied", false},
74 { 'S', "unread", true }
77 /* We end up having to call the destructor explicitly because we had
78 * to use "placement new" in order to initialize C++ objects within a
79 * block that we allocated with talloc. So C++ is making talloc
80 * slightly less simple to use, (we wouldn't need
81 * talloc_set_destructor at all otherwise).
84 _notmuch_message_destructor (notmuch_message_t *message)
86 message->doc.~Document ();
91 static notmuch_message_t *
92 _notmuch_message_create_for_document (const void *talloc_owner,
93 notmuch_database_t *notmuch,
96 notmuch_private_status_t *status)
98 notmuch_message_t *message;
101 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
103 message = talloc (talloc_owner, notmuch_message_t);
104 if (unlikely (message == NULL)) {
106 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
110 message->notmuch = notmuch;
111 message->doc_id = doc_id;
115 message->lazy_flags = 0;
117 /* the message is initially not synchronized with Xapian */
118 message->last_view = 0;
120 /* Each of these will be lazily created as needed. */
121 message->message_id = NULL;
122 message->thread_id = NULL;
123 message->in_reply_to = NULL;
124 message->tag_list = NULL;
125 message->filename_term_list = NULL;
126 message->filename_list = NULL;
127 message->maildir_flags = NULL;
128 message->message_file = NULL;
129 message->author = NULL;
130 message->property_term_list = NULL;
131 message->property_map = NULL;
133 message->replies = _notmuch_message_list_create (message);
134 if (unlikely (message->replies == NULL)) {
136 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
140 /* This is C++'s creepy "placement new", which is really just an
141 * ugly way to call a constructor for a pre-allocated object. So
142 * it's really not an error to not be checking for OUT_OF_MEMORY
143 * here, since this "new" isn't actually allocating memory. This
144 * is language-design comedy of the wrong kind. */
146 new (&message->doc) Xapian::Document;
148 talloc_set_destructor (message, _notmuch_message_destructor);
151 message->termpos = 0;
156 /* Create a new notmuch_message_t object for an existing document in
159 * Here, 'talloc owner' is an optional talloc context to which the new
160 * message will belong. This allows for the caller to not bother
161 * calling notmuch_message_destroy on the message, and know that all
162 * memory will be reclaimed when 'talloc_owner' is freed. The caller
163 * still can call notmuch_message_destroy when finished with the
164 * message if desired.
166 * The 'talloc_owner' argument can also be NULL, in which case the
167 * caller *is* responsible for calling notmuch_message_destroy.
169 * If no document exists in the database with document ID of 'doc_id'
170 * then this function returns NULL and optionally sets *status to
171 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
173 * This function can also fail to due lack of available memory,
174 * returning NULL and optionally setting *status to
175 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
177 * The caller can pass NULL for status if uninterested in
178 * distinguishing these two cases.
181 _notmuch_message_create (const void *talloc_owner,
182 notmuch_database_t *notmuch,
184 notmuch_private_status_t *status)
186 Xapian::Document doc;
189 doc = notmuch->xapian_db->get_document (doc_id);
190 } catch (const Xapian::DocNotFoundError &error) {
192 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
196 return _notmuch_message_create_for_document (talloc_owner, notmuch,
197 doc_id, doc, status);
200 /* Create a new notmuch_message_t object for a specific message ID,
201 * (which may or may not already exist in the database).
203 * The 'notmuch' database will be the talloc owner of the returned
206 * This function returns a valid notmuch_message_t whether or not
207 * there is already a document in the database with the given message
208 * ID. These two cases can be distinguished by the value of *status:
211 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
213 * There is already a document with message ID 'message_id' in the
214 * database. The returned message can be used to query/modify the
215 * document. The message may be a ghost message.
217 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
219 * No document with 'message_id' exists in the database. The
220 * returned message contains a newly created document (not yet
221 * added to the database) and a document ID that is known not to
222 * exist in the database. This message is "blank"; that is, it
223 * contains only a message ID and no other metadata. The caller
224 * can modify the message, and a call to _notmuch_message_sync
225 * will add the document to the database.
227 * If an error occurs, this function will return NULL and *status
228 * will be set as appropriate. (The status pointer argument must
232 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
233 const char *message_id,
234 notmuch_private_status_t *status_ret)
236 notmuch_message_t *message;
237 Xapian::Document doc;
241 *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
245 return talloc_steal (notmuch, message);
246 else if (*status_ret)
249 /* If the message ID is too long, substitute its sha1 instead. */
250 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
251 message_id = _notmuch_message_id_compressed (message, message_id);
253 term = talloc_asprintf (NULL, "%s%s",
254 _find_prefix ("id"), message_id);
256 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
260 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
261 INTERNAL_ERROR ("Failure to ensure database is writable.");
264 doc.add_term (term, 0);
267 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
269 doc_id = _notmuch_database_generate_doc_id (notmuch);
270 } catch (const Xapian::Error &error) {
271 _notmuch_database_log(notmuch_message_get_database (message), "A Xapian exception occurred creating message: %s\n",
272 error.get_msg().c_str());
273 notmuch->exception_reported = true;
274 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
278 message = _notmuch_message_create_for_document (notmuch, notmuch,
279 doc_id, doc, status_ret);
281 /* We want to inform the caller that we had to create a new
283 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
284 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
290 _notmuch_message_get_term (notmuch_message_t *message,
291 Xapian::TermIterator &i, Xapian::TermIterator &end,
294 int prefix_len = strlen (prefix);
302 const std::string &term = *i;
303 if (strncmp (term.c_str(), prefix, prefix_len))
306 value = talloc_strdup (message, term.c_str() + prefix_len);
308 #if DEBUG_DATABASE_SANITY
311 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
312 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
313 message->doc_id, prefix, value,
314 (*i).c_str () + prefix_len);
322 * For special applications where we only want the thread id, reading
323 * in all metadata is a heavy I/O penalty.
326 _notmuch_message_get_thread_id_only (notmuch_message_t *message)
329 Xapian::TermIterator i = message->doc.termlist_begin ();
330 Xapian::TermIterator end = message->doc.termlist_end ();
332 message->thread_id = _notmuch_message_get_term (message, i, end,
333 _find_prefix ("thread"));
334 return message->thread_id;
339 _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
341 Xapian::TermIterator i, end;
343 if (field && (message->last_view >= message->notmuch->view))
346 const char *thread_prefix = _find_prefix ("thread"),
347 *tag_prefix = _find_prefix ("tag"),
348 *id_prefix = _find_prefix ("id"),
349 *type_prefix = _find_prefix ("type"),
350 *filename_prefix = _find_prefix ("file-direntry"),
351 *property_prefix = _find_prefix ("property"),
352 *replyto_prefix = _find_prefix ("replyto");
354 /* We do this all in a single pass because Xapian decompresses the
355 * term list every time you iterate over it. Thus, while this is
356 * slightly more costly than looking up individual fields if only
357 * one field of the message object is actually used, it's a huge
358 * win as more fields are used. */
359 for (int count=0; count < 3; count++) {
361 i = message->doc.termlist_begin ();
362 end = message->doc.termlist_end ();
365 if (!message->thread_id)
367 _notmuch_message_get_term (message, i, end, thread_prefix);
370 assert (strcmp (thread_prefix, tag_prefix) < 0);
371 if (!message->tag_list) {
373 _notmuch_database_get_terms_with_prefix (message, i, end,
375 _notmuch_string_list_sort (message->tag_list);
379 assert (strcmp (tag_prefix, id_prefix) < 0);
380 if (!message->message_id)
381 message->message_id =
382 _notmuch_message_get_term (message, i, end, id_prefix);
384 /* Get document type */
385 assert (strcmp (id_prefix, type_prefix) < 0);
386 if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {
387 i.skip_to (type_prefix);
388 /* "T" is the prefix "type" fields. See
389 * BOOLEAN_PREFIX_INTERNAL. */
391 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
392 else if (*i == "Tghost")
393 NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
395 INTERNAL_ERROR ("Message without type term");
396 NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
399 /* Get filename list. Here we get only the terms. We lazily
400 * expand them to full file names when needed in
401 * _notmuch_message_ensure_filename_list. */
402 assert (strcmp (type_prefix, filename_prefix) < 0);
403 if (!message->filename_term_list && !message->filename_list)
404 message->filename_term_list =
405 _notmuch_database_get_terms_with_prefix (message, i, end,
409 /* Get property terms. Mimic the setup with filenames above */
410 assert (strcmp (filename_prefix, property_prefix) < 0);
411 if (!message->property_map && !message->property_term_list)
412 message->property_term_list =
413 _notmuch_database_get_terms_with_prefix (message, i, end,
417 assert (strcmp (property_prefix, replyto_prefix) < 0);
418 if (!message->in_reply_to)
419 message->in_reply_to =
420 _notmuch_message_get_term (message, i, end, replyto_prefix);
423 /* It's perfectly valid for a message to have no In-Reply-To
424 * header. For these cases, we return an empty string. */
425 if (!message->in_reply_to)
426 message->in_reply_to = talloc_strdup (message, "");
428 /* all the way without an exception */
430 } catch (const Xapian::DatabaseModifiedError &error) {
431 notmuch_status_t status = _notmuch_database_reopen (message->notmuch);
432 if (status != NOTMUCH_STATUS_SUCCESS)
433 INTERNAL_ERROR ("unhandled error from notmuch_database_reopen: %s\n",
434 notmuch_status_to_string (status));
435 } catch (const Xapian::Error &error) {
436 INTERNAL_ERROR ("A Xapian exception occurred fetching message metadata: %s\n",
437 error.get_msg().c_str());
440 message->last_view = message->notmuch->view;
444 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
445 const char *prefix_name)
447 if (strcmp ("thread", prefix_name) == 0) {
448 talloc_free (message->thread_id);
449 message->thread_id = NULL;
452 if (strcmp ("tag", prefix_name) == 0) {
453 talloc_unlink (message, message->tag_list);
454 message->tag_list = NULL;
457 if (strcmp ("type", prefix_name) == 0) {
458 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
459 NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
462 if (strcmp ("file-direntry", prefix_name) == 0) {
463 talloc_free (message->filename_term_list);
464 talloc_free (message->filename_list);
465 message->filename_term_list = message->filename_list = NULL;
468 if (strcmp ("property", prefix_name) == 0) {
470 if (message->property_term_list)
471 talloc_free (message->property_term_list);
472 message->property_term_list = NULL;
474 if (message->property_map)
475 talloc_unlink (message, message->property_map);
477 message->property_map = NULL;
480 if (strcmp ("replyto", prefix_name) == 0) {
481 talloc_free (message->in_reply_to);
482 message->in_reply_to = NULL;
487 _notmuch_message_get_doc_id (notmuch_message_t *message)
489 return message->doc_id;
493 notmuch_message_get_message_id (notmuch_message_t *message)
495 _notmuch_message_ensure_metadata (message, message->message_id);
496 if (!message->message_id)
497 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
499 return message->message_id;
503 _notmuch_message_ensure_message_file (notmuch_message_t *message)
505 const char *filename;
507 if (message->message_file)
510 filename = notmuch_message_get_filename (message);
511 if (unlikely (filename == NULL))
514 message->message_file = _notmuch_message_file_open_ctx (
515 notmuch_message_get_database (message), message, filename);
519 notmuch_message_get_header (notmuch_message_t *message, const char *header)
521 Xapian::valueno slot = Xapian::BAD_VALUENO;
523 /* Fetch header from the appropriate xapian value field if
525 if (strcasecmp (header, "from") == 0)
526 slot = NOTMUCH_VALUE_FROM;
527 else if (strcasecmp (header, "subject") == 0)
528 slot = NOTMUCH_VALUE_SUBJECT;
529 else if (strcasecmp (header, "message-id") == 0)
530 slot = NOTMUCH_VALUE_MESSAGE_ID;
532 if (slot != Xapian::BAD_VALUENO) {
534 std::string value = message->doc.get_value (slot);
536 /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then
537 * empty values indicate empty headers. If we don't, then
538 * it could just mean we didn't record the header. */
539 if ((message->notmuch->features &
540 NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) ||
542 return talloc_strdup (message, value.c_str ());
544 } catch (Xapian::Error &error) {
545 _notmuch_database_log(notmuch_message_get_database (message), "A Xapian exception occurred when reading header: %s\n",
546 error.get_msg().c_str());
547 message->notmuch->exception_reported = true;
552 /* Otherwise fall back to parsing the file */
553 _notmuch_message_ensure_message_file (message);
554 if (message->message_file == NULL)
557 return _notmuch_message_file_get_header (message->message_file, header);
560 /* Return the message ID from the In-Reply-To header of 'message'.
562 * Returns an empty string ("") if 'message' has no In-Reply-To
565 * Returns NULL if any error occurs.
568 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
570 _notmuch_message_ensure_metadata (message, message->in_reply_to);
571 return message->in_reply_to;
575 notmuch_message_get_thread_id (notmuch_message_t *message)
577 _notmuch_message_ensure_metadata (message, message->thread_id);
578 if (!message->thread_id)
579 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
581 return message->thread_id;
585 _notmuch_message_add_reply (notmuch_message_t *message,
586 notmuch_message_t *reply)
588 _notmuch_message_list_add_message (message->replies, reply);
592 notmuch_message_get_replies (notmuch_message_t *message)
594 return _notmuch_messages_create (message->replies);
598 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
600 Xapian::TermIterator i;
601 size_t prefix_len = 0;
603 prefix_len = strlen (prefix);
606 i = message->doc.termlist_begin ();
609 /* Terminate loop when no terms remain with desired prefix. */
610 if (i == message->doc.termlist_end () ||
611 strncmp ((*i).c_str (), prefix, prefix_len))
615 message->doc.remove_term ((*i));
616 message->modified = true;
617 } catch (const Xapian::InvalidArgumentError) {
618 /* Ignore failure to remove non-existent term. */
624 /* Remove all terms generated by indexing, i.e. not tags or
625 * properties, along with any automatic tags*/
626 notmuch_private_status_t
627 _notmuch_message_remove_indexed_terms (notmuch_message_t *message)
629 Xapian::TermIterator i;
632 id_prefix = _find_prefix ("id"),
633 property_prefix = _find_prefix ("property"),
634 tag_prefix = _find_prefix ("tag"),
635 type_prefix = _find_prefix ("type");
637 for (i = message->doc.termlist_begin ();
638 i != message->doc.termlist_end (); i++) {
640 const std::string term = *i;
642 if (term.compare (0, type_prefix.size (), type_prefix) == 0)
645 if (term.compare (0, id_prefix.size (), id_prefix) == 0)
648 if (term.compare (0, property_prefix.size (), property_prefix) == 0)
651 if (term.compare (0, tag_prefix.size (), tag_prefix) == 0 &&
652 term.compare (1, strlen("encrypted"), "encrypted") != 0 &&
653 term.compare (1, strlen("signed"), "signed") != 0 &&
654 term.compare (1, strlen("attachment"), "attachment") != 0)
658 message->doc.remove_term ((*i));
659 message->modified = true;
660 } catch (const Xapian::InvalidArgumentError) {
661 /* Ignore failure to remove non-existent term. */
662 } catch (const Xapian::Error &error) {
663 notmuch_database_t *notmuch = message->notmuch;
665 if (!notmuch->exception_reported) {
666 _notmuch_database_log(notmuch_message_get_database (message), "A Xapian exception occurred creating message: %s\n",
667 error.get_msg().c_str());
668 notmuch->exception_reported = true;
670 return NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
673 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
676 /* Return true if p points at "new" or "cur". */
677 static bool is_maildir (const char *p)
679 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
682 /* Add "folder:" term for directory. */
683 static notmuch_status_t
684 _notmuch_message_add_folder_terms (notmuch_message_t *message,
685 const char *directory)
689 folder = talloc_strdup (NULL, directory);
691 return NOTMUCH_STATUS_OUT_OF_MEMORY;
694 * If the message file is in a leaf directory named "new" or
695 * "cur", presume maildir and index the parent directory. Thus a
696 * "folder:" prefix search matches messages in the specified
697 * maildir folder, i.e. in the specified directory and its "new"
698 * and "cur" subdirectories.
700 * Note that this means the "folder:" prefix can't be used for
701 * distinguishing between message files in "new" or "cur". The
702 * "path:" prefix needs to be used for that.
704 * Note the deliberate difference to _filename_is_in_maildir(). We
705 * don't want to index different things depending on the existence
706 * or non-existence of all maildir sibling directories "new",
707 * "cur", and "tmp". Doing so would be surprising, and difficult
708 * for the user to fix in case all subdirectories were not in
709 * place during indexing.
711 last = strrchr (folder, '/');
713 if (is_maildir (last + 1))
715 } else if (is_maildir (folder)) {
719 _notmuch_message_add_term (message, "folder", folder);
721 talloc_free (folder);
723 message->modified = true;
724 return NOTMUCH_STATUS_SUCCESS;
727 #define RECURSIVE_SUFFIX "/**"
729 /* Add "path:" terms for directory. */
730 static notmuch_status_t
731 _notmuch_message_add_path_terms (notmuch_message_t *message,
732 const char *directory)
734 /* Add exact "path:" term. */
735 _notmuch_message_add_term (message, "path", directory);
737 if (strlen (directory)) {
740 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
742 return NOTMUCH_STATUS_OUT_OF_MEMORY;
744 /* Add recursive "path:" terms for directory and all parents. */
745 for (p = path + strlen (path) - 1; p > path; p--) {
747 strcpy (p, RECURSIVE_SUFFIX);
748 _notmuch_message_add_term (message, "path", path);
755 /* Recursive all-matching path:** for consistency. */
756 _notmuch_message_add_term (message, "path", "**");
758 return NOTMUCH_STATUS_SUCCESS;
761 /* Add directory based terms for all filenames of the message. */
762 static notmuch_status_t
763 _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
765 const char *direntry_prefix = _find_prefix ("file-direntry");
766 int direntry_prefix_len = strlen (direntry_prefix);
767 Xapian::TermIterator i = message->doc.termlist_begin ();
768 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
770 for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {
771 unsigned int directory_id;
772 const char *direntry, *directory;
774 const std::string &term = *i;
776 /* Terminate loop at first term without desired prefix. */
777 if (strncmp (term.c_str (), direntry_prefix, direntry_prefix_len))
780 /* Indicate that there are filenames remaining. */
781 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
783 direntry = term.c_str ();
784 direntry += direntry_prefix_len;
786 directory_id = strtol (direntry, &colon, 10);
788 if (colon == NULL || *colon != ':')
789 INTERNAL_ERROR ("malformed direntry");
791 directory = _notmuch_database_get_directory_path (ctx,
795 _notmuch_message_add_folder_terms (message, directory);
796 _notmuch_message_add_path_terms (message, directory);
802 /* Add an additional 'filename' for 'message'.
804 * This change will not be reflected in the database until the next
805 * call to _notmuch_message_sync. */
807 _notmuch_message_add_filename (notmuch_message_t *message,
808 const char *filename)
810 const char *relative, *directory;
811 notmuch_status_t status;
812 void *local = talloc_new (message);
815 if (filename == NULL)
816 INTERNAL_ERROR ("Message filename cannot be NULL.");
818 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
819 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
820 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
822 relative = _notmuch_database_relative_path (message->notmuch, filename);
824 status = _notmuch_database_split_path (local, relative, &directory, NULL);
828 status = _notmuch_database_filename_to_direntry (
829 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
833 /* New file-direntry allows navigating to this message with
834 * notmuch_directory_get_child_files() . */
835 _notmuch_message_add_term (message, "file-direntry", direntry);
837 _notmuch_message_add_folder_terms (message, directory);
838 _notmuch_message_add_path_terms (message, directory);
842 return NOTMUCH_STATUS_SUCCESS;
845 /* Remove a particular 'filename' from 'message'.
847 * This change will not be reflected in the database until the next
848 * call to _notmuch_message_sync.
850 * If this message still has other filenames, returns
851 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
853 * Note: This function does not remove a document from the database,
854 * even if the specified filename is the only filename for this
855 * message. For that functionality, see
856 * notmuch_database_remove_message. */
858 _notmuch_message_remove_filename (notmuch_message_t *message,
859 const char *filename)
861 void *local = talloc_new (message);
863 notmuch_private_status_t private_status;
864 notmuch_status_t status;
866 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
867 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
868 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
870 status = _notmuch_database_filename_to_direntry (
871 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
872 if (status || !direntry)
875 /* Unlink this file from its parent directory. */
876 private_status = _notmuch_message_remove_term (message,
877 "file-direntry", direntry);
878 status = COERCE_STATUS (private_status,
879 "Unexpected error from _notmuch_message_remove_term");
883 /* Re-synchronize "folder:" and "path:" terms for this message. */
885 /* Remove all "folder:" terms. */
886 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
888 /* Remove all "path:" terms. */
889 _notmuch_message_remove_terms (message, _find_prefix ("path"));
891 /* Add back terms for all remaining filenames of the message. */
892 status = _notmuch_message_add_directory_terms (local, message);
899 /* Upgrade the "folder:" prefix from V1 to V2. */
900 #define FOLDER_PREFIX_V1 "XFOLDER"
901 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
903 _notmuch_message_upgrade_folder (notmuch_message_t *message)
905 /* Remove all old "folder:" terms. */
906 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
908 /* Remove all old "folder:" stemmed terms. */
909 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
911 /* Add new boolean "folder:" and "path:" terms. */
912 _notmuch_message_add_directory_terms (message, message);
916 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
918 return talloc_strdup (message, message->doc.get_data ().c_str ());
922 _notmuch_message_clear_data (notmuch_message_t *message)
924 message->doc.set_data ("");
925 message->modified = true;
929 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
931 notmuch_string_node_t *node;
933 if (message->filename_list)
936 _notmuch_message_ensure_metadata (message, message->filename_term_list);
938 message->filename_list = _notmuch_string_list_create (message);
939 node = message->filename_term_list->head;
942 /* A message document created by an old version of notmuch
943 * (prior to rename support) will have the filename in the
944 * data of the document rather than as a file-direntry term.
946 * It would be nice to do the upgrade of the document directly
947 * here, but the database is likely open in read-only mode. */
949 std::string datastr = message->doc.get_data ();
950 const char *data = datastr.c_str ();
953 INTERNAL_ERROR ("message with no filename");
955 _notmuch_string_list_append (message->filename_list, data);
960 for (; node; node = node->next) {
961 void *local = talloc_new (message);
962 const char *db_path, *directory, *basename, *filename;
963 char *colon, *direntry = NULL;
964 unsigned int directory_id;
966 direntry = node->string;
968 directory_id = strtol (direntry, &colon, 10);
970 if (colon == NULL || *colon != ':')
971 INTERNAL_ERROR ("malformed direntry");
973 basename = colon + 1;
977 db_path = notmuch_database_get_path (message->notmuch);
979 directory = _notmuch_database_get_directory_path (local,
983 if (strlen (directory))
984 filename = talloc_asprintf (message, "%s/%s/%s",
985 db_path, directory, basename);
987 filename = talloc_asprintf (message, "%s/%s",
990 _notmuch_string_list_append (message->filename_list, filename);
995 talloc_free (message->filename_term_list);
996 message->filename_term_list = NULL;
1000 notmuch_message_get_filename (notmuch_message_t *message)
1002 _notmuch_message_ensure_filename_list (message);
1004 if (message->filename_list == NULL)
1007 if (message->filename_list->head == NULL ||
1008 message->filename_list->head->string == NULL)
1010 INTERNAL_ERROR ("message with no filename");
1013 return message->filename_list->head->string;
1016 notmuch_filenames_t *
1017 notmuch_message_get_filenames (notmuch_message_t *message)
1019 _notmuch_message_ensure_filename_list (message);
1021 return _notmuch_filenames_create (message, message->filename_list);
1025 notmuch_message_count_files (notmuch_message_t *message)
1027 _notmuch_message_ensure_filename_list (message);
1029 return _notmuch_string_list_length (message->filename_list);
1033 notmuch_message_get_flag (notmuch_message_t *message,
1034 notmuch_message_flag_t flag)
1036 if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
1037 ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
1038 _notmuch_message_ensure_metadata (message, NULL);
1040 return NOTMUCH_TEST_BIT (message->flags, flag);
1044 notmuch_message_set_flag (notmuch_message_t *message,
1045 notmuch_message_flag_t flag, notmuch_bool_t enable)
1048 NOTMUCH_SET_BIT (&message->flags, flag);
1050 NOTMUCH_CLEAR_BIT (&message->flags, flag);
1051 NOTMUCH_SET_BIT (&message->lazy_flags, flag);
1055 notmuch_message_get_date (notmuch_message_t *message)
1060 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
1061 } catch (Xapian::Error &error) {
1062 _notmuch_database_log(notmuch_message_get_database (message), "A Xapian exception occurred when reading date: %s\n",
1063 error.get_msg().c_str());
1064 message->notmuch->exception_reported = true;
1069 /* sortable_unserialise is undefined on empty string */
1071 return Xapian::sortable_unserialise (value);
1075 notmuch_message_get_tags (notmuch_message_t *message)
1077 notmuch_tags_t *tags;
1079 _notmuch_message_ensure_metadata (message, message->tag_list);
1081 tags = _notmuch_tags_create (message, message->tag_list);
1082 /* _notmuch_tags_create steals the reference to the tag_list, but
1083 * in this case it's still used by the message, so we add an
1084 * *additional* talloc reference to the list. As a result, it's
1085 * possible to modify the message tags (which talloc_unlink's the
1086 * current list from the message) while still iterating because
1087 * the iterator will keep the current list alive. */
1088 if (!talloc_reference (message, message->tag_list))
1095 _notmuch_message_get_author (notmuch_message_t *message)
1097 return message->author;
1101 _notmuch_message_set_author (notmuch_message_t *message,
1104 if (message->author)
1105 talloc_free(message->author);
1106 message->author = talloc_strdup(message, author);
1111 _notmuch_message_set_header_values (notmuch_message_t *message,
1114 const char *subject)
1118 /* GMime really doesn't want to see a NULL date, so protect its
1120 if (date == NULL || *date == '\0') {
1123 time_value = g_mime_utils_header_decode_date_unix (date);
1125 * Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=779923
1131 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
1132 Xapian::sortable_serialise (time_value));
1133 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
1134 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1135 message->modified = true;
1138 /* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD. The caller
1139 * must call _notmuch_message_sync. */
1141 _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
1143 /* _notmuch_message_sync will update the last modification
1144 * revision; we just have to ask it to. */
1145 message->modified = true;
1148 /* Synchronize changes made to message->doc out into the database. */
1150 _notmuch_message_sync (notmuch_message_t *message)
1152 Xapian::WritableDatabase *db;
1154 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
1157 if (! message->modified)
1160 /* Update the last modification of this message. */
1161 if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)
1162 /* sortable_serialise gives a reasonably compact encoding,
1163 * which directly translates to reduced IO when scanning the
1164 * value stream. Since it's built for doubles, we only get 53
1165 * effective bits, but that's still enough for the database to
1166 * last a few centuries at 1 million revisions per second. */
1167 message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,
1168 Xapian::sortable_serialise (
1169 _notmuch_database_new_revision (
1170 message->notmuch)));
1172 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
1173 db->replace_document (message->doc_id, message->doc);
1174 message->modified = false;
1177 /* Delete a message document from the database, leaving a ghost
1178 * message in its place */
1180 _notmuch_message_delete (notmuch_message_t *message)
1182 notmuch_status_t status;
1183 Xapian::WritableDatabase *db;
1184 const char *mid, *tid, *query_string;
1185 notmuch_message_t *ghost;
1186 notmuch_private_status_t private_status;
1187 notmuch_database_t *notmuch;
1188 notmuch_query_t *query;
1189 unsigned int count = 0;
1192 mid = notmuch_message_get_message_id (message);
1193 tid = notmuch_message_get_thread_id (message);
1194 notmuch = message->notmuch;
1196 status = _notmuch_database_ensure_writable (message->notmuch);
1200 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1201 db->delete_document (message->doc_id);
1203 /* if this was a ghost to begin with, we are done */
1204 private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
1206 return COERCE_STATUS (private_status,
1207 "Error trying to determine whether message was a ghost");
1209 return NOTMUCH_STATUS_SUCCESS;
1211 query_string = talloc_asprintf (message, "thread:%s", tid);
1212 query = notmuch_query_create (notmuch, query_string);
1214 return NOTMUCH_STATUS_OUT_OF_MEMORY;
1215 status = notmuch_query_count_messages (query, &count);
1217 notmuch_query_destroy (query);
1222 /* reintroduce a ghost in its place because there are still
1223 * other active messages in this thread: */
1224 ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);
1225 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1226 private_status = _notmuch_message_initialize_ghost (ghost, tid);
1227 if (! private_status)
1228 _notmuch_message_sync (ghost);
1229 } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1230 /* this is deeply weird, and we should not have gotten
1231 into this state. is there a better error message to
1233 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1236 notmuch_message_destroy (ghost);
1237 status = COERCE_STATUS (private_status, "Error converting to ghost message");
1239 /* the thread is empty; drop all ghost messages from it */
1240 notmuch_messages_t *messages;
1241 status = _notmuch_query_search_documents (query,
1244 if (status == NOTMUCH_STATUS_SUCCESS) {
1245 notmuch_status_t last_error = NOTMUCH_STATUS_SUCCESS;
1246 while (notmuch_messages_valid (messages)) {
1247 message = notmuch_messages_get (messages);
1248 status = _notmuch_message_delete (message);
1249 if (status) /* we'll report the last failure we see;
1250 * if there is more than one failure, we
1251 * forget about previous ones */
1252 last_error = status;
1253 notmuch_message_destroy (message);
1254 notmuch_messages_move_to_next (messages);
1256 status = last_error;
1259 notmuch_query_destroy (query);
1263 /* Transform a blank message into a ghost message. The caller must
1264 * _notmuch_message_sync the message. */
1265 notmuch_private_status_t
1266 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1267 const char *thread_id)
1269 notmuch_private_status_t status;
1271 status = _notmuch_message_add_term (message, "type", "ghost");
1274 status = _notmuch_message_add_term (message, "thread", thread_id);
1278 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1281 /* Ensure that 'message' is not holding any file object open. Future
1282 * calls to various functions will still automatically open the
1283 * message file as needed.
1286 _notmuch_message_close (notmuch_message_t *message)
1288 if (message->message_file) {
1289 _notmuch_message_file_close (message->message_file);
1290 message->message_file = NULL;
1294 /* Add a name:value term to 'message', (the actual term will be
1295 * encoded by prefixing the value with a short prefix). See
1296 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1297 * names to prefix values.
1299 * This change will not be reflected in the database until the next
1300 * call to _notmuch_message_sync. */
1301 notmuch_private_status_t
1302 _notmuch_message_add_term (notmuch_message_t *message,
1303 const char *prefix_name,
1310 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1312 term = talloc_asprintf (message, "%s%s",
1313 _find_prefix (prefix_name), value);
1315 if (strlen (term) > NOTMUCH_TERM_MAX)
1316 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1318 message->doc.add_term (term, 0);
1319 message->modified = true;
1323 _notmuch_message_invalidate_metadata (message, prefix_name);
1325 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1328 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1329 * term will be added both prefixed (if prefix_name is not NULL) and
1330 * also non-prefixed). */
1331 notmuch_private_status_t
1332 _notmuch_message_gen_terms (notmuch_message_t *message,
1333 const char *prefix_name,
1336 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1339 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1341 term_gen->set_document (message->doc);
1344 const char *prefix = _find_prefix (prefix_name);
1346 term_gen->set_termpos (message->termpos);
1347 term_gen->index_text (text, 1, prefix);
1348 /* Create a gap between this an the next terms so they don't
1349 * appear to be a phrase. */
1350 message->termpos = term_gen->get_termpos () + 100;
1352 _notmuch_message_invalidate_metadata (message, prefix_name);
1355 term_gen->set_termpos (message->termpos);
1356 term_gen->index_text (text);
1357 /* Create a term gap, as above. */
1358 message->termpos = term_gen->get_termpos () + 100;
1360 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1363 /* Remove a name:value term from 'message', (the actual term will be
1364 * encoded by prefixing the value with a short prefix). See
1365 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1366 * names to prefix values.
1368 * This change will not be reflected in the database until the next
1369 * call to _notmuch_message_sync. */
1370 notmuch_private_status_t
1371 _notmuch_message_remove_term (notmuch_message_t *message,
1372 const char *prefix_name,
1378 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1380 term = talloc_asprintf (message, "%s%s",
1381 _find_prefix (prefix_name), value);
1383 if (strlen (term) > NOTMUCH_TERM_MAX)
1384 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1387 message->doc.remove_term (term);
1388 message->modified = true;
1389 } catch (const Xapian::InvalidArgumentError) {
1390 /* We'll let the philosophers try to wrestle with the
1391 * question of whether failing to remove that which was not
1392 * there in the first place is failure. For us, we'll silently
1393 * consider it all good. */
1398 _notmuch_message_invalidate_metadata (message, prefix_name);
1400 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1403 notmuch_private_status_t
1404 _notmuch_message_has_term (notmuch_message_t *message,
1405 const char *prefix_name,
1411 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
1414 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1416 term = talloc_asprintf (message, "%s%s",
1417 _find_prefix (prefix_name), value);
1419 if (strlen (term) > NOTMUCH_TERM_MAX)
1420 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1423 /* Look for the exact term */
1424 Xapian::TermIterator i = message->doc.termlist_begin ();
1426 if (i != message->doc.termlist_end () &&
1427 !strcmp ((*i).c_str (), term))
1429 } catch (Xapian::Error &error) {
1430 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1439 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1441 notmuch_private_status_t private_status;
1442 notmuch_status_t status;
1444 status = _notmuch_database_ensure_writable (message->notmuch);
1449 return NOTMUCH_STATUS_NULL_POINTER;
1451 if (strlen (tag) > NOTMUCH_TAG_MAX)
1452 return NOTMUCH_STATUS_TAG_TOO_LONG;
1454 private_status = _notmuch_message_add_term (message, "tag", tag);
1455 if (private_status) {
1456 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
1460 if (! message->frozen)
1461 _notmuch_message_sync (message);
1463 return NOTMUCH_STATUS_SUCCESS;
1467 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1469 notmuch_private_status_t private_status;
1470 notmuch_status_t status;
1472 status = _notmuch_database_ensure_writable (message->notmuch);
1477 return NOTMUCH_STATUS_NULL_POINTER;
1479 if (strlen (tag) > NOTMUCH_TAG_MAX)
1480 return NOTMUCH_STATUS_TAG_TOO_LONG;
1482 private_status = _notmuch_message_remove_term (message, "tag", tag);
1483 if (private_status) {
1484 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1488 if (! message->frozen)
1489 _notmuch_message_sync (message);
1491 return NOTMUCH_STATUS_SUCCESS;
1494 /* Is the given filename within a maildir directory?
1496 * Specifically, is the final directory component of 'filename' either
1497 * "cur" or "new". If so, return a pointer to that final directory
1498 * component within 'filename'. If not, return NULL.
1500 * A non-NULL return value is guaranteed to be a valid string pointer
1501 * pointing to the characters "new/" or "cur/", (but not
1505 _filename_is_in_maildir (const char *filename)
1507 const char *slash, *dir = NULL;
1509 /* Find the last '/' separating directory from filename. */
1510 slash = strrchr (filename, '/');
1514 /* Jump back 4 characters to where the previous '/' will be if the
1515 * directory is named "cur" or "new". */
1516 if (slash - filename < 4)
1526 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1527 STRNCMP_LITERAL (dir, "new/") == 0)
1536 _ensure_maildir_flags (notmuch_message_t *message, bool force)
1539 notmuch_filenames_t *filenames;
1540 const char *filename, *dir;
1541 char *combined_flags = talloc_strdup (message, "");
1542 int seen_maildir_info = 0;
1544 if (message->maildir_flags) {
1546 talloc_free (message->maildir_flags);
1547 message->maildir_flags = NULL;
1551 for (filenames = notmuch_message_get_filenames (message);
1552 notmuch_filenames_valid (filenames);
1553 notmuch_filenames_move_to_next (filenames))
1555 filename = notmuch_filenames_get (filenames);
1556 dir = _filename_is_in_maildir (filename);
1561 flags = strstr (filename, ":2,");
1563 seen_maildir_info = 1;
1565 combined_flags = talloc_strdup_append (combined_flags, flags);
1566 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1567 /* Messages are delivered to new/ with no "info" part, but
1568 * they effectively have default maildir flags. According
1569 * to the spec, we should ignore the info part for
1570 * messages in new/, but some MUAs (mutt) can set maildir
1571 * flags on messages in new/, so we're liberal in what we
1573 seen_maildir_info = 1;
1576 if (seen_maildir_info)
1577 message->maildir_flags = combined_flags;
1581 notmuch_message_has_maildir_flag (notmuch_message_t *message, char flag)
1583 _ensure_maildir_flags (message, false);
1584 return message->maildir_flags && (strchr (message->maildir_flags, flag) != NULL);
1588 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1590 notmuch_status_t status;
1593 _ensure_maildir_flags (message, true);
1594 /* If none of the filenames have any maildir info field (not even
1595 * an empty info with no flags set) then there's no information to
1596 * go on, so do nothing. */
1597 if (! message->maildir_flags)
1598 return NOTMUCH_STATUS_SUCCESS;
1600 status = notmuch_message_freeze (message);
1604 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1605 if ((strchr (message->maildir_flags, flag2tag[i].flag) != NULL)
1607 flag2tag[i].inverse)
1609 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1611 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1616 status = notmuch_message_thaw (message);
1621 /* From the set of tags on 'message' and the flag2tag table, compute a
1622 * set of maildir-flag actions to be taken, (flags that should be
1623 * either set or cleared).
1625 * The result is returned as two talloced strings: to_set, and to_clear
1628 _get_maildir_flag_actions (notmuch_message_t *message,
1630 char **to_clear_ret)
1632 char *to_set, *to_clear;
1633 notmuch_tags_t *tags;
1637 to_set = talloc_strdup (message, "");
1638 to_clear = talloc_strdup (message, "");
1640 /* First, find flags for all set tags. */
1641 for (tags = notmuch_message_get_tags (message);
1642 notmuch_tags_valid (tags);
1643 notmuch_tags_move_to_next (tags))
1645 tag = notmuch_tags_get (tags);
1647 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1648 if (strcmp (tag, flag2tag[i].tag) == 0) {
1649 if (flag2tag[i].inverse)
1650 to_clear = talloc_asprintf_append (to_clear,
1654 to_set = talloc_asprintf_append (to_set,
1661 /* Then, find the flags for all tags not present. */
1662 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1663 if (flag2tag[i].inverse) {
1664 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1665 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1667 if (strchr (to_set, flag2tag[i].flag) == NULL)
1668 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1672 *to_set_ret = to_set;
1673 *to_clear_ret = to_clear;
1676 /* Given 'filename' and a set of maildir flags to set and to clear,
1677 * compute the new maildir filename.
1679 * If the existing filename is in the directory "new", the new
1680 * filename will be in the directory "cur", except for the case when
1681 * no flags are changed and the existing filename does not contain
1682 * maildir info (starting with ",2:").
1684 * After a sequence of ":2," in the filename, any subsequent
1685 * single-character flags will be added or removed according to the
1686 * characters in flags_to_set and flags_to_clear. Any existing flags
1687 * not mentioned in either string will remain. The final list of flags
1688 * will be in ASCII order.
1690 * If the original flags seem invalid, (repeated characters or
1691 * non-ASCII ordering of flags), this function will return NULL
1692 * (meaning that renaming would not be safe and should not occur).
1695 _new_maildir_filename (void *ctx,
1696 const char *filename,
1697 const char *flags_to_set,
1698 const char *flags_to_clear)
1700 const char *info, *flags;
1701 unsigned int flag, last_flag;
1702 char *filename_new, *dir;
1704 int flags_in_map = 0;
1705 bool flags_changed = false;
1709 memset (flag_map, 0, sizeof (flag_map));
1711 info = strstr (filename, ":2,");
1714 info = filename + strlen(filename);
1716 /* Loop through existing flags in filename. */
1717 for (flags = info + 3, last_flag = 0;
1719 last_flag = flag, flags++)
1723 /* Original flags not in ASCII order. Abort. */
1724 if (flag < last_flag)
1727 /* Non-ASCII flag. Abort. */
1728 if (flag > sizeof(flag_map) - 1)
1731 /* Repeated flag value. Abort. */
1740 /* Then set and clear our flags from tags. */
1741 for (flags = flags_to_set; *flags; flags++) {
1743 if (flag_map[flag] == 0) {
1746 flags_changed = true;
1750 for (flags = flags_to_clear; *flags; flags++) {
1752 if (flag_map[flag]) {
1755 flags_changed = true;
1759 /* Messages in new/ without maildir info can be kept in new/ if no
1760 * flags have changed. */
1761 dir = (char *) _filename_is_in_maildir (filename);
1762 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && !*info && !flags_changed)
1763 return talloc_strdup (ctx, filename);
1765 filename_new = (char *) talloc_size (ctx,
1767 strlen (":2,") + flags_in_map + 1);
1768 if (unlikely (filename_new == NULL))
1771 strncpy (filename_new, filename, info - filename);
1772 filename_new[info - filename] = '\0';
1774 strcat (filename_new, ":2,");
1776 s = filename_new + strlen (filename_new);
1777 for (i = 0; i < sizeof (flag_map); i++)
1786 /* If message is in new/ move it under cur/. */
1787 dir = (char *) _filename_is_in_maildir (filename_new);
1788 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1789 memcpy (dir, "cur/", 4);
1791 return filename_new;
1795 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1797 notmuch_filenames_t *filenames;
1798 const char *filename;
1800 char *to_set, *to_clear;
1801 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1803 _get_maildir_flag_actions (message, &to_set, &to_clear);
1805 for (filenames = notmuch_message_get_filenames (message);
1806 notmuch_filenames_valid (filenames);
1807 notmuch_filenames_move_to_next (filenames))
1809 filename = notmuch_filenames_get (filenames);
1811 if (! _filename_is_in_maildir (filename))
1814 filename_new = _new_maildir_filename (message, filename,
1816 if (filename_new == NULL)
1819 if (strcmp (filename, filename_new)) {
1821 notmuch_status_t new_status;
1823 err = rename (filename, filename_new);
1827 new_status = _notmuch_message_remove_filename (message,
1829 /* Hold on to only the first error. */
1830 if (! status && new_status
1831 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1832 status = new_status;
1836 new_status = _notmuch_message_add_filename (message,
1838 /* Hold on to only the first error. */
1839 if (! status && new_status) {
1840 status = new_status;
1844 _notmuch_message_sync (message);
1847 talloc_free (filename_new);
1850 talloc_free (to_set);
1851 talloc_free (to_clear);
1857 notmuch_message_remove_all_tags (notmuch_message_t *message)
1859 notmuch_private_status_t private_status;
1860 notmuch_status_t status;
1861 notmuch_tags_t *tags;
1864 status = _notmuch_database_ensure_writable (message->notmuch);
1868 for (tags = notmuch_message_get_tags (message);
1869 notmuch_tags_valid (tags);
1870 notmuch_tags_move_to_next (tags))
1872 tag = notmuch_tags_get (tags);
1874 private_status = _notmuch_message_remove_term (message, "tag", tag);
1875 if (private_status) {
1876 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1881 if (! message->frozen)
1882 _notmuch_message_sync (message);
1885 return NOTMUCH_STATUS_SUCCESS;
1889 notmuch_message_freeze (notmuch_message_t *message)
1891 notmuch_status_t status;
1893 status = _notmuch_database_ensure_writable (message->notmuch);
1899 return NOTMUCH_STATUS_SUCCESS;
1903 notmuch_message_thaw (notmuch_message_t *message)
1905 notmuch_status_t status;
1907 status = _notmuch_database_ensure_writable (message->notmuch);
1911 if (message->frozen > 0) {
1913 if (message->frozen == 0)
1914 _notmuch_message_sync (message);
1915 return NOTMUCH_STATUS_SUCCESS;
1917 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1922 notmuch_message_destroy (notmuch_message_t *message)
1924 talloc_free (message);
1927 notmuch_database_t *
1928 notmuch_message_get_database (const notmuch_message_t *message)
1930 return message->notmuch;
1934 _notmuch_message_ensure_property_map (notmuch_message_t *message)
1936 notmuch_string_node_t *node;
1938 if (message->property_map)
1941 _notmuch_message_ensure_metadata (message, message->property_term_list);
1943 message->property_map = _notmuch_string_map_create (message);
1945 for (node = message->property_term_list->head; node; node = node->next) {
1949 value = strchr(node->string, '=');
1951 INTERNAL_ERROR ("malformed property term");
1957 _notmuch_string_map_append (message->property_map, key, value);
1961 talloc_free (message->property_term_list);
1962 message->property_term_list = NULL;
1965 notmuch_string_map_t *
1966 _notmuch_message_property_map (notmuch_message_t *message)
1968 _notmuch_message_ensure_property_map (message);
1970 return message->property_map;
1974 _notmuch_message_frozen (notmuch_message_t *message)
1976 return message->frozen;
1980 notmuch_message_reindex (notmuch_message_t *message,
1981 notmuch_indexopts_t *indexopts)
1983 notmuch_database_t *notmuch = NULL;
1984 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
1985 notmuch_private_status_t private_status;
1986 notmuch_filenames_t *orig_filenames = NULL;
1987 const char *orig_thread_id = NULL;
1988 notmuch_message_file_t *message_file = NULL;
1992 if (message == NULL)
1993 return NOTMUCH_STATUS_NULL_POINTER;
1995 /* Save in case we need to delete message */
1996 orig_thread_id = notmuch_message_get_thread_id (message);
1997 if (!orig_thread_id) {
1998 /* XXX TODO: make up new error return? */
1999 INTERNAL_ERROR ("message without thread-id");
2002 /* strdup it because the metadata may be invalidated */
2003 orig_thread_id = talloc_strdup (message, orig_thread_id);
2005 notmuch = notmuch_message_get_database (message);
2007 ret = _notmuch_database_ensure_writable (notmuch);
2011 orig_filenames = notmuch_message_get_filenames (message);
2013 private_status = _notmuch_message_remove_indexed_terms (message);
2014 if (private_status) {
2015 ret = COERCE_STATUS(private_status, "error removing terms");
2019 ret = notmuch_message_remove_all_properties_with_prefix (message, "index.");
2021 goto DONE; /* XXX TODO: distinguish from other error returns above? */
2022 if (indexopts && notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE) {
2023 ret = notmuch_message_remove_all_properties (message, "session-key");
2028 /* re-add the filenames with the associated indexopts */
2029 for (; notmuch_filenames_valid (orig_filenames);
2030 notmuch_filenames_move_to_next (orig_filenames)) {
2033 const char *from, *to, *subject;
2034 char *message_id = NULL;
2035 const char *thread_id = NULL;
2037 const char *filename = notmuch_filenames_get (orig_filenames);
2039 message_file = _notmuch_message_file_open (notmuch, filename);
2040 if (message_file == NULL)
2043 ret = _notmuch_message_file_get_headers (message_file,
2044 &from, &subject, &to, &date,
2049 /* XXX TODO: deal with changing message id? */
2051 _notmuch_message_add_filename (message, filename);
2053 ret = _notmuch_database_link_message_to_parents (notmuch, message,
2059 if (thread_id == NULL)
2060 thread_id = orig_thread_id;
2062 _notmuch_message_add_term (message, "thread", thread_id);
2063 /* Take header values only from first filename */
2065 _notmuch_message_set_header_values (message, date, from, subject);
2067 ret = _notmuch_message_index_file (message, indexopts, message_file);
2069 if (ret == NOTMUCH_STATUS_FILE_ERROR)
2075 _notmuch_message_file_close (message_file);
2076 message_file = NULL;
2079 /* put back thread id to help cleanup */
2080 _notmuch_message_add_term (message, "thread", orig_thread_id);
2081 ret = _notmuch_message_delete (message);
2083 _notmuch_message_sync (message);
2088 _notmuch_message_file_close (message_file);
2090 /* XXX TODO destroy orig_filenames? */