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 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_message_get_database (message),
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 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, *query_string;
1360 notmuch_message_t *ghost;
1361 notmuch_private_status_t private_status;
1362 notmuch_database_t *notmuch;
1363 notmuch_query_t *query;
1364 unsigned int count = 0;
1367 mid = notmuch_message_get_message_id (message);
1368 tid = notmuch_message_get_thread_id (message);
1369 notmuch = message->notmuch;
1371 status = _notmuch_database_ensure_writable (message->notmuch);
1375 message->notmuch->writable_xapian_db->delete_document (message->doc_id);
1377 /* if this was a ghost to begin with, we are done */
1378 private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
1380 return COERCE_STATUS (private_status,
1381 "Error trying to determine whether message was a ghost");
1383 return NOTMUCH_STATUS_SUCCESS;
1385 query_string = talloc_asprintf (message, "thread:%s", tid);
1386 query = notmuch_query_create (notmuch, query_string);
1388 return NOTMUCH_STATUS_OUT_OF_MEMORY;
1389 status = notmuch_query_count_messages (query, &count);
1391 notmuch_query_destroy (query);
1396 /* reintroduce a ghost in its place because there are still
1397 * other active messages in this thread: */
1398 ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);
1399 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1400 private_status = _notmuch_message_initialize_ghost (ghost, tid);
1401 if (! private_status)
1402 _notmuch_message_sync (ghost);
1403 } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1404 /* this is deeply weird, and we should not have gotten
1405 * into this state. is there a better error message to
1407 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1410 notmuch_message_destroy (ghost);
1411 status = COERCE_STATUS (private_status, "Error converting to ghost message");
1413 /* the thread is empty; drop all ghost messages from it */
1414 notmuch_messages_t *messages;
1415 status = _notmuch_query_search_documents (query,
1418 if (status == NOTMUCH_STATUS_SUCCESS) {
1419 notmuch_status_t last_error = NOTMUCH_STATUS_SUCCESS;
1420 while (notmuch_messages_valid (messages)) {
1421 message = notmuch_messages_get (messages);
1422 status = _notmuch_message_delete (message);
1423 if (status) /* we'll report the last failure we see;
1424 * if there is more than one failure, we
1425 * forget about previous ones */
1426 last_error = status;
1427 notmuch_message_destroy (message);
1428 notmuch_messages_move_to_next (messages);
1430 status = last_error;
1433 notmuch_query_destroy (query);
1437 /* Transform a blank message into a ghost message. The caller must
1438 * _notmuch_message_sync the message. */
1439 notmuch_private_status_t
1440 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1441 const char *thread_id)
1443 notmuch_private_status_t status;
1445 status = _notmuch_message_add_term (message, "type", "ghost");
1448 status = _notmuch_message_add_term (message, "thread", thread_id);
1452 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1455 /* Ensure that 'message' is not holding any file object open. Future
1456 * calls to various functions will still automatically open the
1457 * message file as needed.
1460 _notmuch_message_close (notmuch_message_t *message)
1462 if (message->message_file) {
1463 _notmuch_message_file_close (message->message_file);
1464 message->message_file = NULL;
1468 /* Add a name:value term to 'message', (the actual term will be
1469 * encoded by prefixing the value with a short prefix). See
1470 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1471 * names to prefix values.
1473 * This change will not be reflected in the database until the next
1474 * call to _notmuch_message_sync. */
1475 notmuch_private_status_t
1476 _notmuch_message_add_term (notmuch_message_t *message,
1477 const char *prefix_name,
1484 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1486 term = talloc_asprintf (message, "%s%s",
1487 _find_prefix (prefix_name), value);
1489 if (strlen (term) > NOTMUCH_TERM_MAX)
1490 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1492 message->doc.add_term (term, 0);
1493 message->modified = true;
1497 _notmuch_message_invalidate_metadata (message, prefix_name);
1499 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1502 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1503 * term will be added with the appropriate prefix if prefix_name is
1506 notmuch_private_status_t
1507 _notmuch_message_gen_terms (notmuch_message_t *message,
1508 const char *prefix_name,
1511 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1514 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1516 term_gen->set_document (message->doc);
1517 term_gen->set_termpos (message->termpos);
1520 const char *prefix = _notmuch_database_prefix (message->notmuch, prefix_name);
1522 return NOTMUCH_PRIVATE_STATUS_BAD_PREFIX;
1524 _notmuch_message_invalidate_metadata (message, prefix_name);
1525 term_gen->index_text (text, 1, prefix);
1527 term_gen->index_text (text);
1530 /* Create a gap between this an the next terms so they don't
1531 * appear to be a phrase. */
1532 message->termpos = term_gen->get_termpos () + 100;
1534 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1537 /* Remove a name:value term from 'message', (the actual term will be
1538 * encoded by prefixing the value with a short prefix). See
1539 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1540 * names to prefix values.
1542 * This change will not be reflected in the database until the next
1543 * call to _notmuch_message_sync. */
1544 notmuch_private_status_t
1545 _notmuch_message_remove_term (notmuch_message_t *message,
1546 const char *prefix_name,
1552 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1554 term = talloc_asprintf (message, "%s%s",
1555 _find_prefix (prefix_name), value);
1557 if (strlen (term) > NOTMUCH_TERM_MAX)
1558 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1561 message->doc.remove_term (term);
1562 message->modified = true;
1563 } catch (const Xapian::InvalidArgumentError) {
1564 /* We'll let the philosophers try to wrestle with the
1565 * question of whether failing to remove that which was not
1566 * there in the first place is failure. For us, we'll silently
1567 * consider it all good. */
1572 _notmuch_message_invalidate_metadata (message, prefix_name);
1574 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1577 notmuch_private_status_t
1578 _notmuch_message_has_term (notmuch_message_t *message,
1579 const char *prefix_name,
1585 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
1588 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1590 term = talloc_asprintf (message, "%s%s",
1591 _find_prefix (prefix_name), value);
1593 if (strlen (term) > NOTMUCH_TERM_MAX)
1594 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1597 /* Look for the exact term */
1598 Xapian::TermIterator i = message->doc.termlist_begin ();
1600 if (i != message->doc.termlist_end () &&
1601 ! strcmp ((*i).c_str (), term))
1603 } catch (Xapian::Error &error) {
1604 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1613 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1615 notmuch_private_status_t private_status;
1616 notmuch_status_t status;
1619 status = _notmuch_database_ensure_writable (message->notmuch);
1624 return NOTMUCH_STATUS_NULL_POINTER;
1626 if (strlen (tag) > NOTMUCH_TAG_MAX)
1627 return NOTMUCH_STATUS_TAG_TOO_LONG;
1629 private_status = _notmuch_message_add_term (message, "tag", tag);
1630 if (private_status) {
1631 return COERCE_STATUS (private_status,
1632 "_notmuch_message_remove_term return unexpected value: %d\n",
1636 if (! message->frozen)
1637 _notmuch_message_sync (message);
1639 } catch (Xapian::Error &error) {
1640 LOG_XAPIAN_EXCEPTION (message, error);
1641 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1644 return NOTMUCH_STATUS_SUCCESS;
1648 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1650 notmuch_private_status_t private_status;
1651 notmuch_status_t status;
1654 status = _notmuch_database_ensure_writable (message->notmuch);
1659 return NOTMUCH_STATUS_NULL_POINTER;
1661 if (strlen (tag) > NOTMUCH_TAG_MAX)
1662 return NOTMUCH_STATUS_TAG_TOO_LONG;
1664 private_status = _notmuch_message_remove_term (message, "tag", tag);
1665 if (private_status) {
1666 return COERCE_STATUS (private_status,
1667 "_notmuch_message_remove_term return unexpected value: %d\n",
1671 if (! message->frozen)
1672 _notmuch_message_sync (message);
1673 } catch (Xapian::Error &error) {
1674 LOG_XAPIAN_EXCEPTION (message, error);
1675 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1678 return NOTMUCH_STATUS_SUCCESS;
1681 /* Is the given filename within a maildir directory?
1683 * Specifically, is the final directory component of 'filename' either
1684 * "cur" or "new". If so, return a pointer to that final directory
1685 * component within 'filename'. If not, return NULL.
1687 * A non-NULL return value is guaranteed to be a valid string pointer
1688 * pointing to the characters "new/" or "cur/", (but not
1692 _filename_is_in_maildir (const char *filename)
1694 const char *slash, *dir = NULL;
1696 /* Find the last '/' separating directory from filename. */
1697 slash = strrchr (filename, '/');
1701 /* Jump back 4 characters to where the previous '/' will be if the
1702 * directory is named "cur" or "new". */
1703 if (slash - filename < 4)
1713 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1714 STRNCMP_LITERAL (dir, "new/") == 0) {
1721 static notmuch_status_t
1722 _ensure_maildir_flags (notmuch_message_t *message, bool force)
1725 notmuch_filenames_t *filenames;
1726 const char *filename, *dir;
1727 char *combined_flags = talloc_strdup (message, "");
1728 int seen_maildir_info = 0;
1730 if (message->maildir_flags) {
1732 talloc_free (message->maildir_flags);
1733 message->maildir_flags = NULL;
1736 filenames = notmuch_message_get_filenames (message);
1738 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1740 notmuch_filenames_valid (filenames);
1741 notmuch_filenames_move_to_next (filenames)) {
1742 filename = notmuch_filenames_get (filenames);
1743 dir = _filename_is_in_maildir (filename);
1748 flags = strstr (filename, ":2,");
1750 seen_maildir_info = 1;
1752 combined_flags = talloc_strdup_append (combined_flags, flags);
1753 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1754 /* Messages are delivered to new/ with no "info" part, but
1755 * they effectively have default maildir flags. According
1756 * to the spec, we should ignore the info part for
1757 * messages in new/, but some MUAs (mutt) can set maildir
1758 * flags on messages in new/, so we're liberal in what we
1760 seen_maildir_info = 1;
1763 if (seen_maildir_info)
1764 message->maildir_flags = combined_flags;
1765 return NOTMUCH_STATUS_SUCCESS;
1769 notmuch_message_has_maildir_flag (notmuch_message_t *message, char flag)
1771 notmuch_status_t status;
1774 status = notmuch_message_has_maildir_flag_st (message, flag, &ret);
1782 notmuch_message_has_maildir_flag_st (notmuch_message_t *message,
1784 notmuch_bool_t *is_set)
1786 notmuch_status_t status;
1789 return NOTMUCH_STATUS_NULL_POINTER;
1791 status = _ensure_maildir_flags (message, false);
1795 *is_set = message->maildir_flags && (strchr (message->maildir_flags, flag) != NULL);
1796 return NOTMUCH_STATUS_SUCCESS;
1800 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1802 notmuch_status_t status;
1805 status = _ensure_maildir_flags (message, true);
1808 /* If none of the filenames have any maildir info field (not even
1809 * an empty info with no flags set) then there's no information to
1810 * go on, so do nothing. */
1811 if (! message->maildir_flags)
1812 return NOTMUCH_STATUS_SUCCESS;
1814 status = notmuch_message_freeze (message);
1818 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1819 if ((strchr (message->maildir_flags, flag2tag[i].flag) != NULL)
1821 flag2tag[i].inverse) {
1822 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1824 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1829 status = notmuch_message_thaw (message);
1834 /* From the set of tags on 'message' and the flag2tag table, compute a
1835 * set of maildir-flag actions to be taken, (flags that should be
1836 * either set or cleared).
1838 * The result is returned as two talloced strings: to_set, and to_clear
1841 _get_maildir_flag_actions (notmuch_message_t *message,
1843 char **to_clear_ret)
1845 char *to_set, *to_clear;
1846 notmuch_tags_t *tags;
1850 to_set = talloc_strdup (message, "");
1851 to_clear = talloc_strdup (message, "");
1853 /* First, find flags for all set tags. */
1854 for (tags = notmuch_message_get_tags (message);
1855 notmuch_tags_valid (tags);
1856 notmuch_tags_move_to_next (tags)) {
1857 tag = notmuch_tags_get (tags);
1859 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1860 if (strcmp (tag, flag2tag[i].tag) == 0) {
1861 if (flag2tag[i].inverse)
1862 to_clear = talloc_asprintf_append (to_clear,
1866 to_set = talloc_asprintf_append (to_set,
1873 /* Then, find the flags for all tags not present. */
1874 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1875 if (flag2tag[i].inverse) {
1876 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1877 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1879 if (strchr (to_set, flag2tag[i].flag) == NULL)
1880 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1884 *to_set_ret = to_set;
1885 *to_clear_ret = to_clear;
1888 /* Given 'filename' and a set of maildir flags to set and to clear,
1889 * compute the new maildir filename.
1891 * If the existing filename is in the directory "new", the new
1892 * filename will be in the directory "cur", except for the case when
1893 * no flags are changed and the existing filename does not contain
1894 * maildir info (starting with ",2:").
1896 * After a sequence of ":2," in the filename, any subsequent
1897 * single-character flags will be added or removed according to the
1898 * characters in flags_to_set and flags_to_clear. Any existing flags
1899 * not mentioned in either string will remain. The final list of flags
1900 * will be in ASCII order.
1902 * If the original flags seem invalid, (repeated characters or
1903 * non-ASCII ordering of flags), this function will return NULL
1904 * (meaning that renaming would not be safe and should not occur).
1907 _new_maildir_filename (void *ctx,
1908 const char *filename,
1909 const char *flags_to_set,
1910 const char *flags_to_clear)
1912 const char *info, *flags;
1913 unsigned int flag, last_flag;
1914 char *filename_new, *dir;
1916 int flags_in_map = 0;
1917 bool flags_changed = false;
1921 memset (flag_map, 0, sizeof (flag_map));
1923 info = strstr (filename, ":2,");
1926 info = filename + strlen (filename);
1928 /* Loop through existing flags in filename. */
1929 for (flags = info + 3, last_flag = 0;
1931 last_flag = flag, flags++) {
1934 /* Original flags not in ASCII order. Abort. */
1935 if (flag < last_flag)
1938 /* Non-ASCII flag. Abort. */
1939 if (flag > sizeof (flag_map) - 1)
1942 /* Repeated flag value. Abort. */
1951 /* Then set and clear our flags from tags. */
1952 for (flags = flags_to_set; *flags; flags++) {
1954 if (flag_map[flag] == 0) {
1957 flags_changed = true;
1961 for (flags = flags_to_clear; *flags; flags++) {
1963 if (flag_map[flag]) {
1966 flags_changed = true;
1970 /* Messages in new/ without maildir info can be kept in new/ if no
1971 * flags have changed. */
1972 dir = (char *) _filename_is_in_maildir (filename);
1973 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && ! *info && ! flags_changed)
1974 return talloc_strdup (ctx, filename);
1976 filename_new = (char *) talloc_size (ctx,
1978 strlen (":2,") + flags_in_map + 1);
1979 if (unlikely (filename_new == NULL))
1982 strncpy (filename_new, filename, info - filename);
1983 filename_new[info - filename] = '\0';
1985 strcat (filename_new, ":2,");
1987 s = filename_new + strlen (filename_new);
1988 for (i = 0; i < sizeof (flag_map); i++) {
1996 /* If message is in new/ move it under cur/. */
1997 dir = (char *) _filename_is_in_maildir (filename_new);
1998 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1999 memcpy (dir, "cur/", 4);
2001 return filename_new;
2005 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
2007 notmuch_filenames_t *filenames;
2008 const char *filename;
2010 char *to_set, *to_clear;
2011 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
2013 _get_maildir_flag_actions (message, &to_set, &to_clear);
2015 for (filenames = notmuch_message_get_filenames (message);
2016 notmuch_filenames_valid (filenames);
2017 notmuch_filenames_move_to_next (filenames)) {
2018 filename = notmuch_filenames_get (filenames);
2020 if (! _filename_is_in_maildir (filename))
2023 filename_new = _new_maildir_filename (message, filename,
2025 if (filename_new == NULL)
2028 if (strcmp (filename, filename_new)) {
2030 notmuch_status_t new_status;
2032 err = rename (filename, filename_new);
2036 new_status = _notmuch_message_remove_filename (message,
2038 /* Hold on to only the first error. */
2039 if (! status && new_status
2040 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
2041 status = new_status;
2045 new_status = _notmuch_message_add_filename (message,
2047 /* Hold on to only the first error. */
2048 if (! status && new_status) {
2049 status = new_status;
2053 _notmuch_message_sync (message);
2056 talloc_free (filename_new);
2059 talloc_free (to_set);
2060 talloc_free (to_clear);
2066 notmuch_message_remove_all_tags (notmuch_message_t *message)
2068 notmuch_private_status_t private_status;
2069 notmuch_status_t status;
2070 notmuch_tags_t *tags;
2073 status = _notmuch_database_ensure_writable (message->notmuch);
2076 tags = notmuch_message_get_tags (message);
2078 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
2081 notmuch_tags_valid (tags);
2082 notmuch_tags_move_to_next (tags)) {
2083 tag = notmuch_tags_get (tags);
2085 private_status = _notmuch_message_remove_term (message, "tag", tag);
2086 if (private_status) {
2087 return COERCE_STATUS (private_status,
2088 "_notmuch_message_remove_term return unexpected value: %d\n",
2093 if (! message->frozen)
2094 _notmuch_message_sync (message);
2097 return NOTMUCH_STATUS_SUCCESS;
2101 notmuch_message_freeze (notmuch_message_t *message)
2103 notmuch_status_t status;
2105 status = _notmuch_database_ensure_writable (message->notmuch);
2111 return NOTMUCH_STATUS_SUCCESS;
2115 notmuch_message_thaw (notmuch_message_t *message)
2117 notmuch_status_t status;
2119 status = _notmuch_database_ensure_writable (message->notmuch);
2123 if (message->frozen > 0) {
2125 if (message->frozen == 0)
2126 _notmuch_message_sync (message);
2127 return NOTMUCH_STATUS_SUCCESS;
2129 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
2134 notmuch_message_destroy (notmuch_message_t *message)
2136 talloc_free (message);
2139 notmuch_database_t *
2140 notmuch_message_get_database (const notmuch_message_t *message)
2142 return message->notmuch;
2146 _notmuch_message_ensure_property_map (notmuch_message_t *message)
2148 notmuch_string_node_t *node;
2150 if (message->property_map)
2153 _notmuch_message_ensure_metadata (message, message->property_term_list);
2155 message->property_map = _notmuch_string_map_create (message);
2157 for (node = message->property_term_list->head; node; node = node->next) {
2161 value = strchr (node->string, '=');
2163 INTERNAL_ERROR ("malformed property term");
2169 _notmuch_string_map_append (message->property_map, key, value);
2173 talloc_free (message->property_term_list);
2174 message->property_term_list = NULL;
2177 notmuch_string_map_t *
2178 _notmuch_message_property_map (notmuch_message_t *message)
2180 _notmuch_message_ensure_property_map (message);
2182 return message->property_map;
2186 _notmuch_message_frozen (notmuch_message_t *message)
2188 return message->frozen;
2192 notmuch_message_reindex (notmuch_message_t *message,
2193 notmuch_indexopts_t *indexopts)
2195 notmuch_database_t *notmuch = NULL;
2196 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
2197 notmuch_private_status_t private_status;
2198 notmuch_filenames_t *orig_filenames = NULL;
2199 const char *orig_thread_id = NULL;
2200 notmuch_message_file_t *message_file = NULL;
2204 if (message == NULL)
2205 return NOTMUCH_STATUS_NULL_POINTER;
2207 /* Save in case we need to delete message */
2208 orig_thread_id = notmuch_message_get_thread_id (message);
2209 if (! orig_thread_id) {
2210 /* the following is correct as long as there is only one reason
2211 * n_m_get_thread_id returns NULL
2213 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
2216 /* strdup it because the metadata may be invalidated */
2217 orig_thread_id = talloc_strdup (message, orig_thread_id);
2219 notmuch = notmuch_message_get_database (message);
2221 ret = _notmuch_database_ensure_writable (notmuch);
2225 orig_filenames = notmuch_message_get_filenames (message);
2227 private_status = _notmuch_message_remove_indexed_terms (message);
2228 if (private_status) {
2229 ret = COERCE_STATUS (private_status, "error removing terms");
2233 ret = notmuch_message_remove_all_properties_with_prefix (message, "index.");
2235 goto DONE; /* XXX TODO: distinguish from other error returns above? */
2236 if (indexopts && notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE) {
2237 ret = notmuch_message_remove_all_properties (message, "session-key");
2242 /* re-add the filenames with the associated indexopts */
2243 for (; notmuch_filenames_valid (orig_filenames);
2244 notmuch_filenames_move_to_next (orig_filenames)) {
2247 const char *from, *to, *subject;
2248 char *message_id = NULL;
2249 const char *thread_id = NULL;
2251 const char *filename = notmuch_filenames_get (orig_filenames);
2253 message_file = _notmuch_message_file_open (notmuch, filename);
2254 if (message_file == NULL)
2257 ret = _notmuch_message_file_get_headers (message_file,
2258 &from, &subject, &to, &date,
2263 /* XXX TODO: deal with changing message id? */
2265 _notmuch_message_add_filename (message, filename);
2267 ret = _notmuch_database_link_message_to_parents (notmuch, message,
2273 if (thread_id == NULL)
2274 thread_id = orig_thread_id;
2276 _notmuch_message_add_term (message, "thread", thread_id);
2277 /* Take header values only from first filename */
2279 _notmuch_message_set_header_values (message, date, from, subject);
2281 ret = _notmuch_message_index_file (message, indexopts, message_file);
2283 if (ret == NOTMUCH_STATUS_FILE_ERROR)
2289 _notmuch_message_file_close (message_file);
2290 message_file = NULL;
2293 /* put back thread id to help cleanup */
2294 _notmuch_message_add_term (message, "thread", orig_thread_id);
2295 ret = _notmuch_message_delete (message);
2297 _notmuch_message_sync (message);
2302 _notmuch_message_file_close (message_file);
2304 /* XXX TODO destroy orig_filenames? */