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>
30 struct _notmuch_message {
31 notmuch_database_t *notmuch;
39 notmuch_message_file_t *message_file;
40 notmuch_message_list_t *replies;
46 /* We end up having to call the destructor explicitly because we had
47 * to use "placement new" in order to initialize C++ objects within a
48 * block that we allocated with talloc. So C++ is making talloc
49 * slightly less simple to use, (we wouldn't need
50 * talloc_set_destructor at all otherwise).
53 _notmuch_message_destructor (notmuch_message_t *message)
55 message->doc.~Document ();
60 static notmuch_message_t *
61 _notmuch_message_create_for_document (const void *talloc_owner,
62 notmuch_database_t *notmuch,
65 notmuch_private_status_t *status)
67 notmuch_message_t *message;
70 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
72 message = talloc (talloc_owner, notmuch_message_t);
73 if (unlikely (message == NULL)) {
75 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
79 message->notmuch = notmuch;
80 message->doc_id = doc_id;
85 /* Each of these will be lazily created as needed. */
86 message->message_id = NULL;
87 message->thread_id = NULL;
88 message->in_reply_to = NULL;
89 message->filename = NULL;
90 message->message_file = NULL;
91 message->author = NULL;
93 message->replies = _notmuch_message_list_create (message);
94 if (unlikely (message->replies == NULL)) {
96 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
100 /* This is C++'s creepy "placement new", which is really just an
101 * ugly way to call a constructor for a pre-allocated object. So
102 * it's really not an error to not be checking for OUT_OF_MEMORY
103 * here, since this "new" isn't actually allocating memory. This
104 * is language-design comedy of the wrong kind. */
106 new (&message->doc) Xapian::Document;
108 talloc_set_destructor (message, _notmuch_message_destructor);
115 /* Create a new notmuch_message_t object for an existing document in
118 * Here, 'talloc owner' is an optional talloc context to which the new
119 * message will belong. This allows for the caller to not bother
120 * calling notmuch_message_destroy on the message, and no that all
121 * memory will be reclaimed with 'talloc_owner' is free. The caller
122 * still can call notmuch_message_destroy when finished with the
123 * message if desired.
125 * The 'talloc_owner' argument can also be NULL, in which case the
126 * caller *is* responsible for calling notmuch_message_destroy.
128 * If no document exists in the database with document ID of 'doc_id'
129 * then this function returns NULL and optionally sets *status to
130 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
132 * This function can also fail to due lack of available memory,
133 * returning NULL and optionally setting *status to
134 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
136 * The caller can pass NULL for status if uninterested in
137 * distinguishing these two cases.
140 _notmuch_message_create (const void *talloc_owner,
141 notmuch_database_t *notmuch,
143 notmuch_private_status_t *status)
145 Xapian::Document doc;
148 doc = notmuch->xapian_db->get_document (doc_id);
149 } catch (const Xapian::DocNotFoundError &error) {
151 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
155 return _notmuch_message_create_for_document (talloc_owner, notmuch,
156 doc_id, doc, status);
159 /* Create a new notmuch_message_t object for a specific message ID,
160 * (which may or may not already exist in the database).
162 * The 'notmuch' database will be the talloc owner of the returned
165 * This function returns a valid notmuch_message_t whether or not
166 * there is already a document in the database with the given message
167 * ID. These two cases can be distinguished by the value of *status:
170 * NOTMUCH_PRIVATE_STATUS_SUCCESS:
172 * There is already a document with message ID 'message_id' in the
173 * database. The returned message can be used to query/modify the
175 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
177 * No document with 'message_id' exists in the database. The
178 * returned message contains a newly created document (not yet
179 * added to the database) and a document ID that is known not to
180 * exist in the database. The caller can modify the message, and a
181 * call to _notmuch_message_sync will add * the document to the
184 * If an error occurs, this function will return NULL and *status
185 * will be set as appropriate. (The status pointer argument must
189 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
190 const char *message_id,
191 notmuch_private_status_t *status_ret)
193 notmuch_message_t *message;
194 Xapian::Document doc;
195 Xapian::WritableDatabase *db;
199 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
201 message = notmuch_database_find_message (notmuch, message_id);
203 return talloc_steal (notmuch, message);
205 term = talloc_asprintf (NULL, "%s%s",
206 _find_prefix ("id"), message_id);
208 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
212 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
213 INTERNAL_ERROR ("Failure to ensure database is writable.");
215 db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
217 doc.add_term (term, 0);
220 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
222 doc_id = _notmuch_database_generate_doc_id (notmuch);
223 } catch (const Xapian::Error &error) {
224 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
225 error.get_msg().c_str());
226 notmuch->exception_reported = TRUE;
227 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
231 message = _notmuch_message_create_for_document (notmuch, notmuch,
232 doc_id, doc, status_ret);
234 /* We want to inform the caller that we had to create a new
236 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
237 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
243 notmuch_message_get_message_id (notmuch_message_t *message)
245 Xapian::TermIterator i;
247 if (message->message_id)
248 return message->message_id;
250 i = message->doc.termlist_begin ();
251 i.skip_to (_find_prefix ("id"));
253 if (i == message->doc.termlist_end ())
254 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
257 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
259 #if DEBUG_DATABASE_SANITY
262 if (i != message->doc.termlist_end () &&
263 strncmp ((*i).c_str (), _find_prefix ("id"),
264 strlen (_find_prefix ("id"))) == 0)
266 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
271 return message->message_id;
275 _notmuch_message_ensure_message_file (notmuch_message_t *message)
277 const char *filename;
279 if (message->message_file)
282 filename = notmuch_message_get_filename (message);
283 if (unlikely (filename == NULL))
286 message->message_file = _notmuch_message_file_open_ctx (message, filename);
290 notmuch_message_get_header (notmuch_message_t *message, const char *header)
292 _notmuch_message_ensure_message_file (message);
293 if (message->message_file == NULL)
296 return notmuch_message_file_get_header (message->message_file, header);
299 /* Return the message ID from the In-Reply-To header of 'message'.
301 * Returns an empty string ("") if 'message' has no In-Reply-To
304 * Returns NULL if any error occurs.
307 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
309 const char *prefix = _find_prefix ("replyto");
310 int prefix_len = strlen (prefix);
311 Xapian::TermIterator i;
312 std::string in_reply_to;
314 if (message->in_reply_to)
315 return message->in_reply_to;
317 i = message->doc.termlist_begin ();
320 if (i != message->doc.termlist_end ())
323 /* It's perfectly valid for a message to have no In-Reply-To
324 * header. For these cases, we return an empty string. */
325 if (i == message->doc.termlist_end () ||
326 strncmp (in_reply_to.c_str (), prefix, prefix_len))
328 message->in_reply_to = talloc_strdup (message, "");
329 return message->in_reply_to;
332 message->in_reply_to = talloc_strdup (message,
333 in_reply_to.c_str () + prefix_len);
335 #if DEBUG_DATABASE_SANITY
340 if (i != message->doc.termlist_end () &&
341 strncmp ((*i).c_str (), prefix, prefix_len) == 0)
343 INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
344 notmuch_message_get_message_id (message),
345 message->in_reply_to,
346 (*i).c_str () + prefix_len);
350 return message->in_reply_to;
354 notmuch_message_get_thread_id (notmuch_message_t *message)
356 const char *prefix = _find_prefix ("thread");
357 Xapian::TermIterator i;
360 /* This code is written with the assumption that "thread" has a
361 * single-character prefix. */
362 assert (strlen (prefix) == 1);
364 if (message->thread_id)
365 return message->thread_id;
367 i = message->doc.termlist_begin ();
370 if (i != message->doc.termlist_end ())
373 if (i == message->doc.termlist_end () || id[0] != *prefix)
374 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
377 message->thread_id = talloc_strdup (message, id.c_str () + 1);
379 #if DEBUG_DATABASE_SANITY
383 if (i != message->doc.termlist_end () && id[0] == *prefix)
385 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
386 notmuch_message_get_message_id (message),
392 return message->thread_id;
396 _notmuch_message_add_reply (notmuch_message_t *message,
397 notmuch_message_node_t *reply)
399 _notmuch_message_list_append (message->replies, reply);
403 notmuch_message_get_replies (notmuch_message_t *message)
405 return _notmuch_messages_create (message->replies);
408 /* Add an additional 'filename' for 'message'.
410 * This change will not be reflected in the database until the next
411 * call to _notmuch_message_sync. */
413 _notmuch_message_add_filename (notmuch_message_t *message,
414 const char *filename)
416 notmuch_status_t status;
417 void *local = talloc_new (message);
420 if (message->filename) {
421 talloc_free (message->filename);
422 message->filename = NULL;
425 if (filename == NULL)
426 INTERNAL_ERROR ("Message filename cannot be NULL.");
428 status = _notmuch_database_filename_to_direntry (local,
430 filename, &direntry);
434 _notmuch_message_add_term (message, "file-direntry", direntry);
438 return NOTMUCH_STATUS_SUCCESS;
442 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
444 return talloc_strdup (message, message->doc.get_data ().c_str ());
448 _notmuch_message_clear_data (notmuch_message_t *message)
450 message->doc.set_data ("");
454 notmuch_message_get_filename (notmuch_message_t *message)
456 const char *prefix = _find_prefix ("file-direntry");
457 int prefix_len = strlen (prefix);
458 Xapian::TermIterator i;
459 char *colon, *direntry = NULL;
460 const char *db_path, *directory, *basename;
461 unsigned int directory_id;
462 void *local = talloc_new (message);
464 if (message->filename)
465 return message->filename;
467 i = message->doc.termlist_begin ();
470 if (i != message->doc.termlist_end ())
471 direntry = talloc_strdup (local, (*i).c_str ());
473 if (i == message->doc.termlist_end () ||
474 strncmp (direntry, prefix, prefix_len))
476 /* A message document created by an old version of notmuch
477 * (prior to rename support) will have the filename in the
478 * data of the document rather than as a file-direntry term.
480 * It would be nice to do the upgrade of the document directly
481 * here, but the database is likely open in read-only mode. */
484 data = message->doc.get_data ().c_str ();
487 INTERNAL_ERROR ("message with no filename");
489 message->filename = talloc_strdup (message, data);
491 return message->filename;
494 direntry += prefix_len;
496 directory_id = strtol (direntry, &colon, 10);
498 if (colon == NULL || *colon != ':')
499 INTERNAL_ERROR ("malformed direntry");
501 basename = colon + 1;
505 db_path = notmuch_database_get_path (message->notmuch);
507 directory = _notmuch_database_get_directory_path (local,
511 if (strlen (directory))
512 message->filename = talloc_asprintf (message, "%s/%s/%s",
513 db_path, directory, basename);
515 message->filename = talloc_asprintf (message, "%s/%s",
517 talloc_free ((void *) directory);
521 return message->filename;
525 notmuch_message_get_flag (notmuch_message_t *message,
526 notmuch_message_flag_t flag)
528 return message->flags & (1 << flag);
532 notmuch_message_set_flag (notmuch_message_t *message,
533 notmuch_message_flag_t flag, notmuch_bool_t enable)
536 message->flags |= (1 << flag);
538 message->flags &= ~(1 << flag);
542 notmuch_message_get_date (notmuch_message_t *message)
547 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
548 } catch (Xapian::Error &error) {
549 INTERNAL_ERROR ("Failed to read timestamp value from document.");
553 return Xapian::sortable_unserialise (value);
557 notmuch_message_get_tags (notmuch_message_t *message)
559 Xapian::TermIterator i, end;
560 i = message->doc.termlist_begin();
561 end = message->doc.termlist_end();
562 return _notmuch_convert_tags(message, i, end);
566 notmuch_message_get_author (notmuch_message_t *message)
568 return message->author;
572 notmuch_message_set_author (notmuch_message_t *message,
576 talloc_free(message->author);
577 message->author = talloc_strdup(message, author);
582 _notmuch_message_set_date (notmuch_message_t *message,
587 /* GMime really doesn't want to see a NULL date, so protect its
589 if (date == NULL || *date == '\0')
592 time_value = g_mime_utils_header_decode_date (date, NULL);
594 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
595 Xapian::sortable_serialise (time_value));
598 /* Synchronize changes made to message->doc out into the database. */
600 _notmuch_message_sync (notmuch_message_t *message)
602 Xapian::WritableDatabase *db;
604 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
607 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
608 db->replace_document (message->doc_id, message->doc);
611 /* Ensure that 'message' is not holding any file object open. Future
612 * calls to various functions will still automatically open the
613 * message file as needed.
616 _notmuch_message_close (notmuch_message_t *message)
618 if (message->message_file) {
619 notmuch_message_file_close (message->message_file);
620 message->message_file = NULL;
624 /* Add a name:value term to 'message', (the actual term will be
625 * encoded by prefixing the value with a short prefix). See
626 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
627 * names to prefix values.
629 * This change will not be reflected in the database until the next
630 * call to _notmuch_message_sync. */
631 notmuch_private_status_t
632 _notmuch_message_add_term (notmuch_message_t *message,
633 const char *prefix_name,
640 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
642 term = talloc_asprintf (message, "%s%s",
643 _find_prefix (prefix_name), value);
645 if (strlen (term) > NOTMUCH_TERM_MAX)
646 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
648 message->doc.add_term (term, 0);
652 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
655 /* Parse 'text' and add a term to 'message' for each parsed word. Each
656 * term will be added both prefixed (if prefix_name is not NULL) and
657 * also unprefixed). */
658 notmuch_private_status_t
659 _notmuch_message_gen_terms (notmuch_message_t *message,
660 const char *prefix_name,
663 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
666 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
668 term_gen->set_document (message->doc);
671 const char *prefix = _find_prefix (prefix_name);
673 term_gen->index_text (text, 1, prefix);
676 term_gen->index_text (text);
678 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
681 /* Remove a name:value term from 'message', (the actual term will be
682 * encoded by prefixing the value with a short prefix). See
683 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
684 * names to prefix values.
686 * This change will not be reflected in the database until the next
687 * call to _notmuch_message_sync. */
688 notmuch_private_status_t
689 _notmuch_message_remove_term (notmuch_message_t *message,
690 const char *prefix_name,
696 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
698 term = talloc_asprintf (message, "%s%s",
699 _find_prefix (prefix_name), value);
701 if (strlen (term) > NOTMUCH_TERM_MAX)
702 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
705 message->doc.remove_term (term);
706 } catch (const Xapian::InvalidArgumentError) {
707 /* We'll let the philosopher's try to wrestle with the
708 * question of whether failing to remove that which was not
709 * there in the first place is failure. For us, we'll silently
710 * consider it all good. */
715 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
719 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
721 notmuch_private_status_t private_status;
722 notmuch_status_t status;
724 status = _notmuch_database_ensure_writable (message->notmuch);
729 return NOTMUCH_STATUS_NULL_POINTER;
731 if (strlen (tag) > NOTMUCH_TAG_MAX)
732 return NOTMUCH_STATUS_TAG_TOO_LONG;
734 private_status = _notmuch_message_add_term (message, "tag", tag);
735 if (private_status) {
736 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
740 if (! message->frozen)
741 _notmuch_message_sync (message);
743 return NOTMUCH_STATUS_SUCCESS;
747 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
749 notmuch_private_status_t private_status;
750 notmuch_status_t status;
752 status = _notmuch_database_ensure_writable (message->notmuch);
757 return NOTMUCH_STATUS_NULL_POINTER;
759 if (strlen (tag) > NOTMUCH_TAG_MAX)
760 return NOTMUCH_STATUS_TAG_TOO_LONG;
762 private_status = _notmuch_message_remove_term (message, "tag", tag);
763 if (private_status) {
764 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
768 if (! message->frozen)
769 _notmuch_message_sync (message);
771 return NOTMUCH_STATUS_SUCCESS;
775 notmuch_message_remove_all_tags (notmuch_message_t *message)
777 notmuch_private_status_t private_status;
778 notmuch_status_t status;
779 notmuch_tags_t *tags;
782 status = _notmuch_database_ensure_writable (message->notmuch);
786 for (tags = notmuch_message_get_tags (message);
787 notmuch_tags_valid (tags);
788 notmuch_tags_move_to_next (tags))
790 tag = notmuch_tags_get (tags);
792 private_status = _notmuch_message_remove_term (message, "tag", tag);
793 if (private_status) {
794 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
799 if (! message->frozen)
800 _notmuch_message_sync (message);
802 return NOTMUCH_STATUS_SUCCESS;
806 notmuch_message_freeze (notmuch_message_t *message)
808 notmuch_status_t status;
810 status = _notmuch_database_ensure_writable (message->notmuch);
816 return NOTMUCH_STATUS_SUCCESS;
820 notmuch_message_thaw (notmuch_message_t *message)
822 notmuch_status_t status;
824 status = _notmuch_database_ensure_writable (message->notmuch);
828 if (message->frozen > 0) {
830 if (message->frozen == 0)
831 _notmuch_message_sync (message);
832 return NOTMUCH_STATUS_SUCCESS;
834 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
839 notmuch_message_destroy (notmuch_message_t *message)
841 talloc_free (message);