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) {
97 notmuch_database_t *notmuch = notmuch_message_get_database (message);
98 _notmuch_database_log (notmuch,
99 "A Xapian exception occurred at %s: %s\n",
101 error.get_msg ().c_str ());
102 notmuch->exception_reported = true;
105 static notmuch_message_t *
106 _notmuch_message_create_for_document (const void *talloc_owner,
107 notmuch_database_t *notmuch,
109 Xapian::Document doc,
110 notmuch_private_status_t *status)
112 notmuch_message_t *message;
115 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
117 message = talloc (talloc_owner, notmuch_message_t);
118 if (unlikely (message == NULL)) {
120 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
124 message->notmuch = notmuch;
125 message->doc_id = doc_id;
129 message->lazy_flags = 0;
131 /* the message is initially not synchronized with Xapian */
132 message->last_view = 0;
134 /* Calculated after the thread structure is computed */
135 message->thread_depth = 0;
137 /* Each of these will be lazily created as needed. */
138 message->message_id = NULL;
139 message->thread_id = NULL;
140 message->in_reply_to = NULL;
141 message->tag_list = NULL;
142 message->filename_term_list = NULL;
143 message->filename_list = NULL;
144 message->maildir_flags = NULL;
145 message->message_file = NULL;
146 message->author = NULL;
147 message->property_term_list = NULL;
148 message->property_map = NULL;
149 message->reference_list = NULL;
151 message->replies = _notmuch_message_list_create (message);
152 if (unlikely (message->replies == NULL)) {
154 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
158 /* This is C++'s creepy "placement new", which is really just an
159 * ugly way to call a constructor for a pre-allocated object. So
160 * it's really not an error to not be checking for OUT_OF_MEMORY
161 * here, since this "new" isn't actually allocating memory. This
162 * is language-design comedy of the wrong kind. */
164 new (&message->doc) Xapian::Document;
166 talloc_set_destructor (message, _notmuch_message_destructor);
169 message->termpos = 0;
174 /* Create a new notmuch_message_t object for an existing document in
177 * Here, 'talloc owner' is an optional talloc context to which the new
178 * message will belong. This allows for the caller to not bother
179 * calling notmuch_message_destroy on the message, and know that all
180 * memory will be reclaimed when 'talloc_owner' is freed. The caller
181 * still can call notmuch_message_destroy when finished with the
182 * message if desired.
184 * The 'talloc_owner' argument can also be NULL, in which case the
185 * caller *is* responsible for calling notmuch_message_destroy.
187 * If no document exists in the database with document ID of 'doc_id'
188 * then this function returns NULL and optionally sets *status to
189 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
191 * This function can also fail to due lack of available memory,
192 * returning NULL and optionally setting *status to
193 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
195 * The caller can pass NULL for status if uninterested in
196 * distinguishing these two cases.
199 _notmuch_message_create (const void *talloc_owner,
200 notmuch_database_t *notmuch,
202 notmuch_private_status_t *status)
204 Xapian::Document doc;
207 doc = notmuch->xapian_db->get_document (doc_id);
208 } catch (const Xapian::DocNotFoundError &error) {
210 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
214 return _notmuch_message_create_for_document (talloc_owner, notmuch,
215 doc_id, doc, status);
218 /* Create a new notmuch_message_t object for a specific message ID,
219 * (which may or may not already exist in the database).
221 * The 'notmuch' database will be the talloc owner of the returned
224 * This function returns a valid notmuch_message_t whether or not
225 * there is already a document in the database with the given message
226 * ID. These two cases can be distinguished by the value of *status:
229 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
231 * There is already a document with message ID 'message_id' in the
232 * database. The returned message can be used to query/modify the
233 * document. The message may be a ghost message.
235 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
237 * No document with 'message_id' exists in the database. The
238 * returned message contains a newly created document (not yet
239 * added to the database) and a document ID that is known not to
240 * exist in the database. This message is "blank"; that is, it
241 * contains only a message ID and no other metadata. The caller
242 * can modify the message, and a call to _notmuch_message_sync
243 * will add the document to the database.
245 * If an error occurs, this function will return NULL and *status
246 * will be set as appropriate. (The status pointer argument must
250 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
251 const char *message_id,
252 notmuch_private_status_t *status_ret)
254 notmuch_message_t *message;
255 Xapian::Document doc;
259 *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
263 return talloc_steal (notmuch, message);
264 else if (*status_ret)
267 /* If the message ID is too long, substitute its sha1 instead. */
268 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
269 message_id = _notmuch_message_id_compressed (message, message_id);
271 term = talloc_asprintf (NULL, "%s%s",
272 _find_prefix ("id"), message_id);
274 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
278 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
279 INTERNAL_ERROR ("Failure to ensure database is writable.");
282 doc.add_term (term, 0);
285 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
287 doc_id = _notmuch_database_generate_doc_id (notmuch);
288 } catch (const Xapian::Error &error) {
289 _notmuch_database_log (notmuch_message_get_database (message), "A Xapian exception occurred creating message: %s\n",
290 error.get_msg ().c_str ());
291 notmuch->exception_reported = true;
292 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
296 message = _notmuch_message_create_for_document (notmuch, notmuch,
297 doc_id, doc, status_ret);
299 /* We want to inform the caller that we had to create a new
301 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
302 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
308 _notmuch_message_get_term (notmuch_message_t *message,
309 Xapian::TermIterator &i, Xapian::TermIterator &end,
312 int prefix_len = strlen (prefix);
320 const std::string &term = *i;
321 if (strncmp (term.c_str (), prefix, prefix_len))
324 value = talloc_strdup (message, term.c_str () + prefix_len);
326 #if DEBUG_DATABASE_SANITY
329 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
330 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
331 message->doc_id, prefix, value,
332 (*i).c_str () + prefix_len);
340 * For special applications where we only want the thread id, reading
341 * in all metadata is a heavy I/O penalty.
344 _notmuch_message_get_thread_id_only (notmuch_message_t *message)
347 Xapian::TermIterator i = message->doc.termlist_begin ();
348 Xapian::TermIterator end = message->doc.termlist_end ();
350 message->thread_id = _notmuch_message_get_term (message, i, end,
351 _find_prefix ("thread"));
352 return message->thread_id;
357 _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
359 Xapian::TermIterator i, end;
361 if (field && (message->last_view >= message->notmuch->view))
364 const char *thread_prefix = _find_prefix ("thread"),
365 *tag_prefix = _find_prefix ("tag"),
366 *id_prefix = _find_prefix ("id"),
367 *type_prefix = _find_prefix ("type"),
368 *filename_prefix = _find_prefix ("file-direntry"),
369 *property_prefix = _find_prefix ("property"),
370 *reference_prefix = _find_prefix ("reference"),
371 *replyto_prefix = _find_prefix ("replyto");
373 /* We do this all in a single pass because Xapian decompresses the
374 * term list every time you iterate over it. Thus, while this is
375 * slightly more costly than looking up individual fields if only
376 * one field of the message object is actually used, it's a huge
377 * win as more fields are used. */
378 for (int count = 0; count < 3; count++) {
380 i = message->doc.termlist_begin ();
381 end = message->doc.termlist_end ();
384 if (! message->thread_id)
386 _notmuch_message_get_term (message, i, end, thread_prefix);
389 assert (strcmp (thread_prefix, tag_prefix) < 0);
390 if (! message->tag_list) {
392 _notmuch_database_get_terms_with_prefix (message, i, end,
394 _notmuch_string_list_sort (message->tag_list);
398 assert (strcmp (tag_prefix, id_prefix) < 0);
399 if (! message->message_id)
400 message->message_id =
401 _notmuch_message_get_term (message, i, end, id_prefix);
403 /* Get document type */
404 assert (strcmp (id_prefix, type_prefix) < 0);
405 if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {
406 i.skip_to (type_prefix);
407 /* "T" is the prefix "type" fields. See
408 * BOOLEAN_PREFIX_INTERNAL. */
410 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
411 else if (*i == "Tghost")
412 NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
414 INTERNAL_ERROR ("Message without type term");
415 NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
418 /* Get filename list. Here we get only the terms. We lazily
419 * expand them to full file names when needed in
420 * _notmuch_message_ensure_filename_list. */
421 assert (strcmp (type_prefix, filename_prefix) < 0);
422 if (! message->filename_term_list && ! message->filename_list)
423 message->filename_term_list =
424 _notmuch_database_get_terms_with_prefix (message, i, end,
428 /* Get property terms. Mimic the setup with filenames above */
429 assert (strcmp (filename_prefix, property_prefix) < 0);
430 if (! message->property_map && ! message->property_term_list)
431 message->property_term_list =
432 _notmuch_database_get_terms_with_prefix (message, i, end,
436 assert (strcmp (property_prefix, reference_prefix) < 0);
437 if (! message->reference_list) {
438 message->reference_list =
439 _notmuch_database_get_terms_with_prefix (message, i, end,
444 assert (strcmp (property_prefix, replyto_prefix) < 0);
445 if (! message->in_reply_to)
446 message->in_reply_to =
447 _notmuch_message_get_term (message, i, end, replyto_prefix);
450 /* It's perfectly valid for a message to have no In-Reply-To
451 * header. For these cases, we return an empty string. */
452 if (! message->in_reply_to)
453 message->in_reply_to = talloc_strdup (message, "");
455 /* all the way without an exception */
457 } catch (const Xapian::DatabaseModifiedError &error) {
458 notmuch_status_t status = _notmuch_database_reopen (message->notmuch);
459 if (status != NOTMUCH_STATUS_SUCCESS)
460 INTERNAL_ERROR ("unhandled error from notmuch_database_reopen: %s\n",
461 notmuch_status_to_string (status));
464 message->last_view = message->notmuch->view;
468 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
469 const char *prefix_name)
471 if (strcmp ("thread", prefix_name) == 0) {
472 talloc_free (message->thread_id);
473 message->thread_id = NULL;
476 if (strcmp ("tag", prefix_name) == 0) {
477 talloc_unlink (message, message->tag_list);
478 message->tag_list = NULL;
481 if (strcmp ("type", prefix_name) == 0) {
482 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
483 NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
486 if (strcmp ("file-direntry", prefix_name) == 0) {
487 talloc_free (message->filename_term_list);
488 talloc_free (message->filename_list);
489 message->filename_term_list = message->filename_list = NULL;
492 if (strcmp ("property", prefix_name) == 0) {
494 if (message->property_term_list)
495 talloc_free (message->property_term_list);
496 message->property_term_list = NULL;
498 if (message->property_map)
499 talloc_unlink (message, message->property_map);
501 message->property_map = NULL;
504 if (strcmp ("replyto", prefix_name) == 0) {
505 talloc_free (message->in_reply_to);
506 message->in_reply_to = NULL;
511 _notmuch_message_get_doc_id (notmuch_message_t *message)
513 return message->doc_id;
517 notmuch_message_get_message_id (notmuch_message_t *message)
520 _notmuch_message_ensure_metadata (message, message->message_id);
521 } catch (const Xapian::Error &error) {
522 LOG_XAPIAN_EXCEPTION (message, error);
526 if (! message->message_id)
527 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
529 return message->message_id;
533 _notmuch_message_ensure_message_file (notmuch_message_t *message)
535 const char *filename;
537 if (message->message_file)
540 filename = notmuch_message_get_filename (message);
541 if (unlikely (filename == NULL))
544 message->message_file = _notmuch_message_file_open_ctx (
545 notmuch_message_get_database (message), message, filename);
549 notmuch_message_get_header (notmuch_message_t *message, const char *header)
551 Xapian::valueno slot = Xapian::BAD_VALUENO;
553 /* Fetch header from the appropriate xapian value field if
555 if (strcasecmp (header, "from") == 0)
556 slot = NOTMUCH_VALUE_FROM;
557 else if (strcasecmp (header, "subject") == 0)
558 slot = NOTMUCH_VALUE_SUBJECT;
559 else if (strcasecmp (header, "message-id") == 0)
560 slot = NOTMUCH_VALUE_MESSAGE_ID;
562 if (slot != Xapian::BAD_VALUENO) {
564 std::string value = message->doc.get_value (slot);
566 /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then
567 * empty values indicate empty headers. If we don't, then
568 * it could just mean we didn't record the header. */
569 if ((message->notmuch->features &
570 NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) ||
572 return talloc_strdup (message, value.c_str ());
574 } catch (Xapian::Error &error) {
575 LOG_XAPIAN_EXCEPTION (message, error);
580 /* Otherwise fall back to parsing the file */
581 _notmuch_message_ensure_message_file (message);
582 if (message->message_file == NULL)
585 return _notmuch_message_file_get_header (message->message_file, header);
588 /* Return the message ID from the In-Reply-To header of 'message'.
590 * Returns an empty string ("") if 'message' has no In-Reply-To
593 * Returns NULL if any error occurs.
596 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
598 _notmuch_message_ensure_metadata (message, message->in_reply_to);
599 return message->in_reply_to;
603 notmuch_message_get_thread_id (notmuch_message_t *message)
606 _notmuch_message_ensure_metadata (message, message->thread_id);
607 } catch (Xapian::Error &error) {
608 LOG_XAPIAN_EXCEPTION (message, error);
611 if (! message->thread_id)
612 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
614 return message->thread_id;
618 _notmuch_message_add_reply (notmuch_message_t *message,
619 notmuch_message_t *reply)
621 _notmuch_message_list_add_message (message->replies, reply);
625 _notmuch_message_get_thread_depth (notmuch_message_t *message)
627 return message->thread_depth;
631 _notmuch_message_label_depths (notmuch_message_t *message,
634 message->thread_depth = depth;
636 for (notmuch_messages_t *messages = _notmuch_messages_create (message->replies);
637 notmuch_messages_valid (messages);
638 notmuch_messages_move_to_next (messages)) {
639 notmuch_message_t *child = notmuch_messages_get (messages);
640 _notmuch_message_label_depths (child, depth + 1);
644 const notmuch_string_list_t *
645 _notmuch_message_get_references (notmuch_message_t *message)
647 _notmuch_message_ensure_metadata (message, message->reference_list);
648 return message->reference_list;
652 _cmpmsg (const void *pa, const void *pb)
654 notmuch_message_t **a = (notmuch_message_t **) pa;
655 notmuch_message_t **b = (notmuch_message_t **) pb;
656 time_t time_a = notmuch_message_get_date (*a);
657 time_t time_b = notmuch_message_get_date (*b);
659 if (time_a == time_b)
661 else if (time_a < time_b)
667 notmuch_message_list_t *
668 _notmuch_message_sort_subtrees (void *ctx, notmuch_message_list_t *list)
672 size_t capacity = 16;
677 void *local = talloc_new (NULL);
678 notmuch_message_list_t *new_list = _notmuch_message_list_create (ctx);
679 notmuch_message_t **message_array = talloc_zero_array (local, notmuch_message_t *, capacity);
681 for (notmuch_messages_t *messages = _notmuch_messages_create (list);
682 notmuch_messages_valid (messages);
683 notmuch_messages_move_to_next (messages)) {
684 notmuch_message_t *root = notmuch_messages_get (messages);
685 if (count >= capacity) {
687 message_array = talloc_realloc (local, message_array, notmuch_message_t *, capacity);
689 message_array[count++] = root;
690 root->replies = _notmuch_message_sort_subtrees (root, root->replies);
693 qsort (message_array, count, sizeof (notmuch_message_t *), _cmpmsg);
694 for (size_t i = 0; i < count; i++) {
695 _notmuch_message_list_add_message (new_list, message_array[i]);
704 notmuch_message_get_replies (notmuch_message_t *message)
706 return _notmuch_messages_create (message->replies);
710 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
712 Xapian::TermIterator i;
713 size_t prefix_len = 0;
715 prefix_len = strlen (prefix);
718 i = message->doc.termlist_begin ();
721 /* Terminate loop when no terms remain with desired prefix. */
722 if (i == message->doc.termlist_end () ||
723 strncmp ((*i).c_str (), prefix, prefix_len))
727 message->doc.remove_term ((*i));
728 message->modified = true;
729 } catch (const Xapian::InvalidArgumentError) {
730 /* Ignore failure to remove non-existent term. */
736 /* Remove all terms generated by indexing, i.e. not tags or
737 * properties, along with any automatic tags*/
738 /* According to Xapian API docs, none of these calls throw
740 notmuch_private_status_t
741 _notmuch_message_remove_indexed_terms (notmuch_message_t *message)
743 Xapian::TermIterator i;
746 id_prefix = _find_prefix ("id"),
747 property_prefix = _find_prefix ("property"),
748 tag_prefix = _find_prefix ("tag"),
749 type_prefix = _find_prefix ("type");
751 /* Make sure we have the data to restore to Xapian*/
752 _notmuch_message_ensure_metadata (message, NULL);
754 /* Empirically, it turns out to be faster to remove all the terms,
755 * and add back the ones we want. */
756 message->doc.clear_terms ();
757 message->modified = true;
759 /* still a mail message */
760 message->doc.add_term (type_prefix + "mail");
762 /* Put back message-id */
763 message->doc.add_term (id_prefix + message->message_id);
765 /* Put back non-automatic tags */
766 for (notmuch_tags_t *tags = notmuch_message_get_tags (message);
767 notmuch_tags_valid (tags);
768 notmuch_tags_move_to_next (tags)) {
770 const char *tag = notmuch_tags_get (tags);
772 if (strcmp (tag, "encrypted") != 0 &&
773 strcmp (tag, "signed") != 0 &&
774 strcmp (tag, "attachment") != 0) {
775 std::string term = tag_prefix + tag;
776 message->doc.add_term (term);
780 /* Put back properties */
781 notmuch_message_properties_t *list;
783 for (list = notmuch_message_get_properties (message, "", false);
784 notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
785 std::string term = property_prefix +
786 notmuch_message_properties_key (list) + "=" +
787 notmuch_message_properties_value (list);
789 message->doc.add_term (term);
792 notmuch_message_properties_destroy (list);
794 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
798 /* Return true if p points at "new" or "cur". */
800 is_maildir (const char *p)
802 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
805 /* Add "folder:" term for directory. */
806 static notmuch_status_t
807 _notmuch_message_add_folder_terms (notmuch_message_t *message,
808 const char *directory)
812 folder = talloc_strdup (NULL, directory);
814 return NOTMUCH_STATUS_OUT_OF_MEMORY;
817 * If the message file is in a leaf directory named "new" or
818 * "cur", presume maildir and index the parent directory. Thus a
819 * "folder:" prefix search matches messages in the specified
820 * maildir folder, i.e. in the specified directory and its "new"
821 * and "cur" subdirectories.
823 * Note that this means the "folder:" prefix can't be used for
824 * distinguishing between message files in "new" or "cur". The
825 * "path:" prefix needs to be used for that.
827 * Note the deliberate difference to _filename_is_in_maildir(). We
828 * don't want to index different things depending on the existence
829 * or non-existence of all maildir sibling directories "new",
830 * "cur", and "tmp". Doing so would be surprising, and difficult
831 * for the user to fix in case all subdirectories were not in
832 * place during indexing.
834 last = strrchr (folder, '/');
836 if (is_maildir (last + 1))
838 } else if (is_maildir (folder)) {
842 _notmuch_message_add_term (message, "folder", folder);
844 talloc_free (folder);
846 message->modified = true;
847 return NOTMUCH_STATUS_SUCCESS;
850 #define RECURSIVE_SUFFIX "/**"
852 /* Add "path:" terms for directory. */
853 static notmuch_status_t
854 _notmuch_message_add_path_terms (notmuch_message_t *message,
855 const char *directory)
857 /* Add exact "path:" term. */
858 _notmuch_message_add_term (message, "path", directory);
860 if (strlen (directory)) {
863 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
865 return NOTMUCH_STATUS_OUT_OF_MEMORY;
867 /* Add recursive "path:" terms for directory and all parents. */
868 for (p = path + strlen (path) - 1; p > path; p--) {
870 strcpy (p, RECURSIVE_SUFFIX);
871 _notmuch_message_add_term (message, "path", path);
878 /* Recursive all-matching path:** for consistency. */
879 _notmuch_message_add_term (message, "path", "**");
881 return NOTMUCH_STATUS_SUCCESS;
884 /* Add directory based terms for all filenames of the message. */
885 static notmuch_status_t
886 _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
888 const char *direntry_prefix = _find_prefix ("file-direntry");
889 int direntry_prefix_len = strlen (direntry_prefix);
890 Xapian::TermIterator i = message->doc.termlist_begin ();
891 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
893 for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {
894 unsigned int directory_id;
895 const char *direntry, *directory;
897 const std::string &term = *i;
899 /* Terminate loop at first term without desired prefix. */
900 if (strncmp (term.c_str (), direntry_prefix, direntry_prefix_len))
903 /* Indicate that there are filenames remaining. */
904 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
906 direntry = term.c_str ();
907 direntry += direntry_prefix_len;
909 directory_id = strtol (direntry, &colon, 10);
911 if (colon == NULL || *colon != ':')
912 INTERNAL_ERROR ("malformed direntry");
914 directory = _notmuch_database_get_directory_path (ctx,
918 _notmuch_message_add_folder_terms (message, directory);
919 _notmuch_message_add_path_terms (message, directory);
925 /* Add an additional 'filename' for 'message'.
927 * This change will not be reflected in the database until the next
928 * call to _notmuch_message_sync. */
930 _notmuch_message_add_filename (notmuch_message_t *message,
931 const char *filename)
933 const char *relative, *directory;
934 notmuch_status_t status;
935 void *local = talloc_new (message);
938 if (filename == NULL)
939 INTERNAL_ERROR ("Message filename cannot be NULL.");
941 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
942 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
943 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
945 relative = _notmuch_database_relative_path (message->notmuch, filename);
947 status = _notmuch_database_split_path (local, relative, &directory, NULL);
951 status = _notmuch_database_filename_to_direntry (
952 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
956 /* New file-direntry allows navigating to this message with
957 * notmuch_directory_get_child_files() . */
958 _notmuch_message_add_term (message, "file-direntry", direntry);
960 _notmuch_message_add_folder_terms (message, directory);
961 _notmuch_message_add_path_terms (message, directory);
965 return NOTMUCH_STATUS_SUCCESS;
968 /* Remove a particular 'filename' from 'message'.
970 * This change will not be reflected in the database until the next
971 * call to _notmuch_message_sync.
973 * If this message still has other filenames, returns
974 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
976 * Note: This function does not remove a document from the database,
977 * even if the specified filename is the only filename for this
978 * message. For that functionality, see
979 * notmuch_database_remove_message. */
981 _notmuch_message_remove_filename (notmuch_message_t *message,
982 const char *filename)
984 void *local = talloc_new (message);
986 notmuch_private_status_t private_status;
987 notmuch_status_t status;
989 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
990 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
991 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
993 status = _notmuch_database_filename_to_direntry (
994 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
995 if (status || ! direntry)
998 /* Unlink this file from its parent directory. */
999 private_status = _notmuch_message_remove_term (message,
1000 "file-direntry", direntry);
1001 status = COERCE_STATUS (private_status,
1002 "Unexpected error from _notmuch_message_remove_term");
1006 /* Re-synchronize "folder:" and "path:" terms for this message. */
1008 /* Remove all "folder:" terms. */
1009 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
1011 /* Remove all "path:" terms. */
1012 _notmuch_message_remove_terms (message, _find_prefix ("path"));
1014 /* Add back terms for all remaining filenames of the message. */
1015 status = _notmuch_message_add_directory_terms (local, message);
1017 talloc_free (local);
1022 /* Upgrade the "folder:" prefix from V1 to V2. */
1023 #define FOLDER_PREFIX_V1 "XFOLDER"
1024 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
1026 _notmuch_message_upgrade_folder (notmuch_message_t *message)
1028 /* Remove all old "folder:" terms. */
1029 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
1031 /* Remove all old "folder:" stemmed terms. */
1032 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
1034 /* Add new boolean "folder:" and "path:" terms. */
1035 _notmuch_message_add_directory_terms (message, message);
1039 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
1041 return talloc_strdup (message, message->doc.get_data ().c_str ());
1045 _notmuch_message_clear_data (notmuch_message_t *message)
1047 message->doc.set_data ("");
1048 message->modified = true;
1052 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
1054 notmuch_string_node_t *node;
1056 if (message->filename_list)
1059 _notmuch_message_ensure_metadata (message, message->filename_term_list);
1061 message->filename_list = _notmuch_string_list_create (message);
1062 node = message->filename_term_list->head;
1065 /* A message document created by an old version of notmuch
1066 * (prior to rename support) will have the filename in the
1067 * data of the document rather than as a file-direntry term.
1069 * It would be nice to do the upgrade of the document directly
1070 * here, but the database is likely open in read-only mode. */
1072 std::string datastr = message->doc.get_data ();
1073 const char *data = datastr.c_str ();
1076 INTERNAL_ERROR ("message with no filename");
1078 _notmuch_string_list_append (message->filename_list, data);
1083 for (; node; node = node->next) {
1084 void *local = talloc_new (message);
1085 const char *db_path, *directory, *basename, *filename;
1086 char *colon, *direntry = NULL;
1087 unsigned int directory_id;
1089 direntry = node->string;
1091 directory_id = strtol (direntry, &colon, 10);
1093 if (colon == NULL || *colon != ':')
1094 INTERNAL_ERROR ("malformed direntry");
1096 basename = colon + 1;
1100 db_path = notmuch_database_get_path (message->notmuch);
1102 directory = _notmuch_database_get_directory_path (local,
1106 if (strlen (directory))
1107 filename = talloc_asprintf (message, "%s/%s/%s",
1108 db_path, directory, basename);
1110 filename = talloc_asprintf (message, "%s/%s",
1113 _notmuch_string_list_append (message->filename_list, filename);
1115 talloc_free (local);
1118 talloc_free (message->filename_term_list);
1119 message->filename_term_list = NULL;
1123 notmuch_message_get_filename (notmuch_message_t *message)
1126 _notmuch_message_ensure_filename_list (message);
1127 } catch (Xapian::Error &error) {
1128 LOG_XAPIAN_EXCEPTION (message, error);
1132 if (message->filename_list == NULL)
1135 if (message->filename_list->head == NULL ||
1136 message->filename_list->head->string == NULL) {
1137 INTERNAL_ERROR ("message with no filename");
1140 return message->filename_list->head->string;
1143 notmuch_filenames_t *
1144 notmuch_message_get_filenames (notmuch_message_t *message)
1147 _notmuch_message_ensure_filename_list (message);
1148 } catch (Xapian::Error &error) {
1149 LOG_XAPIAN_EXCEPTION (message, error);
1153 return _notmuch_filenames_create (message, message->filename_list);
1157 notmuch_message_count_files (notmuch_message_t *message)
1160 _notmuch_message_ensure_filename_list (message);
1161 } catch (Xapian::Error &error) {
1162 LOG_XAPIAN_EXCEPTION (message, error);
1166 return _notmuch_string_list_length (message->filename_list);
1170 notmuch_message_get_flag_st (notmuch_message_t *message,
1171 notmuch_message_flag_t flag,
1172 notmuch_bool_t *is_set)
1175 return NOTMUCH_STATUS_NULL_POINTER;
1178 if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
1179 ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
1180 _notmuch_message_ensure_metadata (message, NULL);
1181 } catch (Xapian::Error &error) {
1182 LOG_XAPIAN_EXCEPTION (message, error);
1183 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1186 *is_set = NOTMUCH_TEST_BIT (message->flags, flag);
1187 return NOTMUCH_STATUS_SUCCESS;
1191 notmuch_message_get_flag (notmuch_message_t *message,
1192 notmuch_message_flag_t flag)
1194 notmuch_bool_t is_set;
1195 notmuch_status_t status;
1197 status = notmuch_message_get_flag_st (message, flag, &is_set);
1206 notmuch_message_set_flag (notmuch_message_t *message,
1207 notmuch_message_flag_t flag, notmuch_bool_t enable)
1210 NOTMUCH_SET_BIT (&message->flags, flag);
1212 NOTMUCH_CLEAR_BIT (&message->flags, flag);
1213 NOTMUCH_SET_BIT (&message->lazy_flags, flag);
1217 notmuch_message_get_date (notmuch_message_t *message)
1222 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
1223 } catch (Xapian::Error &error) {
1224 LOG_XAPIAN_EXCEPTION (message, error);
1229 /* sortable_unserialise is undefined on empty string */
1231 return Xapian::sortable_unserialise (value);
1235 notmuch_message_get_tags (notmuch_message_t *message)
1237 notmuch_tags_t *tags;
1240 _notmuch_message_ensure_metadata (message, message->tag_list);
1241 } catch (Xapian::Error &error) {
1242 LOG_XAPIAN_EXCEPTION (message, error);
1246 tags = _notmuch_tags_create (message, message->tag_list);
1247 /* _notmuch_tags_create steals the reference to the tag_list, but
1248 * in this case it's still used by the message, so we add an
1249 * *additional* talloc reference to the list. As a result, it's
1250 * possible to modify the message tags (which talloc_unlink's the
1251 * current list from the message) while still iterating because
1252 * the iterator will keep the current list alive. */
1253 if (! talloc_reference (message, message->tag_list))
1260 _notmuch_message_get_author (notmuch_message_t *message)
1262 return message->author;
1266 _notmuch_message_set_author (notmuch_message_t *message,
1269 if (message->author)
1270 talloc_free (message->author);
1271 message->author = talloc_strdup (message, author);
1276 _notmuch_message_set_header_values (notmuch_message_t *message,
1279 const char *subject)
1283 /* GMime really doesn't want to see a NULL date, so protect its
1285 if (date == NULL || *date == '\0') {
1288 time_value = g_mime_utils_header_decode_date_unix (date);
1290 * Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=779923
1296 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
1297 Xapian::sortable_serialise (time_value));
1298 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
1299 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1300 message->modified = true;
1304 _notmuch_message_update_subject (notmuch_message_t *message,
1305 const char *subject)
1307 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1308 message->modified = true;
1311 /* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD. The caller
1312 * must call _notmuch_message_sync. */
1314 _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
1316 /* _notmuch_message_sync will update the last modification
1317 * revision; we just have to ask it to. */
1318 message->modified = true;
1321 /* Synchronize changes made to message->doc out into the database. */
1323 _notmuch_message_sync (notmuch_message_t *message)
1325 Xapian::WritableDatabase *db;
1327 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
1330 if (! message->modified)
1333 /* Update the last modification of this message. */
1334 if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)
1335 /* sortable_serialise gives a reasonably compact encoding,
1336 * which directly translates to reduced IO when scanning the
1337 * value stream. Since it's built for doubles, we only get 53
1338 * effective bits, but that's still enough for the database to
1339 * last a few centuries at 1 million revisions per second. */
1340 message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,
1341 Xapian::sortable_serialise (
1342 _notmuch_database_new_revision (
1343 message->notmuch)));
1345 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
1346 db->replace_document (message->doc_id, message->doc);
1347 message->modified = false;
1350 /* Delete a message document from the database, leaving a ghost
1351 * message in its place */
1353 _notmuch_message_delete (notmuch_message_t *message)
1355 notmuch_status_t status;
1356 Xapian::WritableDatabase *db;
1357 const char *mid, *tid, *query_string;
1358 notmuch_message_t *ghost;
1359 notmuch_private_status_t private_status;
1360 notmuch_database_t *notmuch;
1361 notmuch_query_t *query;
1362 unsigned int count = 0;
1365 mid = notmuch_message_get_message_id (message);
1366 tid = notmuch_message_get_thread_id (message);
1367 notmuch = message->notmuch;
1369 status = _notmuch_database_ensure_writable (message->notmuch);
1373 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1374 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 query_string = talloc_asprintf (message, "thread:%s", tid);
1385 query = notmuch_query_create (notmuch, query_string);
1387 return NOTMUCH_STATUS_OUT_OF_MEMORY;
1388 status = notmuch_query_count_messages (query, &count);
1390 notmuch_query_destroy (query);
1395 /* reintroduce a ghost in its place because there are still
1396 * other active messages in this thread: */
1397 ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);
1398 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1399 private_status = _notmuch_message_initialize_ghost (ghost, tid);
1400 if (! private_status)
1401 _notmuch_message_sync (ghost);
1402 } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1403 /* this is deeply weird, and we should not have gotten
1404 * into this state. is there a better error message to
1406 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1409 notmuch_message_destroy (ghost);
1410 status = COERCE_STATUS (private_status, "Error converting to ghost message");
1412 /* the thread is empty; drop all ghost messages from it */
1413 notmuch_messages_t *messages;
1414 status = _notmuch_query_search_documents (query,
1417 if (status == NOTMUCH_STATUS_SUCCESS) {
1418 notmuch_status_t last_error = NOTMUCH_STATUS_SUCCESS;
1419 while (notmuch_messages_valid (messages)) {
1420 message = notmuch_messages_get (messages);
1421 status = _notmuch_message_delete (message);
1422 if (status) /* we'll report the last failure we see;
1423 * if there is more than one failure, we
1424 * forget about previous ones */
1425 last_error = status;
1426 notmuch_message_destroy (message);
1427 notmuch_messages_move_to_next (messages);
1429 status = last_error;
1432 notmuch_query_destroy (query);
1436 /* Transform a blank message into a ghost message. The caller must
1437 * _notmuch_message_sync the message. */
1438 notmuch_private_status_t
1439 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1440 const char *thread_id)
1442 notmuch_private_status_t status;
1444 status = _notmuch_message_add_term (message, "type", "ghost");
1447 status = _notmuch_message_add_term (message, "thread", thread_id);
1451 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1454 /* Ensure that 'message' is not holding any file object open. Future
1455 * calls to various functions will still automatically open the
1456 * message file as needed.
1459 _notmuch_message_close (notmuch_message_t *message)
1461 if (message->message_file) {
1462 _notmuch_message_file_close (message->message_file);
1463 message->message_file = NULL;
1467 /* Add a name:value term to 'message', (the actual term will be
1468 * encoded by prefixing the value with a short prefix). See
1469 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1470 * names to prefix values.
1472 * This change will not be reflected in the database until the next
1473 * call to _notmuch_message_sync. */
1474 notmuch_private_status_t
1475 _notmuch_message_add_term (notmuch_message_t *message,
1476 const char *prefix_name,
1483 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1485 term = talloc_asprintf (message, "%s%s",
1486 _find_prefix (prefix_name), value);
1488 if (strlen (term) > NOTMUCH_TERM_MAX)
1489 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1491 message->doc.add_term (term, 0);
1492 message->modified = true;
1496 _notmuch_message_invalidate_metadata (message, prefix_name);
1498 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1501 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1502 * term will be added with the appropriate prefix if prefix_name is
1505 notmuch_private_status_t
1506 _notmuch_message_gen_terms (notmuch_message_t *message,
1507 const char *prefix_name,
1510 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1513 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1515 term_gen->set_document (message->doc);
1516 term_gen->set_termpos (message->termpos);
1519 const char *prefix = _notmuch_database_prefix (message->notmuch, prefix_name);
1521 return NOTMUCH_PRIVATE_STATUS_BAD_PREFIX;
1523 _notmuch_message_invalidate_metadata (message, prefix_name);
1524 term_gen->index_text (text, 1, prefix);
1526 term_gen->index_text (text);
1529 /* Create a gap between this an the next terms so they don't
1530 * appear to be a phrase. */
1531 message->termpos = term_gen->get_termpos () + 100;
1533 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1536 /* Remove a name:value term from 'message', (the actual term will be
1537 * encoded by prefixing the value with a short prefix). See
1538 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1539 * names to prefix values.
1541 * This change will not be reflected in the database until the next
1542 * call to _notmuch_message_sync. */
1543 notmuch_private_status_t
1544 _notmuch_message_remove_term (notmuch_message_t *message,
1545 const char *prefix_name,
1551 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1553 term = talloc_asprintf (message, "%s%s",
1554 _find_prefix (prefix_name), value);
1556 if (strlen (term) > NOTMUCH_TERM_MAX)
1557 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1560 message->doc.remove_term (term);
1561 message->modified = true;
1562 } catch (const Xapian::InvalidArgumentError) {
1563 /* We'll let the philosophers try to wrestle with the
1564 * question of whether failing to remove that which was not
1565 * there in the first place is failure. For us, we'll silently
1566 * consider it all good. */
1571 _notmuch_message_invalidate_metadata (message, prefix_name);
1573 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1576 notmuch_private_status_t
1577 _notmuch_message_has_term (notmuch_message_t *message,
1578 const char *prefix_name,
1584 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
1587 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1589 term = talloc_asprintf (message, "%s%s",
1590 _find_prefix (prefix_name), value);
1592 if (strlen (term) > NOTMUCH_TERM_MAX)
1593 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1596 /* Look for the exact term */
1597 Xapian::TermIterator i = message->doc.termlist_begin ();
1599 if (i != message->doc.termlist_end () &&
1600 ! strcmp ((*i).c_str (), term))
1602 } catch (Xapian::Error &error) {
1603 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1612 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1614 notmuch_private_status_t private_status;
1615 notmuch_status_t status;
1618 status = _notmuch_database_ensure_writable (message->notmuch);
1623 return NOTMUCH_STATUS_NULL_POINTER;
1625 if (strlen (tag) > NOTMUCH_TAG_MAX)
1626 return NOTMUCH_STATUS_TAG_TOO_LONG;
1628 private_status = _notmuch_message_add_term (message, "tag", tag);
1629 if (private_status) {
1630 return COERCE_STATUS (private_status,
1631 "_notmuch_message_remove_term return unexpected value: %d\n",
1635 if (! message->frozen)
1636 _notmuch_message_sync (message);
1638 } catch (Xapian::Error &error) {
1639 LOG_XAPIAN_EXCEPTION (message, error);
1640 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1643 return NOTMUCH_STATUS_SUCCESS;
1647 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1649 notmuch_private_status_t private_status;
1650 notmuch_status_t status;
1653 status = _notmuch_database_ensure_writable (message->notmuch);
1658 return NOTMUCH_STATUS_NULL_POINTER;
1660 if (strlen (tag) > NOTMUCH_TAG_MAX)
1661 return NOTMUCH_STATUS_TAG_TOO_LONG;
1663 private_status = _notmuch_message_remove_term (message, "tag", tag);
1664 if (private_status) {
1665 return COERCE_STATUS (private_status,
1666 "_notmuch_message_remove_term return unexpected value: %d\n",
1670 if (! message->frozen)
1671 _notmuch_message_sync (message);
1672 } catch (Xapian::Error &error) {
1673 LOG_XAPIAN_EXCEPTION (message, error);
1674 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1677 return NOTMUCH_STATUS_SUCCESS;
1680 /* Is the given filename within a maildir directory?
1682 * Specifically, is the final directory component of 'filename' either
1683 * "cur" or "new". If so, return a pointer to that final directory
1684 * component within 'filename'. If not, return NULL.
1686 * A non-NULL return value is guaranteed to be a valid string pointer
1687 * pointing to the characters "new/" or "cur/", (but not
1691 _filename_is_in_maildir (const char *filename)
1693 const char *slash, *dir = NULL;
1695 /* Find the last '/' separating directory from filename. */
1696 slash = strrchr (filename, '/');
1700 /* Jump back 4 characters to where the previous '/' will be if the
1701 * directory is named "cur" or "new". */
1702 if (slash - filename < 4)
1712 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1713 STRNCMP_LITERAL (dir, "new/") == 0) {
1720 static notmuch_status_t
1721 _ensure_maildir_flags (notmuch_message_t *message, bool force)
1724 notmuch_filenames_t *filenames;
1725 const char *filename, *dir;
1726 char *combined_flags = talloc_strdup (message, "");
1727 int seen_maildir_info = 0;
1729 if (message->maildir_flags) {
1731 talloc_free (message->maildir_flags);
1732 message->maildir_flags = NULL;
1735 filenames = notmuch_message_get_filenames (message);
1737 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1739 notmuch_filenames_valid (filenames);
1740 notmuch_filenames_move_to_next (filenames)) {
1741 filename = notmuch_filenames_get (filenames);
1742 dir = _filename_is_in_maildir (filename);
1747 flags = strstr (filename, ":2,");
1749 seen_maildir_info = 1;
1751 combined_flags = talloc_strdup_append (combined_flags, flags);
1752 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1753 /* Messages are delivered to new/ with no "info" part, but
1754 * they effectively have default maildir flags. According
1755 * to the spec, we should ignore the info part for
1756 * messages in new/, but some MUAs (mutt) can set maildir
1757 * flags on messages in new/, so we're liberal in what we
1759 seen_maildir_info = 1;
1762 if (seen_maildir_info)
1763 message->maildir_flags = combined_flags;
1764 return NOTMUCH_STATUS_SUCCESS;
1768 notmuch_message_has_maildir_flag (notmuch_message_t *message, char flag)
1770 notmuch_status_t status;
1772 status = notmuch_message_has_maildir_flag_st (message, flag, &ret);
1780 notmuch_message_has_maildir_flag_st (notmuch_message_t *message,
1782 notmuch_bool_t *is_set)
1784 notmuch_status_t status;
1787 return NOTMUCH_STATUS_NULL_POINTER;
1789 status = _ensure_maildir_flags (message, false);
1793 *is_set = message->maildir_flags && (strchr (message->maildir_flags, flag) != NULL);
1794 return NOTMUCH_STATUS_SUCCESS;
1798 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1800 notmuch_status_t status;
1803 status = _ensure_maildir_flags (message, true);
1806 /* If none of the filenames have any maildir info field (not even
1807 * an empty info with no flags set) then there's no information to
1808 * go on, so do nothing. */
1809 if (! message->maildir_flags)
1810 return NOTMUCH_STATUS_SUCCESS;
1812 status = notmuch_message_freeze (message);
1816 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1817 if ((strchr (message->maildir_flags, flag2tag[i].flag) != NULL)
1819 flag2tag[i].inverse) {
1820 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1822 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1827 status = notmuch_message_thaw (message);
1832 /* From the set of tags on 'message' and the flag2tag table, compute a
1833 * set of maildir-flag actions to be taken, (flags that should be
1834 * either set or cleared).
1836 * The result is returned as two talloced strings: to_set, and to_clear
1839 _get_maildir_flag_actions (notmuch_message_t *message,
1841 char **to_clear_ret)
1843 char *to_set, *to_clear;
1844 notmuch_tags_t *tags;
1848 to_set = talloc_strdup (message, "");
1849 to_clear = talloc_strdup (message, "");
1851 /* First, find flags for all set tags. */
1852 for (tags = notmuch_message_get_tags (message);
1853 notmuch_tags_valid (tags);
1854 notmuch_tags_move_to_next (tags)) {
1855 tag = notmuch_tags_get (tags);
1857 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1858 if (strcmp (tag, flag2tag[i].tag) == 0) {
1859 if (flag2tag[i].inverse)
1860 to_clear = talloc_asprintf_append (to_clear,
1864 to_set = talloc_asprintf_append (to_set,
1871 /* Then, find the flags for all tags not present. */
1872 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1873 if (flag2tag[i].inverse) {
1874 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1875 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1877 if (strchr (to_set, flag2tag[i].flag) == NULL)
1878 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1882 *to_set_ret = to_set;
1883 *to_clear_ret = to_clear;
1886 /* Given 'filename' and a set of maildir flags to set and to clear,
1887 * compute the new maildir filename.
1889 * If the existing filename is in the directory "new", the new
1890 * filename will be in the directory "cur", except for the case when
1891 * no flags are changed and the existing filename does not contain
1892 * maildir info (starting with ",2:").
1894 * After a sequence of ":2," in the filename, any subsequent
1895 * single-character flags will be added or removed according to the
1896 * characters in flags_to_set and flags_to_clear. Any existing flags
1897 * not mentioned in either string will remain. The final list of flags
1898 * will be in ASCII order.
1900 * If the original flags seem invalid, (repeated characters or
1901 * non-ASCII ordering of flags), this function will return NULL
1902 * (meaning that renaming would not be safe and should not occur).
1905 _new_maildir_filename (void *ctx,
1906 const char *filename,
1907 const char *flags_to_set,
1908 const char *flags_to_clear)
1910 const char *info, *flags;
1911 unsigned int flag, last_flag;
1912 char *filename_new, *dir;
1914 int flags_in_map = 0;
1915 bool flags_changed = false;
1919 memset (flag_map, 0, sizeof (flag_map));
1921 info = strstr (filename, ":2,");
1924 info = filename + strlen (filename);
1926 /* Loop through existing flags in filename. */
1927 for (flags = info + 3, last_flag = 0;
1929 last_flag = flag, flags++) {
1932 /* Original flags not in ASCII order. Abort. */
1933 if (flag < last_flag)
1936 /* Non-ASCII flag. Abort. */
1937 if (flag > sizeof (flag_map) - 1)
1940 /* Repeated flag value. Abort. */
1949 /* Then set and clear our flags from tags. */
1950 for (flags = flags_to_set; *flags; flags++) {
1952 if (flag_map[flag] == 0) {
1955 flags_changed = true;
1959 for (flags = flags_to_clear; *flags; flags++) {
1961 if (flag_map[flag]) {
1964 flags_changed = true;
1968 /* Messages in new/ without maildir info can be kept in new/ if no
1969 * flags have changed. */
1970 dir = (char *) _filename_is_in_maildir (filename);
1971 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && ! *info && ! flags_changed)
1972 return talloc_strdup (ctx, filename);
1974 filename_new = (char *) talloc_size (ctx,
1976 strlen (":2,") + flags_in_map + 1);
1977 if (unlikely (filename_new == NULL))
1980 strncpy (filename_new, filename, info - filename);
1981 filename_new[info - filename] = '\0';
1983 strcat (filename_new, ":2,");
1985 s = filename_new + strlen (filename_new);
1986 for (i = 0; i < sizeof (flag_map); i++) {
1994 /* If message is in new/ move it under cur/. */
1995 dir = (char *) _filename_is_in_maildir (filename_new);
1996 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1997 memcpy (dir, "cur/", 4);
1999 return filename_new;
2003 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
2005 notmuch_filenames_t *filenames;
2006 const char *filename;
2008 char *to_set, *to_clear;
2009 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
2011 _get_maildir_flag_actions (message, &to_set, &to_clear);
2013 for (filenames = notmuch_message_get_filenames (message);
2014 notmuch_filenames_valid (filenames);
2015 notmuch_filenames_move_to_next (filenames)) {
2016 filename = notmuch_filenames_get (filenames);
2018 if (! _filename_is_in_maildir (filename))
2021 filename_new = _new_maildir_filename (message, filename,
2023 if (filename_new == NULL)
2026 if (strcmp (filename, filename_new)) {
2028 notmuch_status_t new_status;
2030 err = rename (filename, filename_new);
2034 new_status = _notmuch_message_remove_filename (message,
2036 /* Hold on to only the first error. */
2037 if (! status && new_status
2038 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
2039 status = new_status;
2043 new_status = _notmuch_message_add_filename (message,
2045 /* Hold on to only the first error. */
2046 if (! status && new_status) {
2047 status = new_status;
2051 _notmuch_message_sync (message);
2054 talloc_free (filename_new);
2057 talloc_free (to_set);
2058 talloc_free (to_clear);
2064 notmuch_message_remove_all_tags (notmuch_message_t *message)
2066 notmuch_private_status_t private_status;
2067 notmuch_status_t status;
2068 notmuch_tags_t *tags;
2071 status = _notmuch_database_ensure_writable (message->notmuch);
2074 tags = notmuch_message_get_tags (message);
2076 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
2079 notmuch_tags_valid (tags);
2080 notmuch_tags_move_to_next (tags)) {
2081 tag = notmuch_tags_get (tags);
2083 private_status = _notmuch_message_remove_term (message, "tag", tag);
2084 if (private_status) {
2085 return COERCE_STATUS (private_status,
2086 "_notmuch_message_remove_term return unexpected value: %d\n",
2091 if (! message->frozen)
2092 _notmuch_message_sync (message);
2095 return NOTMUCH_STATUS_SUCCESS;
2099 notmuch_message_freeze (notmuch_message_t *message)
2101 notmuch_status_t status;
2103 status = _notmuch_database_ensure_writable (message->notmuch);
2109 return NOTMUCH_STATUS_SUCCESS;
2113 notmuch_message_thaw (notmuch_message_t *message)
2115 notmuch_status_t status;
2117 status = _notmuch_database_ensure_writable (message->notmuch);
2121 if (message->frozen > 0) {
2123 if (message->frozen == 0)
2124 _notmuch_message_sync (message);
2125 return NOTMUCH_STATUS_SUCCESS;
2127 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
2132 notmuch_message_destroy (notmuch_message_t *message)
2134 talloc_free (message);
2137 notmuch_database_t *
2138 notmuch_message_get_database (const notmuch_message_t *message)
2140 return message->notmuch;
2144 _notmuch_message_ensure_property_map (notmuch_message_t *message)
2146 notmuch_string_node_t *node;
2148 if (message->property_map)
2151 _notmuch_message_ensure_metadata (message, message->property_term_list);
2153 message->property_map = _notmuch_string_map_create (message);
2155 for (node = message->property_term_list->head; node; node = node->next) {
2159 value = strchr (node->string, '=');
2161 INTERNAL_ERROR ("malformed property term");
2167 _notmuch_string_map_append (message->property_map, key, value);
2171 talloc_free (message->property_term_list);
2172 message->property_term_list = NULL;
2175 notmuch_string_map_t *
2176 _notmuch_message_property_map (notmuch_message_t *message)
2178 _notmuch_message_ensure_property_map (message);
2180 return message->property_map;
2184 _notmuch_message_frozen (notmuch_message_t *message)
2186 return message->frozen;
2190 notmuch_message_reindex (notmuch_message_t *message,
2191 notmuch_indexopts_t *indexopts)
2193 notmuch_database_t *notmuch = NULL;
2194 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
2195 notmuch_private_status_t private_status;
2196 notmuch_filenames_t *orig_filenames = NULL;
2197 const char *orig_thread_id = NULL;
2198 notmuch_message_file_t *message_file = NULL;
2202 if (message == NULL)
2203 return NOTMUCH_STATUS_NULL_POINTER;
2205 /* Save in case we need to delete message */
2206 orig_thread_id = notmuch_message_get_thread_id (message);
2207 if (! orig_thread_id) {
2208 /* the following is correct as long as there is only one reason
2209 n_m_get_thread_id returns NULL
2211 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
2214 /* strdup it because the metadata may be invalidated */
2215 orig_thread_id = talloc_strdup (message, orig_thread_id);
2217 notmuch = notmuch_message_get_database (message);
2219 ret = _notmuch_database_ensure_writable (notmuch);
2223 orig_filenames = notmuch_message_get_filenames (message);
2225 private_status = _notmuch_message_remove_indexed_terms (message);
2226 if (private_status) {
2227 ret = COERCE_STATUS (private_status, "error removing terms");
2231 ret = notmuch_message_remove_all_properties_with_prefix (message, "index.");
2233 goto DONE; /* XXX TODO: distinguish from other error returns above? */
2234 if (indexopts && notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE) {
2235 ret = notmuch_message_remove_all_properties (message, "session-key");
2240 /* re-add the filenames with the associated indexopts */
2241 for (; notmuch_filenames_valid (orig_filenames);
2242 notmuch_filenames_move_to_next (orig_filenames)) {
2245 const char *from, *to, *subject;
2246 char *message_id = NULL;
2247 const char *thread_id = NULL;
2249 const char *filename = notmuch_filenames_get (orig_filenames);
2251 message_file = _notmuch_message_file_open (notmuch, filename);
2252 if (message_file == NULL)
2255 ret = _notmuch_message_file_get_headers (message_file,
2256 &from, &subject, &to, &date,
2261 /* XXX TODO: deal with changing message id? */
2263 _notmuch_message_add_filename (message, filename);
2265 ret = _notmuch_database_link_message_to_parents (notmuch, message,
2271 if (thread_id == NULL)
2272 thread_id = orig_thread_id;
2274 _notmuch_message_add_term (message, "thread", thread_id);
2275 /* Take header values only from first filename */
2277 _notmuch_message_set_header_values (message, date, from, subject);
2279 ret = _notmuch_message_index_file (message, indexopts, message_file);
2281 if (ret == NOTMUCH_STATUS_FILE_ERROR)
2287 _notmuch_message_file_close (message_file);
2288 message_file = NULL;
2291 /* put back thread id to help cleanup */
2292 _notmuch_message_add_term (message, "thread", orig_thread_id);
2293 ret = _notmuch_message_delete (message);
2295 _notmuch_message_sync (message);
2300 _notmuch_message_file_close (message_file);
2302 /* XXX TODO destroy orig_filenames? */