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;
44 Xapian::termcount termpos;
47 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
49 struct maildir_flag_tag {
52 notmuch_bool_t inverse;
55 /* ASCII ordered table of Maildir flags and associated tags */
56 static struct maildir_flag_tag flag2tag[] = {
57 { 'D', "draft", FALSE},
58 { 'F', "flagged", FALSE},
59 { 'P', "passed", FALSE},
60 { 'R', "replied", FALSE},
61 { 'S', "unread", TRUE }
64 /* We end up having to call the destructor explicitly because we had
65 * to use "placement new" in order to initialize C++ objects within a
66 * block that we allocated with talloc. So C++ is making talloc
67 * slightly less simple to use, (we wouldn't need
68 * talloc_set_destructor at all otherwise).
71 _notmuch_message_destructor (notmuch_message_t *message)
73 message->doc.~Document ();
78 static notmuch_message_t *
79 _notmuch_message_create_for_document (const void *talloc_owner,
80 notmuch_database_t *notmuch,
83 notmuch_private_status_t *status)
85 notmuch_message_t *message;
88 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
90 message = talloc (talloc_owner, notmuch_message_t);
91 if (unlikely (message == NULL)) {
93 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
97 message->notmuch = notmuch;
98 message->doc_id = doc_id;
103 /* Each of these will be lazily created as needed. */
104 message->message_id = NULL;
105 message->thread_id = NULL;
106 message->in_reply_to = NULL;
107 message->tag_list = NULL;
108 message->filename_term_list = NULL;
109 message->filename_list = NULL;
110 message->message_file = NULL;
111 message->author = NULL;
113 message->replies = _notmuch_message_list_create (message);
114 if (unlikely (message->replies == NULL)) {
116 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
120 /* This is C++'s creepy "placement new", which is really just an
121 * ugly way to call a constructor for a pre-allocated object. So
122 * it's really not an error to not be checking for OUT_OF_MEMORY
123 * here, since this "new" isn't actually allocating memory. This
124 * is language-design comedy of the wrong kind. */
126 new (&message->doc) Xapian::Document;
128 talloc_set_destructor (message, _notmuch_message_destructor);
131 message->termpos = 0;
136 /* Create a new notmuch_message_t object for an existing document in
139 * Here, 'talloc owner' is an optional talloc context to which the new
140 * message will belong. This allows for the caller to not bother
141 * calling notmuch_message_destroy on the message, and know that all
142 * memory will be reclaimed when 'talloc_owner' is freed. The caller
143 * still can call notmuch_message_destroy when finished with the
144 * message if desired.
146 * The 'talloc_owner' argument can also be NULL, in which case the
147 * caller *is* responsible for calling notmuch_message_destroy.
149 * If no document exists in the database with document ID of 'doc_id'
150 * then this function returns NULL and optionally sets *status to
151 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
153 * This function can also fail to due lack of available memory,
154 * returning NULL and optionally setting *status to
155 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
157 * The caller can pass NULL for status if uninterested in
158 * distinguishing these two cases.
161 _notmuch_message_create (const void *talloc_owner,
162 notmuch_database_t *notmuch,
164 notmuch_private_status_t *status)
166 Xapian::Document doc;
169 doc = notmuch->xapian_db->get_document (doc_id);
170 } catch (const Xapian::DocNotFoundError &error) {
172 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
176 return _notmuch_message_create_for_document (talloc_owner, notmuch,
177 doc_id, doc, status);
180 /* Create a new notmuch_message_t object for a specific message ID,
181 * (which may or may not already exist in the database).
183 * The 'notmuch' database will be the talloc owner of the returned
186 * This function returns a valid notmuch_message_t whether or not
187 * there is already a document in the database with the given message
188 * ID. These two cases can be distinguished by the value of *status:
191 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
193 * There is already a document with message ID 'message_id' in the
194 * database. The returned message can be used to query/modify the
197 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
199 * No document with 'message_id' exists in the database. The
200 * returned message contains a newly created document (not yet
201 * added to the database) and a document ID that is known not to
202 * exist in the database. This message is "blank"; that is, it
203 * contains only a message ID and no other metadata. The caller
204 * can modify the message, and a call to _notmuch_message_sync
205 * will add the document to the database.
207 * If an error occurs, this function will return NULL and *status
208 * will be set as appropriate. (The status pointer argument must
212 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
213 const char *message_id,
214 notmuch_private_status_t *status_ret)
216 notmuch_message_t *message;
217 Xapian::Document doc;
221 *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
225 return talloc_steal (notmuch, message);
226 else if (*status_ret)
229 term = talloc_asprintf (NULL, "%s%s",
230 _find_prefix ("id"), message_id);
232 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
236 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
237 INTERNAL_ERROR ("Failure to ensure database is writable.");
240 doc.add_term (term, 0);
243 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
245 doc_id = _notmuch_database_generate_doc_id (notmuch);
246 } catch (const Xapian::Error &error) {
247 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
248 error.get_msg().c_str());
249 notmuch->exception_reported = TRUE;
250 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
254 message = _notmuch_message_create_for_document (notmuch, notmuch,
255 doc_id, doc, status_ret);
257 /* We want to inform the caller that we had to create a new
259 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
260 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
266 _notmuch_message_get_term (notmuch_message_t *message,
267 Xapian::TermIterator &i, Xapian::TermIterator &end,
270 int prefix_len = strlen (prefix);
278 std::string term = *i;
279 if (strncmp (term.c_str(), prefix, prefix_len))
282 value = talloc_strdup (message, term.c_str() + prefix_len);
284 #if DEBUG_DATABASE_SANITY
287 if (i != end && strncmp ((*i).c_str (), prefix, prefix_len) == 0) {
288 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate %s terms: %s and %s\n",
289 message->doc_id, prefix, value,
290 (*i).c_str () + prefix_len);
298 _notmuch_message_ensure_metadata (notmuch_message_t *message)
300 Xapian::TermIterator i, end;
301 const char *thread_prefix = _find_prefix ("thread"),
302 *tag_prefix = _find_prefix ("tag"),
303 *id_prefix = _find_prefix ("id"),
304 *filename_prefix = _find_prefix ("file-direntry"),
305 *replyto_prefix = _find_prefix ("replyto");
307 /* We do this all in a single pass because Xapian decompresses the
308 * term list every time you iterate over it. Thus, while this is
309 * slightly more costly than looking up individual fields if only
310 * one field of the message object is actually used, it's a huge
311 * win as more fields are used. */
313 i = message->doc.termlist_begin ();
314 end = message->doc.termlist_end ();
317 if (!message->thread_id)
319 _notmuch_message_get_term (message, i, end, thread_prefix);
322 assert (strcmp (thread_prefix, tag_prefix) < 0);
323 if (!message->tag_list) {
325 _notmuch_database_get_terms_with_prefix (message, i, end,
327 _notmuch_string_list_sort (message->tag_list);
331 assert (strcmp (tag_prefix, id_prefix) < 0);
332 if (!message->message_id)
333 message->message_id =
334 _notmuch_message_get_term (message, i, end, id_prefix);
336 /* Get filename list. Here we get only the terms. We lazily
337 * expand them to full file names when needed in
338 * _notmuch_message_ensure_filename_list. */
339 assert (strcmp (id_prefix, filename_prefix) < 0);
340 if (!message->filename_term_list && !message->filename_list)
341 message->filename_term_list =
342 _notmuch_database_get_terms_with_prefix (message, i, end,
346 assert (strcmp (filename_prefix, replyto_prefix) < 0);
347 if (!message->in_reply_to)
348 message->in_reply_to =
349 _notmuch_message_get_term (message, i, end, replyto_prefix);
350 /* It's perfectly valid for a message to have no In-Reply-To
351 * header. For these cases, we return an empty string. */
352 if (!message->in_reply_to)
353 message->in_reply_to = talloc_strdup (message, "");
357 _notmuch_message_invalidate_metadata (notmuch_message_t *message,
358 const char *prefix_name)
360 if (strcmp ("thread", prefix_name) == 0) {
361 talloc_free (message->thread_id);
362 message->thread_id = NULL;
365 if (strcmp ("tag", prefix_name) == 0) {
366 talloc_unlink (message, message->tag_list);
367 message->tag_list = NULL;
370 if (strcmp ("file-direntry", prefix_name) == 0) {
371 talloc_free (message->filename_term_list);
372 talloc_free (message->filename_list);
373 message->filename_term_list = message->filename_list = NULL;
376 if (strcmp ("replyto", prefix_name) == 0) {
377 talloc_free (message->in_reply_to);
378 message->in_reply_to = NULL;
383 _notmuch_message_get_doc_id (notmuch_message_t *message)
385 return message->doc_id;
389 notmuch_message_get_message_id (notmuch_message_t *message)
391 if (!message->message_id)
392 _notmuch_message_ensure_metadata (message);
393 if (!message->message_id)
394 INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
396 return message->message_id;
400 _notmuch_message_ensure_message_file (notmuch_message_t *message)
402 const char *filename;
404 if (message->message_file)
407 filename = notmuch_message_get_filename (message);
408 if (unlikely (filename == NULL))
411 message->message_file = _notmuch_message_file_open_ctx (message, filename);
415 notmuch_message_get_header (notmuch_message_t *message, const char *header)
417 Xapian::valueno slot = Xapian::BAD_VALUENO;
419 /* Fetch header from the appropriate xapian value field if
421 if (strcasecmp (header, "from") == 0)
422 slot = NOTMUCH_VALUE_FROM;
423 else if (strcasecmp (header, "subject") == 0)
424 slot = NOTMUCH_VALUE_SUBJECT;
425 else if (strcasecmp (header, "message-id") == 0)
426 slot = NOTMUCH_VALUE_MESSAGE_ID;
428 if (slot != Xapian::BAD_VALUENO) {
430 std::string value = message->doc.get_value (slot);
432 /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then
433 * empty values indicate empty headers. If we don't, then
434 * it could just mean we didn't record the header. */
435 if ((message->notmuch->features &
436 NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) ||
438 return talloc_strdup (message, value.c_str ());
440 } catch (Xapian::Error &error) {
441 fprintf (stderr, "A Xapian exception occurred when reading header: %s\n",
442 error.get_msg().c_str());
443 message->notmuch->exception_reported = TRUE;
448 /* Otherwise fall back to parsing the file */
449 _notmuch_message_ensure_message_file (message);
450 if (message->message_file == NULL)
453 return _notmuch_message_file_get_header (message->message_file, header);
456 /* Return the message ID from the In-Reply-To header of 'message'.
458 * Returns an empty string ("") if 'message' has no In-Reply-To
461 * Returns NULL if any error occurs.
464 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
466 if (!message->in_reply_to)
467 _notmuch_message_ensure_metadata (message);
468 return message->in_reply_to;
472 notmuch_message_get_thread_id (notmuch_message_t *message)
474 if (!message->thread_id)
475 _notmuch_message_ensure_metadata (message);
476 if (!message->thread_id)
477 INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
479 return message->thread_id;
483 _notmuch_message_add_reply (notmuch_message_t *message,
484 notmuch_message_t *reply)
486 _notmuch_message_list_add_message (message->replies, reply);
490 notmuch_message_get_replies (notmuch_message_t *message)
492 return _notmuch_messages_create (message->replies);
496 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
498 Xapian::TermIterator i;
499 size_t prefix_len = strlen (prefix);
502 i = message->doc.termlist_begin ();
505 /* Terminate loop when no terms remain with desired prefix. */
506 if (i == message->doc.termlist_end () ||
507 strncmp ((*i).c_str (), prefix, prefix_len))
511 message->doc.remove_term ((*i));
512 } catch (const Xapian::InvalidArgumentError) {
513 /* Ignore failure to remove non-existent term. */
518 /* Return true if p points at "new" or "cur". */
519 static bool is_maildir (const char *p)
521 return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;
524 /* Add "folder:" term for directory. */
525 static notmuch_status_t
526 _notmuch_message_add_folder_terms (notmuch_message_t *message,
527 const char *directory)
531 folder = talloc_strdup (NULL, directory);
533 return NOTMUCH_STATUS_OUT_OF_MEMORY;
536 * If the message file is in a leaf directory named "new" or
537 * "cur", presume maildir and index the parent directory. Thus a
538 * "folder:" prefix search matches messages in the specified
539 * maildir folder, i.e. in the specified directory and its "new"
540 * and "cur" subdirectories.
542 * Note that this means the "folder:" prefix can't be used for
543 * distinguishing between message files in "new" or "cur". The
544 * "path:" prefix needs to be used for that.
546 * Note the deliberate difference to _filename_is_in_maildir(). We
547 * don't want to index different things depending on the existence
548 * or non-existence of all maildir sibling directories "new",
549 * "cur", and "tmp". Doing so would be surprising, and difficult
550 * for the user to fix in case all subdirectories were not in
551 * place during indexing.
553 last = strrchr (folder, '/');
555 if (is_maildir (last + 1))
557 } else if (is_maildir (folder)) {
561 _notmuch_message_add_term (message, "folder", folder);
563 talloc_free (folder);
565 return NOTMUCH_STATUS_SUCCESS;
568 #define RECURSIVE_SUFFIX "/**"
570 /* Add "path:" terms for directory. */
571 static notmuch_status_t
572 _notmuch_message_add_path_terms (notmuch_message_t *message,
573 const char *directory)
575 /* Add exact "path:" term. */
576 _notmuch_message_add_term (message, "path", directory);
578 if (strlen (directory)) {
581 path = talloc_asprintf (NULL, "%s%s", directory, RECURSIVE_SUFFIX);
583 return NOTMUCH_STATUS_OUT_OF_MEMORY;
585 /* Add recursive "path:" terms for directory and all parents. */
586 for (p = path + strlen (path) - 1; p > path; p--) {
588 strcpy (p, RECURSIVE_SUFFIX);
589 _notmuch_message_add_term (message, "path", path);
596 /* Recursive all-matching path:** for consistency. */
597 _notmuch_message_add_term (message, "path", "**");
599 return NOTMUCH_STATUS_SUCCESS;
602 /* Add directory based terms for all filenames of the message. */
603 static notmuch_status_t
604 _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
606 const char *direntry_prefix = _find_prefix ("file-direntry");
607 int direntry_prefix_len = strlen (direntry_prefix);
608 Xapian::TermIterator i = message->doc.termlist_begin ();
609 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
611 for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {
612 unsigned int directory_id;
613 const char *direntry, *directory;
616 /* Terminate loop at first term without desired prefix. */
617 if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))
620 /* Indicate that there are filenames remaining. */
621 status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
623 direntry = (*i).c_str ();
624 direntry += direntry_prefix_len;
626 directory_id = strtol (direntry, &colon, 10);
628 if (colon == NULL || *colon != ':')
629 INTERNAL_ERROR ("malformed direntry");
631 directory = _notmuch_database_get_directory_path (ctx,
635 _notmuch_message_add_folder_terms (message, directory);
636 _notmuch_message_add_path_terms (message, directory);
642 /* Add an additional 'filename' for 'message'.
644 * This change will not be reflected in the database until the next
645 * call to _notmuch_message_sync. */
647 _notmuch_message_add_filename (notmuch_message_t *message,
648 const char *filename)
650 const char *relative, *directory;
651 notmuch_status_t status;
652 void *local = talloc_new (message);
655 if (filename == NULL)
656 INTERNAL_ERROR ("Message filename cannot be NULL.");
658 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
659 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
660 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
662 relative = _notmuch_database_relative_path (message->notmuch, filename);
664 status = _notmuch_database_split_path (local, relative, &directory, NULL);
668 status = _notmuch_database_filename_to_direntry (
669 local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
673 /* New file-direntry allows navigating to this message with
674 * notmuch_directory_get_child_files() . */
675 _notmuch_message_add_term (message, "file-direntry", direntry);
677 _notmuch_message_add_folder_terms (message, directory);
678 _notmuch_message_add_path_terms (message, directory);
682 return NOTMUCH_STATUS_SUCCESS;
685 /* Remove a particular 'filename' from 'message'.
687 * This change will not be reflected in the database until the next
688 * call to _notmuch_message_sync.
690 * If this message still has other filenames, returns
691 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID.
693 * Note: This function does not remove a document from the database,
694 * even if the specified filename is the only filename for this
695 * message. For that functionality, see
696 * _notmuch_database_remove_message. */
698 _notmuch_message_remove_filename (notmuch_message_t *message,
699 const char *filename)
701 void *local = talloc_new (message);
703 notmuch_private_status_t private_status;
704 notmuch_status_t status;
706 if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||
707 ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))
708 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
710 status = _notmuch_database_filename_to_direntry (
711 local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
712 if (status || !direntry)
715 /* Unlink this file from its parent directory. */
716 private_status = _notmuch_message_remove_term (message,
717 "file-direntry", direntry);
718 status = COERCE_STATUS (private_status,
719 "Unexpected error from _notmuch_message_remove_term");
723 /* Re-synchronize "folder:" and "path:" terms for this message. */
725 /* Remove all "folder:" terms. */
726 _notmuch_message_remove_terms (message, _find_prefix ("folder"));
728 /* Remove all "path:" terms. */
729 _notmuch_message_remove_terms (message, _find_prefix ("path"));
731 /* Add back terms for all remaining filenames of the message. */
732 status = _notmuch_message_add_directory_terms (local, message);
739 /* Upgrade the "folder:" prefix from V1 to V2. */
740 #define FOLDER_PREFIX_V1 "XFOLDER"
741 #define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1
743 _notmuch_message_upgrade_folder (notmuch_message_t *message)
745 /* Remove all old "folder:" terms. */
746 _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);
748 /* Remove all old "folder:" stemmed terms. */
749 _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);
751 /* Add new boolean "folder:" and "path:" terms. */
752 _notmuch_message_add_directory_terms (message, message);
756 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
758 return talloc_strdup (message, message->doc.get_data ().c_str ());
762 _notmuch_message_clear_data (notmuch_message_t *message)
764 message->doc.set_data ("");
768 _notmuch_message_ensure_filename_list (notmuch_message_t *message)
770 notmuch_string_node_t *node;
772 if (message->filename_list)
775 if (!message->filename_term_list)
776 _notmuch_message_ensure_metadata (message);
778 message->filename_list = _notmuch_string_list_create (message);
779 node = message->filename_term_list->head;
782 /* A message document created by an old version of notmuch
783 * (prior to rename support) will have the filename in the
784 * data of the document rather than as a file-direntry term.
786 * It would be nice to do the upgrade of the document directly
787 * here, but the database is likely open in read-only mode. */
790 data = message->doc.get_data ().c_str ();
793 INTERNAL_ERROR ("message with no filename");
795 _notmuch_string_list_append (message->filename_list, data);
800 for (; node; node = node->next) {
801 void *local = talloc_new (message);
802 const char *db_path, *directory, *basename, *filename;
803 char *colon, *direntry = NULL;
804 unsigned int directory_id;
806 direntry = node->string;
808 directory_id = strtol (direntry, &colon, 10);
810 if (colon == NULL || *colon != ':')
811 INTERNAL_ERROR ("malformed direntry");
813 basename = colon + 1;
817 db_path = notmuch_database_get_path (message->notmuch);
819 directory = _notmuch_database_get_directory_path (local,
823 if (strlen (directory))
824 filename = talloc_asprintf (message, "%s/%s/%s",
825 db_path, directory, basename);
827 filename = talloc_asprintf (message, "%s/%s",
830 _notmuch_string_list_append (message->filename_list, filename);
835 talloc_free (message->filename_term_list);
836 message->filename_term_list = NULL;
840 notmuch_message_get_filename (notmuch_message_t *message)
842 _notmuch_message_ensure_filename_list (message);
844 if (message->filename_list == NULL)
847 if (message->filename_list->head == NULL ||
848 message->filename_list->head->string == NULL)
850 INTERNAL_ERROR ("message with no filename");
853 return message->filename_list->head->string;
856 notmuch_filenames_t *
857 notmuch_message_get_filenames (notmuch_message_t *message)
859 _notmuch_message_ensure_filename_list (message);
861 return _notmuch_filenames_create (message, message->filename_list);
865 notmuch_message_get_flag (notmuch_message_t *message,
866 notmuch_message_flag_t flag)
868 return message->flags & (1 << flag);
872 notmuch_message_set_flag (notmuch_message_t *message,
873 notmuch_message_flag_t flag, notmuch_bool_t enable)
876 message->flags |= (1 << flag);
878 message->flags &= ~(1 << flag);
882 notmuch_message_get_date (notmuch_message_t *message)
887 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
888 } catch (Xapian::Error &error) {
889 fprintf (stderr, "A Xapian exception occurred when reading date: %s\n",
890 error.get_msg().c_str());
891 message->notmuch->exception_reported = TRUE;
895 return Xapian::sortable_unserialise (value);
899 notmuch_message_get_tags (notmuch_message_t *message)
901 notmuch_tags_t *tags;
903 if (!message->tag_list)
904 _notmuch_message_ensure_metadata (message);
906 tags = _notmuch_tags_create (message, message->tag_list);
907 /* _notmuch_tags_create steals the reference to the tag_list, but
908 * in this case it's still used by the message, so we add an
909 * *additional* talloc reference to the list. As a result, it's
910 * possible to modify the message tags (which talloc_unlink's the
911 * current list from the message) while still iterating because
912 * the iterator will keep the current list alive. */
913 if (!talloc_reference (message, message->tag_list))
920 _notmuch_message_get_author (notmuch_message_t *message)
922 return message->author;
926 _notmuch_message_set_author (notmuch_message_t *message,
930 talloc_free(message->author);
931 message->author = talloc_strdup(message, author);
936 _notmuch_message_set_header_values (notmuch_message_t *message,
943 /* GMime really doesn't want to see a NULL date, so protect its
945 if (date == NULL || *date == '\0')
948 time_value = g_mime_utils_header_decode_date (date, NULL);
950 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
951 Xapian::sortable_serialise (time_value));
952 message->doc.add_value (NOTMUCH_VALUE_FROM, from);
953 message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
956 /* Synchronize changes made to message->doc out into the database. */
958 _notmuch_message_sync (notmuch_message_t *message)
960 Xapian::WritableDatabase *db;
962 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
965 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
966 db->replace_document (message->doc_id, message->doc);
969 /* Delete a message document from the database. */
971 _notmuch_message_delete (notmuch_message_t *message)
973 notmuch_status_t status;
974 Xapian::WritableDatabase *db;
976 status = _notmuch_database_ensure_writable (message->notmuch);
980 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
981 db->delete_document (message->doc_id);
982 return NOTMUCH_STATUS_SUCCESS;
985 /* Ensure that 'message' is not holding any file object open. Future
986 * calls to various functions will still automatically open the
987 * message file as needed.
990 _notmuch_message_close (notmuch_message_t *message)
992 if (message->message_file) {
993 _notmuch_message_file_close (message->message_file);
994 message->message_file = NULL;
998 /* Add a name:value term to 'message', (the actual term will be
999 * encoded by prefixing the value with a short prefix). See
1000 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1001 * names to prefix values.
1003 * This change will not be reflected in the database until the next
1004 * call to _notmuch_message_sync. */
1005 notmuch_private_status_t
1006 _notmuch_message_add_term (notmuch_message_t *message,
1007 const char *prefix_name,
1014 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1016 term = talloc_asprintf (message, "%s%s",
1017 _find_prefix (prefix_name), value);
1019 if (strlen (term) > NOTMUCH_TERM_MAX)
1020 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1022 message->doc.add_term (term, 0);
1026 _notmuch_message_invalidate_metadata (message, prefix_name);
1028 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1031 /* Parse 'text' and add a term to 'message' for each parsed word. Each
1032 * term will be added both prefixed (if prefix_name is not NULL) and
1033 * also non-prefixed). */
1034 notmuch_private_status_t
1035 _notmuch_message_gen_terms (notmuch_message_t *message,
1036 const char *prefix_name,
1039 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
1042 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1044 term_gen->set_document (message->doc);
1047 const char *prefix = _find_prefix (prefix_name);
1049 term_gen->set_termpos (message->termpos);
1050 term_gen->index_text (text, 1, prefix);
1051 /* Create a gap between this an the next terms so they don't
1052 * appear to be a phrase. */
1053 message->termpos = term_gen->get_termpos () + 100;
1055 _notmuch_message_invalidate_metadata (message, prefix_name);
1058 term_gen->set_termpos (message->termpos);
1059 term_gen->index_text (text);
1060 /* Create a term gap, as above. */
1061 message->termpos = term_gen->get_termpos () + 100;
1063 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1066 /* Remove a name:value term from 'message', (the actual term will be
1067 * encoded by prefixing the value with a short prefix). See
1068 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
1069 * names to prefix values.
1071 * This change will not be reflected in the database until the next
1072 * call to _notmuch_message_sync. */
1073 notmuch_private_status_t
1074 _notmuch_message_remove_term (notmuch_message_t *message,
1075 const char *prefix_name,
1081 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
1083 term = talloc_asprintf (message, "%s%s",
1084 _find_prefix (prefix_name), value);
1086 if (strlen (term) > NOTMUCH_TERM_MAX)
1087 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
1090 message->doc.remove_term (term);
1091 } catch (const Xapian::InvalidArgumentError) {
1092 /* We'll let the philosopher's try to wrestle with the
1093 * question of whether failing to remove that which was not
1094 * there in the first place is failure. For us, we'll silently
1095 * consider it all good. */
1100 _notmuch_message_invalidate_metadata (message, prefix_name);
1102 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
1106 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
1108 notmuch_private_status_t private_status;
1109 notmuch_status_t status;
1111 status = _notmuch_database_ensure_writable (message->notmuch);
1116 return NOTMUCH_STATUS_NULL_POINTER;
1118 if (strlen (tag) > NOTMUCH_TAG_MAX)
1119 return NOTMUCH_STATUS_TAG_TOO_LONG;
1121 private_status = _notmuch_message_add_term (message, "tag", tag);
1122 if (private_status) {
1123 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
1127 if (! message->frozen)
1128 _notmuch_message_sync (message);
1130 return NOTMUCH_STATUS_SUCCESS;
1134 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
1136 notmuch_private_status_t private_status;
1137 notmuch_status_t status;
1139 status = _notmuch_database_ensure_writable (message->notmuch);
1144 return NOTMUCH_STATUS_NULL_POINTER;
1146 if (strlen (tag) > NOTMUCH_TAG_MAX)
1147 return NOTMUCH_STATUS_TAG_TOO_LONG;
1149 private_status = _notmuch_message_remove_term (message, "tag", tag);
1150 if (private_status) {
1151 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1155 if (! message->frozen)
1156 _notmuch_message_sync (message);
1158 return NOTMUCH_STATUS_SUCCESS;
1161 /* Is the given filename within a maildir directory?
1163 * Specifically, is the final directory component of 'filename' either
1164 * "cur" or "new". If so, return a pointer to that final directory
1165 * component within 'filename'. If not, return NULL.
1167 * A non-NULL return value is guaranteed to be a valid string pointer
1168 * pointing to the characters "new/" or "cur/", (but not
1172 _filename_is_in_maildir (const char *filename)
1174 const char *slash, *dir = NULL;
1176 /* Find the last '/' separating directory from filename. */
1177 slash = strrchr (filename, '/');
1181 /* Jump back 4 characters to where the previous '/' will be if the
1182 * directory is named "cur" or "new". */
1183 if (slash - filename < 4)
1193 if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
1194 STRNCMP_LITERAL (dir, "new/") == 0)
1203 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
1206 notmuch_status_t status;
1207 notmuch_filenames_t *filenames;
1208 const char *filename, *dir;
1209 char *combined_flags = talloc_strdup (message, "");
1211 int seen_maildir_info = 0;
1213 for (filenames = notmuch_message_get_filenames (message);
1214 notmuch_filenames_valid (filenames);
1215 notmuch_filenames_move_to_next (filenames))
1217 filename = notmuch_filenames_get (filenames);
1218 dir = _filename_is_in_maildir (filename);
1223 flags = strstr (filename, ":2,");
1225 seen_maildir_info = 1;
1227 combined_flags = talloc_strdup_append (combined_flags, flags);
1228 } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
1229 /* Messages are delivered to new/ with no "info" part, but
1230 * they effectively have default maildir flags. According
1231 * to the spec, we should ignore the info part for
1232 * messages in new/, but some MUAs (mutt) can set maildir
1233 * flags on messages in new/, so we're liberal in what we
1235 seen_maildir_info = 1;
1239 /* If none of the filenames have any maildir info field (not even
1240 * an empty info with no flags set) then there's no information to
1241 * go on, so do nothing. */
1242 if (! seen_maildir_info)
1243 return NOTMUCH_STATUS_SUCCESS;
1245 status = notmuch_message_freeze (message);
1249 for (i = 0; i < ARRAY_SIZE(flag2tag); i++) {
1250 if ((strchr (combined_flags, flag2tag[i].flag) != NULL)
1252 flag2tag[i].inverse)
1254 status = notmuch_message_add_tag (message, flag2tag[i].tag);
1256 status = notmuch_message_remove_tag (message, flag2tag[i].tag);
1261 status = notmuch_message_thaw (message);
1263 talloc_free (combined_flags);
1268 /* From the set of tags on 'message' and the flag2tag table, compute a
1269 * set of maildir-flag actions to be taken, (flags that should be
1270 * either set or cleared).
1272 * The result is returned as two talloced strings: to_set, and to_clear
1275 _get_maildir_flag_actions (notmuch_message_t *message,
1277 char **to_clear_ret)
1279 char *to_set, *to_clear;
1280 notmuch_tags_t *tags;
1284 to_set = talloc_strdup (message, "");
1285 to_clear = talloc_strdup (message, "");
1287 /* First, find flags for all set tags. */
1288 for (tags = notmuch_message_get_tags (message);
1289 notmuch_tags_valid (tags);
1290 notmuch_tags_move_to_next (tags))
1292 tag = notmuch_tags_get (tags);
1294 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1295 if (strcmp (tag, flag2tag[i].tag) == 0) {
1296 if (flag2tag[i].inverse)
1297 to_clear = talloc_asprintf_append (to_clear,
1301 to_set = talloc_asprintf_append (to_set,
1308 /* Then, find the flags for all tags not present. */
1309 for (i = 0; i < ARRAY_SIZE (flag2tag); i++) {
1310 if (flag2tag[i].inverse) {
1311 if (strchr (to_clear, flag2tag[i].flag) == NULL)
1312 to_set = talloc_asprintf_append (to_set, "%c", flag2tag[i].flag);
1314 if (strchr (to_set, flag2tag[i].flag) == NULL)
1315 to_clear = talloc_asprintf_append (to_clear, "%c", flag2tag[i].flag);
1319 *to_set_ret = to_set;
1320 *to_clear_ret = to_clear;
1323 /* Given 'filename' and a set of maildir flags to set and to clear,
1324 * compute the new maildir filename.
1326 * If the existing filename is in the directory "new", the new
1327 * filename will be in the directory "cur", except for the case when
1328 * no flags are changed and the existing filename does not contain
1329 * maildir info (starting with ",2:").
1331 * After a sequence of ":2," in the filename, any subsequent
1332 * single-character flags will be added or removed according to the
1333 * characters in flags_to_set and flags_to_clear. Any existing flags
1334 * not mentioned in either string will remain. The final list of flags
1335 * will be in ASCII order.
1337 * If the original flags seem invalid, (repeated characters or
1338 * non-ASCII ordering of flags), this function will return NULL
1339 * (meaning that renaming would not be safe and should not occur).
1342 _new_maildir_filename (void *ctx,
1343 const char *filename,
1344 const char *flags_to_set,
1345 const char *flags_to_clear)
1347 const char *info, *flags;
1348 unsigned int flag, last_flag;
1349 char *filename_new, *dir;
1351 int flags_in_map = 0;
1352 notmuch_bool_t flags_changed = FALSE;
1356 memset (flag_map, 0, sizeof (flag_map));
1358 info = strstr (filename, ":2,");
1361 info = filename + strlen(filename);
1363 /* Loop through existing flags in filename. */
1364 for (flags = info + 3, last_flag = 0;
1366 last_flag = flag, flags++)
1370 /* Original flags not in ASCII order. Abort. */
1371 if (flag < last_flag)
1374 /* Non-ASCII flag. Abort. */
1375 if (flag > sizeof(flag_map) - 1)
1378 /* Repeated flag value. Abort. */
1387 /* Then set and clear our flags from tags. */
1388 for (flags = flags_to_set; *flags; flags++) {
1390 if (flag_map[flag] == 0) {
1393 flags_changed = TRUE;
1397 for (flags = flags_to_clear; *flags; flags++) {
1399 if (flag_map[flag]) {
1402 flags_changed = TRUE;
1406 /* Messages in new/ without maildir info can be kept in new/ if no
1407 * flags have changed. */
1408 dir = (char *) _filename_is_in_maildir (filename);
1409 if (dir && STRNCMP_LITERAL (dir, "new/") == 0 && !*info && !flags_changed)
1410 return talloc_strdup (ctx, filename);
1412 filename_new = (char *) talloc_size (ctx,
1414 strlen (":2,") + flags_in_map + 1);
1415 if (unlikely (filename_new == NULL))
1418 strncpy (filename_new, filename, info - filename);
1419 filename_new[info - filename] = '\0';
1421 strcat (filename_new, ":2,");
1423 s = filename_new + strlen (filename_new);
1424 for (i = 0; i < sizeof (flag_map); i++)
1433 /* If message is in new/ move it under cur/. */
1434 dir = (char *) _filename_is_in_maildir (filename_new);
1435 if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
1436 memcpy (dir, "cur/", 4);
1438 return filename_new;
1442 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
1444 notmuch_filenames_t *filenames;
1445 const char *filename;
1447 char *to_set, *to_clear;
1448 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
1450 _get_maildir_flag_actions (message, &to_set, &to_clear);
1452 for (filenames = notmuch_message_get_filenames (message);
1453 notmuch_filenames_valid (filenames);
1454 notmuch_filenames_move_to_next (filenames))
1456 filename = notmuch_filenames_get (filenames);
1458 if (! _filename_is_in_maildir (filename))
1461 filename_new = _new_maildir_filename (message, filename,
1463 if (filename_new == NULL)
1466 if (strcmp (filename, filename_new)) {
1468 notmuch_status_t new_status;
1470 err = rename (filename, filename_new);
1474 new_status = _notmuch_message_remove_filename (message,
1476 /* Hold on to only the first error. */
1477 if (! status && new_status
1478 && new_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
1479 status = new_status;
1483 new_status = _notmuch_message_add_filename (message,
1485 /* Hold on to only the first error. */
1486 if (! status && new_status) {
1487 status = new_status;
1491 _notmuch_message_sync (message);
1494 talloc_free (filename_new);
1497 talloc_free (to_set);
1498 talloc_free (to_clear);
1504 notmuch_message_remove_all_tags (notmuch_message_t *message)
1506 notmuch_private_status_t private_status;
1507 notmuch_status_t status;
1508 notmuch_tags_t *tags;
1511 status = _notmuch_database_ensure_writable (message->notmuch);
1515 for (tags = notmuch_message_get_tags (message);
1516 notmuch_tags_valid (tags);
1517 notmuch_tags_move_to_next (tags))
1519 tag = notmuch_tags_get (tags);
1521 private_status = _notmuch_message_remove_term (message, "tag", tag);
1522 if (private_status) {
1523 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
1528 if (! message->frozen)
1529 _notmuch_message_sync (message);
1532 return NOTMUCH_STATUS_SUCCESS;
1536 notmuch_message_freeze (notmuch_message_t *message)
1538 notmuch_status_t status;
1540 status = _notmuch_database_ensure_writable (message->notmuch);
1546 return NOTMUCH_STATUS_SUCCESS;
1550 notmuch_message_thaw (notmuch_message_t *message)
1552 notmuch_status_t status;
1554 status = _notmuch_database_ensure_writable (message->notmuch);
1558 if (message->frozen > 0) {
1560 if (message->frozen == 0)
1561 _notmuch_message_sync (message);
1562 return NOTMUCH_STATUS_SUCCESS;
1564 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
1569 notmuch_message_destroy (notmuch_message_t *message)
1571 talloc_free (message);