1 /* message.cc - Results of message-based searches from a notmuch database
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
23 #include "message-private.h"
27 #include <gmime/gmime.h>
29 struct _notmuch_message {
30 notmuch_database_t *notmuch;
36 notmuch_string_list_t *tag_list;
37 notmuch_string_list_t *filename_term_list;
38 notmuch_string_list_t *filename_list;
41 notmuch_message_file_t *message_file;
42 notmuch_string_list_t *property_term_list;
43 notmuch_string_map_t *property_map;
44 notmuch_string_list_t *reference_list;
45 notmuch_message_list_t *replies;
47 /* For flags that are initialized on-demand, lazy_flags indicates
48 * if each flag has been initialized. */
49 unsigned long lazy_flags;
51 /* Message document modified since last sync */
54 /* last view of database the struct is synced with */
55 unsigned long last_view;
58 Xapian::termcount termpos;
61 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
63 struct maildir_flag_tag {
69 /* ASCII ordered table of Maildir flags and associated tags */
70 static struct maildir_flag_tag flag2tag[] = {
71 { 'D', "draft", false},
72 { 'F', "flagged", false},
73 { 'P', "passed", false},
74 { 'R', "replied", false},
75 { 'S', "unread", true }
78 /* We end up having to call the destructor explicitly because we had
79 * to use "placement new" in order to initialize C++ objects within a
80 * block that we allocated with talloc. So C++ is making talloc
81 * slightly less simple to use, (we wouldn't need
82 * talloc_set_destructor at all otherwise).
85 _notmuch_message_destructor (notmuch_message_t *message)
87 message->doc.~Document ();
92 static notmuch_message_t *
93 _notmuch_message_create_for_document (const void *talloc_owner,
94 notmuch_database_t *notmuch,
97 notmuch_private_status_t *status)
99 notmuch_message_t *message;
102 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
104 message = talloc (talloc_owner, notmuch_message_t);
105 if (unlikely (message == NULL)) {
107 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
111 message->notmuch = notmuch;
112 message->doc_id = doc_id;
116 message->lazy_flags = 0;
118 /* the message is initially not synchronized with Xapian */
119 message->last_view = 0;
121 /* Each of these will be lazily created as needed. */
122 message->message_id = NULL;
123 message->thread_id = NULL;
124 message->in_reply_to = NULL;
125 message->tag_list = NULL;
126 message->filename_term_list = NULL;
127 message->filename_list = NULL;
128 message->maildir_flags = NULL;
129 message->message_file = NULL;
130 message->author = NULL;
131 message->property_term_list = NULL;
132 message->property_map = NULL;
133 message->reference_list = NULL;
135 message->replies = _notmuch_message_list_create (message);
136 if (unlikely (message->replies == NULL)) {
138 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
142 /* This is C++'s creepy "placement new", which is really just an
143 * ugly way to call a constructor for a pre-allocated object. So
144 * it's really not an error to not be checking for OUT_OF_MEMORY
145 * here, since this "new" isn't actually allocating memory. This
146 * is language-design comedy of the wrong kind. */
148 new (&message->doc) Xapian::Document;
150 talloc_set_destructor (message, _notmuch_message_destructor);
153 message->termpos = 0;
158 /* Create a new notmuch_message_t object for an existing document in
161 * Here, 'talloc owner' is an optional talloc context to which the new
162 * message will belong. This allows for the caller to not bother
163 * calling notmuch_message_destroy on the message, and know that all
164 * memory will be reclaimed when 'talloc_owner' is freed. The caller
165 * still can call notmuch_message_destroy when finished with the
166 * message if desired.
168 * The 'talloc_owner' argument can also be NULL, in which case the
169 * caller *is* responsible for calling notmuch_message_destroy.
171 * If no document exists in the database with document ID of 'doc_id'
172 * then this function returns NULL and optionally sets *status to
173 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
175 * This function can also fail to due lack of available memory,
176 * returning NULL and optionally setting *status to
177 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
179 * The caller can pass NULL for status if uninterested in
180 * distinguishing these two cases.
183 _notmuch_message_create (const void *talloc_owner,
184 notmuch_database_t *notmuch,
186 notmuch_private_status_t *status)
188 Xapian::Document doc;
191 doc = notmuch->xapian_db->get_document (doc_id);
192 } catch (const Xapian::DocNotFoundError &error) {
194 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
198 return _notmuch_message_create_for_document (talloc_owner, notmuch,
199 doc_id, doc, status);
202 /* Create a new notmuch_message_t object for a specific message ID,
203 * (which may or may not already exist in the database).
205 * The 'notmuch' database will be the talloc owner of the returned
208 * This function returns a valid notmuch_message_t whether or not
209 * there is already a document in the database with the given message
210 * ID. These two cases can be distinguished by the value of *status:
213 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
215 * There is already a document with message ID 'message_id' in the
216 * database. The returned message can be used to query/modify the
217 * document. The message may be a ghost message.
219 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
221 * No document with 'message_id' exists in the database. The
222 * returned message contains a newly created document (not yet
223 * added to the database) and a document ID that is known not to
224 * exist in the database. This message is "blank"; that is, it
225 * contains only a message ID and no other metadata. The caller
226 * can modify the message, and a call to _notmuch_message_sync
227 * will add the document to the database.
229 * If an error occurs, this function will return NULL and *status
230 * will be set as appropriate. (The status pointer argument must
234 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
235 const char *message_id,
236 notmuch_private_status_t *status_ret)
238 notmuch_message_t *message;
239 Xapian::Document doc;
243 *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
247 return talloc_steal (notmuch, message);
248 else if (*status_ret)
251 /* If the message ID is too long, substitute its sha1 instead. */
252 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
253 message_id = _notmuch_message_id_compressed (message, message_id);
255 term = talloc_asprintf (NULL, "%s%s",
256 _find_prefix ("id"), message_id);
258 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
262 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
263 INTERNAL_ERROR ("Failure to ensure database is writable.");
266 doc.add_term (term, 0);
269 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
271 doc_id = _notmuch_database_generate_doc_id (notmuch);
272 } catch (const Xapian::Error &error) {
273 _notmuch_database_log(notmuch_message_get_database (message), "A Xapian exception occurred creating message: %s\n",
274 error.get_msg().c_str());
275 notmuch->exception_reported = true;
276 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
280 message = _notmuch_message_create_for_document (notmuch, notmuch,
281 doc_id, doc, status_ret);
283 /* We want to inform the caller that we had to create a new
285 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
286 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
292 _notmuch_message_get_term (notmuch_message_t *message,
293 Xapian::TermIterator &i, Xapian::TermIterator &end,
296 int prefix_len = strlen (prefix);
304 const std::string &term = *i;
305 if (strncmp (term.c_str(), prefix, prefix_len))
308 value = talloc_strdup (message, term.c_str() + prefix_len);
310 #if DEBUG_DATABASE_SANITY
313 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
314 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
315 message->doc_id, prefix, value,
316 (*i).c_str () + prefix_len);
324 * For special applications where we only want the thread id, reading
325 * in all metadata is a heavy I/O penalty.
328 _notmuch_message_get_thread_id_only (notmuch_message_t *message)
331 Xapian::TermIterator i = message->doc.termlist_begin ();
332 Xapian::TermIterator end = message->doc.termlist_end ();
334 message->thread_id = _notmuch_message_get_term (message, i, end,
335 _find_prefix ("thread"));
336 return message->thread_id;
341 _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
343 Xapian::TermIterator i, end;
345 if (field && (message->last_view >= message->notmuch->view))
348 const char *thread_prefix = _find_prefix ("thread"),
349 *tag_prefix = _find_prefix ("tag"),
350 *id_prefix = _find_prefix ("id"),
351 *type_prefix = _find_prefix ("type"),
352 *filename_prefix = _find_prefix ("file-direntry"),
353 *property_prefix = _find_prefix ("property"),
354 *reference_prefix = _find_prefix ("reference"),
355 *replyto_prefix = _find_prefix ("replyto");
357 /* We do this all in a single pass because Xapian decompresses the
358 * term list every time you iterate over it. Thus, while this is
359 * slightly more costly than looking up individual fields if only
360 * one field of the message object is actually used, it's a huge
361 * win as more fields are used. */
362 for (int count=0; count < 3; count++) {
364 i = message->doc.termlist_begin ();
365 end = message->doc.termlist_end ();
368 if (!message->thread_id)
370 _notmuch_message_get_term (message, i, end, thread_prefix);
373 assert (strcmp (thread_prefix, tag_prefix) < 0);
374 if (!message->tag_list) {
376 _notmuch_database_get_terms_with_prefix (message, i, end,
378 _notmuch_string_list_sort (message->tag_list);
382 assert (strcmp (tag_prefix, id_prefix) < 0);
383 if (!message->message_id)
384 message->message_id =
385 _notmuch_message_get_term (message, i, end, id_prefix);
387 /* Get document type */
388 assert (strcmp (id_prefix, type_prefix) < 0);
389 if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {
390 i.skip_to (type_prefix);
391 /* "T" is the prefix "type" fields. See
392 * BOOLEAN_PREFIX_INTERNAL. */
394 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
395 else if (*i == "Tghost")
396 NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
398 INTERNAL_ERROR ("Message without type term");
399 NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
402 /* Get filename list. Here we get only the terms. We lazily
403 * expand them to full file names when needed in
404 * _notmuch_message_ensure_filename_list. */
405 assert (strcmp (type_prefix, filename_prefix) < 0);
406 if (!message->filename_term_list && !message->filename_list)
407 message->filename_term_list =
408 _notmuch_database_get_terms_with_prefix (message, i, end,
412 /* Get property terms. Mimic the setup with filenames above */
413 assert (strcmp (filename_prefix, property_prefix) < 0);
414 if (!message->property_map && !message->property_term_list)
415 message->property_term_list =
416 _notmuch_database_get_terms_with_prefix (message, i, end,
420 assert (strcmp (property_prefix, reference_prefix) < 0);
421 if (!message->reference_list) {
422 message->reference_list =
423 _notmuch_database_get_terms_with_prefix (message, i, end,
428 assert (strcmp (property_prefix, replyto_prefix) < 0);
429 if (!message->in_reply_to)
430 message->in_reply_to =
431 _notmuch_message_get_term (message, i, end, replyto_prefix);
434 /* It's perfectly valid for a message to have no In-Reply-To
435 * header. For these cases, we return an empty string. */
436 if (!message->in_reply_to)
437 message->in_reply_to = talloc_strdup (message, "");
439 /* all the way without an exception */
441 } catch (const Xapian::DatabaseModifiedError &error) {
442 notmuch_status_t status = _notmuch_database_reopen (message->notmuch);
443 if (status != NOTMUCH_STATUS_SUCCESS)
444 INTERNAL_ERROR ("unhandled error from notmuch_database_reopen: %s\n",
445 notmuch_status_to_string (status));
446 } catch (const Xapian::Error &error) {
447 INTERNAL_ERROR ("A Xapian exception occurred fetching message metadata: %s\n",
448 error.get_msg().c_str());
451 message->last_view = message->notmuch->view;
455 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
456 const char *prefix_name)
458 if (strcmp ("thread", prefix_name) == 0) {
459 talloc_free (message->thread_id);
460 message->thread_id = NULL;
463 if (strcmp ("tag", prefix_name) == 0) {
464 talloc_unlink (message, message->tag_list);
465 message->tag_list = NULL;
468 if (strcmp ("type", prefix_name) == 0) {
469 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
470 NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
473 if (strcmp ("file-direntry", prefix_name) == 0) {
474 talloc_free (message->filename_term_list);
475 talloc_free (message->filename_list);
476 message->filename_term_list = message->filename_list = NULL;
479 if (strcmp ("property", prefix_name) == 0) {
481 if (message->property_term_list)
482 talloc_free (message->property_term_list);
483 message->property_term_list = NULL;
485 if (message->property_map)
486 talloc_unlink (message, message->property_map);
488 message->property_map = NULL;
491 if (strcmp ("replyto", prefix_name) == 0) {
492 talloc_free (message->in_reply_to);
493 message->in_reply_to = NULL;
498 _notmuch_message_get_doc_id (notmuch_message_t *message)
500 return message->doc_id;
504 notmuch_message_get_message_id (notmuch_message_t *message)
506 _notmuch_message_ensure_metadata (message, message->message_id);
507 if (!message->message_id)
508 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
510 return message->message_id;
514 _notmuch_message_ensure_message_file (notmuch_message_t *message)
516 const char *filename;
518 if (message->message_file)
521 filename = notmuch_message_get_filename (message);
522 if (unlikely (filename == NULL))
525 message->message_file = _notmuch_message_file_open_ctx (
526 notmuch_message_get_database (message), message, filename);
530 notmuch_message_get_header (notmuch_message_t *message, const char *header)
532 Xapian::valueno slot = Xapian::BAD_VALUENO;
534 /* Fetch header from the appropriate xapian value field if
536 if (strcasecmp (header, "from") == 0)
537 slot = NOTMUCH_VALUE_FROM;
538 else if (strcasecmp (header, "subject") == 0)
539 slot = NOTMUCH_VALUE_SUBJECT;
540 else if (strcasecmp (header, "message-id") == 0)
541 slot = NOTMUCH_VALUE_MESSAGE_ID;
543 if (slot != Xapian::BAD_VALUENO) {
545 std::string value = message->doc.get_value (slot);
547 /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then
548 * empty values indicate empty headers. If we don't, then
549 * it could just mean we didn't record the header. */
550 if ((message->notmuch->features &
551 NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) ||
553 return talloc_strdup (message, value.c_str ());
555 } catch (Xapian::Error &error) {
556 _notmuch_database_log(notmuch_message_get_database (message), "A Xapian exception occurred when reading header: %s\n",
557 error.get_msg().c_str());
558 message->notmuch->exception_reported = true;
563 /* Otherwise fall back to parsing the file */
564 _notmuch_message_ensure_message_file (message);
565 if (message->message_file == NULL)
568 return _notmuch_message_file_get_header (message->message_file, header);
571 /* Return the message ID from the In-Reply-To header of 'message'.
573 * Returns an empty string ("") if 'message' has no In-Reply-To
576 * Returns NULL if any error occurs.
579 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
581 _notmuch_message_ensure_metadata (message, message->in_reply_to);
582 return message->in_reply_to;
586 notmuch_message_get_thread_id (notmuch_message_t *message)
588 _notmuch_message_ensure_metadata (message, message->thread_id);
589 if (!message->thread_id)
590 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
592 return message->thread_id;
596 _notmuch_message_add_reply (notmuch_message_t *message,
597 notmuch_message_t *reply)
599 _notmuch_message_list_add_message (message->replies, reply);
602 const notmuch_string_list_t *
603 _notmuch_message_get_references (notmuch_message_t *message)
605 _notmuch_message_ensure_metadata (message, message->reference_list);
606 return message->reference_list;
610 _cmpmsg (const void *pa, const void *pb)
612 notmuch_message_t **a = (notmuch_message_t **) pa;
613 notmuch_message_t **b = (notmuch_message_t **) pb;
614 time_t time_a = notmuch_message_get_date (*a);
615 time_t time_b = notmuch_message_get_date (*b);
617 if (time_a == time_b)
619 else if (time_a < time_b)
625 notmuch_message_list_t *
626 _notmuch_message_sort_subtrees (void *ctx, notmuch_message_list_t *list)
630 size_t capacity = 16;
635 void *local = talloc_new (NULL);
636 notmuch_message_list_t *new_list = _notmuch_message_list_create (ctx);
637 notmuch_message_t **message_array = talloc_zero_array (local, notmuch_message_t *, capacity);
639 for (notmuch_messages_t *messages = _notmuch_messages_create (list);
640 notmuch_messages_valid (messages);
641 notmuch_messages_move_to_next (messages)) {
642 notmuch_message_t *root = notmuch_messages_get (messages);
643 if (count >= capacity) {
645 message_array = talloc_realloc (local, message_array, notmuch_message_t *, capacity);
647 message_array[count++] = root;
648 root->replies = _notmuch_message_sort_subtrees (root, root->replies);
651 qsort (message_array, count, sizeof (notmuch_message_t *), _cmpmsg);
652 for (size_t i = 0; i < count; i++) {
653 _notmuch_message_list_add_message (new_list, message_array[i]);
662 notmuch_message_get_replies (notmuch_message_t *message)
664 return _notmuch_messages_create (message->replies);
668 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
670 Xapian::TermIterator i;
671 size_t prefix_len = 0;
673 prefix_len = strlen (prefix);
676 i = message->doc.termlist_begin ();
679 /* Terminate loop when no terms remain with desired prefix. */
680 if (i == message->doc.termlist_end () ||
681 strncmp ((*i).c_str (), prefix, prefix_len))
685 message->doc.remove_term ((*i));
686 message->modified = true;
687 } catch (const Xapian::InvalidArgumentError) {
688 /* Ignore failure to remove non-existent term. */
694 /* Remove all terms generated by indexing, i.e. not tags or
695 * properties, along with any automatic tags*/
696 notmuch_private_status_t
697 _notmuch_message_remove_indexed_terms (notmuch_message_t *message)
699 Xapian::TermIterator i;
702 id_prefix = _find_prefix ("id"),
703 property_prefix = _find_prefix ("property"),
704 tag_prefix = _find_prefix ("tag"),
705 type_prefix = _find_prefix ("type");
707 for (i = message->doc.termlist_begin ();
708 i != message->doc.termlist_end (); i++) {
710 const std::string term = *i;
712 if (term.compare (0, type_prefix.size (), type_prefix) == 0)
715 if (term.compare (0, id_prefix.size (), id_prefix) == 0)
718 if (term.compare (0, property_prefix.size (), property_prefix) == 0)
721 if (term.compare (0, tag_prefix.size (), tag_prefix) == 0 &&
722 term.compare (1, strlen("encrypted"), "encrypted") != 0 &&
723 term.compare (1, strlen("signed"), "signed") != 0 &&
724 term.compare (1, strlen("attachment"), "attachment") != 0)
728 message->doc.remove_term ((*i));
729 message->modified = true;
730 } catch (const Xapian::InvalidArgumentError) {
731 /* Ignore failure to remove non-existent term. */
732 } catch (const Xapian::Error &error) {
733 notmuch_database_t *notmuch = message->notmuch;
735 if (!notmuch->exception_reported) {
736 _notmuch_database_log(notmuch_message_get_database (message), "A Xapian exception occurred creating message: %s\n",
737 error.get_msg().c_str());
738 notmuch->exception_reported = true;
740 return NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
743 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
746 /* Return true if p points at "new" or "cur". */
747 static bool is_maildir (const char *p)
749 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
752 /* Add "folder:" term for directory. */
753 static notmuch_status_t
754 _notmuch_message_add_folder_terms (notmuch_message_t *message,
755 const char *directory)
759 folder = talloc_strdup (NULL, directory);
761 return NOTMUCH_STATUS_OUT_OF_MEMORY;
764 * If the message file is in a leaf directory named "new" or
765 * "cur", presume maildir and index the parent directory. Thus a
766 * "folder:" prefix search matches messages in the specified
767 * maildir folder, i.e. in the specified directory and its "new"
768 * and "cur" subdirectories.
770 * Note that this means the "folder:" prefix can't be used for
771 * distinguishing between message files in "new" or "cur". The
772 * "path:" prefix needs to be used for that.
774 * Note the deliberate difference to _filename_is_in_maildir(). We
775 * don't want to index different things depending on the existence
776 * or non-existence of all maildir sibling directories "new",
777 * "cur", and "tmp". Doing so would be surprising, and difficult
778 * for the user to fix in case all subdirectories were not in
779 * place during indexing.
781 last = strrchr (folder, '/');
783 if (is_maildir (last + 1))
785 } else if (is_maildir (folder)) {
789 _notmuch_message_add_term (message, "folder", folder);
791 talloc_free (folder);
793 message->modified = true;
794 return NOTMUCH_STATUS_SUCCESS;
797 #define RECURSIVE_SUFFIX "/**"
799 /* Add "path:" terms for directory. */
800 static notmuch_status_t
801 _notmuch_message_add_path_terms (notmuch_message_t *message,
802 const char *directory)
804 /* Add exact "path:" term. */
805 _notmuch_message_add_term (message, "path", directory);
807 if (strlen (directory)) {
810 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
812 return NOTMUCH_STATUS_OUT_OF_MEMORY;
814 /* Add recursive "path:" terms for directory and all parents. */
815 for (p = path + strlen (path) - 1; p > path; p--) {
817 strcpy (p, RECURSIVE_SUFFIX);
818 _notmuch_message_add_term (message, "path", path);
825 /* Recursive all-matching path:** for consistency. */
826 _notmuch_message_add_term (message, "path", "**");
828 return NOTMUCH_STATUS_SUCCESS;
831 /* Add directory based terms for all filenames of the message. */
832 static notmuch_status_t
833 _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
835 const char *direntry_prefix = _find_prefix ("file-direntry");
836 int direntry_prefix_len = strlen (direntry_prefix);
837 Xapian::TermIterator i = message->doc.termlist_begin ();
838 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
840 for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {
841 unsigned int directory_id;
842 const char *direntry, *directory;
844 const std::string &term = *i;
846 /* Terminate loop at first term without desired prefix. */
847 if (strncmp (term.c_str (), direntry_prefix, direntry_prefix_len))
850 /* Indicate that there are filenames remaining. */
851 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
853 direntry = term.c_str ();
854 direntry += direntry_prefix_len;
856 directory_id = strtol (direntry, &colon, 10);
858 if (colon == NULL || *colon != ':')
859 INTERNAL_ERROR ("malformed direntry");
861 directory = _notmuch_database_get_directory_path (ctx,
865 _notmuch_message_add_folder_terms (message, directory);
866 _notmuch_message_add_path_terms (message, directory);
872 /* Add an additional 'filename' for 'message'.
874 * This change will not be reflected in the database until the next
875 * call to _notmuch_message_sync. */
877 _notmuch_message_add_filename (notmuch_message_t *message,
878 const char *filename)
880 const char *relative, *directory;
881 notmuch_status_t status;
882 void *local = talloc_new (message);
885 if (filename == NULL)
886 INTERNAL_ERROR ("Message filename cannot be NULL.");
888 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
889 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
890 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
892 relative = _notmuch_database_relative_path (message->notmuch, filename);
894 status = _notmuch_database_split_path (local, relative, &directory, NULL);
898 status = _notmuch_database_filename_to_direntry (
899 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
903 /* New file-direntry allows navigating to this message with
904 * notmuch_directory_get_child_files() . */
905 _notmuch_message_add_term (message, "file-direntry", direntry);
907 _notmuch_message_add_folder_terms (message, directory);
908 _notmuch_message_add_path_terms (message, directory);
912 return NOTMUCH_STATUS_SUCCESS;
915 /* Remove a particular 'filename' from 'message'.
917 * This change will not be reflected in the database until the next
918 * call to _notmuch_message_sync.
920 * If this message still has other filenames, returns
921 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
923 * Note: This function does not remove a document from the database,
924 * even if the specified filename is the only filename for this
925 * message. For that functionality, see
926 * notmuch_database_remove_message. */
928 _notmuch_message_remove_filename (notmuch_message_t *message,
929 const char *filename)
931 void *local = talloc_new (message);
933 notmuch_private_status_t private_status;
934 notmuch_status_t status;
936 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
937 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
938 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
940 status = _notmuch_database_filename_to_direntry (
941 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
942 if (status || !direntry)
945 /* Unlink this file from its parent directory. */
946 private_status = _notmuch_message_remove_term (message,
947 "file-direntry", direntry);
948 status = COERCE_STATUS (private_status,
949 "Unexpected error from _notmuch_message_remove_term");
953 /* Re-synchronize "folder:" and "path:" terms for this message. */
955 /* Remove all "folder:" terms. */
956 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
958 /* Remove all "path:" terms. */
959 _notmuch_message_remove_terms (message, _find_prefix ("path"));
961 /* Add back terms for all remaining filenames of the message. */
962 status = _notmuch_message_add_directory_terms (local, message);
969 /* Upgrade the "folder:" prefix from V1 to V2. */
970 #define FOLDER_PREFIX_V1 "XFOLDER"
971 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
973 _notmuch_message_upgrade_folder (notmuch_message_t *message)
975 /* Remove all old "folder:" terms. */
976 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
978 /* Remove all old "folder:" stemmed terms. */
979 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
981 /* Add new boolean "folder:" and "path:" terms. */
982 _notmuch_message_add_directory_terms (message, message);
986 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
988 return talloc_strdup (message, message->doc.get_data ().c_str ());
992 _notmuch_message_clear_data (notmuch_message_t *message)
994 message->doc.set_data ("");
995 message->modified = true;
999 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
1001 notmuch_string_node_t *node;
1003 if (message->filename_list)
1006 _notmuch_message_ensure_metadata (message, message->filename_term_list);
1008 message->filename_list = _notmuch_string_list_create (message);
1009 node = message->filename_term_list->head;
1012 /* A message document created by an old version of notmuch
1013 * (prior to rename support) will have the filename in the
1014 * data of the document rather than as a file-direntry term.
1016 * It would be nice to do the upgrade of the document directly
1017 * here, but the database is likely open in read-only mode. */
1019 std::string datastr = message->doc.get_data ();
1020 const char *data = datastr.c_str ();
1023 INTERNAL_ERROR ("message with no filename");
1025 _notmuch_string_list_append (message->filename_list, data);
1030 for (; node; node = node->next) {
1031 void *local = talloc_new (message);
1032 const char *db_path, *directory, *basename, *filename;
1033 char *colon, *direntry = NULL;
1034 unsigned int directory_id;
1036 direntry = node->string;
1038 directory_id = strtol (direntry, &colon, 10);
1040 if (colon == NULL || *colon != ':')
1041 INTERNAL_ERROR ("malformed direntry");
1043 basename = colon + 1;
1047 db_path = notmuch_database_get_path (message->notmuch);
1049 directory = _notmuch_database_get_directory_path (local,
1053 if (strlen (directory))
1054 filename = talloc_asprintf (message, "%s/%s/%s",
1055 db_path, directory, basename);
1057 filename = talloc_asprintf (message, "%s/%s",
1060 _notmuch_string_list_append (message->filename_list, filename);
1062 talloc_free (local);
1065 talloc_free (message->filename_term_list);
1066 message->filename_term_list = NULL;
1070 notmuch_message_get_filename (notmuch_message_t *message)
1072 _notmuch_message_ensure_filename_list (message);
1074 if (message->filename_list == NULL)
1077 if (message->filename_list->head == NULL ||
1078 message->filename_list->head->string == NULL)
1080 INTERNAL_ERROR ("message with no filename");
1083 return message->filename_list->head->string;
1086 notmuch_filenames_t *
1087 notmuch_message_get_filenames (notmuch_message_t *message)
1089 _notmuch_message_ensure_filename_list (message);
1091 return _notmuch_filenames_create (message, message->filename_list);
1095 notmuch_message_count_files (notmuch_message_t *message)
1097 _notmuch_message_ensure_filename_list (message);
1099 return _notmuch_string_list_length (message->filename_list);
1103 notmuch_message_get_flag (notmuch_message_t *message,
1104 notmuch_message_flag_t flag)
1106 if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
1107 ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
1108 _notmuch_message_ensure_metadata (message, NULL);
1110 return NOTMUCH_TEST_BIT (message->flags, flag);
1114 notmuch_message_set_flag (notmuch_message_t *message,
1115 notmuch_message_flag_t flag, notmuch_bool_t enable)
1118 NOTMUCH_SET_BIT (&message->flags, flag);
1120 NOTMUCH_CLEAR_BIT (&message->flags, flag);
1121 NOTMUCH_SET_BIT (&message->lazy_flags, flag);
1125 notmuch_message_get_date (notmuch_message_t *message)
1130 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
1131 } catch (Xapian::Error &error) {
1132 _notmuch_database_log(notmuch_message_get_database (message), "A Xapian exception occurred when reading date: %s\n",
1133 error.get_msg().c_str());
1134 message->notmuch->exception_reported = true;
1139 /* sortable_unserialise is undefined on empty string */
1141 return Xapian::sortable_unserialise (value);
1145 notmuch_message_get_tags (notmuch_message_t *message)
1147 notmuch_tags_t *tags;
1149 _notmuch_message_ensure_metadata (message, message->tag_list);
1151 tags = _notmuch_tags_create (message, message->tag_list);
1152 /* _notmuch_tags_create steals the reference to the tag_list, but
1153 * in this case it's still used by the message, so we add an
1154 * *additional* talloc reference to the list. As a result, it's
1155 * possible to modify the message tags (which talloc_unlink's the
1156 * current list from the message) while still iterating because
1157 * the iterator will keep the current list alive. */
1158 if (!talloc_reference (message, message->tag_list))
1165 _notmuch_message_get_author (notmuch_message_t *message)
1167 return message->author;
1171 _notmuch_message_set_author (notmuch_message_t *message,
1174 if (message->author)
1175 talloc_free(message->author);
1176 message->author = talloc_strdup(message, author);
1181 _notmuch_message_set_header_values (notmuch_message_t *message,
1184 const char *subject)
1188 /* GMime really doesn't want to see a NULL date, so protect its
1190 if (date == NULL || *date == '\0') {
1193 time_value = g_mime_utils_header_decode_date_unix (date);
1195 * Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=779923
1201 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
1202 Xapian::sortable_serialise (time_value));
1203 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
1204 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
1205 message->modified = true;
1208 /* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD. The caller
1209 * must call _notmuch_message_sync. */
1211 _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
1213 /* _notmuch_message_sync will update the last modification
1214 * revision; we just have to ask it to. */
1215 message->modified = true;
1218 /* Synchronize changes made to message->doc out into the database. */
1220 _notmuch_message_sync (notmuch_message_t *message)
1222 Xapian::WritableDatabase *db;
1224 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
1227 if (! message->modified)
1230 /* Update the last modification of this message. */
1231 if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)
1232 /* sortable_serialise gives a reasonably compact encoding,
1233 * which directly translates to reduced IO when scanning the
1234 * value stream. Since it's built for doubles, we only get 53
1235 * effective bits, but that's still enough for the database to
1236 * last a few centuries at 1 million revisions per second. */
1237 message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,
1238 Xapian::sortable_serialise (
1239 _notmuch_database_new_revision (
1240 message->notmuch)));
1242 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
1243 db->replace_document (message->doc_id, message->doc);
1244 message->modified = false;
1247 /* Delete a message document from the database, leaving a ghost
1248 * message in its place */
1250 _notmuch_message_delete (notmuch_message_t *message)
1252 notmuch_status_t status;
1253 Xapian::WritableDatabase *db;
1254 const char *mid, *tid, *query_string;
1255 notmuch_message_t *ghost;
1256 notmuch_private_status_t private_status;
1257 notmuch_database_t *notmuch;
1258 notmuch_query_t *query;
1259 unsigned int count = 0;
1262 mid = notmuch_message_get_message_id (message);
1263 tid = notmuch_message_get_thread_id (message);
1264 notmuch = message->notmuch;
1266 status = _notmuch_database_ensure_writable (message->notmuch);
1270 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1271 db->delete_document (message->doc_id);
1273 /* if this was a ghost to begin with, we are done */
1274 private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
1276 return COERCE_STATUS (private_status,
1277 "Error trying to determine whether message was a ghost");
1279 return NOTMUCH_STATUS_SUCCESS;
1281 query_string = talloc_asprintf (message, "thread:%s", tid);
1282 query = notmuch_query_create (notmuch, query_string);
1284 return NOTMUCH_STATUS_OUT_OF_MEMORY;
1285 status = notmuch_query_count_messages (query, &count);
1287 notmuch_query_destroy (query);
1292 /* reintroduce a ghost in its place because there are still
1293 * other active messages in this thread: */
1294 ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);
1295 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1296 private_status = _notmuch_message_initialize_ghost (ghost, tid);
1297 if (! private_status)
1298 _notmuch_message_sync (ghost);
1299 } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1300 /* this is deeply weird, and we should not have gotten
1301 into this state. is there a better error message to
1303 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1306 notmuch_message_destroy (ghost);
1307 status = COERCE_STATUS (private_status, "Error converting to ghost message");
1309 /* the thread is empty; drop all ghost messages from it */
1310 notmuch_messages_t *messages;
1311 status = _notmuch_query_search_documents (query,
1314 if (status == NOTMUCH_STATUS_SUCCESS) {
1315 notmuch_status_t last_error = NOTMUCH_STATUS_SUCCESS;
1316 while (notmuch_messages_valid (messages)) {
1317 message = notmuch_messages_get (messages);
1318 status = _notmuch_message_delete (message);
1319 if (status) /* we'll report the last failure we see;
1320 * if there is more than one failure, we
1321 * forget about previous ones */
1322 last_error = status;
1323 notmuch_message_destroy (message);
1324 notmuch_messages_move_to_next (messages);
1326 status = last_error;
1329 notmuch_query_destroy (query);
1333 /* Transform a blank message into a ghost message. The caller must
1334 * _notmuch_message_sync the message. */
1335 notmuch_private_status_t
1336 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1337 const char *thread_id)
1339 notmuch_private_status_t status;
1341 status = _notmuch_message_add_term (message, "type", "ghost");
1344 status = _notmuch_message_add_term (message, "thread", thread_id);
1348 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1351 /* Ensure that 'message' is not holding any file object open. Future
1352 * calls to various functions will still automatically open the
1353 * message file as needed.
1356 _notmuch_message_close (notmuch_message_t *message)
1358 if (message->message_file) {
1359 _notmuch_message_file_close (message->message_file);
1360 message->message_file = NULL;
1364 /* Add a name:value term to 'message', (the actual term will be
1365 * encoded by prefixing the value with a short prefix). See
1366 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1367 * names to prefix values.
1369 * This change will not be reflected in the database until the next
1370 * call to _notmuch_message_sync. */
1371 notmuch_private_status_t
1372 _notmuch_message_add_term (notmuch_message_t *message,
1373 const char *prefix_name,
1380 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1382 term = talloc_asprintf (message, "%s%s",
1383 _find_prefix (prefix_name), value);
1385 if (strlen (term) > NOTMUCH_TERM_MAX)
1386 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1388 message->doc.add_term (term, 0);
1389 message->modified = true;
1393 _notmuch_message_invalidate_metadata (message, prefix_name);
1395 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1398 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1399 * term will be added both prefixed (if prefix_name is not NULL) and
1400 * also non-prefixed). */
1401 notmuch_private_status_t
1402 _notmuch_message_gen_terms (notmuch_message_t *message,
1403 const char *prefix_name,
1406 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1409 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1411 term_gen->set_document (message->doc);
1414 const char *prefix = _find_prefix (prefix_name);
1416 term_gen->set_termpos (message->termpos);
1417 term_gen->index_text (text, 1, prefix);
1418 /* Create a gap between this an the next terms so they don't
1419 * appear to be a phrase. */
1420 message->termpos = term_gen->get_termpos () + 100;
1422 _notmuch_message_invalidate_metadata (message, prefix_name);
1425 term_gen->set_termpos (message->termpos);
1426 term_gen->index_text (text);
1427 /* Create a term gap, as above. */
1428 message->termpos = term_gen->get_termpos () + 100;
1430 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1433 /* Remove a name:value term from 'message', (the actual term will be
1434 * encoded by prefixing the value with a short prefix). See
1435 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1436 * names to prefix values.
1438 * This change will not be reflected in the database until the next
1439 * call to _notmuch_message_sync. */
1440 notmuch_private_status_t
1441 _notmuch_message_remove_term (notmuch_message_t *message,
1442 const char *prefix_name,
1448 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1450 term = talloc_asprintf (message, "%s%s",
1451 _find_prefix (prefix_name), value);
1453 if (strlen (term) > NOTMUCH_TERM_MAX)
1454 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1457 message->doc.remove_term (term);
1458 message->modified = true;
1459 } catch (const Xapian::InvalidArgumentError) {
1460 /* We'll let the philosophers try to wrestle with the
1461 * question of whether failing to remove that which was not
1462 * there in the first place is failure. For us, we'll silently
1463 * consider it all good. */
1468 _notmuch_message_invalidate_metadata (message, prefix_name);
1470 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1473 notmuch_private_status_t
1474 _notmuch_message_has_term (notmuch_message_t *message,
1475 const char *prefix_name,
1481 notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
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;
1493 /* Look for the exact term */
1494 Xapian::TermIterator i = message->doc.termlist_begin ();
1496 if (i != message->doc.termlist_end () &&
1497 !strcmp ((*i).c_str (), term))
1499 } catch (Xapian::Error &error) {
1500 status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
1509 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1511 notmuch_private_status_t private_status;
1512 notmuch_status_t status;
1514 status = _notmuch_database_ensure_writable (message->notmuch);
1519 return NOTMUCH_STATUS_NULL_POINTER;
1521 if (strlen (tag) > NOTMUCH_TAG_MAX)
1522 return NOTMUCH_STATUS_TAG_TOO_LONG;
1524 private_status = _notmuch_message_add_term (message, "tag", tag);
1525 if (private_status) {
1526 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
1530 if (! message->frozen)
1531 _notmuch_message_sync (message);
1533 return NOTMUCH_STATUS_SUCCESS;
1537 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1539 notmuch_private_status_t private_status;
1540 notmuch_status_t status;
1542 status = _notmuch_database_ensure_writable (message->notmuch);
1547 return NOTMUCH_STATUS_NULL_POINTER;
1549 if (strlen (tag) > NOTMUCH_TAG_MAX)
1550 return NOTMUCH_STATUS_TAG_TOO_LONG;
1552 private_status = _notmuch_message_remove_term (message, "tag", tag);
1553 if (private_status) {
1554 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1558 if (! message->frozen)
1559 _notmuch_message_sync (message);
1561 return NOTMUCH_STATUS_SUCCESS;
1564 /* Is the given filename within a maildir directory?
1566 * Specifically, is the final directory component of 'filename' either
1567 * "cur" or "new". If so, return a pointer to that final directory
1568 * component within 'filename'. If not, return NULL.
1570 * A non-NULL return value is guaranteed to be a valid string pointer
1571 * pointing to the characters "new/" or "cur/", (but not
1575 _filename_is_in_maildir (const char *filename)
1577 const char *slash, *dir = NULL;
1579 /* Find the last '/' separating directory from filename. */
1580 slash = strrchr (filename, '/');
1584 /* Jump back 4 characters to where the previous '/' will be if the
1585 * directory is named "cur" or "new". */
1586 if (slash - filename < 4)
1596 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1597 STRNCMP_LITERAL (dir, "new/") == 0)
1606 _ensure_maildir_flags (notmuch_message_t *message, bool force)
1609 notmuch_filenames_t *filenames;
1610 const char *filename, *dir;
1611 char *combined_flags = talloc_strdup (message, "");
1612 int seen_maildir_info = 0;
1614 if (message->maildir_flags) {
1616 talloc_free (message->maildir_flags);
1617 message->maildir_flags = NULL;
1621 for (filenames = notmuch_message_get_filenames (message);
1622 notmuch_filenames_valid (filenames);
1623 notmuch_filenames_move_to_next (filenames))
1625 filename = notmuch_filenames_get (filenames);
1626 dir = _filename_is_in_maildir (filename);
1631 flags = strstr (filename, ":2,");
1633 seen_maildir_info = 1;
1635 combined_flags = talloc_strdup_append (combined_flags, flags);
1636 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1637 /* Messages are delivered to new/ with no "info" part, but
1638 * they effectively have default maildir flags. According
1639 * to the spec, we should ignore the info part for
1640 * messages in new/, but some MUAs (mutt) can set maildir
1641 * flags on messages in new/, so we're liberal in what we
1643 seen_maildir_info = 1;
1646 if (seen_maildir_info)
1647 message->maildir_flags = combined_flags;
1651 notmuch_message_has_maildir_flag (notmuch_message_t *message, char flag)
1653 _ensure_maildir_flags (message, false);
1654 return message->maildir_flags && (strchr (message->maildir_flags, flag) != NULL);
1658 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1660 notmuch_status_t status;
1663 _ensure_maildir_flags (message, true);
1664 /* If none of the filenames have any maildir info field (not even
1665 * an empty info with no flags set) then there's no information to
1666 * go on, so do nothing. */
1667 if (! message->maildir_flags)
1668 return NOTMUCH_STATUS_SUCCESS;
1670 status = notmuch_message_freeze (message);
1674 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1675 if ((strchr (message->maildir_flags, flag2tag[i].flag) != NULL)
1677 flag2tag[i].inverse)
1679 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1681 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1686 status = notmuch_message_thaw (message);
1691 /* From the set of tags on 'message' and the flag2tag table, compute a
1692 * set of maildir-flag actions to be taken, (flags that should be
1693 * either set or cleared).
1695 * The result is returned as two talloced strings: to_set, and to_clear
1698 _get_maildir_flag_actions (notmuch_message_t *message,
1700 char **to_clear_ret)
1702 char *to_set, *to_clear;
1703 notmuch_tags_t *tags;
1707 to_set = talloc_strdup (message, "");
1708 to_clear = talloc_strdup (message, "");
1710 /* First, find flags for all set tags. */
1711 for (tags = notmuch_message_get_tags (message);
1712 notmuch_tags_valid (tags);
1713 notmuch_tags_move_to_next (tags))
1715 tag = notmuch_tags_get (tags);
1717 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1718 if (strcmp (tag, flag2tag[i].tag) == 0) {
1719 if (flag2tag[i].inverse)
1720 to_clear = talloc_asprintf_append (to_clear,
1724 to_set = talloc_asprintf_append (to_set,
1731 /* Then, find the flags for all tags not present. */
1732 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1733 if (flag2tag[i].inverse) {
1734 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1735 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1737 if (strchr (to_set, flag2tag[i].flag) == NULL)
1738 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1742 *to_set_ret = to_set;
1743 *to_clear_ret = to_clear;
1746 /* Given 'filename' and a set of maildir flags to set and to clear,
1747 * compute the new maildir filename.
1749 * If the existing filename is in the directory "new", the new
1750 * filename will be in the directory "cur", except for the case when
1751 * no flags are changed and the existing filename does not contain
1752 * maildir info (starting with ",2:").
1754 * After a sequence of ":2," in the filename, any subsequent
1755 * single-character flags will be added or removed according to the
1756 * characters in flags_to_set and flags_to_clear. Any existing flags
1757 * not mentioned in either string will remain. The final list of flags
1758 * will be in ASCII order.
1760 * If the original flags seem invalid, (repeated characters or
1761 * non-ASCII ordering of flags), this function will return NULL
1762 * (meaning that renaming would not be safe and should not occur).
1765 _new_maildir_filename (void *ctx,
1766 const char *filename,
1767 const char *flags_to_set,
1768 const char *flags_to_clear)
1770 const char *info, *flags;
1771 unsigned int flag, last_flag;
1772 char *filename_new, *dir;
1774 int flags_in_map = 0;
1775 bool flags_changed = false;
1779 memset (flag_map, 0, sizeof (flag_map));
1781 info = strstr (filename, ":2,");
1784 info = filename + strlen(filename);
1786 /* Loop through existing flags in filename. */
1787 for (flags = info + 3, last_flag = 0;
1789 last_flag = flag, flags++)
1793 /* Original flags not in ASCII order. Abort. */
1794 if (flag < last_flag)
1797 /* Non-ASCII flag. Abort. */
1798 if (flag > sizeof(flag_map) - 1)
1801 /* Repeated flag value. Abort. */
1810 /* Then set and clear our flags from tags. */
1811 for (flags = flags_to_set; *flags; flags++) {
1813 if (flag_map[flag] == 0) {
1816 flags_changed = true;
1820 for (flags = flags_to_clear; *flags; flags++) {
1822 if (flag_map[flag]) {
1825 flags_changed = true;
1829 /* Messages in new/ without maildir info can be kept in new/ if no
1830 * flags have changed. */
1831 dir = (char *) _filename_is_in_maildir (filename);
1832 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && !*info && !flags_changed)
1833 return talloc_strdup (ctx, filename);
1835 filename_new = (char *) talloc_size (ctx,
1837 strlen (":2,") + flags_in_map + 1);
1838 if (unlikely (filename_new == NULL))
1841 strncpy (filename_new, filename, info - filename);
1842 filename_new[info - filename] = '\0';
1844 strcat (filename_new, ":2,");
1846 s = filename_new + strlen (filename_new);
1847 for (i = 0; i < sizeof (flag_map); i++)
1856 /* If message is in new/ move it under cur/. */
1857 dir = (char *) _filename_is_in_maildir (filename_new);
1858 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1859 memcpy (dir, "cur/", 4);
1861 return filename_new;
1865 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1867 notmuch_filenames_t *filenames;
1868 const char *filename;
1870 char *to_set, *to_clear;
1871 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1873 _get_maildir_flag_actions (message, &to_set, &to_clear);
1875 for (filenames = notmuch_message_get_filenames (message);
1876 notmuch_filenames_valid (filenames);
1877 notmuch_filenames_move_to_next (filenames))
1879 filename = notmuch_filenames_get (filenames);
1881 if (! _filename_is_in_maildir (filename))
1884 filename_new = _new_maildir_filename (message, filename,
1886 if (filename_new == NULL)
1889 if (strcmp (filename, filename_new)) {
1891 notmuch_status_t new_status;
1893 err = rename (filename, filename_new);
1897 new_status = _notmuch_message_remove_filename (message,
1899 /* Hold on to only the first error. */
1900 if (! status && new_status
1901 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1902 status = new_status;
1906 new_status = _notmuch_message_add_filename (message,
1908 /* Hold on to only the first error. */
1909 if (! status && new_status) {
1910 status = new_status;
1914 _notmuch_message_sync (message);
1917 talloc_free (filename_new);
1920 talloc_free (to_set);
1921 talloc_free (to_clear);
1927 notmuch_message_remove_all_tags (notmuch_message_t *message)
1929 notmuch_private_status_t private_status;
1930 notmuch_status_t status;
1931 notmuch_tags_t *tags;
1934 status = _notmuch_database_ensure_writable (message->notmuch);
1938 for (tags = notmuch_message_get_tags (message);
1939 notmuch_tags_valid (tags);
1940 notmuch_tags_move_to_next (tags))
1942 tag = notmuch_tags_get (tags);
1944 private_status = _notmuch_message_remove_term (message, "tag", tag);
1945 if (private_status) {
1946 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1951 if (! message->frozen)
1952 _notmuch_message_sync (message);
1955 return NOTMUCH_STATUS_SUCCESS;
1959 notmuch_message_freeze (notmuch_message_t *message)
1961 notmuch_status_t status;
1963 status = _notmuch_database_ensure_writable (message->notmuch);
1969 return NOTMUCH_STATUS_SUCCESS;
1973 notmuch_message_thaw (notmuch_message_t *message)
1975 notmuch_status_t status;
1977 status = _notmuch_database_ensure_writable (message->notmuch);
1981 if (message->frozen > 0) {
1983 if (message->frozen == 0)
1984 _notmuch_message_sync (message);
1985 return NOTMUCH_STATUS_SUCCESS;
1987 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1992 notmuch_message_destroy (notmuch_message_t *message)
1994 talloc_free (message);
1997 notmuch_database_t *
1998 notmuch_message_get_database (const notmuch_message_t *message)
2000 return message->notmuch;
2004 _notmuch_message_ensure_property_map (notmuch_message_t *message)
2006 notmuch_string_node_t *node;
2008 if (message->property_map)
2011 _notmuch_message_ensure_metadata (message, message->property_term_list);
2013 message->property_map = _notmuch_string_map_create (message);
2015 for (node = message->property_term_list->head; node; node = node->next) {
2019 value = strchr(node->string, '=');
2021 INTERNAL_ERROR ("malformed property term");
2027 _notmuch_string_map_append (message->property_map, key, value);
2031 talloc_free (message->property_term_list);
2032 message->property_term_list = NULL;
2035 notmuch_string_map_t *
2036 _notmuch_message_property_map (notmuch_message_t *message)
2038 _notmuch_message_ensure_property_map (message);
2040 return message->property_map;
2044 _notmuch_message_frozen (notmuch_message_t *message)
2046 return message->frozen;
2050 notmuch_message_reindex (notmuch_message_t *message,
2051 notmuch_indexopts_t *indexopts)
2053 notmuch_database_t *notmuch = NULL;
2054 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
2055 notmuch_private_status_t private_status;
2056 notmuch_filenames_t *orig_filenames = NULL;
2057 const char *orig_thread_id = NULL;
2058 notmuch_message_file_t *message_file = NULL;
2062 if (message == NULL)
2063 return NOTMUCH_STATUS_NULL_POINTER;
2065 /* Save in case we need to delete message */
2066 orig_thread_id = notmuch_message_get_thread_id (message);
2067 if (!orig_thread_id) {
2068 /* XXX TODO: make up new error return? */
2069 INTERNAL_ERROR ("message without thread-id");
2072 /* strdup it because the metadata may be invalidated */
2073 orig_thread_id = talloc_strdup (message, orig_thread_id);
2075 notmuch = notmuch_message_get_database (message);
2077 ret = _notmuch_database_ensure_writable (notmuch);
2081 orig_filenames = notmuch_message_get_filenames (message);
2083 private_status = _notmuch_message_remove_indexed_terms (message);
2084 if (private_status) {
2085 ret = COERCE_STATUS(private_status, "error removing terms");
2089 ret = notmuch_message_remove_all_properties_with_prefix (message, "index.");
2091 goto DONE; /* XXX TODO: distinguish from other error returns above? */
2092 if (indexopts && notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE) {
2093 ret = notmuch_message_remove_all_properties (message, "session-key");
2098 /* re-add the filenames with the associated indexopts */
2099 for (; notmuch_filenames_valid (orig_filenames);
2100 notmuch_filenames_move_to_next (orig_filenames)) {
2103 const char *from, *to, *subject;
2104 char *message_id = NULL;
2105 const char *thread_id = NULL;
2107 const char *filename = notmuch_filenames_get (orig_filenames);
2109 message_file = _notmuch_message_file_open (notmuch, filename);
2110 if (message_file == NULL)
2113 ret = _notmuch_message_file_get_headers (message_file,
2114 &from, &subject, &to, &date,
2119 /* XXX TODO: deal with changing message id? */
2121 _notmuch_message_add_filename (message, filename);
2123 ret = _notmuch_database_link_message_to_parents (notmuch, message,
2129 if (thread_id == NULL)
2130 thread_id = orig_thread_id;
2132 _notmuch_message_add_term (message, "thread", thread_id);
2133 /* Take header values only from first filename */
2135 _notmuch_message_set_header_values (message, date, from, subject);
2137 ret = _notmuch_message_index_file (message, indexopts, message_file);
2139 if (ret == NOTMUCH_STATUS_FILE_ERROR)
2145 _notmuch_message_file_close (message_file);
2146 message_file = NULL;
2149 /* put back thread id to help cleanup */
2150 _notmuch_message_add_term (message, "thread", orig_thread_id);
2151 ret = _notmuch_message_delete (message);
2153 _notmuch_message_sync (message);
2158 _notmuch_message_file_close (message_file);
2160 /* XXX TODO destroy orig_filenames? */