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 _cmpmsg (const void *pa, const void *pb)
594 notmuch_message_t **a = (notmuch_message_t **) pa;
595 notmuch_message_t **b = (notmuch_message_t **) pb;
596 time_t time_a = notmuch_message_get_date (*a);
597 time_t time_b = notmuch_message_get_date (*b);
599 if (time_a == time_b)
601 else if (time_a < time_b)
607 notmuch_message_list_t *
608 _notmuch_message_sort_subtrees (void *ctx, notmuch_message_list_t *list)
612 size_t capacity = 16;
617 void *local = talloc_new (NULL);
618 notmuch_message_list_t *new_list = _notmuch_message_list_create (ctx);
619 notmuch_message_t **message_array = talloc_zero_array (local, notmuch_message_t *, capacity);
621 for (notmuch_messages_t *messages = _notmuch_messages_create (list);
622 notmuch_messages_valid (messages);
623 notmuch_messages_move_to_next (messages)) {
624 notmuch_message_t *root = notmuch_messages_get (messages);
625 if (count >= capacity) {
627 message_array = talloc_realloc (local, message_array, notmuch_message_t *, capacity);
629 message_array[count++] = root;
630 root->replies = _notmuch_message_sort_subtrees (root, root->replies);
633 qsort (message_array, count, sizeof (notmuch_message_t *), _cmpmsg);
634 for (size_t i = 0; i < count; i++) {
635 _notmuch_message_list_add_message (new_list, message_array[i]);
644 notmuch_message_get_replies (notmuch_message_t *message)
646 return _notmuch_messages_create (message->replies);
650 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
652 Xapian::TermIterator i;
653 size_t prefix_len = 0;
655 prefix_len = strlen (prefix);
658 i = message->doc.termlist_begin ();
661 /* Terminate loop when no terms remain with desired prefix. */
662 if (i == message->doc.termlist_end () ||
663 strncmp ((*i).c_str (), prefix, prefix_len))
667 message->doc.remove_term ((*i));
668 message->modified = true;
669 } catch (const Xapian::InvalidArgumentError) {
670 /* Ignore failure to remove non-existent term. */
676 /* Remove all terms generated by indexing, i.e. not tags or
677 * properties, along with any automatic tags*/
678 notmuch_private_status_t
679 _notmuch_message_remove_indexed_terms (notmuch_message_t *message)
681 Xapian::TermIterator i;
684 id_prefix = _find_prefix ("id"),
685 property_prefix = _find_prefix ("property"),
686 tag_prefix = _find_prefix ("tag"),
687 type_prefix = _find_prefix ("type");
689 for (i = message->doc.termlist_begin ();
690 i != message->doc.termlist_end (); i++) {
692 const std::string term = *i;
694 if (term.compare (0, type_prefix.size (), type_prefix) == 0)
697 if (term.compare (0, id_prefix.size (), id_prefix) == 0)
700 if (term.compare (0, property_prefix.size (), property_prefix) == 0)
703 if (term.compare (0, tag_prefix.size (), tag_prefix) == 0 &&
704 term.compare (1, strlen("encrypted"), "encrypted") != 0 &&
705 term.compare (1, strlen("signed"), "signed") != 0 &&
706 term.compare (1, strlen("attachment"), "attachment") != 0)
710 message->doc.remove_term ((*i));
711 message->modified = true;
712 } catch (const Xapian::InvalidArgumentError) {
713 /* Ignore failure to remove non-existent term. */
714 } catch (const Xapian::Error &error) {
715 notmuch_database_t *notmuch = message->notmuch;
717 if (!notmuch->exception_reported) {
718 _notmuch_database_log(notmuch_message_get_database (message), "A Xapian exception occurred creating message: %s\n",
719 error.get_msg().c_str());
720 notmuch->exception_reported = true;
722 return NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
725 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
728 /* Return true if p points at "new" or "cur". */
729 static bool is_maildir (const char *p)
731 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
734 /* Add "folder:" term for directory. */
735 static notmuch_status_t
736 _notmuch_message_add_folder_terms (notmuch_message_t *message,
737 const char *directory)
741 folder = talloc_strdup (NULL, directory);
743 return NOTMUCH_STATUS_OUT_OF_MEMORY;
746 * If the message file is in a leaf directory named "new" or
747 * "cur", presume maildir and index the parent directory. Thus a
748 * "folder:" prefix search matches messages in the specified
749 * maildir folder, i.e. in the specified directory and its "new"
750 * and "cur" subdirectories.
752 * Note that this means the "folder:" prefix can't be used for
753 * distinguishing between message files in "new" or "cur". The
754 * "path:" prefix needs to be used for that.
756 * Note the deliberate difference to _filename_is_in_maildir(). We
757 * don't want to index different things depending on the existence
758 * or non-existence of all maildir sibling directories "new",
759 * "cur", and "tmp". Doing so would be surprising, and difficult
760 * for the user to fix in case all subdirectories were not in
761 * place during indexing.
763 last = strrchr (folder, '/');
765 if (is_maildir (last + 1))
767 } else if (is_maildir (folder)) {
771 _notmuch_message_add_term (message, "folder", folder);
773 talloc_free (folder);
775 message->modified = true;
776 return NOTMUCH_STATUS_SUCCESS;
779 #define RECURSIVE_SUFFIX "/**"
781 /* Add "path:" terms for directory. */
782 static notmuch_status_t
783 _notmuch_message_add_path_terms (notmuch_message_t *message,
784 const char *directory)
786 /* Add exact "path:" term. */
787 _notmuch_message_add_term (message, "path", directory);
789 if (strlen (directory)) {
792 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
794 return NOTMUCH_STATUS_OUT_OF_MEMORY;
796 /* Add recursive "path:" terms for directory and all parents. */
797 for (p = path + strlen (path) - 1; p > path; p--) {
799 strcpy (p, RECURSIVE_SUFFIX);
800 _notmuch_message_add_term (message, "path", path);
807 /* Recursive all-matching path:** for consistency. */
808 _notmuch_message_add_term (message, "path", "**");
810 return NOTMUCH_STATUS_SUCCESS;
813 /* Add directory based terms for all filenames of the message. */
814 static notmuch_status_t
815 _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
817 const char *direntry_prefix = _find_prefix ("file-direntry");
818 int direntry_prefix_len = strlen (direntry_prefix);
819 Xapian::TermIterator i = message->doc.termlist_begin ();
820 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
822 for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {
823 unsigned int directory_id;
824 const char *direntry, *directory;
826 const std::string &term = *i;
828 /* Terminate loop at first term without desired prefix. */
829 if (strncmp (term.c_str (), direntry_prefix, direntry_prefix_len))
832 /* Indicate that there are filenames remaining. */
833 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
835 direntry = term.c_str ();
836 direntry += direntry_prefix_len;
838 directory_id = strtol (direntry, &colon, 10);
840 if (colon == NULL || *colon != ':')
841 INTERNAL_ERROR ("malformed direntry");
843 directory = _notmuch_database_get_directory_path (ctx,
847 _notmuch_message_add_folder_terms (message, directory);
848 _notmuch_message_add_path_terms (message, directory);
854 /* Add an additional 'filename' for 'message'.
856 * This change will not be reflected in the database until the next
857 * call to _notmuch_message_sync. */
859 _notmuch_message_add_filename (notmuch_message_t *message,
860 const char *filename)
862 const char *relative, *directory;
863 notmuch_status_t status;
864 void *local = talloc_new (message);
867 if (filename == NULL)
868 INTERNAL_ERROR ("Message filename cannot be NULL.");
870 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
871 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
872 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
874 relative = _notmuch_database_relative_path (message->notmuch, filename);
876 status = _notmuch_database_split_path (local, relative, &directory, NULL);
880 status = _notmuch_database_filename_to_direntry (
881 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
885 /* New file-direntry allows navigating to this message with
886 * notmuch_directory_get_child_files() . */
887 _notmuch_message_add_term (message, "file-direntry", direntry);
889 _notmuch_message_add_folder_terms (message, directory);
890 _notmuch_message_add_path_terms (message, directory);
894 return NOTMUCH_STATUS_SUCCESS;
897 /* Remove a particular 'filename' from 'message'.
899 * This change will not be reflected in the database until the next
900 * call to _notmuch_message_sync.
902 * If this message still has other filenames, returns
903 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
905 * Note: This function does not remove a document from the database,
906 * even if the specified filename is the only filename for this
907 * message. For that functionality, see
908 * notmuch_database_remove_message. */
910 _notmuch_message_remove_filename (notmuch_message_t *message,
911 const char *filename)
913 void *local = talloc_new (message);
915 notmuch_private_status_t private_status;
916 notmuch_status_t status;
918 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
919 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
920 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
922 status = _notmuch_database_filename_to_direntry (
923 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
924 if (status || !direntry)
927 /* Unlink this file from its parent directory. */
928 private_status = _notmuch_message_remove_term (message,
929 "file-direntry", direntry);
930 status = COERCE_STATUS (private_status,
931 "Unexpected error from _notmuch_message_remove_term");
935 /* Re-synchronize "folder:" and "path:" terms for this message. */
937 /* Remove all "folder:" terms. */
938 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
940 /* Remove all "path:" terms. */
941 _notmuch_message_remove_terms (message, _find_prefix ("path"));
943 /* Add back terms for all remaining filenames of the message. */
944 status = _notmuch_message_add_directory_terms (local, message);
951 /* Upgrade the "folder:" prefix from V1 to V2. */
952 #define FOLDER_PREFIX_V1 "XFOLDER"
953 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
955 _notmuch_message_upgrade_folder (notmuch_message_t *message)
957 /* Remove all old "folder:" terms. */
958 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
960 /* Remove all old "folder:" stemmed terms. */
961 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
963 /* Add new boolean "folder:" and "path:" terms. */
964 _notmuch_message_add_directory_terms (message, message);
968 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
970 return talloc_strdup (message, message->doc.get_data ().c_str ());
974 _notmuch_message_clear_data (notmuch_message_t *message)
976 message->doc.set_data ("");
977 message->modified = true;
981 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
983 notmuch_string_node_t *node;
985 if (message->filename_list)
988 _notmuch_message_ensure_metadata (message, message->filename_term_list);
990 message->filename_list = _notmuch_string_list_create (message);
991 node = message->filename_term_list->head;
994 /* A message document created by an old version of notmuch
995 * (prior to rename support) will have the filename in the
996 * data of the document rather than as a file-direntry term.
998 * It would be nice to do the upgrade of the document directly
999 * here, but the database is likely open in read-only mode. */
1001 std::string datastr = message->doc.get_data ();
1002 const char *data = datastr.c_str ();
1005 INTERNAL_ERROR ("message with no filename");
1007 _notmuch_string_list_append (message->filename_list, data);
1012 for (; node; node = node->next) {
1013 void *local = talloc_new (message);
1014 const char *db_path, *directory, *basename, *filename;
1015 char *colon, *direntry = NULL;
1016 unsigned int directory_id;
1018 direntry = node->string;
1020 directory_id = strtol (direntry, &colon, 10);
1022 if (colon == NULL || *colon != ':')
1023 INTERNAL_ERROR ("malformed direntry");
1025 basename = colon + 1;
1029 db_path = notmuch_database_get_path (message->notmuch);
1031 directory = _notmuch_database_get_directory_path (local,
1035 if (strlen (directory))
1036 filename = talloc_asprintf (message, "%s/%s/%s",
1037 db_path, directory, basename);
1039 filename = talloc_asprintf (message, "%s/%s",
1042 _notmuch_string_list_append (message->filename_list, filename);
1044 talloc_free (local);
1047 talloc_free (message->filename_term_list);
1048 message->filename_term_list = NULL;
1052 notmuch_message_get_filename (notmuch_message_t *message)
1054 _notmuch_message_ensure_filename_list (message);
1056 if (message->filename_list == NULL)
1059 if (message->filename_list->head == NULL ||
1060 message->filename_list->head->string == NULL)
1062 INTERNAL_ERROR ("message with no filename");
1065 return message->filename_list->head->string;
1068 notmuch_filenames_t *
1069 notmuch_message_get_filenames (notmuch_message_t *message)
1071 _notmuch_message_ensure_filename_list (message);
1073 return _notmuch_filenames_create (message, message->filename_list);
1077 notmuch_message_count_files (notmuch_message_t *message)
1079 _notmuch_message_ensure_filename_list (message);
1081 return _notmuch_string_list_length (message->filename_list);
1085 notmuch_message_get_flag (notmuch_message_t *message,
1086 notmuch_message_flag_t flag)
1088 if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
1089 ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
1090 _notmuch_message_ensure_metadata (message, NULL);
1092 return NOTMUCH_TEST_BIT (message->flags, flag);
1096 notmuch_message_set_flag (notmuch_message_t *message,
1097 notmuch_message_flag_t flag, notmuch_bool_t enable)
1100 NOTMUCH_SET_BIT (&message->flags, flag);
1102 NOTMUCH_CLEAR_BIT (&message->flags, flag);
1103 NOTMUCH_SET_BIT (&message->lazy_flags, flag);
1107 notmuch_message_get_date (notmuch_message_t *message)
1112 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
1113 } catch (Xapian::Error &error) {
1114 _notmuch_database_log(notmuch_message_get_database (message), "A Xapian exception occurred when reading date: %s\n",
1115 error.get_msg().c_str());
1116 message->notmuch->exception_reported = true;
1121 /* sortable_unserialise is undefined on empty string */
1123 return Xapian::sortable_unserialise (value);
1127 notmuch_message_get_tags (notmuch_message_t *message)
1129 notmuch_tags_t *tags;
1131 _notmuch_message_ensure_metadata (message, message->tag_list);
1133 tags = _notmuch_tags_create (message, message->tag_list);
1134 /* _notmuch_tags_create steals the reference to the tag_list, but
1135 * in this case it's still used by the message, so we add an
1136 * *additional* talloc reference to the list. As a result, it's
1137 * possible to modify the message tags (which talloc_unlink's the
1138 * current list from the message) while still iterating because
1139 * the iterator will keep the current list alive. */
1140 if (!talloc_reference (message, message->tag_list))
1147 _notmuch_message_get_author (notmuch_message_t *message)
1149 return message->author;
1153 _notmuch_message_set_author (notmuch_message_t *message,
1156 if (message->author)
1157 talloc_free(message->author);
1158 message->author = talloc_strdup(message, author);
1163 _notmuch_message_set_header_values (notmuch_message_t *message,
1166 const char *subject)
1170 /* GMime really doesn't want to see a NULL date, so protect its
1172 if (date == NULL || *date == '\0') {
1175 time_value = g_mime_utils_header_decode_date_unix (date);
1177 * Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=779923
1183 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
1184 Xapian::sortable_serialise (time_value));
1185 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
1186 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1187 message->modified = true;
1190 /* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD. The caller
1191 * must call _notmuch_message_sync. */
1193 _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
1195 /* _notmuch_message_sync will update the last modification
1196 * revision; we just have to ask it to. */
1197 message->modified = true;
1200 /* Synchronize changes made to message->doc out into the database. */
1202 _notmuch_message_sync (notmuch_message_t *message)
1204 Xapian::WritableDatabase *db;
1206 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
1209 if (! message->modified)
1212 /* Update the last modification of this message. */
1213 if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)
1214 /* sortable_serialise gives a reasonably compact encoding,
1215 * which directly translates to reduced IO when scanning the
1216 * value stream. Since it's built for doubles, we only get 53
1217 * effective bits, but that's still enough for the database to
1218 * last a few centuries at 1 million revisions per second. */
1219 message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,
1220 Xapian::sortable_serialise (
1221 _notmuch_database_new_revision (
1222 message->notmuch)));
1224 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
1225 db->replace_document (message->doc_id, message->doc);
1226 message->modified = false;
1229 /* Delete a message document from the database, leaving a ghost
1230 * message in its place */
1232 _notmuch_message_delete (notmuch_message_t *message)
1234 notmuch_status_t status;
1235 Xapian::WritableDatabase *db;
1236 const char *mid, *tid, *query_string;
1237 notmuch_message_t *ghost;
1238 notmuch_private_status_t private_status;
1239 notmuch_database_t *notmuch;
1240 notmuch_query_t *query;
1241 unsigned int count = 0;
1244 mid = notmuch_message_get_message_id (message);
1245 tid = notmuch_message_get_thread_id (message);
1246 notmuch = message->notmuch;
1248 status = _notmuch_database_ensure_writable (message->notmuch);
1252 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1253 db->delete_document (message->doc_id);
1255 /* if this was a ghost to begin with, we are done */
1256 private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
1258 return COERCE_STATUS (private_status,
1259 "Error trying to determine whether message was a ghost");
1261 return NOTMUCH_STATUS_SUCCESS;
1263 query_string = talloc_asprintf (message, "thread:%s", tid);
1264 query = notmuch_query_create (notmuch, query_string);
1266 return NOTMUCH_STATUS_OUT_OF_MEMORY;
1267 status = notmuch_query_count_messages (query, &count);
1269 notmuch_query_destroy (query);
1274 /* reintroduce a ghost in its place because there are still
1275 * other active messages in this thread: */
1276 ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);
1277 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1278 private_status = _notmuch_message_initialize_ghost (ghost, tid);
1279 if (! private_status)
1280 _notmuch_message_sync (ghost);
1281 } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1282 /* this is deeply weird, and we should not have gotten
1283 into this state. is there a better error message to
1285 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1288 notmuch_message_destroy (ghost);
1289 status = COERCE_STATUS (private_status, "Error converting to ghost message");
1291 /* the thread is empty; drop all ghost messages from it */
1292 notmuch_messages_t *messages;
1293 status = _notmuch_query_search_documents (query,
1296 if (status == NOTMUCH_STATUS_SUCCESS) {
1297 notmuch_status_t last_error = NOTMUCH_STATUS_SUCCESS;
1298 while (notmuch_messages_valid (messages)) {
1299 message = notmuch_messages_get (messages);
1300 status = _notmuch_message_delete (message);
1301 if (status) /* we'll report the last failure we see;
1302 * if there is more than one failure, we
1303 * forget about previous ones */
1304 last_error = status;
1305 notmuch_message_destroy (message);
1306 notmuch_messages_move_to_next (messages);
1308 status = last_error;
1311 notmuch_query_destroy (query);
1315 /* Transform a blank message into a ghost message. The caller must
1316 * _notmuch_message_sync the message. */
1317 notmuch_private_status_t
1318 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1319 const char *thread_id)
1321 notmuch_private_status_t status;
1323 status = _notmuch_message_add_term (message, "type", "ghost");
1326 status = _notmuch_message_add_term (message, "thread", thread_id);
1330 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1333 /* Ensure that 'message' is not holding any file object open. Future
1334 * calls to various functions will still automatically open the
1335 * message file as needed.
1338 _notmuch_message_close (notmuch_message_t *message)
1340 if (message->message_file) {
1341 _notmuch_message_file_close (message->message_file);
1342 message->message_file = NULL;
1346 /* Add a name:value term to 'message', (the actual term will be
1347 * encoded by prefixing the value with a short prefix). See
1348 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1349 * names to prefix values.
1351 * This change will not be reflected in the database until the next
1352 * call to _notmuch_message_sync. */
1353 notmuch_private_status_t
1354 _notmuch_message_add_term (notmuch_message_t *message,
1355 const char *prefix_name,
1362 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1364 term = talloc_asprintf (message, "%s%s",
1365 _find_prefix (prefix_name), value);
1367 if (strlen (term) > NOTMUCH_TERM_MAX)
1368 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1370 message->doc.add_term (term, 0);
1371 message->modified = true;
1375 _notmuch_message_invalidate_metadata (message, prefix_name);
1377 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1380 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1381 * term will be added both prefixed (if prefix_name is not NULL) and
1382 * also non-prefixed). */
1383 notmuch_private_status_t
1384 _notmuch_message_gen_terms (notmuch_message_t *message,
1385 const char *prefix_name,
1388 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1391 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1393 term_gen->set_document (message->doc);
1396 const char *prefix = _find_prefix (prefix_name);
1398 term_gen->set_termpos (message->termpos);
1399 term_gen->index_text (text, 1, prefix);
1400 /* Create a gap between this an the next terms so they don't
1401 * appear to be a phrase. */
1402 message->termpos = term_gen->get_termpos () + 100;
1404 _notmuch_message_invalidate_metadata (message, prefix_name);
1407 term_gen->set_termpos (message->termpos);
1408 term_gen->index_text (text);
1409 /* Create a term gap, as above. */
1410 message->termpos = term_gen->get_termpos () + 100;
1412 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1415 /* Remove a name:value term from 'message', (the actual term will be
1416 * encoded by prefixing the value with a short prefix). See
1417 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1418 * names to prefix values.
1420 * This change will not be reflected in the database until the next
1421 * call to _notmuch_message_sync. */
1422 notmuch_private_status_t
1423 _notmuch_message_remove_term (notmuch_message_t *message,
1424 const char *prefix_name,
1430 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1432 term = talloc_asprintf (message, "%s%s",
1433 _find_prefix (prefix_name), value);
1435 if (strlen (term) > NOTMUCH_TERM_MAX)
1436 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1439 message->doc.remove_term (term);
1440 message->modified = true;
1441 } catch (const Xapian::InvalidArgumentError) {
1442 /* We'll let the philosophers try to wrestle with the
1443 * question of whether failing to remove that which was not
1444 * there in the first place is failure. For us, we'll silently
1445 * consider it all good. */
1450 _notmuch_message_invalidate_metadata (message, prefix_name);
1452 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1455 notmuch_private_status_t
1456 _notmuch_message_has_term (notmuch_message_t *message,
1457 const char *prefix_name,
1463 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
1466 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1468 term = talloc_asprintf (message, "%s%s",
1469 _find_prefix (prefix_name), value);
1471 if (strlen (term) > NOTMUCH_TERM_MAX)
1472 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1475 /* Look for the exact term */
1476 Xapian::TermIterator i = message->doc.termlist_begin ();
1478 if (i != message->doc.termlist_end () &&
1479 !strcmp ((*i).c_str (), term))
1481 } catch (Xapian::Error &error) {
1482 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1491 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1493 notmuch_private_status_t private_status;
1494 notmuch_status_t status;
1496 status = _notmuch_database_ensure_writable (message->notmuch);
1501 return NOTMUCH_STATUS_NULL_POINTER;
1503 if (strlen (tag) > NOTMUCH_TAG_MAX)
1504 return NOTMUCH_STATUS_TAG_TOO_LONG;
1506 private_status = _notmuch_message_add_term (message, "tag", tag);
1507 if (private_status) {
1508 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
1512 if (! message->frozen)
1513 _notmuch_message_sync (message);
1515 return NOTMUCH_STATUS_SUCCESS;
1519 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1521 notmuch_private_status_t private_status;
1522 notmuch_status_t status;
1524 status = _notmuch_database_ensure_writable (message->notmuch);
1529 return NOTMUCH_STATUS_NULL_POINTER;
1531 if (strlen (tag) > NOTMUCH_TAG_MAX)
1532 return NOTMUCH_STATUS_TAG_TOO_LONG;
1534 private_status = _notmuch_message_remove_term (message, "tag", tag);
1535 if (private_status) {
1536 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1540 if (! message->frozen)
1541 _notmuch_message_sync (message);
1543 return NOTMUCH_STATUS_SUCCESS;
1546 /* Is the given filename within a maildir directory?
1548 * Specifically, is the final directory component of 'filename' either
1549 * "cur" or "new". If so, return a pointer to that final directory
1550 * component within 'filename'. If not, return NULL.
1552 * A non-NULL return value is guaranteed to be a valid string pointer
1553 * pointing to the characters "new/" or "cur/", (but not
1557 _filename_is_in_maildir (const char *filename)
1559 const char *slash, *dir = NULL;
1561 /* Find the last '/' separating directory from filename. */
1562 slash = strrchr (filename, '/');
1566 /* Jump back 4 characters to where the previous '/' will be if the
1567 * directory is named "cur" or "new". */
1568 if (slash - filename < 4)
1578 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1579 STRNCMP_LITERAL (dir, "new/") == 0)
1588 _ensure_maildir_flags (notmuch_message_t *message, bool force)
1591 notmuch_filenames_t *filenames;
1592 const char *filename, *dir;
1593 char *combined_flags = talloc_strdup (message, "");
1594 int seen_maildir_info = 0;
1596 if (message->maildir_flags) {
1598 talloc_free (message->maildir_flags);
1599 message->maildir_flags = NULL;
1603 for (filenames = notmuch_message_get_filenames (message);
1604 notmuch_filenames_valid (filenames);
1605 notmuch_filenames_move_to_next (filenames))
1607 filename = notmuch_filenames_get (filenames);
1608 dir = _filename_is_in_maildir (filename);
1613 flags = strstr (filename, ":2,");
1615 seen_maildir_info = 1;
1617 combined_flags = talloc_strdup_append (combined_flags, flags);
1618 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1619 /* Messages are delivered to new/ with no "info" part, but
1620 * they effectively have default maildir flags. According
1621 * to the spec, we should ignore the info part for
1622 * messages in new/, but some MUAs (mutt) can set maildir
1623 * flags on messages in new/, so we're liberal in what we
1625 seen_maildir_info = 1;
1628 if (seen_maildir_info)
1629 message->maildir_flags = combined_flags;
1633 notmuch_message_has_maildir_flag (notmuch_message_t *message, char flag)
1635 _ensure_maildir_flags (message, false);
1636 return message->maildir_flags && (strchr (message->maildir_flags, flag) != NULL);
1640 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1642 notmuch_status_t status;
1645 _ensure_maildir_flags (message, true);
1646 /* If none of the filenames have any maildir info field (not even
1647 * an empty info with no flags set) then there's no information to
1648 * go on, so do nothing. */
1649 if (! message->maildir_flags)
1650 return NOTMUCH_STATUS_SUCCESS;
1652 status = notmuch_message_freeze (message);
1656 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1657 if ((strchr (message->maildir_flags, flag2tag[i].flag) != NULL)
1659 flag2tag[i].inverse)
1661 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1663 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1668 status = notmuch_message_thaw (message);
1673 /* From the set of tags on 'message' and the flag2tag table, compute a
1674 * set of maildir-flag actions to be taken, (flags that should be
1675 * either set or cleared).
1677 * The result is returned as two talloced strings: to_set, and to_clear
1680 _get_maildir_flag_actions (notmuch_message_t *message,
1682 char **to_clear_ret)
1684 char *to_set, *to_clear;
1685 notmuch_tags_t *tags;
1689 to_set = talloc_strdup (message, "");
1690 to_clear = talloc_strdup (message, "");
1692 /* First, find flags for all set tags. */
1693 for (tags = notmuch_message_get_tags (message);
1694 notmuch_tags_valid (tags);
1695 notmuch_tags_move_to_next (tags))
1697 tag = notmuch_tags_get (tags);
1699 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1700 if (strcmp (tag, flag2tag[i].tag) == 0) {
1701 if (flag2tag[i].inverse)
1702 to_clear = talloc_asprintf_append (to_clear,
1706 to_set = talloc_asprintf_append (to_set,
1713 /* Then, find the flags for all tags not present. */
1714 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1715 if (flag2tag[i].inverse) {
1716 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1717 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1719 if (strchr (to_set, flag2tag[i].flag) == NULL)
1720 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1724 *to_set_ret = to_set;
1725 *to_clear_ret = to_clear;
1728 /* Given 'filename' and a set of maildir flags to set and to clear,
1729 * compute the new maildir filename.
1731 * If the existing filename is in the directory "new", the new
1732 * filename will be in the directory "cur", except for the case when
1733 * no flags are changed and the existing filename does not contain
1734 * maildir info (starting with ",2:").
1736 * After a sequence of ":2," in the filename, any subsequent
1737 * single-character flags will be added or removed according to the
1738 * characters in flags_to_set and flags_to_clear. Any existing flags
1739 * not mentioned in either string will remain. The final list of flags
1740 * will be in ASCII order.
1742 * If the original flags seem invalid, (repeated characters or
1743 * non-ASCII ordering of flags), this function will return NULL
1744 * (meaning that renaming would not be safe and should not occur).
1747 _new_maildir_filename (void *ctx,
1748 const char *filename,
1749 const char *flags_to_set,
1750 const char *flags_to_clear)
1752 const char *info, *flags;
1753 unsigned int flag, last_flag;
1754 char *filename_new, *dir;
1756 int flags_in_map = 0;
1757 bool flags_changed = false;
1761 memset (flag_map, 0, sizeof (flag_map));
1763 info = strstr (filename, ":2,");
1766 info = filename + strlen(filename);
1768 /* Loop through existing flags in filename. */
1769 for (flags = info + 3, last_flag = 0;
1771 last_flag = flag, flags++)
1775 /* Original flags not in ASCII order. Abort. */
1776 if (flag < last_flag)
1779 /* Non-ASCII flag. Abort. */
1780 if (flag > sizeof(flag_map) - 1)
1783 /* Repeated flag value. Abort. */
1792 /* Then set and clear our flags from tags. */
1793 for (flags = flags_to_set; *flags; flags++) {
1795 if (flag_map[flag] == 0) {
1798 flags_changed = true;
1802 for (flags = flags_to_clear; *flags; flags++) {
1804 if (flag_map[flag]) {
1807 flags_changed = true;
1811 /* Messages in new/ without maildir info can be kept in new/ if no
1812 * flags have changed. */
1813 dir = (char *) _filename_is_in_maildir (filename);
1814 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && !*info && !flags_changed)
1815 return talloc_strdup (ctx, filename);
1817 filename_new = (char *) talloc_size (ctx,
1819 strlen (":2,") + flags_in_map + 1);
1820 if (unlikely (filename_new == NULL))
1823 strncpy (filename_new, filename, info - filename);
1824 filename_new[info - filename] = '\0';
1826 strcat (filename_new, ":2,");
1828 s = filename_new + strlen (filename_new);
1829 for (i = 0; i < sizeof (flag_map); i++)
1838 /* If message is in new/ move it under cur/. */
1839 dir = (char *) _filename_is_in_maildir (filename_new);
1840 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1841 memcpy (dir, "cur/", 4);
1843 return filename_new;
1847 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1849 notmuch_filenames_t *filenames;
1850 const char *filename;
1852 char *to_set, *to_clear;
1853 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1855 _get_maildir_flag_actions (message, &to_set, &to_clear);
1857 for (filenames = notmuch_message_get_filenames (message);
1858 notmuch_filenames_valid (filenames);
1859 notmuch_filenames_move_to_next (filenames))
1861 filename = notmuch_filenames_get (filenames);
1863 if (! _filename_is_in_maildir (filename))
1866 filename_new = _new_maildir_filename (message, filename,
1868 if (filename_new == NULL)
1871 if (strcmp (filename, filename_new)) {
1873 notmuch_status_t new_status;
1875 err = rename (filename, filename_new);
1879 new_status = _notmuch_message_remove_filename (message,
1881 /* Hold on to only the first error. */
1882 if (! status && new_status
1883 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1884 status = new_status;
1888 new_status = _notmuch_message_add_filename (message,
1890 /* Hold on to only the first error. */
1891 if (! status && new_status) {
1892 status = new_status;
1896 _notmuch_message_sync (message);
1899 talloc_free (filename_new);
1902 talloc_free (to_set);
1903 talloc_free (to_clear);
1909 notmuch_message_remove_all_tags (notmuch_message_t *message)
1911 notmuch_private_status_t private_status;
1912 notmuch_status_t status;
1913 notmuch_tags_t *tags;
1916 status = _notmuch_database_ensure_writable (message->notmuch);
1920 for (tags = notmuch_message_get_tags (message);
1921 notmuch_tags_valid (tags);
1922 notmuch_tags_move_to_next (tags))
1924 tag = notmuch_tags_get (tags);
1926 private_status = _notmuch_message_remove_term (message, "tag", tag);
1927 if (private_status) {
1928 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1933 if (! message->frozen)
1934 _notmuch_message_sync (message);
1937 return NOTMUCH_STATUS_SUCCESS;
1941 notmuch_message_freeze (notmuch_message_t *message)
1943 notmuch_status_t status;
1945 status = _notmuch_database_ensure_writable (message->notmuch);
1951 return NOTMUCH_STATUS_SUCCESS;
1955 notmuch_message_thaw (notmuch_message_t *message)
1957 notmuch_status_t status;
1959 status = _notmuch_database_ensure_writable (message->notmuch);
1963 if (message->frozen > 0) {
1965 if (message->frozen == 0)
1966 _notmuch_message_sync (message);
1967 return NOTMUCH_STATUS_SUCCESS;
1969 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1974 notmuch_message_destroy (notmuch_message_t *message)
1976 talloc_free (message);
1979 notmuch_database_t *
1980 notmuch_message_get_database (const notmuch_message_t *message)
1982 return message->notmuch;
1986 _notmuch_message_ensure_property_map (notmuch_message_t *message)
1988 notmuch_string_node_t *node;
1990 if (message->property_map)
1993 _notmuch_message_ensure_metadata (message, message->property_term_list);
1995 message->property_map = _notmuch_string_map_create (message);
1997 for (node = message->property_term_list->head; node; node = node->next) {
2001 value = strchr(node->string, '=');
2003 INTERNAL_ERROR ("malformed property term");
2009 _notmuch_string_map_append (message->property_map, key, value);
2013 talloc_free (message->property_term_list);
2014 message->property_term_list = NULL;
2017 notmuch_string_map_t *
2018 _notmuch_message_property_map (notmuch_message_t *message)
2020 _notmuch_message_ensure_property_map (message);
2022 return message->property_map;
2026 _notmuch_message_frozen (notmuch_message_t *message)
2028 return message->frozen;
2032 notmuch_message_reindex (notmuch_message_t *message,
2033 notmuch_indexopts_t *indexopts)
2035 notmuch_database_t *notmuch = NULL;
2036 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
2037 notmuch_private_status_t private_status;
2038 notmuch_filenames_t *orig_filenames = NULL;
2039 const char *orig_thread_id = NULL;
2040 notmuch_message_file_t *message_file = NULL;
2044 if (message == NULL)
2045 return NOTMUCH_STATUS_NULL_POINTER;
2047 /* Save in case we need to delete message */
2048 orig_thread_id = notmuch_message_get_thread_id (message);
2049 if (!orig_thread_id) {
2050 /* XXX TODO: make up new error return? */
2051 INTERNAL_ERROR ("message without thread-id");
2054 /* strdup it because the metadata may be invalidated */
2055 orig_thread_id = talloc_strdup (message, orig_thread_id);
2057 notmuch = notmuch_message_get_database (message);
2059 ret = _notmuch_database_ensure_writable (notmuch);
2063 orig_filenames = notmuch_message_get_filenames (message);
2065 private_status = _notmuch_message_remove_indexed_terms (message);
2066 if (private_status) {
2067 ret = COERCE_STATUS(private_status, "error removing terms");
2071 ret = notmuch_message_remove_all_properties_with_prefix (message, "index.");
2073 goto DONE; /* XXX TODO: distinguish from other error returns above? */
2074 if (indexopts && notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE) {
2075 ret = notmuch_message_remove_all_properties (message, "session-key");
2080 /* re-add the filenames with the associated indexopts */
2081 for (; notmuch_filenames_valid (orig_filenames);
2082 notmuch_filenames_move_to_next (orig_filenames)) {
2085 const char *from, *to, *subject;
2086 char *message_id = NULL;
2087 const char *thread_id = NULL;
2089 const char *filename = notmuch_filenames_get (orig_filenames);
2091 message_file = _notmuch_message_file_open (notmuch, filename);
2092 if (message_file == NULL)
2095 ret = _notmuch_message_file_get_headers (message_file,
2096 &from, &subject, &to, &date,
2101 /* XXX TODO: deal with changing message id? */
2103 _notmuch_message_add_filename (message, filename);
2105 ret = _notmuch_database_link_message_to_parents (notmuch, message,
2111 if (thread_id == NULL)
2112 thread_id = orig_thread_id;
2114 _notmuch_message_add_term (message, "thread", thread_id);
2115 /* Take header values only from first filename */
2117 _notmuch_message_set_header_values (message, date, from, subject);
2119 ret = _notmuch_message_index_file (message, indexopts, message_file);
2121 if (ret == NOTMUCH_STATUS_FILE_ERROR)
2127 _notmuch_message_file_close (message_file);
2128 message_file = NULL;
2131 /* put back thread id to help cleanup */
2132 _notmuch_message_add_term (message, "thread", orig_thread_id);
2133 ret = _notmuch_message_delete (message);
2135 _notmuch_message_sync (message);
2140 _notmuch_message_file_close (message_file);
2142 /* XXX TODO destroy orig_filenames? */