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 /* Create a new notmuch_message_t object for an existing document in
63 * Here, 'talloc owner' is an optional talloc context to which the new
64 * message will belong. This allows for the caller to not bother
65 * calling notmuch_message_destroy on the message, and no that all
66 * memory will be reclaimed with 'talloc_owner' is free. The caller
67 * still can call notmuch_message_destroy when finished with the
70 * The 'talloc_owner' argument can also be NULL, in which case the
71 * caller *is* responsible for calling notmuch_message_destroy.
73 * If no document exists in the database with document ID of 'doc_id'
74 * then this function returns NULL and optionally sets *status to
75 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
77 * This function can also fail to due lack of available memory,
78 * returning NULL and optionally setting *status to
79 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
81 * The caller can pass NULL for status if uninterested in
82 * distinguishing these two cases.
85 _notmuch_message_create (const void *talloc_owner,
86 notmuch_database_t *notmuch,
88 notmuch_private_status_t *status)
90 notmuch_message_t *message;
93 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
95 message = talloc (talloc_owner, notmuch_message_t);
96 if (unlikely (message == NULL)) {
98 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
102 message->notmuch = notmuch;
103 message->doc_id = doc_id;
108 /* Each of these will be lazily created as needed. */
109 message->message_id = NULL;
110 message->thread_id = NULL;
111 message->in_reply_to = NULL;
112 message->filename = NULL;
113 message->message_file = NULL;
114 message->author = NULL;
116 message->replies = _notmuch_message_list_create (message);
117 if (unlikely (message->replies == NULL)) {
119 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
123 /* This is C++'s creepy "placement new", which is really just an
124 * ugly way to call a constructor for a pre-allocated object. So
125 * it's really not an error to not be checking for OUT_OF_MEMORY
126 * here, since this "new" isn't actually allocating memory. This
127 * is language-design comedy of the wrong kind. */
129 new (&message->doc) Xapian::Document;
131 talloc_set_destructor (message, _notmuch_message_destructor);
134 message->doc = notmuch->xapian_db->get_document (doc_id);
135 } catch (const Xapian::DocNotFoundError &error) {
136 talloc_free (message);
138 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
145 /* Create a new notmuch_message_t object for a specific message ID,
146 * (which may or may not already exist in the database).
148 * The 'notmuch' database will be the talloc owner of the returned
151 * If there is already a document with message ID 'message_id' in the
152 * database, then the returned message can be used to query/modify the
153 * document. Otherwise, a new document will be inserted into the
154 * database before this function returns, (and *status will be set
155 * to NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND).
157 * If an error occurs, this function will return NULL and *status
158 * will be set as appropriate. (The status pointer argument must
162 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
163 const char *message_id,
164 notmuch_private_status_t *status_ret)
166 notmuch_message_t *message;
167 Xapian::Document doc;
168 Xapian::WritableDatabase *db;
172 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
174 message = notmuch_database_find_message (notmuch, message_id);
176 return talloc_steal (notmuch, message);
178 term = talloc_asprintf (NULL, "%s%s",
179 _find_prefix ("id"), message_id);
181 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
185 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
186 INTERNAL_ERROR ("Failure to ensure database is writable.");
188 db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
190 doc.add_term (term, 0);
193 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
195 doc_id = db->add_document (doc);
196 } catch (const Xapian::Error &error) {
197 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
198 error.get_msg().c_str());
199 notmuch->exception_reported = TRUE;
200 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
204 message = _notmuch_message_create (notmuch, notmuch,
207 /* We want to inform the caller that we had to create a new
209 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
210 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
216 notmuch_message_get_message_id (notmuch_message_t *message)
218 Xapian::TermIterator i;
220 if (message->message_id)
221 return message->message_id;
223 i = message->doc.termlist_begin ();
224 i.skip_to (_find_prefix ("id"));
226 if (i == message->doc.termlist_end ())
227 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
230 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
232 #if DEBUG_DATABASE_SANITY
235 if (i != message->doc.termlist_end () &&
236 strncmp ((*i).c_str (), _find_prefix ("id"),
237 strlen (_find_prefix ("id"))) == 0)
239 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
244 return message->message_id;
248 _notmuch_message_ensure_message_file (notmuch_message_t *message)
250 const char *filename;
252 if (message->message_file)
255 filename = notmuch_message_get_filename (message);
256 if (unlikely (filename == NULL))
259 message->message_file = _notmuch_message_file_open_ctx (message, filename);
263 notmuch_message_get_header (notmuch_message_t *message, const char *header)
265 _notmuch_message_ensure_message_file (message);
266 if (message->message_file == NULL)
269 return notmuch_message_file_get_header (message->message_file, header);
272 /* Return the message ID from the In-Reply-To header of 'message'.
274 * Returns an empty string ("") if 'message' has no In-Reply-To
277 * Returns NULL if any error occurs.
280 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
282 const char *prefix = _find_prefix ("replyto");
283 int prefix_len = strlen (prefix);
284 Xapian::TermIterator i;
285 std::string in_reply_to;
287 if (message->in_reply_to)
288 return message->in_reply_to;
290 i = message->doc.termlist_begin ();
293 if (i != message->doc.termlist_end ())
296 /* It's perfectly valid for a message to have no In-Reply-To
297 * header. For these cases, we return an empty string. */
298 if (i == message->doc.termlist_end () ||
299 strncmp (in_reply_to.c_str (), prefix, prefix_len))
301 message->in_reply_to = talloc_strdup (message, "");
302 return message->in_reply_to;
305 message->in_reply_to = talloc_strdup (message,
306 in_reply_to.c_str () + prefix_len);
308 #if DEBUG_DATABASE_SANITY
313 if (i != message->doc.termlist_end () &&
314 strncmp ((*i).c_str (), prefix, prefix_len) == 0)
316 INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
317 notmuch_message_get_message_id (message),
318 message->in_reply_to,
319 (*i).c_str () + prefix_len);
323 return message->in_reply_to;
327 notmuch_message_get_thread_id (notmuch_message_t *message)
329 const char *prefix = _find_prefix ("thread");
330 Xapian::TermIterator i;
333 /* This code is written with the assumption that "thread" has a
334 * single-character prefix. */
335 assert (strlen (prefix) == 1);
337 if (message->thread_id)
338 return message->thread_id;
340 i = message->doc.termlist_begin ();
343 if (i != message->doc.termlist_end ())
346 if (i == message->doc.termlist_end () || id[0] != *prefix)
347 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
350 message->thread_id = talloc_strdup (message, id.c_str () + 1);
352 #if DEBUG_DATABASE_SANITY
356 if (i != message->doc.termlist_end () && id[0] == *prefix)
358 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
359 notmuch_message_get_message_id (message),
365 return message->thread_id;
369 _notmuch_message_add_reply (notmuch_message_t *message,
370 notmuch_message_node_t *reply)
372 _notmuch_message_list_append (message->replies, reply);
376 notmuch_message_get_replies (notmuch_message_t *message)
378 return _notmuch_messages_create (message->replies);
381 /* Add an additional 'filename' for 'message'.
383 * This change will not be reflected in the database until the next
384 * call to _notmuch_message_set_sync. */
386 _notmuch_message_add_filename (notmuch_message_t *message,
387 const char *filename)
389 notmuch_status_t status;
390 void *local = talloc_new (message);
393 if (message->filename) {
394 talloc_free (message->filename);
395 message->filename = NULL;
398 if (filename == NULL)
399 INTERNAL_ERROR ("Message filename cannot be NULL.");
401 status = _notmuch_database_filename_to_direntry (local,
403 filename, &direntry);
407 _notmuch_message_add_term (message, "file-direntry", direntry);
411 return NOTMUCH_STATUS_SUCCESS;
415 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
417 return talloc_strdup (message, message->doc.get_data ().c_str ());
421 _notmuch_message_clear_data (notmuch_message_t *message)
423 message->doc.set_data ("");
427 notmuch_message_get_filename (notmuch_message_t *message)
429 const char *prefix = _find_prefix ("file-direntry");
430 int prefix_len = strlen (prefix);
431 Xapian::TermIterator i;
432 char *colon, *direntry = NULL;
433 const char *db_path, *directory, *basename;
434 unsigned int directory_id;
435 void *local = talloc_new (message);
437 if (message->filename)
438 return message->filename;
440 i = message->doc.termlist_begin ();
443 if (i != message->doc.termlist_end ())
444 direntry = talloc_strdup (local, (*i).c_str ());
446 if (i == message->doc.termlist_end () ||
447 strncmp (direntry, prefix, prefix_len))
449 /* A message document created by an old version of notmuch
450 * (prior to rename support) will have the filename in the
451 * data of the document rather than as a file-direntry term.
453 * It would be nice to do the upgrade of the document directly
454 * here, but the database is likely open in read-only mode. */
457 data = message->doc.get_data ().c_str ();
460 INTERNAL_ERROR ("message with no filename");
462 message->filename = talloc_strdup (message, data);
464 return message->filename;
467 direntry += prefix_len;
469 directory_id = strtol (direntry, &colon, 10);
471 if (colon == NULL || *colon != ':')
472 INTERNAL_ERROR ("malformed direntry");
474 basename = colon + 1;
478 db_path = notmuch_database_get_path (message->notmuch);
480 directory = _notmuch_database_get_directory_path (local,
484 if (strlen (directory))
485 message->filename = talloc_asprintf (message, "%s/%s/%s",
486 db_path, directory, basename);
488 message->filename = talloc_asprintf (message, "%s/%s",
490 talloc_free ((void *) directory);
494 return message->filename;
498 notmuch_message_get_flag (notmuch_message_t *message,
499 notmuch_message_flag_t flag)
501 return message->flags & (1 << flag);
505 notmuch_message_set_flag (notmuch_message_t *message,
506 notmuch_message_flag_t flag, notmuch_bool_t enable)
509 message->flags |= (1 << flag);
511 message->flags &= ~(1 << flag);
515 notmuch_message_get_date (notmuch_message_t *message)
520 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
521 } catch (Xapian::Error &error) {
522 INTERNAL_ERROR ("Failed to read timestamp value from document.");
526 return Xapian::sortable_unserialise (value);
530 notmuch_message_get_tags (notmuch_message_t *message)
532 Xapian::TermIterator i, end;
533 i = message->doc.termlist_begin();
534 end = message->doc.termlist_end();
535 return _notmuch_convert_tags(message, i, end);
539 notmuch_message_get_author (notmuch_message_t *message)
541 return message->author;
545 notmuch_message_set_author (notmuch_message_t *message,
549 talloc_free(message->author);
550 message->author = talloc_strdup(message, author);
555 _notmuch_message_set_date (notmuch_message_t *message,
560 /* GMime really doesn't want to see a NULL date, so protect its
562 if (date == NULL || *date == '\0')
565 time_value = g_mime_utils_header_decode_date (date, NULL);
567 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
568 Xapian::sortable_serialise (time_value));
571 /* Synchronize changes made to message->doc out into the database. */
573 _notmuch_message_sync (notmuch_message_t *message)
575 Xapian::WritableDatabase *db;
577 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
580 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
581 db->replace_document (message->doc_id, message->doc);
584 /* Ensure that 'message' is not holding any file object open. Future
585 * calls to various functions will still automatically open the
586 * message file as needed.
589 _notmuch_message_close (notmuch_message_t *message)
591 if (message->message_file) {
592 notmuch_message_file_close (message->message_file);
593 message->message_file = NULL;
597 /* Add a name:value term to 'message', (the actual term will be
598 * encoded by prefixing the value with a short prefix). See
599 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
600 * names to prefix values.
602 * This change will not be reflected in the database until the next
603 * call to _notmuch_message_set_sync. */
604 notmuch_private_status_t
605 _notmuch_message_add_term (notmuch_message_t *message,
606 const char *prefix_name,
613 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
615 term = talloc_asprintf (message, "%s%s",
616 _find_prefix (prefix_name), value);
618 if (strlen (term) > NOTMUCH_TERM_MAX)
619 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
621 message->doc.add_term (term, 0);
625 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
628 /* Parse 'text' and add a term to 'message' for each parsed word. Each
629 * term will be added both prefixed (if prefix_name is not NULL) and
630 * also unprefixed). */
631 notmuch_private_status_t
632 _notmuch_message_gen_terms (notmuch_message_t *message,
633 const char *prefix_name,
636 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
639 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
641 term_gen->set_document (message->doc);
644 const char *prefix = _find_prefix (prefix_name);
646 term_gen->index_text (text, 1, prefix);
649 term_gen->index_text (text);
651 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
654 /* Remove a name:value term from 'message', (the actual term will be
655 * encoded by prefixing the value with a short prefix). See
656 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
657 * names to prefix values.
659 * This change will not be reflected in the database until the next
660 * call to _notmuch_message_set_sync. */
661 notmuch_private_status_t
662 _notmuch_message_remove_term (notmuch_message_t *message,
663 const char *prefix_name,
669 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
671 term = talloc_asprintf (message, "%s%s",
672 _find_prefix (prefix_name), value);
674 if (strlen (term) > NOTMUCH_TERM_MAX)
675 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
678 message->doc.remove_term (term);
679 } catch (const Xapian::InvalidArgumentError) {
680 /* We'll let the philosopher's try to wrestle with the
681 * question of whether failing to remove that which was not
682 * there in the first place is failure. For us, we'll silently
683 * consider it all good. */
688 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
692 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
694 notmuch_private_status_t private_status;
695 notmuch_status_t status;
697 status = _notmuch_database_ensure_writable (message->notmuch);
702 return NOTMUCH_STATUS_NULL_POINTER;
704 if (strlen (tag) > NOTMUCH_TAG_MAX)
705 return NOTMUCH_STATUS_TAG_TOO_LONG;
707 private_status = _notmuch_message_add_term (message, "tag", tag);
708 if (private_status) {
709 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
713 if (! message->frozen)
714 _notmuch_message_sync (message);
716 return NOTMUCH_STATUS_SUCCESS;
720 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
722 notmuch_private_status_t private_status;
723 notmuch_status_t status;
725 status = _notmuch_database_ensure_writable (message->notmuch);
730 return NOTMUCH_STATUS_NULL_POINTER;
732 if (strlen (tag) > NOTMUCH_TAG_MAX)
733 return NOTMUCH_STATUS_TAG_TOO_LONG;
735 private_status = _notmuch_message_remove_term (message, "tag", tag);
736 if (private_status) {
737 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
741 if (! message->frozen)
742 _notmuch_message_sync (message);
744 return NOTMUCH_STATUS_SUCCESS;
748 notmuch_message_remove_all_tags (notmuch_message_t *message)
750 notmuch_private_status_t private_status;
751 notmuch_status_t status;
752 notmuch_tags_t *tags;
755 status = _notmuch_database_ensure_writable (message->notmuch);
759 for (tags = notmuch_message_get_tags (message);
760 notmuch_tags_valid (tags);
761 notmuch_tags_move_to_next (tags))
763 tag = notmuch_tags_get (tags);
765 private_status = _notmuch_message_remove_term (message, "tag", tag);
766 if (private_status) {
767 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
772 if (! message->frozen)
773 _notmuch_message_sync (message);
775 return NOTMUCH_STATUS_SUCCESS;
779 notmuch_message_freeze (notmuch_message_t *message)
781 notmuch_status_t status;
783 status = _notmuch_database_ensure_writable (message->notmuch);
789 return NOTMUCH_STATUS_SUCCESS;
793 notmuch_message_thaw (notmuch_message_t *message)
795 notmuch_status_t status;
797 status = _notmuch_database_ensure_writable (message->notmuch);
801 if (message->frozen > 0) {
803 if (message->frozen == 0)
804 _notmuch_message_sync (message);
805 return NOTMUCH_STATUS_SUCCESS;
807 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
812 notmuch_message_destroy (notmuch_message_t *message)
814 talloc_free (message);