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;
37 notmuch_string_list_t *tag_list;
38 notmuch_string_list_t *filename_term_list;
39 notmuch_string_list_t *filename_list;
42 notmuch_message_file_t *message_file;
43 notmuch_string_list_t *property_term_list;
44 notmuch_string_map_t *property_map;
45 notmuch_string_list_t *reference_list;
46 notmuch_message_list_t *replies;
48 /* For flags that are initialized on-demand, lazy_flags indicates
49 * if each flag has been initialized. */
50 unsigned long lazy_flags;
52 /* Message document modified since last sync */
55 /* last view of database the struct is synced with */
56 unsigned long last_view;
59 Xapian::termcount termpos;
62 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
64 struct maildir_flag_tag {
70 /* ASCII ordered table of Maildir flags and associated tags */
71 static const struct maildir_flag_tag flag2tag[] = {
72 { 'D', "draft", false },
73 { 'F', "flagged", false },
74 { 'P', "passed", false },
75 { 'R', "replied", false },
76 { 'S', "unread", true }
79 /* We end up having to call the destructor explicitly because we had
80 * to use "placement new" in order to initialize C++ objects within a
81 * block that we allocated with talloc. So C++ is making talloc
82 * slightly less simple to use, (we wouldn't need
83 * talloc_set_destructor at all otherwise).
86 _notmuch_message_destructor (notmuch_message_t *message)
88 message->doc.~Document ();
93 #define LOG_XAPIAN_EXCEPTION(message, error) _log_xapian_exception (__location__, message, error)
96 _log_xapian_exception (const char *where, notmuch_message_t *message, const Xapian::Error error)
98 notmuch_database_t *notmuch = notmuch_message_get_database (message);
100 _notmuch_database_log (notmuch,
101 "A Xapian exception occurred at %s: %s\n",
103 error.get_msg ().c_str ());
104 notmuch->exception_reported = true;
107 static notmuch_message_t *
108 _notmuch_message_create_for_document (const void *talloc_owner,
109 notmuch_database_t *notmuch,
111 Xapian::Document doc,
112 notmuch_private_status_t *status)
114 notmuch_message_t *message;
117 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
119 message = talloc (talloc_owner, notmuch_message_t);
120 if (unlikely (message == NULL)) {
122 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
126 message->notmuch = notmuch;
127 message->doc_id = doc_id;
131 message->lazy_flags = 0;
133 /* the message is initially not synchronized with Xapian */
134 message->last_view = 0;
136 /* Calculated after the thread structure is computed */
137 message->thread_depth = 0;
139 /* Each of these will be lazily created as needed. */
140 message->message_id = NULL;
141 message->thread_id = NULL;
142 message->in_reply_to = NULL;
143 message->tag_list = NULL;
144 message->filename_term_list = NULL;
145 message->filename_list = NULL;
146 message->maildir_flags = NULL;
147 message->message_file = NULL;
148 message->author = NULL;
149 message->property_term_list = NULL;
150 message->property_map = NULL;
151 message->reference_list = NULL;
153 message->replies = _notmuch_message_list_create (message);
154 if (unlikely (message->replies == NULL)) {
156 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
160 /* This is C++'s creepy "placement new", which is really just an
161 * ugly way to call a constructor for a pre-allocated object. So
162 * it's really not an error to not be checking for OUT_OF_MEMORY
163 * here, since this "new" isn't actually allocating memory. This
164 * is language-design comedy of the wrong kind. */
166 new (&message->doc) Xapian::Document;
168 talloc_set_destructor (message, _notmuch_message_destructor);
171 message->termpos = 0;
172 message->modified = false;
177 /* Create a new notmuch_message_t object for an existing document in
180 * Here, 'talloc owner' is an optional talloc context to which the new
181 * message will belong. This allows for the caller to not bother
182 * calling notmuch_message_destroy on the message, and know that all
183 * memory will be reclaimed when 'talloc_owner' is freed. The caller
184 * still can call notmuch_message_destroy when finished with the
185 * message if desired.
187 * The 'talloc_owner' argument can also be NULL, in which case the
188 * caller *is* responsible for calling notmuch_message_destroy.
190 * If no document exists in the database with document ID of 'doc_id'
191 * then this function returns NULL and optionally sets *status to
192 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
194 * This function can also fail to due lack of available memory,
195 * returning NULL and optionally setting *status to
196 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
198 * The caller can pass NULL for status if uninterested in
199 * distinguishing these two cases.
202 _notmuch_message_create (const void *talloc_owner,
203 notmuch_database_t *notmuch,
205 notmuch_private_status_t *status)
207 Xapian::Document doc;
210 doc = notmuch->xapian_db->get_document (doc_id);
211 } catch (const Xapian::DocNotFoundError &error) {
213 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
217 return _notmuch_message_create_for_document (talloc_owner, notmuch,
218 doc_id, doc, status);
221 /* Create a new notmuch_message_t object for a specific message ID,
222 * (which may or may not already exist in the database).
224 * The 'notmuch' database will be the talloc owner of the returned
227 * This function returns a valid notmuch_message_t whether or not
228 * there is already a document in the database with the given message
229 * ID. These two cases can be distinguished by the value of *status:
232 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
234 * There is already a document with message ID 'message_id' in the
235 * database. The returned message can be used to query/modify the
236 * document. The message may be a ghost message.
238 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
240 * No document with 'message_id' exists in the database. The
241 * returned message contains a newly created document (not yet
242 * added to the database) and a document ID that is known not to
243 * exist in the database. This message is "blank"; that is, it
244 * contains only a message ID and no other metadata. The caller
245 * can modify the message, and a call to _notmuch_message_sync
246 * will add the document to the database.
248 * If an error occurs, this function will return NULL and *status
249 * will be set as appropriate. (The status pointer argument must
253 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
254 const char *message_id,
255 notmuch_private_status_t *status_ret)
257 notmuch_message_t *message;
258 Xapian::Document doc;
262 *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
266 return talloc_steal (notmuch, message);
267 else if (*status_ret)
270 /* If the message ID is too long, substitute its sha1 instead. */
271 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
272 message_id = _notmuch_message_id_compressed (message, message_id);
274 term = talloc_asprintf (NULL, "%s%s",
275 _find_prefix ("id"), message_id);
277 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
281 if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
282 INTERNAL_ERROR ("Failure to ensure database is writable.");
285 doc.add_term (term, 0);
288 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
290 doc_id = _notmuch_database_generate_doc_id (notmuch);
291 } catch (const Xapian::Error &error) {
292 _notmuch_database_log (notmuch,
293 "A Xapian exception occurred creating message: %s\n",
294 error.get_msg ().c_str ());
295 notmuch->exception_reported = true;
296 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
300 message = _notmuch_message_create_for_document (notmuch, notmuch,
301 doc_id, doc, status_ret);
303 /* We want to inform the caller that we had to create a new
305 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
306 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
312 _notmuch_message_get_term (notmuch_message_t *message,
313 Xapian::TermIterator &i, Xapian::TermIterator &end,
316 int prefix_len = strlen (prefix);
324 const std::string &term = *i;
326 if (strncmp (term.c_str (), prefix, prefix_len))
329 value = talloc_strdup (message, term.c_str () + prefix_len);
331 #if DEBUG_DATABASE_SANITY
334 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
335 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
336 message->doc_id, prefix, value,
337 (*i).c_str () + prefix_len);
345 _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
347 Xapian::TermIterator i, end;
349 if (field && (message->last_view >= message->notmuch->view))
352 const char *thread_prefix = _find_prefix ("thread"),
353 *tag_prefix = _find_prefix ("tag"),
354 *id_prefix = _find_prefix ("id"),
355 *type_prefix = _find_prefix ("type"),
356 *filename_prefix = _find_prefix ("file-direntry"),
357 *property_prefix = _find_prefix ("property"),
358 *reference_prefix = _find_prefix ("reference"),
359 *replyto_prefix = _find_prefix ("replyto");
361 /* We do this all in a single pass because Xapian decompresses the
362 * term list every time you iterate over it. Thus, while this is
363 * slightly more costly than looking up individual fields if only
364 * one field of the message object is actually used, it's a huge
365 * win as more fields are used. */
366 for (int count = 0; count < 3; count++) {
368 i = message->doc.termlist_begin ();
369 end = message->doc.termlist_end ();
372 if (! message->thread_id)
374 _notmuch_message_get_term (message, i, end, thread_prefix);
377 assert (strcmp (thread_prefix, tag_prefix) < 0);
378 if (! message->tag_list) {
380 _notmuch_database_get_terms_with_prefix (message, i, end,
382 _notmuch_string_list_sort (message->tag_list);
386 assert (strcmp (tag_prefix, id_prefix) < 0);
387 if (! message->message_id)
388 message->message_id =
389 _notmuch_message_get_term (message, i, end, id_prefix);
391 /* Get document type */
392 assert (strcmp (id_prefix, type_prefix) < 0);
393 if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {
394 i.skip_to (type_prefix);
395 /* "T" is the prefix "type" fields. See
396 * BOOLEAN_PREFIX_INTERNAL. */
398 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
399 else if (*i == "Tghost")
400 NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
402 INTERNAL_ERROR ("Message without type term");
403 NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
406 /* Get filename list. Here we get only the terms. We lazily
407 * expand them to full file names when needed in
408 * _notmuch_message_ensure_filename_list. */
409 assert (strcmp (type_prefix, filename_prefix) < 0);
410 if (! message->filename_term_list && ! message->filename_list)
411 message->filename_term_list =
412 _notmuch_database_get_terms_with_prefix (message, i, end,
416 /* Get property terms. Mimic the setup with filenames above */
417 assert (strcmp (filename_prefix, property_prefix) < 0);
418 if (! message->property_map && ! message->property_term_list)
419 message->property_term_list =
420 _notmuch_database_get_terms_with_prefix (message, i, end,
424 assert (strcmp (property_prefix, reference_prefix) < 0);
425 if (! message->reference_list) {
426 message->reference_list =
427 _notmuch_database_get_terms_with_prefix (message, i, end,
432 assert (strcmp (property_prefix, replyto_prefix) < 0);
433 if (! message->in_reply_to)
434 message->in_reply_to =
435 _notmuch_message_get_term (message, i, end, replyto_prefix);
438 /* It's perfectly valid for a message to have no In-Reply-To
439 * header. For these cases, we return an empty string. */
440 if (! message->in_reply_to)
441 message->in_reply_to = talloc_strdup (message, "");
443 /* all the way without an exception */
445 } catch (const Xapian::DatabaseModifiedError &error) {
446 notmuch_status_t status = notmuch_database_reopen (message->notmuch,
447 NOTMUCH_DATABASE_MODE_READ_ONLY);
448 if (status != NOTMUCH_STATUS_SUCCESS)
449 INTERNAL_ERROR ("unhandled error from notmuch_database_reopen: %s\n",
450 notmuch_status_to_string (status));
453 message->last_view = message->notmuch->view;
457 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
458 const char *prefix_name)
460 if (strcmp ("thread", prefix_name) == 0) {
461 talloc_free (message->thread_id);
462 message->thread_id = NULL;
465 if (strcmp ("tag", prefix_name) == 0) {
466 talloc_unlink (message, message->tag_list);
467 message->tag_list = NULL;
470 if (strcmp ("type", prefix_name) == 0) {
471 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
472 NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
475 if (strcmp ("file-direntry", prefix_name) == 0) {
476 talloc_free (message->filename_term_list);
477 talloc_free (message->filename_list);
478 message->filename_term_list = message->filename_list = NULL;
481 if (strcmp ("property", prefix_name) == 0) {
483 if (message->property_term_list)
484 talloc_free (message->property_term_list);
485 message->property_term_list = NULL;
487 if (message->property_map)
488 talloc_unlink (message, message->property_map);
490 message->property_map = NULL;
493 if (strcmp ("replyto", prefix_name) == 0) {
494 talloc_free (message->in_reply_to);
495 message->in_reply_to = NULL;
500 _notmuch_message_get_doc_id (notmuch_message_t *message)
502 return message->doc_id;
506 notmuch_message_get_message_id (notmuch_message_t *message)
509 _notmuch_message_ensure_metadata (message, message->message_id);
510 } catch (const Xapian::Error &error) {
511 LOG_XAPIAN_EXCEPTION (message, error);
515 if (! message->message_id)
516 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
518 return message->message_id;
522 _notmuch_message_ensure_message_file (notmuch_message_t *message)
524 const char *filename;
526 if (message->message_file)
529 filename = notmuch_message_get_filename (message);
530 if (unlikely (filename == NULL))
533 message->message_file = _notmuch_message_file_open_ctx (
534 notmuch_message_get_database (message), message, filename);
538 notmuch_message_get_header (notmuch_message_t *message, const char *header)
540 Xapian::valueno slot = Xapian::BAD_VALUENO;
542 /* Fetch header from the appropriate xapian value field if
544 if (strcasecmp (header, "from") == 0)
545 slot = NOTMUCH_VALUE_FROM;
546 else if (strcasecmp (header, "subject") == 0)
547 slot = NOTMUCH_VALUE_SUBJECT;
548 else if (strcasecmp (header, "message-id") == 0)
549 slot = NOTMUCH_VALUE_MESSAGE_ID;
551 if (slot != Xapian::BAD_VALUENO) {
553 std::string value = message->doc.get_value (slot);
555 /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then
556 * empty values indicate empty headers. If we don't, then
557 * it could just mean we didn't record the header. */
558 if ((message->notmuch->features &
559 NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) ||
561 return talloc_strdup (message, value.c_str ());
563 } catch (Xapian::Error &error) {
564 LOG_XAPIAN_EXCEPTION (message, error);
569 /* Otherwise fall back to parsing the file */
570 _notmuch_message_ensure_message_file (message);
571 if (message->message_file == NULL)
574 return _notmuch_message_file_get_header (message->message_file, header);
577 /* Return the message ID from the In-Reply-To header of 'message'.
579 * Returns an empty string ("") if 'message' has no In-Reply-To
582 * Returns NULL if any error occurs.
585 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
587 _notmuch_message_ensure_metadata (message, message->in_reply_to);
588 return message->in_reply_to;
592 notmuch_message_get_thread_id (notmuch_message_t *message)
595 _notmuch_message_ensure_metadata (message, message->thread_id);
596 } catch (Xapian::Error &error) {
597 LOG_XAPIAN_EXCEPTION (message, error);
600 if (! message->thread_id)
601 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
603 return message->thread_id;
607 _notmuch_message_add_reply (notmuch_message_t *message,
608 notmuch_message_t *reply)
610 _notmuch_message_list_add_message (message->replies, reply);
614 _notmuch_message_get_thread_depth (notmuch_message_t *message)
616 return message->thread_depth;
620 _notmuch_message_label_depths (notmuch_message_t *message,
623 message->thread_depth = depth;
625 for (notmuch_messages_t *messages = _notmuch_messages_create (message->replies);
626 notmuch_messages_valid (messages);
627 notmuch_messages_move_to_next (messages)) {
628 notmuch_message_t *child = notmuch_messages_get (messages);
629 _notmuch_message_label_depths (child, depth + 1);
633 const notmuch_string_list_t *
634 _notmuch_message_get_references (notmuch_message_t *message)
636 _notmuch_message_ensure_metadata (message, message->reference_list);
637 return message->reference_list;
641 _cmpmsg (const void *pa, const void *pb)
643 notmuch_message_t **a = (notmuch_message_t **) pa;
644 notmuch_message_t **b = (notmuch_message_t **) pb;
645 time_t time_a = notmuch_message_get_date (*a);
646 time_t time_b = notmuch_message_get_date (*b);
648 if (time_a == time_b)
650 else if (time_a < time_b)
656 notmuch_message_list_t *
657 _notmuch_message_sort_subtrees (void *ctx, notmuch_message_list_t *list)
661 size_t capacity = 16;
666 void *local = talloc_new (NULL);
667 notmuch_message_list_t *new_list = _notmuch_message_list_create (ctx);
668 notmuch_message_t **message_array = talloc_zero_array (local, notmuch_message_t *, capacity);
670 for (notmuch_messages_t *messages = _notmuch_messages_create (list);
671 notmuch_messages_valid (messages);
672 notmuch_messages_move_to_next (messages)) {
673 notmuch_message_t *root = notmuch_messages_get (messages);
674 if (count >= capacity) {
676 message_array = talloc_realloc (local, message_array, notmuch_message_t *, capacity);
678 message_array[count++] = root;
679 root->replies = _notmuch_message_sort_subtrees (root, root->replies);
682 qsort (message_array, count, sizeof (notmuch_message_t *), _cmpmsg);
683 for (size_t i = 0; i < count; i++) {
684 _notmuch_message_list_add_message (new_list, message_array[i]);
693 notmuch_message_get_replies (notmuch_message_t *message)
695 return _notmuch_messages_create (message->replies);
699 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
701 Xapian::TermIterator i;
702 size_t prefix_len = 0;
704 prefix_len = strlen (prefix);
707 i = message->doc.termlist_begin ();
710 /* Terminate loop when no terms remain with desired prefix. */
711 if (i == message->doc.termlist_end () ||
712 strncmp ((*i).c_str (), prefix, prefix_len))
716 message->doc.remove_term ((*i));
717 message->modified = true;
718 } catch (const Xapian::InvalidArgumentError) {
719 /* Ignore failure to remove non-existent term. */
723 _notmuch_message_invalidate_metadata (message, "property");
727 /* Remove all terms generated by indexing, i.e. not tags or
728 * properties, along with any automatic tags*/
729 /* According to Xapian API docs, none of these calls throw
731 static notmuch_private_status_t
732 _notmuch_message_remove_indexed_terms (notmuch_message_t *message)
734 Xapian::TermIterator i;
737 id_prefix = _find_prefix ("id"),
738 property_prefix = _find_prefix ("property"),
739 tag_prefix = _find_prefix ("tag"),
740 type_prefix = _find_prefix ("type");
742 /* Make sure we have the data to restore to Xapian*/
743 _notmuch_message_ensure_metadata (message, NULL);
745 /* Empirically, it turns out to be faster to remove all the terms,
746 * and add back the ones we want. */
747 message->doc.clear_terms ();
748 message->modified = true;
750 /* still a mail message */
751 message->doc.add_term (type_prefix + "mail");
753 /* Put back message-id */
754 message->doc.add_term (id_prefix + message->message_id);
756 /* Put back non-automatic tags */
757 for (notmuch_tags_t *tags = notmuch_message_get_tags (message);
758 notmuch_tags_valid (tags);
759 notmuch_tags_move_to_next (tags)) {
761 const char *tag = notmuch_tags_get (tags);
763 if (strcmp (tag, "encrypted") != 0 &&
764 strcmp (tag, "signed") != 0 &&
765 strcmp (tag, "attachment") != 0) {
766 std::string term = tag_prefix + tag;
767 message->doc.add_term (term);
771 /* Put back properties */
772 notmuch_message_properties_t *list;
774 for (list = notmuch_message_get_properties (message, "", false);
775 notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
776 std::string term = property_prefix +
777 notmuch_message_properties_key (list) + "=" +
778 notmuch_message_properties_value (list);
780 message->doc.add_term (term);
783 notmuch_message_properties_destroy (list);
785 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
789 /* Return true if p points at "new" or "cur". */
791 is_maildir (const char *p)
793 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
796 /* Add "folder:" term for directory. */
797 NODISCARD static notmuch_status_t
798 _notmuch_message_add_folder_terms (notmuch_message_t *message,
799 const char *directory)
803 folder = talloc_strdup (NULL, directory);
805 return NOTMUCH_STATUS_OUT_OF_MEMORY;
808 * If the message file is in a leaf directory named "new" or
809 * "cur", presume maildir and index the parent directory. Thus a
810 * "folder:" prefix search matches messages in the specified
811 * maildir folder, i.e. in the specified directory and its "new"
812 * and "cur" subdirectories.
814 * Note that this means the "folder:" prefix can't be used for
815 * distinguishing between message files in "new" or "cur". The
816 * "path:" prefix needs to be used for that.
818 * Note the deliberate difference to _filename_is_in_maildir(). We
819 * don't want to index different things depending on the existence
820 * or non-existence of all maildir sibling directories "new",
821 * "cur", and "tmp". Doing so would be surprising, and difficult
822 * for the user to fix in case all subdirectories were not in
823 * place during indexing.
825 last = strrchr (folder, '/');
827 if (is_maildir (last + 1))
829 } else if (is_maildir (folder)) {
833 if (notmuch_status_t status = COERCE_STATUS (_notmuch_message_add_term (message, "folder",
835 "adding folder term"))
838 talloc_free (folder);
840 message->modified = true;
841 return NOTMUCH_STATUS_SUCCESS;
844 #define RECURSIVE_SUFFIX "/**"
846 /* Add "path:" terms for directory. */
847 NODISCARD static notmuch_status_t
848 _notmuch_message_add_path_terms (notmuch_message_t *message,
849 const char *directory)
851 notmuch_status_t status;
853 /* Add exact "path:" term. */
854 status = COERCE_STATUS (_notmuch_message_add_term (message, "path", directory),
859 if (strlen (directory)) {
862 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
864 return NOTMUCH_STATUS_OUT_OF_MEMORY;
866 /* Add recursive "path:" terms for directory and all parents. */
867 for (p = path + strlen (path) - 1; p > path; p--) {
869 strcpy (p, RECURSIVE_SUFFIX);
870 status = COERCE_STATUS (_notmuch_message_add_term (message, "path", path),
880 /* Recursive all-matching path:** for consistency. */
881 status = COERCE_STATUS (_notmuch_message_add_term (message, "path", "**"),
886 return NOTMUCH_STATUS_SUCCESS;
889 /* Add directory based terms for all filenames of the message. */
890 static notmuch_status_t
891 _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
893 const char *direntry_prefix = _find_prefix ("file-direntry");
894 int direntry_prefix_len = strlen (direntry_prefix);
895 Xapian::TermIterator i = message->doc.termlist_begin ();
896 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
898 for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {
899 unsigned int directory_id;
900 const char *direntry, *directory;
902 const std::string &term = *i;
903 notmuch_status_t term_status;
905 /* Terminate loop at first term without desired prefix. */
906 if (strncmp (term.c_str (), direntry_prefix, direntry_prefix_len))
909 /* Indicate that there are filenames remaining. */
910 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
912 direntry = term.c_str ();
913 direntry += direntry_prefix_len;
915 directory_id = strtol (direntry, &colon, 10);
917 if (colon == NULL || *colon != ':')
918 INTERNAL_ERROR ("malformed direntry");
920 directory = _notmuch_database_get_directory_path (ctx,
924 term_status = _notmuch_message_add_folder_terms (message, directory);
928 term_status = _notmuch_message_add_path_terms (message, directory);
936 /* Add an additional 'filename' for 'message'.
938 * This change will not be reflected in the database until the next
939 * call to _notmuch_message_sync. */
941 _notmuch_message_add_filename (notmuch_message_t *message,
942 const char *filename)
944 const char *relative, *directory;
945 notmuch_status_t status;
946 notmuch_private_status_t private_status;
947 void *local = talloc_new (message);
950 if (filename == NULL)
951 INTERNAL_ERROR ("Message filename cannot be NULL.");
953 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
954 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
955 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
957 relative = _notmuch_database_relative_path (message->notmuch, filename);
959 status = _notmuch_database_split_path (local, relative, &directory, NULL);
963 status = _notmuch_database_filename_to_direntry (
964 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
968 /* New file-direntry allows navigating to this message with
969 * notmuch_directory_get_child_files() . */
970 private_status = _notmuch_message_add_term (message, "file-direntry", direntry);
971 switch (private_status) {
972 case NOTMUCH_PRIVATE_STATUS_SUCCESS:
974 case NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG:
975 _notmuch_database_log (message->notmuch, "filename too long for file-direntry term: %s\n",
977 return NOTMUCH_STATUS_PATH_ERROR;
979 return COERCE_STATUS (private_status, "adding file-direntry term");
982 status = _notmuch_message_add_folder_terms (message, directory);
986 status = _notmuch_message_add_path_terms (message, directory);
992 return NOTMUCH_STATUS_SUCCESS;
995 /* Remove a particular 'filename' from 'message'.
997 * This change will not be reflected in the database until the next
998 * call to _notmuch_message_sync.
1000 * If this message still has other filenames, returns
1001 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
1003 * Note: This function does not remove a document from the database,
1004 * even if the specified filename is the only filename for this
1005 * message. For that functionality, see
1006 * notmuch_database_remove_message. */
1008 _notmuch_message_remove_filename (notmuch_message_t *message,
1009 const char *filename)
1011 void *local = talloc_new (message);
1013 notmuch_private_status_t private_status;
1014 notmuch_status_t status;
1016 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
1017 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
1018 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
1020 status = _notmuch_database_filename_to_direntry (
1021 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
1022 if (status || ! direntry)
1025 /* Unlink this file from its parent directory. */
1026 private_status = _notmuch_message_remove_term (message,
1027 "file-direntry", direntry);
1028 status = COERCE_STATUS (private_status,
1029 "Unexpected error from _notmuch_message_remove_term");
1033 /* Re-synchronize "folder:" and "path:" terms for this message. */
1035 /* Remove all "folder:" terms. */
1036 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
1038 /* Remove all "path:" terms. */
1039 _notmuch_message_remove_terms (message, _find_prefix ("path"));
1041 /* Add back terms for all remaining filenames of the message. */
1042 status = _notmuch_message_add_directory_terms (local, message);
1044 talloc_free (local);
1049 /* Upgrade the "folder:" prefix from V1 to V2. */
1050 #define FOLDER_PREFIX_V1 "XFOLDER"
1051 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
1053 _notmuch_message_upgrade_folder (notmuch_message_t *message)
1055 /* Remove all old "folder:" terms. */
1056 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
1058 /* Remove all old "folder:" stemmed terms. */
1059 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
1061 /* Add new boolean "folder:" and "path:" terms. */
1062 _notmuch_message_add_directory_terms (message, message);
1066 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
1068 return talloc_strdup (message, message->doc.get_data ().c_str ());
1072 _notmuch_message_clear_data (notmuch_message_t *message)
1074 message->doc.set_data ("");
1075 message->modified = true;
1079 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
1081 notmuch_string_node_t *node;
1083 if (message->filename_list)
1086 _notmuch_message_ensure_metadata (message, message->filename_term_list);
1088 message->filename_list = _notmuch_string_list_create (message);
1089 node = message->filename_term_list->head;
1092 /* A message document created by an old version of notmuch
1093 * (prior to rename support) will have the filename in the
1094 * data of the document rather than as a file-direntry term.
1096 * It would be nice to do the upgrade of the document directly
1097 * here, but the database is likely open in read-only mode. */
1099 std::string datastr = message->doc.get_data ();
1100 const char *data = datastr.c_str ();
1103 INTERNAL_ERROR ("message with no filename");
1105 _notmuch_string_list_append (message->filename_list, data);
1110 for (; node; node = node->next) {
1111 void *local = talloc_new (message);
1112 const char *db_path, *directory, *basename, *filename;
1113 char *colon, *direntry = NULL;
1114 unsigned int directory_id;
1116 direntry = node->string;
1118 directory_id = strtol (direntry, &colon, 10);
1120 if (colon == NULL || *colon != ':')
1121 INTERNAL_ERROR ("malformed direntry");
1123 basename = colon + 1;
1127 db_path = notmuch_config_get (message->notmuch, NOTMUCH_CONFIG_MAIL_ROOT);
1129 directory = _notmuch_database_get_directory_path (local,
1133 if (strlen (directory))
1134 filename = talloc_asprintf (message, "%s/%s/%s",
1135 db_path, directory, basename);
1137 filename = talloc_asprintf (message, "%s/%s",
1140 _notmuch_string_list_append (message->filename_list, filename);
1142 talloc_free (local);
1145 talloc_free (message->filename_term_list);
1146 message->filename_term_list = NULL;
1150 notmuch_message_get_filename (notmuch_message_t *message)
1153 _notmuch_message_ensure_filename_list (message);
1154 } catch (Xapian::Error &error) {
1155 LOG_XAPIAN_EXCEPTION (message, error);
1159 if (message->filename_list == NULL)
1162 if (message->filename_list->head == NULL ||
1163 message->filename_list->head->string == NULL) {
1164 INTERNAL_ERROR ("message with no filename");
1167 return message->filename_list->head->string;
1170 notmuch_filenames_t *
1171 notmuch_message_get_filenames (notmuch_message_t *message)
1174 _notmuch_message_ensure_filename_list (message);
1175 } catch (Xapian::Error &error) {
1176 LOG_XAPIAN_EXCEPTION (message, error);
1180 return _notmuch_filenames_create (message, message->filename_list);
1184 notmuch_message_count_files (notmuch_message_t *message)
1187 _notmuch_message_ensure_filename_list (message);
1188 } catch (Xapian::Error &error) {
1189 LOG_XAPIAN_EXCEPTION (message, error);
1193 return _notmuch_string_list_length (message->filename_list);
1197 notmuch_message_get_flag_st (notmuch_message_t *message,
1198 notmuch_message_flag_t flag,
1199 notmuch_bool_t *is_set)
1202 return NOTMUCH_STATUS_NULL_POINTER;
1205 if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
1206 ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
1207 _notmuch_message_ensure_metadata (message, NULL);
1208 } catch (Xapian::Error &error) {
1209 LOG_XAPIAN_EXCEPTION (message, error);
1210 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1213 *is_set = NOTMUCH_TEST_BIT (message->flags, flag);
1214 return NOTMUCH_STATUS_SUCCESS;
1218 notmuch_message_get_flag (notmuch_message_t *message,
1219 notmuch_message_flag_t flag)
1221 notmuch_bool_t is_set;
1222 notmuch_status_t status;
1224 status = notmuch_message_get_flag_st (message, flag, &is_set);
1233 notmuch_message_set_flag (notmuch_message_t *message,
1234 notmuch_message_flag_t flag, notmuch_bool_t enable)
1237 NOTMUCH_SET_BIT (&message->flags, flag);
1239 NOTMUCH_CLEAR_BIT (&message->flags, flag);
1240 NOTMUCH_SET_BIT (&message->lazy_flags, flag);
1244 notmuch_message_get_date (notmuch_message_t *message)
1249 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
1250 } catch (Xapian::Error &error) {
1251 LOG_XAPIAN_EXCEPTION (message, error);
1256 /* sortable_unserialise is undefined on empty string */
1258 return Xapian::sortable_unserialise (value);
1262 notmuch_message_get_tags (notmuch_message_t *message)
1264 notmuch_tags_t *tags;
1267 _notmuch_message_ensure_metadata (message, message->tag_list);
1268 } catch (Xapian::Error &error) {
1269 LOG_XAPIAN_EXCEPTION (message, error);
1273 tags = _notmuch_tags_create (message, message->tag_list);
1274 /* _notmuch_tags_create steals the reference to the tag_list, but
1275 * in this case it's still used by the message, so we add an
1276 * *additional* talloc reference to the list. As a result, it's
1277 * possible to modify the message tags (which talloc_unlink's the
1278 * current list from the message) while still iterating because
1279 * the iterator will keep the current list alive. */
1280 if (! talloc_reference (message, message->tag_list))
1287 _notmuch_message_get_author (notmuch_message_t *message)
1289 return message->author;
1293 _notmuch_message_set_author (notmuch_message_t *message,
1296 if (message->author)
1297 talloc_free (message->author);
1298 message->author = talloc_strdup (message, author);
1303 _notmuch_message_set_header_values (notmuch_message_t *message,
1306 const char *subject)
1310 /* GMime really doesn't want to see a NULL date, so protect its
1312 if (date == NULL || *date == '\0') {
1315 time_value = g_mime_utils_header_decode_date_unix (date);
1317 * Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=779923
1323 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
1324 Xapian::sortable_serialise (time_value));
1325 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
1326 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1327 message->modified = true;
1331 _notmuch_message_update_subject (notmuch_message_t *message,
1332 const char *subject)
1334 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1335 message->modified = true;
1338 /* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD. The caller
1339 * must call _notmuch_message_sync. */
1341 _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
1343 /* _notmuch_message_sync will update the last modification
1344 * revision; we just have to ask it to. */
1345 message->modified = true;
1348 /* Synchronize changes made to message->doc out into the database. */
1350 _notmuch_message_sync (notmuch_message_t *message)
1352 if (_notmuch_database_mode (message->notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
1355 if (! message->modified)
1358 /* Update the last modification of this message. */
1359 if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)
1360 /* sortable_serialise gives a reasonably compact encoding,
1361 * which directly translates to reduced IO when scanning the
1362 * value stream. Since it's built for doubles, we only get 53
1363 * effective bits, but that's still enough for the database to
1364 * last a few centuries at 1 million revisions per second. */
1365 message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,
1366 Xapian::sortable_serialise (
1367 _notmuch_database_new_revision (
1368 message->notmuch)));
1370 message->notmuch->writable_xapian_db->
1371 replace_document (message->doc_id, message->doc);
1372 message->modified = false;
1375 /* Delete a message document from the database, leaving a ghost
1376 * message in its place */
1378 _notmuch_message_delete (notmuch_message_t *message)
1380 notmuch_status_t status;
1381 const char *mid, *tid;
1382 notmuch_message_t *ghost;
1383 notmuch_private_status_t private_status;
1384 notmuch_database_t *notmuch;
1385 unsigned int count = 0;
1388 mid = notmuch_message_get_message_id (message);
1389 tid = notmuch_message_get_thread_id (message);
1390 notmuch = message->notmuch;
1392 status = _notmuch_database_ensure_writable (message->notmuch);
1397 Xapian::PostingIterator thread_doc, thread_doc_end;
1398 Xapian::PostingIterator mail_doc, mail_doc_end;
1400 /* look for a non-ghost message in the same thread */
1401 /* if this was a ghost to begin with, we are done */
1402 private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
1404 return COERCE_STATUS (private_status,
1405 "Error trying to determine whether message was a ghost");
1407 message->notmuch->writable_xapian_db->delete_document (message->doc_id);
1410 return NOTMUCH_STATUS_SUCCESS;
1412 _notmuch_database_find_doc_ids (message->notmuch, "thread", tid, &thread_doc,
1414 _notmuch_database_find_doc_ids (message->notmuch, "type", "mail", &mail_doc, &mail_doc_end);
1416 while (count == 0 &&
1417 thread_doc != thread_doc_end &&
1418 mail_doc != mail_doc_end) {
1419 thread_doc.skip_to (*mail_doc);
1420 if (thread_doc != thread_doc_end) {
1421 if (*thread_doc == *mail_doc) {
1424 mail_doc.skip_to (*thread_doc);
1425 if (mail_doc != mail_doc_end && *thread_doc == *mail_doc)
1430 } catch (Xapian::Error &error) {
1431 LOG_XAPIAN_EXCEPTION (message, error);
1432 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1435 /* reintroduce a ghost in its place because there are still
1436 * other active messages in this thread: */
1437 ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);
1438 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1439 private_status = _notmuch_message_initialize_ghost (ghost, tid);
1440 if (! private_status)
1441 _notmuch_message_sync (ghost);
1442 } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1443 /* this is deeply weird, and we should not have gotten
1444 * into this state. is there a better error message to
1446 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1449 notmuch_message_destroy (ghost);
1450 status = COERCE_STATUS (private_status, "Error converting to ghost message");
1452 /* the thread now contains only ghosts: delete them */
1454 Xapian::PostingIterator doc, doc_end;
1456 _notmuch_database_find_doc_ids (message->notmuch, "thread", tid, &doc, &doc_end);
1458 for (; doc != doc_end; doc++) {
1459 message->notmuch->writable_xapian_db->delete_document (*doc);
1461 } catch (Xapian::Error &error) {
1462 LOG_XAPIAN_EXCEPTION (message, error);
1463 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1470 /* Transform a blank message into a ghost message. The caller must
1471 * _notmuch_message_sync the message. */
1472 notmuch_private_status_t
1473 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1474 const char *thread_id)
1476 notmuch_private_status_t status;
1478 status = _notmuch_message_add_term (message, "type", "ghost");
1481 status = _notmuch_message_add_term (message, "thread", thread_id);
1485 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1488 /* Ensure that 'message' is not holding any file object open. Future
1489 * calls to various functions will still automatically open the
1490 * message file as needed.
1493 _notmuch_message_close (notmuch_message_t *message)
1495 if (message->message_file) {
1496 _notmuch_message_file_close (message->message_file);
1497 message->message_file = NULL;
1501 /* Add a name:value term to 'message', (the actual term will be
1502 * encoded by prefixing the value with a short prefix). See
1503 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1504 * names to prefix values.
1506 * This change will not be reflected in the database until the next
1507 * call to _notmuch_message_sync. */
1508 NODISCARD notmuch_private_status_t
1509 _notmuch_message_add_term (notmuch_message_t *message,
1510 const char *prefix_name,
1515 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
1518 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1520 term = talloc_asprintf (message, "%s%s",
1521 _find_prefix (prefix_name), value);
1522 if (strlen (term) > NOTMUCH_TERM_MAX) {
1523 status = NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1528 message->doc.add_term (term, 0);
1529 message->modified = true;
1530 _notmuch_message_invalidate_metadata (message, prefix_name);
1531 } catch (Xapian::Error &error) {
1532 LOG_XAPIAN_EXCEPTION (message, error);
1533 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1541 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1542 * term will be added with the appropriate prefix if prefix_name is
1545 notmuch_private_status_t
1546 _notmuch_message_gen_terms (notmuch_message_t *message,
1547 const char *prefix_name,
1550 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1553 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1555 term_gen->set_document (message->doc);
1556 term_gen->set_termpos (message->termpos);
1559 const char *prefix = _notmuch_database_prefix (message->notmuch, prefix_name);
1561 return NOTMUCH_PRIVATE_STATUS_BAD_PREFIX;
1563 _notmuch_message_invalidate_metadata (message, prefix_name);
1564 term_gen->index_text (text, 1, prefix);
1566 term_gen->index_text (text);
1569 /* Create a gap between this an the next terms so they don't
1570 * appear to be a phrase. */
1571 message->termpos = term_gen->get_termpos () + 100;
1573 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1576 /* Remove a name:value term from 'message', (the actual term will be
1577 * encoded by prefixing the value with a short prefix). See
1578 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1579 * names to prefix values.
1581 * This change will not be reflected in the database until the next
1582 * call to _notmuch_message_sync. */
1583 NODISCARD notmuch_private_status_t
1584 _notmuch_message_remove_term (notmuch_message_t *message,
1585 const char *prefix_name,
1591 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1593 term = talloc_asprintf (message, "%s%s",
1594 _find_prefix (prefix_name), value);
1596 if (strlen (term) > NOTMUCH_TERM_MAX)
1597 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1600 message->doc.remove_term (term);
1601 message->modified = true;
1602 } catch (const Xapian::InvalidArgumentError &error) {
1603 /* We'll let the philosophers try to wrestle with the
1604 * question of whether failing to remove that which was not
1605 * there in the first place is failure. For us, we'll silently
1606 * consider it all good. */
1607 LOG_XAPIAN_EXCEPTION (message, error);
1612 _notmuch_message_invalidate_metadata (message, prefix_name);
1614 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1617 notmuch_private_status_t
1618 _notmuch_message_has_term (notmuch_message_t *message,
1619 const char *prefix_name,
1625 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
1628 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1630 term = talloc_asprintf (message, "%s%s",
1631 _find_prefix (prefix_name), value);
1633 if (strlen (term) > NOTMUCH_TERM_MAX)
1634 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1637 /* Look for the exact term */
1638 Xapian::TermIterator i = message->doc.termlist_begin ();
1640 if (i != message->doc.termlist_end () &&
1641 ! strcmp ((*i).c_str (), term))
1643 } catch (Xapian::Error &error) {
1644 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1653 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1655 notmuch_private_status_t private_status;
1656 notmuch_status_t status;
1659 status = _notmuch_database_ensure_writable (message->notmuch);
1664 return NOTMUCH_STATUS_NULL_POINTER;
1666 if (strlen (tag) > NOTMUCH_TAG_MAX)
1667 return NOTMUCH_STATUS_TAG_TOO_LONG;
1669 private_status = _notmuch_message_add_term (message, "tag", tag);
1670 if (private_status) {
1671 return COERCE_STATUS (private_status,
1672 "_notmuch_message_remove_term return unexpected value: %d\n",
1676 if (! message->frozen)
1677 _notmuch_message_sync (message);
1679 } catch (Xapian::Error &error) {
1680 LOG_XAPIAN_EXCEPTION (message, error);
1681 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1684 return NOTMUCH_STATUS_SUCCESS;
1688 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1690 notmuch_private_status_t private_status;
1691 notmuch_status_t status;
1694 status = _notmuch_database_ensure_writable (message->notmuch);
1699 return NOTMUCH_STATUS_NULL_POINTER;
1701 if (strlen (tag) > NOTMUCH_TAG_MAX)
1702 return NOTMUCH_STATUS_TAG_TOO_LONG;
1704 private_status = _notmuch_message_remove_term (message, "tag", tag);
1705 if (private_status) {
1706 return COERCE_STATUS (private_status,
1707 "_notmuch_message_remove_term return unexpected value: %d\n",
1711 if (! message->frozen)
1712 _notmuch_message_sync (message);
1713 } catch (Xapian::Error &error) {
1714 LOG_XAPIAN_EXCEPTION (message, error);
1715 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1718 return NOTMUCH_STATUS_SUCCESS;
1721 /* Is the given filename within a maildir directory?
1723 * Specifically, is the final directory component of 'filename' either
1724 * "cur" or "new". If so, return a pointer to that final directory
1725 * component within 'filename'. If not, return NULL.
1727 * A non-NULL return value is guaranteed to be a valid string pointer
1728 * pointing to the characters "new/" or "cur/", (but not
1732 _filename_is_in_maildir (const char *filename)
1734 const char *slash, *dir = NULL;
1736 /* Find the last '/' separating directory from filename. */
1737 slash = strrchr (filename, '/');
1741 /* Jump back 4 characters to where the previous '/' will be if the
1742 * directory is named "cur" or "new". */
1743 if (slash - filename < 4)
1753 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1754 STRNCMP_LITERAL (dir, "new/") == 0) {
1761 static notmuch_status_t
1762 _ensure_maildir_flags (notmuch_message_t *message, bool force)
1765 notmuch_filenames_t *filenames;
1766 const char *filename, *dir;
1767 char *combined_flags = talloc_strdup (message, "");
1768 int seen_maildir_info = 0;
1770 if (message->maildir_flags) {
1772 talloc_free (message->maildir_flags);
1773 message->maildir_flags = NULL;
1776 filenames = notmuch_message_get_filenames (message);
1778 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1780 notmuch_filenames_valid (filenames);
1781 notmuch_filenames_move_to_next (filenames)) {
1782 filename = notmuch_filenames_get (filenames);
1783 dir = _filename_is_in_maildir (filename);
1788 flags = strstr (filename, ":2,");
1790 seen_maildir_info = 1;
1792 combined_flags = talloc_strdup_append (combined_flags, flags);
1793 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1794 /* Messages are delivered to new/ with no "info" part, but
1795 * they effectively have default maildir flags. According
1796 * to the spec, we should ignore the info part for
1797 * messages in new/, but some MUAs (mutt) can set maildir
1798 * flags on messages in new/, so we're liberal in what we
1800 seen_maildir_info = 1;
1803 if (seen_maildir_info)
1804 message->maildir_flags = combined_flags;
1805 return NOTMUCH_STATUS_SUCCESS;
1809 notmuch_message_has_maildir_flag (notmuch_message_t *message, char flag)
1811 notmuch_status_t status;
1814 status = notmuch_message_has_maildir_flag_st (message, flag, &ret);
1822 notmuch_message_has_maildir_flag_st (notmuch_message_t *message,
1824 notmuch_bool_t *is_set)
1826 notmuch_status_t status;
1829 return NOTMUCH_STATUS_NULL_POINTER;
1831 status = _ensure_maildir_flags (message, false);
1835 *is_set = message->maildir_flags && (strchr (message->maildir_flags, flag) != NULL);
1836 return NOTMUCH_STATUS_SUCCESS;
1840 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1842 notmuch_status_t status;
1845 status = _ensure_maildir_flags (message, true);
1848 /* If none of the filenames have any maildir info field (not even
1849 * an empty info with no flags set) then there's no information to
1850 * go on, so do nothing. */
1851 if (! message->maildir_flags)
1852 return NOTMUCH_STATUS_SUCCESS;
1854 status = notmuch_message_freeze (message);
1858 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1859 if ((strchr (message->maildir_flags, flag2tag[i].flag) != NULL)
1861 flag2tag[i].inverse) {
1862 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1864 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1869 status = notmuch_message_thaw (message);
1874 /* From the set of tags on 'message' and the flag2tag table, compute a
1875 * set of maildir-flag actions to be taken, (flags that should be
1876 * either set or cleared).
1878 * The result is returned as two talloced strings: to_set, and to_clear
1881 _get_maildir_flag_actions (notmuch_message_t *message,
1883 char **to_clear_ret)
1885 char *to_set, *to_clear;
1886 notmuch_tags_t *tags;
1890 to_set = talloc_strdup (message, "");
1891 to_clear = talloc_strdup (message, "");
1893 /* First, find flags for all set tags. */
1894 for (tags = notmuch_message_get_tags (message);
1895 notmuch_tags_valid (tags);
1896 notmuch_tags_move_to_next (tags)) {
1897 tag = notmuch_tags_get (tags);
1899 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1900 if (strcmp (tag, flag2tag[i].tag) == 0) {
1901 if (flag2tag[i].inverse)
1902 to_clear = talloc_asprintf_append (to_clear,
1906 to_set = talloc_asprintf_append (to_set,
1913 /* Then, find the flags for all tags not present. */
1914 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1915 if (flag2tag[i].inverse) {
1916 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1917 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1919 if (strchr (to_set, flag2tag[i].flag) == NULL)
1920 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1924 *to_set_ret = to_set;
1925 *to_clear_ret = to_clear;
1928 /* Given 'filename' and a set of maildir flags to set and to clear,
1929 * compute the new maildir filename.
1931 * If the existing filename is in the directory "new", the new
1932 * filename will be in the directory "cur", except for the case when
1933 * no flags are changed and the existing filename does not contain
1934 * maildir info (starting with ",2:").
1936 * After a sequence of ":2," in the filename, any subsequent
1937 * single-character flags will be added or removed according to the
1938 * characters in flags_to_set and flags_to_clear. Any existing flags
1939 * not mentioned in either string will remain. The final list of flags
1940 * will be in ASCII order.
1942 * If the original flags seem invalid, (repeated characters or
1943 * non-ASCII ordering of flags), this function will return NULL
1944 * (meaning that renaming would not be safe and should not occur).
1947 _new_maildir_filename (void *ctx,
1948 const char *filename,
1949 const char *flags_to_set,
1950 const char *flags_to_clear)
1952 const char *info, *flags;
1953 unsigned int flag, last_flag;
1954 char *filename_new, *dir;
1956 int flags_in_map = 0;
1957 bool flags_changed = false;
1961 memset (flag_map, 0, sizeof (flag_map));
1963 info = strstr (filename, ":2,");
1966 info = filename + strlen (filename);
1968 /* Loop through existing flags in filename. */
1969 for (flags = info + 3, last_flag = 0;
1971 last_flag = flag, flags++) {
1974 /* Original flags not in ASCII order. Abort. */
1975 if (flag < last_flag)
1978 /* Non-ASCII flag. Abort. */
1979 if (flag > sizeof (flag_map) - 1)
1982 /* Repeated flag value. Abort. */
1991 /* Then set and clear our flags from tags. */
1992 for (flags = flags_to_set; *flags; flags++) {
1994 if (flag_map[flag] == 0) {
1997 flags_changed = true;
2001 for (flags = flags_to_clear; *flags; flags++) {
2003 if (flag_map[flag]) {
2006 flags_changed = true;
2010 /* Messages in new/ without maildir info can be kept in new/ if no
2011 * flags have changed. */
2012 dir = (char *) _filename_is_in_maildir (filename);
2013 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && ! *info && ! flags_changed)
2014 return talloc_strdup (ctx, filename);
2016 filename_new = (char *) talloc_size (ctx,
2018 strlen (":2,") + flags_in_map + 1);
2019 if (unlikely (filename_new == NULL))
2022 strncpy (filename_new, filename, info - filename);
2023 filename_new[info - filename] = '\0';
2025 strcat (filename_new, ":2,");
2027 s = filename_new + strlen (filename_new);
2028 for (i = 0; i < sizeof (flag_map); i++) {
2036 /* If message is in new/ move it under cur/. */
2037 dir = (char *) _filename_is_in_maildir (filename_new);
2038 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
2039 memcpy (dir, "cur/", 4);
2041 return filename_new;
2045 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
2047 notmuch_filenames_t *filenames;
2048 const char *filename;
2050 char *to_set, *to_clear;
2051 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
2053 status = _notmuch_database_ensure_writable (message->notmuch);
2057 _get_maildir_flag_actions (message, &to_set, &to_clear);
2059 for (filenames = notmuch_message_get_filenames (message);
2060 notmuch_filenames_valid (filenames);
2061 notmuch_filenames_move_to_next (filenames)) {
2062 filename = notmuch_filenames_get (filenames);
2064 if (! _filename_is_in_maildir (filename))
2067 filename_new = _new_maildir_filename (message, filename,
2069 if (filename_new == NULL)
2072 if (strcmp (filename, filename_new)) {
2074 notmuch_status_t new_status;
2076 err = rename (filename, filename_new);
2080 new_status = _notmuch_message_remove_filename (message,
2082 /* Hold on to only the first error. */
2083 if (! status && new_status
2084 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
2085 status = new_status;
2089 new_status = _notmuch_message_add_filename (message,
2091 /* Hold on to only the first error. */
2092 if (! status && new_status) {
2093 status = new_status;
2097 _notmuch_message_sync (message);
2100 talloc_free (filename_new);
2103 talloc_free (to_set);
2104 talloc_free (to_clear);
2110 notmuch_message_remove_all_tags (notmuch_message_t *message)
2112 notmuch_private_status_t private_status;
2113 notmuch_status_t status;
2114 notmuch_tags_t *tags;
2117 status = _notmuch_database_ensure_writable (message->notmuch);
2120 tags = notmuch_message_get_tags (message);
2122 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
2125 notmuch_tags_valid (tags);
2126 notmuch_tags_move_to_next (tags)) {
2127 tag = notmuch_tags_get (tags);
2129 private_status = _notmuch_message_remove_term (message, "tag", tag);
2130 if (private_status) {
2131 return COERCE_STATUS (private_status,
2132 "_notmuch_message_remove_term return unexpected value: %d\n",
2137 if (! message->frozen)
2138 _notmuch_message_sync (message);
2141 return NOTMUCH_STATUS_SUCCESS;
2145 notmuch_message_freeze (notmuch_message_t *message)
2147 notmuch_status_t status;
2149 status = _notmuch_database_ensure_writable (message->notmuch);
2155 return NOTMUCH_STATUS_SUCCESS;
2159 notmuch_message_thaw (notmuch_message_t *message)
2161 notmuch_status_t status;
2163 status = _notmuch_database_ensure_writable (message->notmuch);
2167 if (message->frozen > 0) {
2169 if (message->frozen == 0)
2170 _notmuch_message_sync (message);
2171 return NOTMUCH_STATUS_SUCCESS;
2173 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
2178 notmuch_message_destroy (notmuch_message_t *message)
2180 talloc_free (message);
2183 notmuch_database_t *
2184 notmuch_message_get_database (const notmuch_message_t *message)
2186 return message->notmuch;
2190 _notmuch_message_ensure_property_map (notmuch_message_t *message)
2192 notmuch_string_node_t *node;
2194 if (message->property_map)
2197 _notmuch_message_ensure_metadata (message, message->property_term_list);
2199 message->property_map = _notmuch_string_map_create (message);
2201 for (node = message->property_term_list->head; node; node = node->next) {
2205 value = strchr (node->string, '=');
2207 INTERNAL_ERROR ("malformed property term");
2213 _notmuch_string_map_append (message->property_map, key, value);
2217 talloc_free (message->property_term_list);
2218 message->property_term_list = NULL;
2221 notmuch_string_map_t *
2222 _notmuch_message_property_map (notmuch_message_t *message)
2224 _notmuch_message_ensure_property_map (message);
2226 return message->property_map;
2230 _notmuch_message_frozen (notmuch_message_t *message)
2232 return message->frozen;
2236 notmuch_message_reindex (notmuch_message_t *message,
2237 notmuch_indexopts_t *indexopts)
2239 notmuch_database_t *notmuch = NULL;
2240 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
2241 notmuch_private_status_t private_status;
2242 notmuch_filenames_t *orig_filenames = NULL;
2243 const char *orig_thread_id = NULL;
2244 notmuch_message_file_t *message_file = NULL;
2248 if (message == NULL)
2249 return NOTMUCH_STATUS_NULL_POINTER;
2251 /* Save in case we need to delete message */
2252 orig_thread_id = notmuch_message_get_thread_id (message);
2253 if (! orig_thread_id) {
2254 /* the following is correct as long as there is only one reason
2255 * n_m_get_thread_id returns NULL
2257 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
2260 /* strdup it because the metadata may be invalidated */
2261 orig_thread_id = talloc_strdup (message, orig_thread_id);
2263 notmuch = notmuch_message_get_database (message);
2265 ret = _notmuch_database_ensure_writable (notmuch);
2269 orig_filenames = notmuch_message_get_filenames (message);
2271 private_status = _notmuch_message_remove_indexed_terms (message);
2272 if (private_status) {
2273 ret = COERCE_STATUS (private_status, "error removing terms");
2277 ret = notmuch_message_remove_all_properties_with_prefix (message, "index.");
2279 goto DONE; /* XXX TODO: distinguish from other error returns above? */
2280 if (indexopts && notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE) {
2281 ret = notmuch_message_remove_all_properties (message, "session-key");
2286 /* re-add the filenames with the associated indexopts */
2287 for (; notmuch_filenames_valid (orig_filenames);
2288 notmuch_filenames_move_to_next (orig_filenames)) {
2291 const char *from, *to, *subject;
2292 char *message_id = NULL;
2293 const char *thread_id = NULL;
2295 const char *filename = notmuch_filenames_get (orig_filenames);
2297 message_file = _notmuch_message_file_open (notmuch, filename);
2298 if (message_file == NULL)
2301 ret = _notmuch_message_file_get_headers (message_file,
2302 &from, &subject, &to, &date,
2307 /* XXX TODO: deal with changing message id? */
2309 _notmuch_message_add_filename (message, filename);
2311 ret = _notmuch_database_link_message_to_parents (notmuch, message,
2317 if (thread_id == NULL)
2318 thread_id = orig_thread_id;
2320 ret = COERCE_STATUS (_notmuch_message_add_term (message, "thread", thread_id),
2321 "adding thread term");
2325 /* Take header values only from first filename */
2327 _notmuch_message_set_header_values (message, date, from, subject);
2329 ret = _notmuch_message_index_file (message, indexopts, message_file);
2331 if (ret == NOTMUCH_STATUS_FILE_ERROR)
2337 _notmuch_message_file_close (message_file);
2338 message_file = NULL;
2341 /* put back thread id to help cleanup */
2342 ret = COERCE_STATUS (_notmuch_message_add_term (message, "thread", orig_thread_id),
2343 "adding thread term");
2347 ret = _notmuch_message_delete (message);
2349 _notmuch_message_sync (message);
2354 _notmuch_message_file_close (message_file);
2356 /* XXX TODO destroy orig_filenames? */