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 http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
26 #include <gmime/gmime.h>
28 struct visible _notmuch_message {
29 notmuch_database_t *notmuch;
35 notmuch_string_list_t *tag_list;
36 notmuch_string_list_t *filename_term_list;
37 notmuch_string_list_t *filename_list;
39 notmuch_message_file_t *message_file;
40 notmuch_message_list_t *replies;
42 /* For flags that are initialized on-demand, lazy_flags indicates
43 * if each flag has been initialized. */
44 unsigned long lazy_flags;
47 Xapian::termcount termpos;
50 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
52 struct maildir_flag_tag {
55 notmuch_bool_t inverse;
58 /* ASCII ordered table of Maildir flags and associated tags */
59 static struct maildir_flag_tag flag2tag[] = {
60 { 'D', "draft", FALSE},
61 { 'F', "flagged", FALSE},
62 { 'P', "passed", FALSE},
63 { 'R', "replied", FALSE},
64 { 'S', "unread", TRUE }
67 /* We end up having to call the destructor explicitly because we had
68 * to use "placement new" in order to initialize C++ objects within a
69 * block that we allocated with talloc. So C++ is making talloc
70 * slightly less simple to use, (we wouldn't need
71 * talloc_set_destructor at all otherwise).
74 _notmuch_message_destructor (notmuch_message_t *message)
76 message->doc.~Document ();
81 static notmuch_message_t *
82 _notmuch_message_create_for_document (const void *talloc_owner,
83 notmuch_database_t *notmuch,
86 notmuch_private_status_t *status)
88 notmuch_message_t *message;
91 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
93 message = talloc (talloc_owner, notmuch_message_t);
94 if (unlikely (message == NULL)) {
96 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
100 message->notmuch = notmuch;
101 message->doc_id = doc_id;
105 message->lazy_flags = 0;
107 /* Each of these will be lazily created as needed. */
108 message->message_id = NULL;
109 message->thread_id = NULL;
110 message->in_reply_to = NULL;
111 message->tag_list = NULL;
112 message->filename_term_list = NULL;
113 message->filename_list = NULL;
114 message->message_file = NULL;
115 message->author = NULL;
117 message->replies = _notmuch_message_list_create (message);
118 if (unlikely (message->replies == NULL)) {
120 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
124 /* This is C++'s creepy "placement new", which is really just an
125 * ugly way to call a constructor for a pre-allocated object. So
126 * it's really not an error to not be checking for OUT_OF_MEMORY
127 * here, since this "new" isn't actually allocating memory. This
128 * is language-design comedy of the wrong kind. */
130 new (&message->doc) Xapian::Document;
132 talloc_set_destructor (message, _notmuch_message_destructor);
135 message->termpos = 0;
140 /* Create a new notmuch_message_t object for an existing document in
143 * Here, 'talloc owner' is an optional talloc context to which the new
144 * message will belong. This allows for the caller to not bother
145 * calling notmuch_message_destroy on the message, and know that all
146 * memory will be reclaimed when 'talloc_owner' is freed. The caller
147 * still can call notmuch_message_destroy when finished with the
148 * message if desired.
150 * The 'talloc_owner' argument can also be NULL, in which case the
151 * caller *is* responsible for calling notmuch_message_destroy.
153 * If no document exists in the database with document ID of 'doc_id'
154 * then this function returns NULL and optionally sets *status to
155 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
157 * This function can also fail to due lack of available memory,
158 * returning NULL and optionally setting *status to
159 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
161 * The caller can pass NULL for status if uninterested in
162 * distinguishing these two cases.
165 _notmuch_message_create (const void *talloc_owner,
166 notmuch_database_t *notmuch,
168 notmuch_private_status_t *status)
170 Xapian::Document doc;
173 doc = notmuch->xapian_db->get_document (doc_id);
174 } catch (const Xapian::DocNotFoundError &error) {
176 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
180 return _notmuch_message_create_for_document (talloc_owner, notmuch,
181 doc_id, doc, status);
184 /* Create a new notmuch_message_t object for a specific message ID,
185 * (which may or may not already exist in the database).
187 * The 'notmuch' database will be the talloc owner of the returned
190 * This function returns a valid notmuch_message_t whether or not
191 * there is already a document in the database with the given message
192 * ID. These two cases can be distinguished by the value of *status:
195 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
197 * There is already a document with message ID 'message_id' in the
198 * database. The returned message can be used to query/modify the
199 * document. The message may be a ghost message.
201 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
203 * No document with 'message_id' exists in the database. The
204 * returned message contains a newly created document (not yet
205 * added to the database) and a document ID that is known not to
206 * exist in the database. This message is "blank"; that is, it
207 * contains only a message ID and no other metadata. The caller
208 * can modify the message, and a call to _notmuch_message_sync
209 * will add the document to the database.
211 * If an error occurs, this function will return NULL and *status
212 * will be set as appropriate. (The status pointer argument must
216 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
217 const char *message_id,
218 notmuch_private_status_t *status_ret)
220 notmuch_message_t *message;
221 Xapian::Document doc;
225 *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
229 return talloc_steal (notmuch, message);
230 else if (*status_ret)
233 /* If the message ID is too long, substitute its sha1 instead. */
234 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
235 message_id = _notmuch_message_id_compressed (message, message_id);
237 term = talloc_asprintf (NULL, "%s%s",
238 _find_prefix ("id"), message_id);
240 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
244 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
245 INTERNAL_ERROR ("Failure to ensure database is writable.");
248 doc.add_term (term, 0);
251 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
253 doc_id = _notmuch_database_generate_doc_id (notmuch);
254 } catch (const Xapian::Error &error) {
255 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
256 error.get_msg().c_str());
257 notmuch->exception_reported = TRUE;
258 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
262 message = _notmuch_message_create_for_document (notmuch, notmuch,
263 doc_id, doc, status_ret);
265 /* We want to inform the caller that we had to create a new
267 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
268 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
274 _notmuch_message_get_term (notmuch_message_t *message,
275 Xapian::TermIterator &i, Xapian::TermIterator &end,
278 int prefix_len = strlen (prefix);
286 std::string term = *i;
287 if (strncmp (term.c_str(), prefix, prefix_len))
290 value = talloc_strdup (message, term.c_str() + prefix_len);
292 #if DEBUG_DATABASE_SANITY
295 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
296 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
297 message->doc_id, prefix, value,
298 (*i).c_str () + prefix_len);
306 _notmuch_message_ensure_metadata (notmuch_message_t *message)
308 Xapian::TermIterator i, end;
309 const char *thread_prefix = _find_prefix ("thread"),
310 *tag_prefix = _find_prefix ("tag"),
311 *id_prefix = _find_prefix ("id"),
312 *type_prefix = _find_prefix ("type"),
313 *filename_prefix = _find_prefix ("file-direntry"),
314 *replyto_prefix = _find_prefix ("replyto");
316 /* We do this all in a single pass because Xapian decompresses the
317 * term list every time you iterate over it. Thus, while this is
318 * slightly more costly than looking up individual fields if only
319 * one field of the message object is actually used, it's a huge
320 * win as more fields are used. */
322 i = message->doc.termlist_begin ();
323 end = message->doc.termlist_end ();
326 if (!message->thread_id)
328 _notmuch_message_get_term (message, i, end, thread_prefix);
331 assert (strcmp (thread_prefix, tag_prefix) < 0);
332 if (!message->tag_list) {
334 _notmuch_database_get_terms_with_prefix (message, i, end,
336 _notmuch_string_list_sort (message->tag_list);
340 assert (strcmp (tag_prefix, id_prefix) < 0);
341 if (!message->message_id)
342 message->message_id =
343 _notmuch_message_get_term (message, i, end, id_prefix);
345 /* Get document type */
346 assert (strcmp (id_prefix, type_prefix) < 0);
347 if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {
348 i.skip_to (type_prefix);
349 /* "T" is the prefix "type" fields. See
350 * BOOLEAN_PREFIX_INTERNAL. */
352 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
353 else if (*i == "Tghost")
354 NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
356 INTERNAL_ERROR ("Message without type term");
357 NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
360 /* Get filename list. Here we get only the terms. We lazily
361 * expand them to full file names when needed in
362 * _notmuch_message_ensure_filename_list. */
363 assert (strcmp (type_prefix, filename_prefix) < 0);
364 if (!message->filename_term_list && !message->filename_list)
365 message->filename_term_list =
366 _notmuch_database_get_terms_with_prefix (message, i, end,
370 assert (strcmp (filename_prefix, replyto_prefix) < 0);
371 if (!message->in_reply_to)
372 message->in_reply_to =
373 _notmuch_message_get_term (message, i, end, replyto_prefix);
374 /* It's perfectly valid for a message to have no In-Reply-To
375 * header. For these cases, we return an empty string. */
376 if (!message->in_reply_to)
377 message->in_reply_to = talloc_strdup (message, "");
381 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
382 const char *prefix_name)
384 if (strcmp ("thread", prefix_name) == 0) {
385 talloc_free (message->thread_id);
386 message->thread_id = NULL;
389 if (strcmp ("tag", prefix_name) == 0) {
390 talloc_unlink (message, message->tag_list);
391 message->tag_list = NULL;
394 if (strcmp ("type", prefix_name) == 0) {
395 NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
396 NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
399 if (strcmp ("file-direntry", prefix_name) == 0) {
400 talloc_free (message->filename_term_list);
401 talloc_free (message->filename_list);
402 message->filename_term_list = message->filename_list = NULL;
405 if (strcmp ("replyto", prefix_name) == 0) {
406 talloc_free (message->in_reply_to);
407 message->in_reply_to = NULL;
412 _notmuch_message_get_doc_id (notmuch_message_t *message)
414 return message->doc_id;
418 notmuch_message_get_message_id (notmuch_message_t *message)
420 if (!message->message_id)
421 _notmuch_message_ensure_metadata (message);
422 if (!message->message_id)
423 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
425 return message->message_id;
429 _notmuch_message_ensure_message_file (notmuch_message_t *message)
431 const char *filename;
433 if (message->message_file)
436 filename = notmuch_message_get_filename (message);
437 if (unlikely (filename == NULL))
440 message->message_file = _notmuch_message_file_open_ctx (message, filename);
444 notmuch_message_get_header (notmuch_message_t *message, const char *header)
446 Xapian::valueno slot = Xapian::BAD_VALUENO;
448 /* Fetch header from the appropriate xapian value field if
450 if (strcasecmp (header, "from") == 0)
451 slot = NOTMUCH_VALUE_FROM;
452 else if (strcasecmp (header, "subject") == 0)
453 slot = NOTMUCH_VALUE_SUBJECT;
454 else if (strcasecmp (header, "message-id") == 0)
455 slot = NOTMUCH_VALUE_MESSAGE_ID;
457 if (slot != Xapian::BAD_VALUENO) {
459 std::string value = message->doc.get_value (slot);
461 /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then
462 * empty values indicate empty headers. If we don't, then
463 * it could just mean we didn't record the header. */
464 if ((message->notmuch->features &
465 NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) ||
467 return talloc_strdup (message, value.c_str ());
469 } catch (Xapian::Error &error) {
470 fprintf (stderr, "A Xapian exception occurred when reading header: %s\n",
471 error.get_msg().c_str());
472 message->notmuch->exception_reported = TRUE;
477 /* Otherwise fall back to parsing the file */
478 _notmuch_message_ensure_message_file (message);
479 if (message->message_file == NULL)
482 return _notmuch_message_file_get_header (message->message_file, header);
485 /* Return the message ID from the In-Reply-To header of 'message'.
487 * Returns an empty string ("") if 'message' has no In-Reply-To
490 * Returns NULL if any error occurs.
493 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
495 if (!message->in_reply_to)
496 _notmuch_message_ensure_metadata (message);
497 return message->in_reply_to;
501 notmuch_message_get_thread_id (notmuch_message_t *message)
503 if (!message->thread_id)
504 _notmuch_message_ensure_metadata (message);
505 if (!message->thread_id)
506 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
508 return message->thread_id;
512 _notmuch_message_add_reply (notmuch_message_t *message,
513 notmuch_message_t *reply)
515 _notmuch_message_list_add_message (message->replies, reply);
519 notmuch_message_get_replies (notmuch_message_t *message)
521 return _notmuch_messages_create (message->replies);
525 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
527 Xapian::TermIterator i;
528 size_t prefix_len = strlen (prefix);
531 i = message->doc.termlist_begin ();
534 /* Terminate loop when no terms remain with desired prefix. */
535 if (i == message->doc.termlist_end () ||
536 strncmp ((*i).c_str (), prefix, prefix_len))
540 message->doc.remove_term ((*i));
541 } catch (const Xapian::InvalidArgumentError) {
542 /* Ignore failure to remove non-existent term. */
547 /* Return true if p points at "new" or "cur". */
548 static bool is_maildir (const char *p)
550 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
553 /* Add "folder:" term for directory. */
554 static notmuch_status_t
555 _notmuch_message_add_folder_terms (notmuch_message_t *message,
556 const char *directory)
560 folder = talloc_strdup (NULL, directory);
562 return NOTMUCH_STATUS_OUT_OF_MEMORY;
565 * If the message file is in a leaf directory named "new" or
566 * "cur", presume maildir and index the parent directory. Thus a
567 * "folder:" prefix search matches messages in the specified
568 * maildir folder, i.e. in the specified directory and its "new"
569 * and "cur" subdirectories.
571 * Note that this means the "folder:" prefix can't be used for
572 * distinguishing between message files in "new" or "cur". The
573 * "path:" prefix needs to be used for that.
575 * Note the deliberate difference to _filename_is_in_maildir(). We
576 * don't want to index different things depending on the existence
577 * or non-existence of all maildir sibling directories "new",
578 * "cur", and "tmp". Doing so would be surprising, and difficult
579 * for the user to fix in case all subdirectories were not in
580 * place during indexing.
582 last = strrchr (folder, '/');
584 if (is_maildir (last + 1))
586 } else if (is_maildir (folder)) {
590 _notmuch_message_add_term (message, "folder", folder);
592 talloc_free (folder);
594 return NOTMUCH_STATUS_SUCCESS;
597 #define RECURSIVE_SUFFIX "/**"
599 /* Add "path:" terms for directory. */
600 static notmuch_status_t
601 _notmuch_message_add_path_terms (notmuch_message_t *message,
602 const char *directory)
604 /* Add exact "path:" term. */
605 _notmuch_message_add_term (message, "path", directory);
607 if (strlen (directory)) {
610 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
612 return NOTMUCH_STATUS_OUT_OF_MEMORY;
614 /* Add recursive "path:" terms for directory and all parents. */
615 for (p = path + strlen (path) - 1; p > path; p--) {
617 strcpy (p, RECURSIVE_SUFFIX);
618 _notmuch_message_add_term (message, "path", path);
625 /* Recursive all-matching path:** for consistency. */
626 _notmuch_message_add_term (message, "path", "**");
628 return NOTMUCH_STATUS_SUCCESS;
631 /* Add directory based terms for all filenames of the message. */
632 static notmuch_status_t
633 _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
635 const char *direntry_prefix = _find_prefix ("file-direntry");
636 int direntry_prefix_len = strlen (direntry_prefix);
637 Xapian::TermIterator i = message->doc.termlist_begin ();
638 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
640 for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {
641 unsigned int directory_id;
642 const char *direntry, *directory;
645 /* Terminate loop at first term without desired prefix. */
646 if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))
649 /* Indicate that there are filenames remaining. */
650 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
652 direntry = (*i).c_str ();
653 direntry += direntry_prefix_len;
655 directory_id = strtol (direntry, &colon, 10);
657 if (colon == NULL || *colon != ':')
658 INTERNAL_ERROR ("malformed direntry");
660 directory = _notmuch_database_get_directory_path (ctx,
664 _notmuch_message_add_folder_terms (message, directory);
665 _notmuch_message_add_path_terms (message, directory);
671 /* Add an additional 'filename' for 'message'.
673 * This change will not be reflected in the database until the next
674 * call to _notmuch_message_sync. */
676 _notmuch_message_add_filename (notmuch_message_t *message,
677 const char *filename)
679 const char *relative, *directory;
680 notmuch_status_t status;
681 void *local = talloc_new (message);
684 if (filename == NULL)
685 INTERNAL_ERROR ("Message filename cannot be NULL.");
687 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
688 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
689 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
691 relative = _notmuch_database_relative_path (message->notmuch, filename);
693 status = _notmuch_database_split_path (local, relative, &directory, NULL);
697 status = _notmuch_database_filename_to_direntry (
698 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
702 /* New file-direntry allows navigating to this message with
703 * notmuch_directory_get_child_files() . */
704 _notmuch_message_add_term (message, "file-direntry", direntry);
706 _notmuch_message_add_folder_terms (message, directory);
707 _notmuch_message_add_path_terms (message, directory);
711 return NOTMUCH_STATUS_SUCCESS;
714 /* Remove a particular 'filename' from 'message'.
716 * This change will not be reflected in the database until the next
717 * call to _notmuch_message_sync.
719 * If this message still has other filenames, returns
720 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
722 * Note: This function does not remove a document from the database,
723 * even if the specified filename is the only filename for this
724 * message. For that functionality, see
725 * _notmuch_database_remove_message. */
727 _notmuch_message_remove_filename (notmuch_message_t *message,
728 const char *filename)
730 void *local = talloc_new (message);
732 notmuch_private_status_t private_status;
733 notmuch_status_t status;
735 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
736 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
737 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
739 status = _notmuch_database_filename_to_direntry (
740 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
741 if (status || !direntry)
744 /* Unlink this file from its parent directory. */
745 private_status = _notmuch_message_remove_term (message,
746 "file-direntry", direntry);
747 status = COERCE_STATUS (private_status,
748 "Unexpected error from _notmuch_message_remove_term");
752 /* Re-synchronize "folder:" and "path:" terms for this message. */
754 /* Remove all "folder:" terms. */
755 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
757 /* Remove all "path:" terms. */
758 _notmuch_message_remove_terms (message, _find_prefix ("path"));
760 /* Add back terms for all remaining filenames of the message. */
761 status = _notmuch_message_add_directory_terms (local, message);
768 /* Upgrade the "folder:" prefix from V1 to V2. */
769 #define FOLDER_PREFIX_V1 "XFOLDER"
770 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
772 _notmuch_message_upgrade_folder (notmuch_message_t *message)
774 /* Remove all old "folder:" terms. */
775 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
777 /* Remove all old "folder:" stemmed terms. */
778 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
780 /* Add new boolean "folder:" and "path:" terms. */
781 _notmuch_message_add_directory_terms (message, message);
785 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
787 return talloc_strdup (message, message->doc.get_data ().c_str ());
791 _notmuch_message_clear_data (notmuch_message_t *message)
793 message->doc.set_data ("");
797 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
799 notmuch_string_node_t *node;
801 if (message->filename_list)
804 if (!message->filename_term_list)
805 _notmuch_message_ensure_metadata (message);
807 message->filename_list = _notmuch_string_list_create (message);
808 node = message->filename_term_list->head;
811 /* A message document created by an old version of notmuch
812 * (prior to rename support) will have the filename in the
813 * data of the document rather than as a file-direntry term.
815 * It would be nice to do the upgrade of the document directly
816 * here, but the database is likely open in read-only mode. */
819 data = message->doc.get_data ().c_str ();
822 INTERNAL_ERROR ("message with no filename");
824 _notmuch_string_list_append (message->filename_list, data);
829 for (; node; node = node->next) {
830 void *local = talloc_new (message);
831 const char *db_path, *directory, *basename, *filename;
832 char *colon, *direntry = NULL;
833 unsigned int directory_id;
835 direntry = node->string;
837 directory_id = strtol (direntry, &colon, 10);
839 if (colon == NULL || *colon != ':')
840 INTERNAL_ERROR ("malformed direntry");
842 basename = colon + 1;
846 db_path = notmuch_database_get_path (message->notmuch);
848 directory = _notmuch_database_get_directory_path (local,
852 if (strlen (directory))
853 filename = talloc_asprintf (message, "%s/%s/%s",
854 db_path, directory, basename);
856 filename = talloc_asprintf (message, "%s/%s",
859 _notmuch_string_list_append (message->filename_list, filename);
864 talloc_free (message->filename_term_list);
865 message->filename_term_list = NULL;
869 notmuch_message_get_filename (notmuch_message_t *message)
871 _notmuch_message_ensure_filename_list (message);
873 if (message->filename_list == NULL)
876 if (message->filename_list->head == NULL ||
877 message->filename_list->head->string == NULL)
879 INTERNAL_ERROR ("message with no filename");
882 return message->filename_list->head->string;
885 notmuch_filenames_t *
886 notmuch_message_get_filenames (notmuch_message_t *message)
888 _notmuch_message_ensure_filename_list (message);
890 return _notmuch_filenames_create (message, message->filename_list);
894 notmuch_message_get_flag (notmuch_message_t *message,
895 notmuch_message_flag_t flag)
897 if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
898 ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
899 _notmuch_message_ensure_metadata (message);
901 return NOTMUCH_TEST_BIT (message->flags, flag);
905 notmuch_message_set_flag (notmuch_message_t *message,
906 notmuch_message_flag_t flag, notmuch_bool_t enable)
909 NOTMUCH_SET_BIT (&message->flags, flag);
911 NOTMUCH_CLEAR_BIT (&message->flags, flag);
912 NOTMUCH_SET_BIT (&message->lazy_flags, flag);
916 notmuch_message_get_date (notmuch_message_t *message)
921 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
922 } catch (Xapian::Error &error) {
923 fprintf (stderr, "A Xapian exception occurred when reading date: %s\n",
924 error.get_msg().c_str());
925 message->notmuch->exception_reported = TRUE;
930 /* sortable_unserialise is undefined on empty string */
932 return Xapian::sortable_unserialise (value);
936 notmuch_message_get_tags (notmuch_message_t *message)
938 notmuch_tags_t *tags;
940 if (!message->tag_list)
941 _notmuch_message_ensure_metadata (message);
943 tags = _notmuch_tags_create (message, message->tag_list);
944 /* _notmuch_tags_create steals the reference to the tag_list, but
945 * in this case it's still used by the message, so we add an
946 * *additional* talloc reference to the list. As a result, it's
947 * possible to modify the message tags (which talloc_unlink's the
948 * current list from the message) while still iterating because
949 * the iterator will keep the current list alive. */
950 if (!talloc_reference (message, message->tag_list))
957 _notmuch_message_get_author (notmuch_message_t *message)
959 return message->author;
963 _notmuch_message_set_author (notmuch_message_t *message,
967 talloc_free(message->author);
968 message->author = talloc_strdup(message, author);
973 _notmuch_message_set_header_values (notmuch_message_t *message,
980 /* GMime really doesn't want to see a NULL date, so protect its
982 if (date == NULL || *date == '\0')
985 time_value = g_mime_utils_header_decode_date (date, NULL);
987 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
988 Xapian::sortable_serialise (time_value));
989 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
990 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
993 /* Synchronize changes made to message->doc out into the database. */
995 _notmuch_message_sync (notmuch_message_t *message)
997 Xapian::WritableDatabase *db;
999 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
1002 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
1003 db->replace_document (message->doc_id, message->doc);
1006 /* Delete a message document from the database. */
1008 _notmuch_message_delete (notmuch_message_t *message)
1010 notmuch_status_t status;
1011 Xapian::WritableDatabase *db;
1013 status = _notmuch_database_ensure_writable (message->notmuch);
1017 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
1018 db->delete_document (message->doc_id);
1019 return NOTMUCH_STATUS_SUCCESS;
1022 /* Transform a blank message into a ghost message. The caller must
1023 * _notmuch_message_sync the message. */
1024 notmuch_private_status_t
1025 _notmuch_message_initialize_ghost (notmuch_message_t *message,
1026 const char *thread_id)
1028 notmuch_private_status_t status;
1030 status = _notmuch_message_add_term (message, "type", "ghost");
1033 status = _notmuch_message_add_term (message, "thread", thread_id);
1037 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1040 /* Ensure that 'message' is not holding any file object open. Future
1041 * calls to various functions will still automatically open the
1042 * message file as needed.
1045 _notmuch_message_close (notmuch_message_t *message)
1047 if (message->message_file) {
1048 _notmuch_message_file_close (message->message_file);
1049 message->message_file = NULL;
1053 /* Add a name:value term to 'message', (the actual term will be
1054 * encoded by prefixing the value with a short prefix). See
1055 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1056 * names to prefix values.
1058 * This change will not be reflected in the database until the next
1059 * call to _notmuch_message_sync. */
1060 notmuch_private_status_t
1061 _notmuch_message_add_term (notmuch_message_t *message,
1062 const char *prefix_name,
1069 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1071 term = talloc_asprintf (message, "%s%s",
1072 _find_prefix (prefix_name), value);
1074 if (strlen (term) > NOTMUCH_TERM_MAX)
1075 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1077 message->doc.add_term (term, 0);
1081 _notmuch_message_invalidate_metadata (message, prefix_name);
1083 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1086 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1087 * term will be added both prefixed (if prefix_name is not NULL) and
1088 * also non-prefixed). */
1089 notmuch_private_status_t
1090 _notmuch_message_gen_terms (notmuch_message_t *message,
1091 const char *prefix_name,
1094 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1097 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1099 term_gen->set_document (message->doc);
1102 const char *prefix = _find_prefix (prefix_name);
1104 term_gen->set_termpos (message->termpos);
1105 term_gen->index_text (text, 1, prefix);
1106 /* Create a gap between this an the next terms so they don't
1107 * appear to be a phrase. */
1108 message->termpos = term_gen->get_termpos () + 100;
1110 _notmuch_message_invalidate_metadata (message, prefix_name);
1113 term_gen->set_termpos (message->termpos);
1114 term_gen->index_text (text);
1115 /* Create a term gap, as above. */
1116 message->termpos = term_gen->get_termpos () + 100;
1118 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1121 /* Remove a name:value term from 'message', (the actual term will be
1122 * encoded by prefixing the value with a short prefix). See
1123 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1124 * names to prefix values.
1126 * This change will not be reflected in the database until the next
1127 * call to _notmuch_message_sync. */
1128 notmuch_private_status_t
1129 _notmuch_message_remove_term (notmuch_message_t *message,
1130 const char *prefix_name,
1136 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1138 term = talloc_asprintf (message, "%s%s",
1139 _find_prefix (prefix_name), value);
1141 if (strlen (term) > NOTMUCH_TERM_MAX)
1142 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1145 message->doc.remove_term (term);
1146 } catch (const Xapian::InvalidArgumentError) {
1147 /* We'll let the philosopher's try to wrestle with the
1148 * question of whether failing to remove that which was not
1149 * there in the first place is failure. For us, we'll silently
1150 * consider it all good. */
1155 _notmuch_message_invalidate_metadata (message, prefix_name);
1157 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1161 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1163 notmuch_private_status_t private_status;
1164 notmuch_status_t status;
1166 status = _notmuch_database_ensure_writable (message->notmuch);
1171 return NOTMUCH_STATUS_NULL_POINTER;
1173 if (strlen (tag) > NOTMUCH_TAG_MAX)
1174 return NOTMUCH_STATUS_TAG_TOO_LONG;
1176 private_status = _notmuch_message_add_term (message, "tag", tag);
1177 if (private_status) {
1178 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
1182 if (! message->frozen)
1183 _notmuch_message_sync (message);
1185 return NOTMUCH_STATUS_SUCCESS;
1189 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1191 notmuch_private_status_t private_status;
1192 notmuch_status_t status;
1194 status = _notmuch_database_ensure_writable (message->notmuch);
1199 return NOTMUCH_STATUS_NULL_POINTER;
1201 if (strlen (tag) > NOTMUCH_TAG_MAX)
1202 return NOTMUCH_STATUS_TAG_TOO_LONG;
1204 private_status = _notmuch_message_remove_term (message, "tag", tag);
1205 if (private_status) {
1206 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1210 if (! message->frozen)
1211 _notmuch_message_sync (message);
1213 return NOTMUCH_STATUS_SUCCESS;
1216 /* Is the given filename within a maildir directory?
1218 * Specifically, is the final directory component of 'filename' either
1219 * "cur" or "new". If so, return a pointer to that final directory
1220 * component within 'filename'. If not, return NULL.
1222 * A non-NULL return value is guaranteed to be a valid string pointer
1223 * pointing to the characters "new/" or "cur/", (but not
1227 _filename_is_in_maildir (const char *filename)
1229 const char *slash, *dir = NULL;
1231 /* Find the last '/' separating directory from filename. */
1232 slash = strrchr (filename, '/');
1236 /* Jump back 4 characters to where the previous '/' will be if the
1237 * directory is named "cur" or "new". */
1238 if (slash - filename < 4)
1248 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1249 STRNCMP_LITERAL (dir, "new/") == 0)
1258 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1261 notmuch_status_t status;
1262 notmuch_filenames_t *filenames;
1263 const char *filename, *dir;
1264 char *combined_flags = talloc_strdup (message, "");
1266 int seen_maildir_info = 0;
1268 for (filenames = notmuch_message_get_filenames (message);
1269 notmuch_filenames_valid (filenames);
1270 notmuch_filenames_move_to_next (filenames))
1272 filename = notmuch_filenames_get (filenames);
1273 dir = _filename_is_in_maildir (filename);
1278 flags = strstr (filename, ":2,");
1280 seen_maildir_info = 1;
1282 combined_flags = talloc_strdup_append (combined_flags, flags);
1283 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1284 /* Messages are delivered to new/ with no "info" part, but
1285 * they effectively have default maildir flags. According
1286 * to the spec, we should ignore the info part for
1287 * messages in new/, but some MUAs (mutt) can set maildir
1288 * flags on messages in new/, so we're liberal in what we
1290 seen_maildir_info = 1;
1294 /* If none of the filenames have any maildir info field (not even
1295 * an empty info with no flags set) then there's no information to
1296 * go on, so do nothing. */
1297 if (! seen_maildir_info)
1298 return NOTMUCH_STATUS_SUCCESS;
1300 status = notmuch_message_freeze (message);
1304 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1305 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
1307 flag2tag[i].inverse)
1309 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1311 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1316 status = notmuch_message_thaw (message);
1318 talloc_free (combined_flags);
1323 /* From the set of tags on 'message' and the flag2tag table, compute a
1324 * set of maildir-flag actions to be taken, (flags that should be
1325 * either set or cleared).
1327 * The result is returned as two talloced strings: to_set, and to_clear
1330 _get_maildir_flag_actions (notmuch_message_t *message,
1332 char **to_clear_ret)
1334 char *to_set, *to_clear;
1335 notmuch_tags_t *tags;
1339 to_set = talloc_strdup (message, "");
1340 to_clear = talloc_strdup (message, "");
1342 /* First, find flags for all set tags. */
1343 for (tags = notmuch_message_get_tags (message);
1344 notmuch_tags_valid (tags);
1345 notmuch_tags_move_to_next (tags))
1347 tag = notmuch_tags_get (tags);
1349 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1350 if (strcmp (tag, flag2tag[i].tag) == 0) {
1351 if (flag2tag[i].inverse)
1352 to_clear = talloc_asprintf_append (to_clear,
1356 to_set = talloc_asprintf_append (to_set,
1363 /* Then, find the flags for all tags not present. */
1364 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1365 if (flag2tag[i].inverse) {
1366 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1367 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1369 if (strchr (to_set, flag2tag[i].flag) == NULL)
1370 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1374 *to_set_ret = to_set;
1375 *to_clear_ret = to_clear;
1378 /* Given 'filename' and a set of maildir flags to set and to clear,
1379 * compute the new maildir filename.
1381 * If the existing filename is in the directory "new", the new
1382 * filename will be in the directory "cur", except for the case when
1383 * no flags are changed and the existing filename does not contain
1384 * maildir info (starting with ",2:").
1386 * After a sequence of ":2," in the filename, any subsequent
1387 * single-character flags will be added or removed according to the
1388 * characters in flags_to_set and flags_to_clear. Any existing flags
1389 * not mentioned in either string will remain. The final list of flags
1390 * will be in ASCII order.
1392 * If the original flags seem invalid, (repeated characters or
1393 * non-ASCII ordering of flags), this function will return NULL
1394 * (meaning that renaming would not be safe and should not occur).
1397 _new_maildir_filename (void *ctx,
1398 const char *filename,
1399 const char *flags_to_set,
1400 const char *flags_to_clear)
1402 const char *info, *flags;
1403 unsigned int flag, last_flag;
1404 char *filename_new, *dir;
1406 int flags_in_map = 0;
1407 notmuch_bool_t flags_changed = FALSE;
1411 memset (flag_map, 0, sizeof (flag_map));
1413 info = strstr (filename, ":2,");
1416 info = filename + strlen(filename);
1418 /* Loop through existing flags in filename. */
1419 for (flags = info + 3, last_flag = 0;
1421 last_flag = flag, flags++)
1425 /* Original flags not in ASCII order. Abort. */
1426 if (flag < last_flag)
1429 /* Non-ASCII flag. Abort. */
1430 if (flag > sizeof(flag_map) - 1)
1433 /* Repeated flag value. Abort. */
1442 /* Then set and clear our flags from tags. */
1443 for (flags = flags_to_set; *flags; flags++) {
1445 if (flag_map[flag] == 0) {
1448 flags_changed = TRUE;
1452 for (flags = flags_to_clear; *flags; flags++) {
1454 if (flag_map[flag]) {
1457 flags_changed = TRUE;
1461 /* Messages in new/ without maildir info can be kept in new/ if no
1462 * flags have changed. */
1463 dir = (char *) _filename_is_in_maildir (filename);
1464 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && !*info && !flags_changed)
1465 return talloc_strdup (ctx, filename);
1467 filename_new = (char *) talloc_size (ctx,
1469 strlen (":2,") + flags_in_map + 1);
1470 if (unlikely (filename_new == NULL))
1473 strncpy (filename_new, filename, info - filename);
1474 filename_new[info - filename] = '\0';
1476 strcat (filename_new, ":2,");
1478 s = filename_new + strlen (filename_new);
1479 for (i = 0; i < sizeof (flag_map); i++)
1488 /* If message is in new/ move it under cur/. */
1489 dir = (char *) _filename_is_in_maildir (filename_new);
1490 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1491 memcpy (dir, "cur/", 4);
1493 return filename_new;
1497 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1499 notmuch_filenames_t *filenames;
1500 const char *filename;
1502 char *to_set, *to_clear;
1503 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1505 _get_maildir_flag_actions (message, &to_set, &to_clear);
1507 for (filenames = notmuch_message_get_filenames (message);
1508 notmuch_filenames_valid (filenames);
1509 notmuch_filenames_move_to_next (filenames))
1511 filename = notmuch_filenames_get (filenames);
1513 if (! _filename_is_in_maildir (filename))
1516 filename_new = _new_maildir_filename (message, filename,
1518 if (filename_new == NULL)
1521 if (strcmp (filename, filename_new)) {
1523 notmuch_status_t new_status;
1525 err = rename (filename, filename_new);
1529 new_status = _notmuch_message_remove_filename (message,
1531 /* Hold on to only the first error. */
1532 if (! status && new_status
1533 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1534 status = new_status;
1538 new_status = _notmuch_message_add_filename (message,
1540 /* Hold on to only the first error. */
1541 if (! status && new_status) {
1542 status = new_status;
1546 _notmuch_message_sync (message);
1549 talloc_free (filename_new);
1552 talloc_free (to_set);
1553 talloc_free (to_clear);
1559 notmuch_message_remove_all_tags (notmuch_message_t *message)
1561 notmuch_private_status_t private_status;
1562 notmuch_status_t status;
1563 notmuch_tags_t *tags;
1566 status = _notmuch_database_ensure_writable (message->notmuch);
1570 for (tags = notmuch_message_get_tags (message);
1571 notmuch_tags_valid (tags);
1572 notmuch_tags_move_to_next (tags))
1574 tag = notmuch_tags_get (tags);
1576 private_status = _notmuch_message_remove_term (message, "tag", tag);
1577 if (private_status) {
1578 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1583 if (! message->frozen)
1584 _notmuch_message_sync (message);
1587 return NOTMUCH_STATUS_SUCCESS;
1591 notmuch_message_freeze (notmuch_message_t *message)
1593 notmuch_status_t status;
1595 status = _notmuch_database_ensure_writable (message->notmuch);
1601 return NOTMUCH_STATUS_SUCCESS;
1605 notmuch_message_thaw (notmuch_message_t *message)
1607 notmuch_status_t status;
1609 status = _notmuch_database_ensure_writable (message->notmuch);
1613 if (message->frozen > 0) {
1615 if (message->frozen == 0)
1616 _notmuch_message_sync (message);
1617 return NOTMUCH_STATUS_SUCCESS;
1619 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1624 notmuch_message_destroy (notmuch_message_t *message)
1626 talloc_free (message);