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;
176 /* Create a new notmuch_message_t object for an existing document in
179 * Here, 'talloc owner' is an optional talloc context to which the new
180 * message will belong. This allows for the caller to not bother
181 * calling notmuch_message_destroy on the message, and know that all
182 * memory will be reclaimed when 'talloc_owner' is freed. The caller
183 * still can call notmuch_message_destroy when finished with the
184 * message if desired.
186 * The 'talloc_owner' argument can also be NULL, in which case the
187 * caller *is* responsible for calling notmuch_message_destroy.
189 * If no document exists in the database with document ID of 'doc_id'
190 * then this function returns NULL and optionally sets *status to
191 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
193 * This function can also fail to due lack of available memory,
194 * returning NULL and optionally setting *status to
195 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
197 * The caller can pass NULL for status if uninterested in
198 * distinguishing these two cases.
201 _notmuch_message_create (const void *talloc_owner,
202 notmuch_database_t *notmuch,
204 notmuch_private_status_t *status)
206 Xapian::Document doc;
209 doc = notmuch->xapian_db->get_document (doc_id);
210 } catch (const Xapian::DocNotFoundError &error) {
212 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
216 return _notmuch_message_create_for_document (talloc_owner, notmuch,
217 doc_id, doc, status);
220 /* Create a new notmuch_message_t object for a specific message ID,
221 * (which may or may not already exist in the database).
223 * The 'notmuch' database will be the talloc owner of the returned
226 * This function returns a valid notmuch_message_t whether or not
227 * there is already a document in the database with the given message
228 * ID. These two cases can be distinguished by the value of *status:
231 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
233 * There is already a document with message ID 'message_id' in the
234 * database. The returned message can be used to query/modify the
235 * document. The message may be a ghost message.
237 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
239 * No document with 'message_id' exists in the database. The
240 * returned message contains a newly created document (not yet
241 * added to the database) and a document ID that is known not to
242 * exist in the database. This message is "blank"; that is, it
243 * contains only a message ID and no other metadata. The caller
244 * can modify the message, and a call to _notmuch_message_sync
245 * will add the document to the database.
247 * If an error occurs, this function will return NULL and *status
248 * will be set as appropriate. (The status pointer argument must
252 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
253 const char *message_id,
254 notmuch_private_status_t *status_ret)
256 notmuch_message_t *message;
257 Xapian::Document doc;
261 *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
265 return talloc_steal (notmuch, message);
266 else if (*status_ret)
269 /* If the message ID is too long, substitute its sha1 instead. */
270 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
271 message_id = _notmuch_message_id_compressed (message, message_id);
273 term = talloc_asprintf (NULL, "%s%s",
274 _find_prefix ("id"), message_id);
276 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
280 if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
281 INTERNAL_ERROR ("Failure to ensure database is writable.");
284 doc.add_term (term, 0);
287 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
289 doc_id = _notmuch_database_generate_doc_id (notmuch);
290 } catch (const Xapian::Error &error) {
291 _notmuch_database_log (notmuch,
292 "A Xapian exception occurred creating message: %s\n",
293 error.get_msg ().c_str ());
294 notmuch->exception_reported = true;
295 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
299 message = _notmuch_message_create_for_document (notmuch, notmuch,
300 doc_id, doc, status_ret);
302 /* We want to inform the caller that we had to create a new
304 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
305 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
311 _notmuch_message_get_term (notmuch_message_t *message,
312 Xapian::TermIterator &i, Xapian::TermIterator &end,
315 int prefix_len = strlen (prefix);
323 const std::string &term = *i;
325 if (strncmp (term.c_str (), prefix, prefix_len))
328 value = talloc_strdup (message, term.c_str () + prefix_len);
330 #if DEBUG_DATABASE_SANITY
333 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
334 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
335 message->doc_id, prefix, value,
336 (*i).c_str () + prefix_len);
344 * For special applications where we only want the thread id, reading
345 * in all metadata is a heavy I/O penalty.
348 _notmuch_message_get_thread_id_only (notmuch_message_t *message)
351 Xapian::TermIterator i = message->doc.termlist_begin ();
352 Xapian::TermIterator end = message->doc.termlist_end ();
354 message->thread_id = _notmuch_message_get_term (message, i, end,
355 _find_prefix ("thread"));
356 return message->thread_id;
361 _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
363 Xapian::TermIterator i, end;
365 if (field && (message->last_view >= message->notmuch->view))
368 const char *thread_prefix = _find_prefix ("thread"),
369 *tag_prefix = _find_prefix ("tag"),
370 *id_prefix = _find_prefix ("id"),
371 *type_prefix = _find_prefix ("type"),
372 *filename_prefix = _find_prefix ("file-direntry"),
373 *property_prefix = _find_prefix ("property"),
374 *reference_prefix = _find_prefix ("reference"),
375 *replyto_prefix = _find_prefix ("replyto");
377 /* We do this all in a single pass because Xapian decompresses the
378 * term list every time you iterate over it. Thus, while this is
379 * slightly more costly than looking up individual fields if only
380 * one field of the message object is actually used, it's a huge
381 * win as more fields are used. */
382 for (int count = 0; count < 3; count++) {
384 i = message->doc.termlist_begin ();
385 end = message->doc.termlist_end ();
388 if (! message->thread_id)
390 _notmuch_message_get_term (message, i, end, thread_prefix);
393 assert (strcmp (thread_prefix, tag_prefix) < 0);
394 if (! message->tag_list) {
396 _notmuch_database_get_terms_with_prefix (message, i, end,
398 _notmuch_string_list_sort (message->tag_list);
402 assert (strcmp (tag_prefix, id_prefix) < 0);
403 if (! message->message_id)
404 message->message_id =
405 _notmuch_message_get_term (message, i, end, id_prefix);
407 /* Get document type */
408 assert (strcmp (id_prefix, type_prefix) < 0);
409 if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {
410 i.skip_to (type_prefix);
411 /* "T" is the prefix "type" fields. See
412 * BOOLEAN_PREFIX_INTERNAL. */
414 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
415 else if (*i == "Tghost")
416 NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
418 INTERNAL_ERROR ("Message without type term");
419 NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
422 /* Get filename list. Here we get only the terms. We lazily
423 * expand them to full file names when needed in
424 * _notmuch_message_ensure_filename_list. */
425 assert (strcmp (type_prefix, filename_prefix) < 0);
426 if (! message->filename_term_list && ! message->filename_list)
427 message->filename_term_list =
428 _notmuch_database_get_terms_with_prefix (message, i, end,
432 /* Get property terms. Mimic the setup with filenames above */
433 assert (strcmp (filename_prefix, property_prefix) < 0);
434 if (! message->property_map && ! message->property_term_list)
435 message->property_term_list =
436 _notmuch_database_get_terms_with_prefix (message, i, end,
440 assert (strcmp (property_prefix, reference_prefix) < 0);
441 if (! message->reference_list) {
442 message->reference_list =
443 _notmuch_database_get_terms_with_prefix (message, i, end,
448 assert (strcmp (property_prefix, replyto_prefix) < 0);
449 if (! message->in_reply_to)
450 message->in_reply_to =
451 _notmuch_message_get_term (message, i, end, replyto_prefix);
454 /* It's perfectly valid for a message to have no In-Reply-To
455 * header. For these cases, we return an empty string. */
456 if (! message->in_reply_to)
457 message->in_reply_to = talloc_strdup (message, "");
459 /* all the way without an exception */
461 } catch (const Xapian::DatabaseModifiedError &error) {
462 notmuch_status_t status = notmuch_database_reopen (message->notmuch,
463 NOTMUCH_DATABASE_MODE_READ_ONLY);
464 if (status != NOTMUCH_STATUS_SUCCESS)
465 INTERNAL_ERROR ("unhandled error from notmuch_database_reopen: %s\n",
466 notmuch_status_to_string (status));
469 message->last_view = message->notmuch->view;
473 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
474 const char *prefix_name)
476 if (strcmp ("thread", prefix_name) == 0) {
477 talloc_free (message->thread_id);
478 message->thread_id = NULL;
481 if (strcmp ("tag", prefix_name) == 0) {
482 talloc_unlink (message, message->tag_list);
483 message->tag_list = NULL;
486 if (strcmp ("type", prefix_name) == 0) {
487 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
488 NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
491 if (strcmp ("file-direntry", prefix_name) == 0) {
492 talloc_free (message->filename_term_list);
493 talloc_free (message->filename_list);
494 message->filename_term_list = message->filename_list = NULL;
497 if (strcmp ("property", prefix_name) == 0) {
499 if (message->property_term_list)
500 talloc_free (message->property_term_list);
501 message->property_term_list = NULL;
503 if (message->property_map)
504 talloc_unlink (message, message->property_map);
506 message->property_map = NULL;
509 if (strcmp ("replyto", prefix_name) == 0) {
510 talloc_free (message->in_reply_to);
511 message->in_reply_to = NULL;
516 _notmuch_message_get_doc_id (notmuch_message_t *message)
518 return message->doc_id;
522 notmuch_message_get_message_id (notmuch_message_t *message)
525 _notmuch_message_ensure_metadata (message, message->message_id);
526 } catch (const Xapian::Error &error) {
527 LOG_XAPIAN_EXCEPTION (message, error);
531 if (! message->message_id)
532 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
534 return message->message_id;
538 _notmuch_message_ensure_message_file (notmuch_message_t *message)
540 const char *filename;
542 if (message->message_file)
545 filename = notmuch_message_get_filename (message);
546 if (unlikely (filename == NULL))
549 message->message_file = _notmuch_message_file_open_ctx (
550 notmuch_message_get_database (message), message, filename);
554 notmuch_message_get_header (notmuch_message_t *message, const char *header)
556 Xapian::valueno slot = Xapian::BAD_VALUENO;
558 /* Fetch header from the appropriate xapian value field if
560 if (strcasecmp (header, "from") == 0)
561 slot = NOTMUCH_VALUE_FROM;
562 else if (strcasecmp (header, "subject") == 0)
563 slot = NOTMUCH_VALUE_SUBJECT;
564 else if (strcasecmp (header, "message-id") == 0)
565 slot = NOTMUCH_VALUE_MESSAGE_ID;
567 if (slot != Xapian::BAD_VALUENO) {
569 std::string value = message->doc.get_value (slot);
571 /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then
572 * empty values indicate empty headers. If we don't, then
573 * it could just mean we didn't record the header. */
574 if ((message->notmuch->features &
575 NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) ||
577 return talloc_strdup (message, value.c_str ());
579 } catch (Xapian::Error &error) {
580 LOG_XAPIAN_EXCEPTION (message, error);
585 /* Otherwise fall back to parsing the file */
586 _notmuch_message_ensure_message_file (message);
587 if (message->message_file == NULL)
590 return _notmuch_message_file_get_header (message->message_file, header);
593 /* Return the message ID from the In-Reply-To header of 'message'.
595 * Returns an empty string ("") if 'message' has no In-Reply-To
598 * Returns NULL if any error occurs.
601 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
603 _notmuch_message_ensure_metadata (message, message->in_reply_to);
604 return message->in_reply_to;
608 notmuch_message_get_thread_id (notmuch_message_t *message)
611 _notmuch_message_ensure_metadata (message, message->thread_id);
612 } catch (Xapian::Error &error) {
613 LOG_XAPIAN_EXCEPTION (message, error);
616 if (! message->thread_id)
617 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
619 return message->thread_id;
623 _notmuch_message_add_reply (notmuch_message_t *message,
624 notmuch_message_t *reply)
626 _notmuch_message_list_add_message (message->replies, reply);
630 _notmuch_message_get_thread_depth (notmuch_message_t *message)
632 return message->thread_depth;
636 _notmuch_message_label_depths (notmuch_message_t *message,
639 message->thread_depth = depth;
641 for (notmuch_messages_t *messages = _notmuch_messages_create (message->replies);
642 notmuch_messages_valid (messages);
643 notmuch_messages_move_to_next (messages)) {
644 notmuch_message_t *child = notmuch_messages_get (messages);
645 _notmuch_message_label_depths (child, depth + 1);
649 const notmuch_string_list_t *
650 _notmuch_message_get_references (notmuch_message_t *message)
652 _notmuch_message_ensure_metadata (message, message->reference_list);
653 return message->reference_list;
657 _cmpmsg (const void *pa, const void *pb)
659 notmuch_message_t **a = (notmuch_message_t **) pa;
660 notmuch_message_t **b = (notmuch_message_t **) pb;
661 time_t time_a = notmuch_message_get_date (*a);
662 time_t time_b = notmuch_message_get_date (*b);
664 if (time_a == time_b)
666 else if (time_a < time_b)
672 notmuch_message_list_t *
673 _notmuch_message_sort_subtrees (void *ctx, notmuch_message_list_t *list)
677 size_t capacity = 16;
682 void *local = talloc_new (NULL);
683 notmuch_message_list_t *new_list = _notmuch_message_list_create (ctx);
684 notmuch_message_t **message_array = talloc_zero_array (local, notmuch_message_t *, capacity);
686 for (notmuch_messages_t *messages = _notmuch_messages_create (list);
687 notmuch_messages_valid (messages);
688 notmuch_messages_move_to_next (messages)) {
689 notmuch_message_t *root = notmuch_messages_get (messages);
690 if (count >= capacity) {
692 message_array = talloc_realloc (local, message_array, notmuch_message_t *, capacity);
694 message_array[count++] = root;
695 root->replies = _notmuch_message_sort_subtrees (root, root->replies);
698 qsort (message_array, count, sizeof (notmuch_message_t *), _cmpmsg);
699 for (size_t i = 0; i < count; i++) {
700 _notmuch_message_list_add_message (new_list, message_array[i]);
709 notmuch_message_get_replies (notmuch_message_t *message)
711 return _notmuch_messages_create (message->replies);
715 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
717 Xapian::TermIterator i;
718 size_t prefix_len = 0;
720 prefix_len = strlen (prefix);
723 i = message->doc.termlist_begin ();
726 /* Terminate loop when no terms remain with desired prefix. */
727 if (i == message->doc.termlist_end () ||
728 strncmp ((*i).c_str (), prefix, prefix_len))
732 message->doc.remove_term ((*i));
733 message->modified = true;
734 } catch (const Xapian::InvalidArgumentError) {
735 /* Ignore failure to remove non-existent term. */
741 /* Remove all terms generated by indexing, i.e. not tags or
742 * properties, along with any automatic tags*/
743 /* According to Xapian API docs, none of these calls throw
745 static notmuch_private_status_t
746 _notmuch_message_remove_indexed_terms (notmuch_message_t *message)
748 Xapian::TermIterator i;
751 id_prefix = _find_prefix ("id"),
752 property_prefix = _find_prefix ("property"),
753 tag_prefix = _find_prefix ("tag"),
754 type_prefix = _find_prefix ("type");
756 /* Make sure we have the data to restore to Xapian*/
757 _notmuch_message_ensure_metadata (message, NULL);
759 /* Empirically, it turns out to be faster to remove all the terms,
760 * and add back the ones we want. */
761 message->doc.clear_terms ();
762 message->modified = true;
764 /* still a mail message */
765 message->doc.add_term (type_prefix + "mail");
767 /* Put back message-id */
768 message->doc.add_term (id_prefix + message->message_id);
770 /* Put back non-automatic tags */
771 for (notmuch_tags_t *tags = notmuch_message_get_tags (message);
772 notmuch_tags_valid (tags);
773 notmuch_tags_move_to_next (tags)) {
775 const char *tag = notmuch_tags_get (tags);
777 if (strcmp (tag, "encrypted") != 0 &&
778 strcmp (tag, "signed") != 0 &&
779 strcmp (tag, "attachment") != 0) {
780 std::string term = tag_prefix + tag;
781 message->doc.add_term (term);
785 /* Put back properties */
786 notmuch_message_properties_t *list;
788 for (list = notmuch_message_get_properties (message, "", false);
789 notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
790 std::string term = property_prefix +
791 notmuch_message_properties_key (list) + "=" +
792 notmuch_message_properties_value (list);
794 message->doc.add_term (term);
797 notmuch_message_properties_destroy (list);
799 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
803 /* Return true if p points at "new" or "cur". */
805 is_maildir (const char *p)
807 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
810 /* Add "folder:" term for directory. */
811 static notmuch_status_t
812 _notmuch_message_add_folder_terms (notmuch_message_t *message,
813 const char *directory)
817 folder = talloc_strdup (NULL, directory);
819 return NOTMUCH_STATUS_OUT_OF_MEMORY;
822 * If the message file is in a leaf directory named "new" or
823 * "cur", presume maildir and index the parent directory. Thus a
824 * "folder:" prefix search matches messages in the specified
825 * maildir folder, i.e. in the specified directory and its "new"
826 * and "cur" subdirectories.
828 * Note that this means the "folder:" prefix can't be used for
829 * distinguishing between message files in "new" or "cur". The
830 * "path:" prefix needs to be used for that.
832 * Note the deliberate difference to _filename_is_in_maildir(). We
833 * don't want to index different things depending on the existence
834 * or non-existence of all maildir sibling directories "new",
835 * "cur", and "tmp". Doing so would be surprising, and difficult
836 * for the user to fix in case all subdirectories were not in
837 * place during indexing.
839 last = strrchr (folder, '/');
841 if (is_maildir (last + 1))
843 } else if (is_maildir (folder)) {
847 _notmuch_message_add_term (message, "folder", folder);
849 talloc_free (folder);
851 message->modified = true;
852 return NOTMUCH_STATUS_SUCCESS;
855 #define RECURSIVE_SUFFIX "/**"
857 /* Add "path:" terms for directory. */
858 static notmuch_status_t
859 _notmuch_message_add_path_terms (notmuch_message_t *message,
860 const char *directory)
862 /* Add exact "path:" term. */
863 _notmuch_message_add_term (message, "path", directory);
865 if (strlen (directory)) {
868 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
870 return NOTMUCH_STATUS_OUT_OF_MEMORY;
872 /* Add recursive "path:" terms for directory and all parents. */
873 for (p = path + strlen (path) - 1; p > path; p--) {
875 strcpy (p, RECURSIVE_SUFFIX);
876 _notmuch_message_add_term (message, "path", path);
883 /* Recursive all-matching path:** for consistency. */
884 _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;
904 /* Terminate loop at first term without desired prefix. */
905 if (strncmp (term.c_str (), direntry_prefix, direntry_prefix_len))
908 /* Indicate that there are filenames remaining. */
909 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
911 direntry = term.c_str ();
912 direntry += direntry_prefix_len;
914 directory_id = strtol (direntry, &colon, 10);
916 if (colon == NULL || *colon != ':')
917 INTERNAL_ERROR ("malformed direntry");
919 directory = _notmuch_database_get_directory_path (ctx,
923 _notmuch_message_add_folder_terms (message, directory);
924 _notmuch_message_add_path_terms (message, directory);
930 /* Add an additional 'filename' for 'message'.
932 * This change will not be reflected in the database until the next
933 * call to _notmuch_message_sync. */
935 _notmuch_message_add_filename (notmuch_message_t *message,
936 const char *filename)
938 const char *relative, *directory;
939 notmuch_status_t status;
940 void *local = talloc_new (message);
943 if (filename == NULL)
944 INTERNAL_ERROR ("Message filename cannot be NULL.");
946 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
947 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
948 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
950 relative = _notmuch_database_relative_path (message->notmuch, filename);
952 status = _notmuch_database_split_path (local, relative, &directory, NULL);
956 status = _notmuch_database_filename_to_direntry (
957 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
961 /* New file-direntry allows navigating to this message with
962 * notmuch_directory_get_child_files() . */
963 _notmuch_message_add_term (message, "file-direntry", direntry);
965 _notmuch_message_add_folder_terms (message, directory);
966 _notmuch_message_add_path_terms (message, directory);
970 return NOTMUCH_STATUS_SUCCESS;
973 /* Remove a particular 'filename' from 'message'.
975 * This change will not be reflected in the database until the next
976 * call to _notmuch_message_sync.
978 * If this message still has other filenames, returns
979 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
981 * Note: This function does not remove a document from the database,
982 * even if the specified filename is the only filename for this
983 * message. For that functionality, see
984 * notmuch_database_remove_message. */
986 _notmuch_message_remove_filename (notmuch_message_t *message,
987 const char *filename)
989 void *local = talloc_new (message);
991 notmuch_private_status_t private_status;
992 notmuch_status_t status;
994 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
995 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
996 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
998 status = _notmuch_database_filename_to_direntry (
999 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
1000 if (status || ! direntry)
1003 /* Unlink this file from its parent directory. */
1004 private_status = _notmuch_message_remove_term (message,
1005 "file-direntry", direntry);
1006 status = COERCE_STATUS (private_status,
1007 "Unexpected error from _notmuch_message_remove_term");
1011 /* Re-synchronize "folder:" and "path:" terms for this message. */
1013 /* Remove all "folder:" terms. */
1014 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
1016 /* Remove all "path:" terms. */
1017 _notmuch_message_remove_terms (message, _find_prefix ("path"));
1019 /* Add back terms for all remaining filenames of the message. */
1020 status = _notmuch_message_add_directory_terms (local, message);
1022 talloc_free (local);
1027 /* Upgrade the "folder:" prefix from V1 to V2. */
1028 #define FOLDER_PREFIX_V1 "XFOLDER"
1029 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
1031 _notmuch_message_upgrade_folder (notmuch_message_t *message)
1033 /* Remove all old "folder:" terms. */
1034 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
1036 /* Remove all old "folder:" stemmed terms. */
1037 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
1039 /* Add new boolean "folder:" and "path:" terms. */
1040 _notmuch_message_add_directory_terms (message, message);
1044 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
1046 return talloc_strdup (message, message->doc.get_data ().c_str ());
1050 _notmuch_message_clear_data (notmuch_message_t *message)
1052 message->doc.set_data ("");
1053 message->modified = true;
1057 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
1059 notmuch_string_node_t *node;
1061 if (message->filename_list)
1064 _notmuch_message_ensure_metadata (message, message->filename_term_list);
1066 message->filename_list = _notmuch_string_list_create (message);
1067 node = message->filename_term_list->head;
1070 /* A message document created by an old version of notmuch
1071 * (prior to rename support) will have the filename in the
1072 * data of the document rather than as a file-direntry term.
1074 * It would be nice to do the upgrade of the document directly
1075 * here, but the database is likely open in read-only mode. */
1077 std::string datastr = message->doc.get_data ();
1078 const char *data = datastr.c_str ();
1081 INTERNAL_ERROR ("message with no filename");
1083 _notmuch_string_list_append (message->filename_list, data);
1088 for (; node; node = node->next) {
1089 void *local = talloc_new (message);
1090 const char *db_path, *directory, *basename, *filename;
1091 char *colon, *direntry = NULL;
1092 unsigned int directory_id;
1094 direntry = node->string;
1096 directory_id = strtol (direntry, &colon, 10);
1098 if (colon == NULL || *colon != ':')
1099 INTERNAL_ERROR ("malformed direntry");
1101 basename = colon + 1;
1105 db_path = notmuch_config_get (message->notmuch, NOTMUCH_CONFIG_MAIL_ROOT);
1107 directory = _notmuch_database_get_directory_path (local,
1111 if (strlen (directory))
1112 filename = talloc_asprintf (message, "%s/%s/%s",
1113 db_path, directory, basename);
1115 filename = talloc_asprintf (message, "%s/%s",
1118 _notmuch_string_list_append (message->filename_list, filename);
1120 talloc_free (local);
1123 talloc_free (message->filename_term_list);
1124 message->filename_term_list = NULL;
1128 notmuch_message_get_filename (notmuch_message_t *message)
1131 _notmuch_message_ensure_filename_list (message);
1132 } catch (Xapian::Error &error) {
1133 LOG_XAPIAN_EXCEPTION (message, error);
1137 if (message->filename_list == NULL)
1140 if (message->filename_list->head == NULL ||
1141 message->filename_list->head->string == NULL) {
1142 INTERNAL_ERROR ("message with no filename");
1145 return message->filename_list->head->string;
1148 notmuch_filenames_t *
1149 notmuch_message_get_filenames (notmuch_message_t *message)
1152 _notmuch_message_ensure_filename_list (message);
1153 } catch (Xapian::Error &error) {
1154 LOG_XAPIAN_EXCEPTION (message, error);
1158 return _notmuch_filenames_create (message, message->filename_list);
1162 notmuch_message_count_files (notmuch_message_t *message)
1165 _notmuch_message_ensure_filename_list (message);
1166 } catch (Xapian::Error &error) {
1167 LOG_XAPIAN_EXCEPTION (message, error);
1171 return _notmuch_string_list_length (message->filename_list);
1175 notmuch_message_get_flag_st (notmuch_message_t *message,
1176 notmuch_message_flag_t flag,
1177 notmuch_bool_t *is_set)
1180 return NOTMUCH_STATUS_NULL_POINTER;
1183 if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
1184 ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
1185 _notmuch_message_ensure_metadata (message, NULL);
1186 } catch (Xapian::Error &error) {
1187 LOG_XAPIAN_EXCEPTION (message, error);
1188 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1191 *is_set = NOTMUCH_TEST_BIT (message->flags, flag);
1192 return NOTMUCH_STATUS_SUCCESS;
1196 notmuch_message_get_flag (notmuch_message_t *message,
1197 notmuch_message_flag_t flag)
1199 notmuch_bool_t is_set;
1200 notmuch_status_t status;
1202 status = notmuch_message_get_flag_st (message, flag, &is_set);
1211 notmuch_message_set_flag (notmuch_message_t *message,
1212 notmuch_message_flag_t flag, notmuch_bool_t enable)
1215 NOTMUCH_SET_BIT (&message->flags, flag);
1217 NOTMUCH_CLEAR_BIT (&message->flags, flag);
1218 NOTMUCH_SET_BIT (&message->lazy_flags, flag);
1222 notmuch_message_get_date (notmuch_message_t *message)
1227 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
1228 } catch (Xapian::Error &error) {
1229 LOG_XAPIAN_EXCEPTION (message, error);
1234 /* sortable_unserialise is undefined on empty string */
1236 return Xapian::sortable_unserialise (value);
1240 notmuch_message_get_tags (notmuch_message_t *message)
1242 notmuch_tags_t *tags;
1245 _notmuch_message_ensure_metadata (message, message->tag_list);
1246 } catch (Xapian::Error &error) {
1247 LOG_XAPIAN_EXCEPTION (message, error);
1251 tags = _notmuch_tags_create (message, message->tag_list);
1252 /* _notmuch_tags_create steals the reference to the tag_list, but
1253 * in this case it's still used by the message, so we add an
1254 * *additional* talloc reference to the list. As a result, it's
1255 * possible to modify the message tags (which talloc_unlink's the
1256 * current list from the message) while still iterating because
1257 * the iterator will keep the current list alive. */
1258 if (! talloc_reference (message, message->tag_list))
1265 _notmuch_message_get_author (notmuch_message_t *message)
1267 return message->author;
1271 _notmuch_message_set_author (notmuch_message_t *message,
1274 if (message->author)
1275 talloc_free (message->author);
1276 message->author = talloc_strdup (message, author);
1281 _notmuch_message_set_header_values (notmuch_message_t *message,
1284 const char *subject)
1288 /* GMime really doesn't want to see a NULL date, so protect its
1290 if (date == NULL || *date == '\0') {
1293 time_value = g_mime_utils_header_decode_date_unix (date);
1295 * Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=779923
1301 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
1302 Xapian::sortable_serialise (time_value));
1303 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
1304 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1305 message->modified = true;
1309 _notmuch_message_update_subject (notmuch_message_t *message,
1310 const char *subject)
1312 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1313 message->modified = true;
1316 /* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD. The caller
1317 * must call _notmuch_message_sync. */
1319 _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
1321 /* _notmuch_message_sync will update the last modification
1322 * revision; we just have to ask it to. */
1323 message->modified = true;
1326 /* Synchronize changes made to message->doc out into the database. */
1328 _notmuch_message_sync (notmuch_message_t *message)
1330 if (_notmuch_database_mode (message->notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
1333 if (! message->modified)
1336 /* Update the last modification of this message. */
1337 if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)
1338 /* sortable_serialise gives a reasonably compact encoding,
1339 * which directly translates to reduced IO when scanning the
1340 * value stream. Since it's built for doubles, we only get 53
1341 * effective bits, but that's still enough for the database to
1342 * last a few centuries at 1 million revisions per second. */
1343 message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,
1344 Xapian::sortable_serialise (
1345 _notmuch_database_new_revision (
1346 message->notmuch)));
1348 message->notmuch->writable_xapian_db->
1349 replace_document (message->doc_id, message->doc);
1350 message->modified = false;
1353 /* Delete a message document from the database, leaving a ghost
1354 * message in its place */
1356 _notmuch_message_delete (notmuch_message_t *message)
1358 notmuch_status_t status;
1359 const char *mid, *tid;
1360 notmuch_message_t *ghost;
1361 notmuch_private_status_t private_status;
1362 notmuch_database_t *notmuch;
1363 unsigned int count = 0;
1366 mid = notmuch_message_get_message_id (message);
1367 tid = notmuch_message_get_thread_id (message);
1368 notmuch = message->notmuch;
1370 status = _notmuch_database_ensure_writable (message->notmuch);
1374 message->notmuch->writable_xapian_db->delete_document (message->doc_id);
1376 /* if this was a ghost to begin with, we are done */
1377 private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
1379 return COERCE_STATUS (private_status,
1380 "Error trying to determine whether message was a ghost");
1382 return NOTMUCH_STATUS_SUCCESS;
1384 /* look for a non-ghost message in the same thread */
1386 Xapian::PostingIterator thread_doc, thread_doc_end;
1387 Xapian::PostingIterator mail_doc, mail_doc_end;
1389 _notmuch_database_find_doc_ids (message->notmuch, "thread", tid, &thread_doc,
1391 _notmuch_database_find_doc_ids (message->notmuch, "type", "mail", &mail_doc, &mail_doc_end);
1393 while (count == 0 &&
1394 thread_doc != thread_doc_end &&
1395 mail_doc != mail_doc_end) {
1396 thread_doc.skip_to (*mail_doc);
1397 if (thread_doc != thread_doc_end) {
1398 if (*thread_doc == *mail_doc) {
1401 mail_doc.skip_to (*thread_doc);
1402 if (mail_doc != mail_doc_end && *thread_doc == *mail_doc)
1407 } catch (Xapian::Error &error) {
1408 LOG_XAPIAN_EXCEPTION (message, error);
1409 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1412 /* reintroduce a ghost in its place because there are still
1413 * other active messages in this thread: */
1414 ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);
1415 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1416 private_status = _notmuch_message_initialize_ghost (ghost, tid);
1417 if (! private_status)
1418 _notmuch_message_sync (ghost);
1419 } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1420 /* this is deeply weird, and we should not have gotten
1421 * into this state. is there a better error message to
1423 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1426 notmuch_message_destroy (ghost);
1427 status = COERCE_STATUS (private_status, "Error converting to ghost message");
1429 /* the thread now contains only ghosts: delete them */
1431 Xapian::PostingIterator doc, doc_end;
1433 _notmuch_database_find_doc_ids (message->notmuch, "thread", tid, &doc, &doc_end);
1435 for (; doc != doc_end; doc++) {
1436 message->notmuch->writable_xapian_db->delete_document (*doc);
1438 } catch (Xapian::Error &error) {
1439 LOG_XAPIAN_EXCEPTION (message, error);
1440 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1447 /* Transform a blank message into a ghost message. The caller must
1448 * _notmuch_message_sync the message. */
1449 notmuch_private_status_t
1450 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1451 const char *thread_id)
1453 notmuch_private_status_t status;
1455 status = _notmuch_message_add_term (message, "type", "ghost");
1458 status = _notmuch_message_add_term (message, "thread", thread_id);
1462 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1465 /* Ensure that 'message' is not holding any file object open. Future
1466 * calls to various functions will still automatically open the
1467 * message file as needed.
1470 _notmuch_message_close (notmuch_message_t *message)
1472 if (message->message_file) {
1473 _notmuch_message_file_close (message->message_file);
1474 message->message_file = NULL;
1478 /* Add a name:value term to 'message', (the actual term will be
1479 * encoded by prefixing the value with a short prefix). See
1480 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1481 * names to prefix values.
1483 * This change will not be reflected in the database until the next
1484 * call to _notmuch_message_sync. */
1485 notmuch_private_status_t
1486 _notmuch_message_add_term (notmuch_message_t *message,
1487 const char *prefix_name,
1494 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1496 term = talloc_asprintf (message, "%s%s",
1497 _find_prefix (prefix_name), value);
1499 if (strlen (term) > NOTMUCH_TERM_MAX)
1500 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1502 message->doc.add_term (term, 0);
1503 message->modified = true;
1507 _notmuch_message_invalidate_metadata (message, prefix_name);
1509 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1512 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1513 * term will be added with the appropriate prefix if prefix_name is
1516 notmuch_private_status_t
1517 _notmuch_message_gen_terms (notmuch_message_t *message,
1518 const char *prefix_name,
1521 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1524 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1526 term_gen->set_document (message->doc);
1527 term_gen->set_termpos (message->termpos);
1530 const char *prefix = _notmuch_database_prefix (message->notmuch, prefix_name);
1532 return NOTMUCH_PRIVATE_STATUS_BAD_PREFIX;
1534 _notmuch_message_invalidate_metadata (message, prefix_name);
1535 term_gen->index_text (text, 1, prefix);
1537 term_gen->index_text (text);
1540 /* Create a gap between this an the next terms so they don't
1541 * appear to be a phrase. */
1542 message->termpos = term_gen->get_termpos () + 100;
1544 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1547 /* Remove a name:value term from 'message', (the actual term will be
1548 * encoded by prefixing the value with a short prefix). See
1549 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1550 * names to prefix values.
1552 * This change will not be reflected in the database until the next
1553 * call to _notmuch_message_sync. */
1554 notmuch_private_status_t
1555 _notmuch_message_remove_term (notmuch_message_t *message,
1556 const char *prefix_name,
1562 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1564 term = talloc_asprintf (message, "%s%s",
1565 _find_prefix (prefix_name), value);
1567 if (strlen (term) > NOTMUCH_TERM_MAX)
1568 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1571 message->doc.remove_term (term);
1572 message->modified = true;
1573 } catch (const Xapian::InvalidArgumentError) {
1574 /* We'll let the philosophers try to wrestle with the
1575 * question of whether failing to remove that which was not
1576 * there in the first place is failure. For us, we'll silently
1577 * consider it all good. */
1582 _notmuch_message_invalidate_metadata (message, prefix_name);
1584 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1587 notmuch_private_status_t
1588 _notmuch_message_has_term (notmuch_message_t *message,
1589 const char *prefix_name,
1595 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
1598 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1600 term = talloc_asprintf (message, "%s%s",
1601 _find_prefix (prefix_name), value);
1603 if (strlen (term) > NOTMUCH_TERM_MAX)
1604 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1607 /* Look for the exact term */
1608 Xapian::TermIterator i = message->doc.termlist_begin ();
1610 if (i != message->doc.termlist_end () &&
1611 ! strcmp ((*i).c_str (), term))
1613 } catch (Xapian::Error &error) {
1614 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1623 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1625 notmuch_private_status_t private_status;
1626 notmuch_status_t status;
1629 status = _notmuch_database_ensure_writable (message->notmuch);
1634 return NOTMUCH_STATUS_NULL_POINTER;
1636 if (strlen (tag) > NOTMUCH_TAG_MAX)
1637 return NOTMUCH_STATUS_TAG_TOO_LONG;
1639 private_status = _notmuch_message_add_term (message, "tag", tag);
1640 if (private_status) {
1641 return COERCE_STATUS (private_status,
1642 "_notmuch_message_remove_term return unexpected value: %d\n",
1646 if (! message->frozen)
1647 _notmuch_message_sync (message);
1649 } catch (Xapian::Error &error) {
1650 LOG_XAPIAN_EXCEPTION (message, error);
1651 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1654 return NOTMUCH_STATUS_SUCCESS;
1658 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1660 notmuch_private_status_t private_status;
1661 notmuch_status_t status;
1664 status = _notmuch_database_ensure_writable (message->notmuch);
1669 return NOTMUCH_STATUS_NULL_POINTER;
1671 if (strlen (tag) > NOTMUCH_TAG_MAX)
1672 return NOTMUCH_STATUS_TAG_TOO_LONG;
1674 private_status = _notmuch_message_remove_term (message, "tag", tag);
1675 if (private_status) {
1676 return COERCE_STATUS (private_status,
1677 "_notmuch_message_remove_term return unexpected value: %d\n",
1681 if (! message->frozen)
1682 _notmuch_message_sync (message);
1683 } catch (Xapian::Error &error) {
1684 LOG_XAPIAN_EXCEPTION (message, error);
1685 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1688 return NOTMUCH_STATUS_SUCCESS;
1691 /* Is the given filename within a maildir directory?
1693 * Specifically, is the final directory component of 'filename' either
1694 * "cur" or "new". If so, return a pointer to that final directory
1695 * component within 'filename'. If not, return NULL.
1697 * A non-NULL return value is guaranteed to be a valid string pointer
1698 * pointing to the characters "new/" or "cur/", (but not
1702 _filename_is_in_maildir (const char *filename)
1704 const char *slash, *dir = NULL;
1706 /* Find the last '/' separating directory from filename. */
1707 slash = strrchr (filename, '/');
1711 /* Jump back 4 characters to where the previous '/' will be if the
1712 * directory is named "cur" or "new". */
1713 if (slash - filename < 4)
1723 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1724 STRNCMP_LITERAL (dir, "new/") == 0) {
1731 static notmuch_status_t
1732 _ensure_maildir_flags (notmuch_message_t *message, bool force)
1735 notmuch_filenames_t *filenames;
1736 const char *filename, *dir;
1737 char *combined_flags = talloc_strdup (message, "");
1738 int seen_maildir_info = 0;
1740 if (message->maildir_flags) {
1742 talloc_free (message->maildir_flags);
1743 message->maildir_flags = NULL;
1746 filenames = notmuch_message_get_filenames (message);
1748 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1750 notmuch_filenames_valid (filenames);
1751 notmuch_filenames_move_to_next (filenames)) {
1752 filename = notmuch_filenames_get (filenames);
1753 dir = _filename_is_in_maildir (filename);
1758 flags = strstr (filename, ":2,");
1760 seen_maildir_info = 1;
1762 combined_flags = talloc_strdup_append (combined_flags, flags);
1763 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1764 /* Messages are delivered to new/ with no "info" part, but
1765 * they effectively have default maildir flags. According
1766 * to the spec, we should ignore the info part for
1767 * messages in new/, but some MUAs (mutt) can set maildir
1768 * flags on messages in new/, so we're liberal in what we
1770 seen_maildir_info = 1;
1773 if (seen_maildir_info)
1774 message->maildir_flags = combined_flags;
1775 return NOTMUCH_STATUS_SUCCESS;
1779 notmuch_message_has_maildir_flag (notmuch_message_t *message, char flag)
1781 notmuch_status_t status;
1784 status = notmuch_message_has_maildir_flag_st (message, flag, &ret);
1792 notmuch_message_has_maildir_flag_st (notmuch_message_t *message,
1794 notmuch_bool_t *is_set)
1796 notmuch_status_t status;
1799 return NOTMUCH_STATUS_NULL_POINTER;
1801 status = _ensure_maildir_flags (message, false);
1805 *is_set = message->maildir_flags && (strchr (message->maildir_flags, flag) != NULL);
1806 return NOTMUCH_STATUS_SUCCESS;
1810 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1812 notmuch_status_t status;
1815 status = _ensure_maildir_flags (message, true);
1818 /* If none of the filenames have any maildir info field (not even
1819 * an empty info with no flags set) then there's no information to
1820 * go on, so do nothing. */
1821 if (! message->maildir_flags)
1822 return NOTMUCH_STATUS_SUCCESS;
1824 status = notmuch_message_freeze (message);
1828 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1829 if ((strchr (message->maildir_flags, flag2tag[i].flag) != NULL)
1831 flag2tag[i].inverse) {
1832 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1834 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1839 status = notmuch_message_thaw (message);
1844 /* From the set of tags on 'message' and the flag2tag table, compute a
1845 * set of maildir-flag actions to be taken, (flags that should be
1846 * either set or cleared).
1848 * The result is returned as two talloced strings: to_set, and to_clear
1851 _get_maildir_flag_actions (notmuch_message_t *message,
1853 char **to_clear_ret)
1855 char *to_set, *to_clear;
1856 notmuch_tags_t *tags;
1860 to_set = talloc_strdup (message, "");
1861 to_clear = talloc_strdup (message, "");
1863 /* First, find flags for all set tags. */
1864 for (tags = notmuch_message_get_tags (message);
1865 notmuch_tags_valid (tags);
1866 notmuch_tags_move_to_next (tags)) {
1867 tag = notmuch_tags_get (tags);
1869 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1870 if (strcmp (tag, flag2tag[i].tag) == 0) {
1871 if (flag2tag[i].inverse)
1872 to_clear = talloc_asprintf_append (to_clear,
1876 to_set = talloc_asprintf_append (to_set,
1883 /* Then, find the flags for all tags not present. */
1884 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1885 if (flag2tag[i].inverse) {
1886 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1887 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1889 if (strchr (to_set, flag2tag[i].flag) == NULL)
1890 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1894 *to_set_ret = to_set;
1895 *to_clear_ret = to_clear;
1898 /* Given 'filename' and a set of maildir flags to set and to clear,
1899 * compute the new maildir filename.
1901 * If the existing filename is in the directory "new", the new
1902 * filename will be in the directory "cur", except for the case when
1903 * no flags are changed and the existing filename does not contain
1904 * maildir info (starting with ",2:").
1906 * After a sequence of ":2," in the filename, any subsequent
1907 * single-character flags will be added or removed according to the
1908 * characters in flags_to_set and flags_to_clear. Any existing flags
1909 * not mentioned in either string will remain. The final list of flags
1910 * will be in ASCII order.
1912 * If the original flags seem invalid, (repeated characters or
1913 * non-ASCII ordering of flags), this function will return NULL
1914 * (meaning that renaming would not be safe and should not occur).
1917 _new_maildir_filename (void *ctx,
1918 const char *filename,
1919 const char *flags_to_set,
1920 const char *flags_to_clear)
1922 const char *info, *flags;
1923 unsigned int flag, last_flag;
1924 char *filename_new, *dir;
1926 int flags_in_map = 0;
1927 bool flags_changed = false;
1931 memset (flag_map, 0, sizeof (flag_map));
1933 info = strstr (filename, ":2,");
1936 info = filename + strlen (filename);
1938 /* Loop through existing flags in filename. */
1939 for (flags = info + 3, last_flag = 0;
1941 last_flag = flag, flags++) {
1944 /* Original flags not in ASCII order. Abort. */
1945 if (flag < last_flag)
1948 /* Non-ASCII flag. Abort. */
1949 if (flag > sizeof (flag_map) - 1)
1952 /* Repeated flag value. Abort. */
1961 /* Then set and clear our flags from tags. */
1962 for (flags = flags_to_set; *flags; flags++) {
1964 if (flag_map[flag] == 0) {
1967 flags_changed = true;
1971 for (flags = flags_to_clear; *flags; flags++) {
1973 if (flag_map[flag]) {
1976 flags_changed = true;
1980 /* Messages in new/ without maildir info can be kept in new/ if no
1981 * flags have changed. */
1982 dir = (char *) _filename_is_in_maildir (filename);
1983 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && ! *info && ! flags_changed)
1984 return talloc_strdup (ctx, filename);
1986 filename_new = (char *) talloc_size (ctx,
1988 strlen (":2,") + flags_in_map + 1);
1989 if (unlikely (filename_new == NULL))
1992 strncpy (filename_new, filename, info - filename);
1993 filename_new[info - filename] = '\0';
1995 strcat (filename_new, ":2,");
1997 s = filename_new + strlen (filename_new);
1998 for (i = 0; i < sizeof (flag_map); i++) {
2006 /* If message is in new/ move it under cur/. */
2007 dir = (char *) _filename_is_in_maildir (filename_new);
2008 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
2009 memcpy (dir, "cur/", 4);
2011 return filename_new;
2015 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
2017 notmuch_filenames_t *filenames;
2018 const char *filename;
2020 char *to_set, *to_clear;
2021 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
2023 _get_maildir_flag_actions (message, &to_set, &to_clear);
2025 for (filenames = notmuch_message_get_filenames (message);
2026 notmuch_filenames_valid (filenames);
2027 notmuch_filenames_move_to_next (filenames)) {
2028 filename = notmuch_filenames_get (filenames);
2030 if (! _filename_is_in_maildir (filename))
2033 filename_new = _new_maildir_filename (message, filename,
2035 if (filename_new == NULL)
2038 if (strcmp (filename, filename_new)) {
2040 notmuch_status_t new_status;
2042 err = rename (filename, filename_new);
2046 new_status = _notmuch_message_remove_filename (message,
2048 /* Hold on to only the first error. */
2049 if (! status && new_status
2050 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
2051 status = new_status;
2055 new_status = _notmuch_message_add_filename (message,
2057 /* Hold on to only the first error. */
2058 if (! status && new_status) {
2059 status = new_status;
2063 _notmuch_message_sync (message);
2066 talloc_free (filename_new);
2069 talloc_free (to_set);
2070 talloc_free (to_clear);
2076 notmuch_message_remove_all_tags (notmuch_message_t *message)
2078 notmuch_private_status_t private_status;
2079 notmuch_status_t status;
2080 notmuch_tags_t *tags;
2083 status = _notmuch_database_ensure_writable (message->notmuch);
2086 tags = notmuch_message_get_tags (message);
2088 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
2091 notmuch_tags_valid (tags);
2092 notmuch_tags_move_to_next (tags)) {
2093 tag = notmuch_tags_get (tags);
2095 private_status = _notmuch_message_remove_term (message, "tag", tag);
2096 if (private_status) {
2097 return COERCE_STATUS (private_status,
2098 "_notmuch_message_remove_term return unexpected value: %d\n",
2103 if (! message->frozen)
2104 _notmuch_message_sync (message);
2107 return NOTMUCH_STATUS_SUCCESS;
2111 notmuch_message_freeze (notmuch_message_t *message)
2113 notmuch_status_t status;
2115 status = _notmuch_database_ensure_writable (message->notmuch);
2121 return NOTMUCH_STATUS_SUCCESS;
2125 notmuch_message_thaw (notmuch_message_t *message)
2127 notmuch_status_t status;
2129 status = _notmuch_database_ensure_writable (message->notmuch);
2133 if (message->frozen > 0) {
2135 if (message->frozen == 0)
2136 _notmuch_message_sync (message);
2137 return NOTMUCH_STATUS_SUCCESS;
2139 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
2144 notmuch_message_destroy (notmuch_message_t *message)
2146 talloc_free (message);
2149 notmuch_database_t *
2150 notmuch_message_get_database (const notmuch_message_t *message)
2152 return message->notmuch;
2156 _notmuch_message_ensure_property_map (notmuch_message_t *message)
2158 notmuch_string_node_t *node;
2160 if (message->property_map)
2163 _notmuch_message_ensure_metadata (message, message->property_term_list);
2165 message->property_map = _notmuch_string_map_create (message);
2167 for (node = message->property_term_list->head; node; node = node->next) {
2171 value = strchr (node->string, '=');
2173 INTERNAL_ERROR ("malformed property term");
2179 _notmuch_string_map_append (message->property_map, key, value);
2183 talloc_free (message->property_term_list);
2184 message->property_term_list = NULL;
2187 notmuch_string_map_t *
2188 _notmuch_message_property_map (notmuch_message_t *message)
2190 _notmuch_message_ensure_property_map (message);
2192 return message->property_map;
2196 _notmuch_message_frozen (notmuch_message_t *message)
2198 return message->frozen;
2202 notmuch_message_reindex (notmuch_message_t *message,
2203 notmuch_indexopts_t *indexopts)
2205 notmuch_database_t *notmuch = NULL;
2206 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
2207 notmuch_private_status_t private_status;
2208 notmuch_filenames_t *orig_filenames = NULL;
2209 const char *orig_thread_id = NULL;
2210 notmuch_message_file_t *message_file = NULL;
2214 if (message == NULL)
2215 return NOTMUCH_STATUS_NULL_POINTER;
2217 /* Save in case we need to delete message */
2218 orig_thread_id = notmuch_message_get_thread_id (message);
2219 if (! orig_thread_id) {
2220 /* the following is correct as long as there is only one reason
2221 * n_m_get_thread_id returns NULL
2223 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
2226 /* strdup it because the metadata may be invalidated */
2227 orig_thread_id = talloc_strdup (message, orig_thread_id);
2229 notmuch = notmuch_message_get_database (message);
2231 ret = _notmuch_database_ensure_writable (notmuch);
2235 orig_filenames = notmuch_message_get_filenames (message);
2237 private_status = _notmuch_message_remove_indexed_terms (message);
2238 if (private_status) {
2239 ret = COERCE_STATUS (private_status, "error removing terms");
2243 ret = notmuch_message_remove_all_properties_with_prefix (message, "index.");
2245 goto DONE; /* XXX TODO: distinguish from other error returns above? */
2246 if (indexopts && notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE) {
2247 ret = notmuch_message_remove_all_properties (message, "session-key");
2252 /* re-add the filenames with the associated indexopts */
2253 for (; notmuch_filenames_valid (orig_filenames);
2254 notmuch_filenames_move_to_next (orig_filenames)) {
2257 const char *from, *to, *subject;
2258 char *message_id = NULL;
2259 const char *thread_id = NULL;
2261 const char *filename = notmuch_filenames_get (orig_filenames);
2263 message_file = _notmuch_message_file_open (notmuch, filename);
2264 if (message_file == NULL)
2267 ret = _notmuch_message_file_get_headers (message_file,
2268 &from, &subject, &to, &date,
2273 /* XXX TODO: deal with changing message id? */
2275 _notmuch_message_add_filename (message, filename);
2277 ret = _notmuch_database_link_message_to_parents (notmuch, message,
2283 if (thread_id == NULL)
2284 thread_id = orig_thread_id;
2286 _notmuch_message_add_term (message, "thread", thread_id);
2287 /* Take header values only from first filename */
2289 _notmuch_message_set_header_values (message, date, from, subject);
2291 ret = _notmuch_message_index_file (message, indexopts, message_file);
2293 if (ret == NOTMUCH_STATUS_FILE_ERROR)
2299 _notmuch_message_file_close (message_file);
2300 message_file = NULL;
2303 /* put back thread id to help cleanup */
2304 _notmuch_message_add_term (message, "thread", orig_thread_id);
2305 ret = _notmuch_message_delete (message);
2307 _notmuch_message_sync (message);
2312 _notmuch_message_file_close (message_file);
2314 /* XXX TODO destroy orig_filenames? */