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;
38 notmuch_message_file_t *message_file;
39 notmuch_message_list_t *replies;
45 /* We end up having to call the destructor explicitly because we had
46 * to use "placement new" in order to initialize C++ objects within a
47 * block that we allocated with talloc. So C++ is making talloc
48 * slightly less simple to use, (we wouldn't need
49 * talloc_set_destructor at all otherwise).
52 _notmuch_message_destructor (notmuch_message_t *message)
54 message->doc.~Document ();
59 /* Create a new notmuch_message_t object for an existing document in
62 * Here, 'talloc owner' is an optional talloc context to which the new
63 * message will belong. This allows for the caller to not bother
64 * calling notmuch_message_destroy on the message, and no that all
65 * memory will be reclaimed with 'talloc_owner' is free. The caller
66 * still can call notmuch_message_destroy when finished with the
69 * The 'talloc_owner' argument can also be NULL, in which case the
70 * caller *is* responsible for calling notmuch_message_destroy.
72 * If no document exists in the database with document ID of 'doc_id'
73 * then this function returns NULL and optionally sets *status to
74 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
76 * This function can also fail to due lack of available memory,
77 * returning NULL and optionally setting *status to
78 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
80 * The caller can pass NULL for status if uninterested in
81 * distinguishing these two cases.
84 _notmuch_message_create (const void *talloc_owner,
85 notmuch_database_t *notmuch,
87 notmuch_private_status_t *status)
89 notmuch_message_t *message;
92 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
94 message = talloc (talloc_owner, notmuch_message_t);
95 if (unlikely (message == NULL)) {
97 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
101 message->notmuch = notmuch;
102 message->doc_id = doc_id;
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->filename = NULL;
112 message->message_file = NULL;
114 message->replies = _notmuch_message_list_create (message);
115 if (unlikely (message->replies == NULL)) {
117 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
121 /* This is C++'s creepy "placement new", which is really just an
122 * ugly way to call a constructor for a pre-allocated object. So
123 * it's really not an error to not be checking for OUT_OF_MEMORY
124 * here, since this "new" isn't actually allocating memory. This
125 * is language-design comedy of the wrong kind. */
127 new (&message->doc) Xapian::Document;
129 talloc_set_destructor (message, _notmuch_message_destructor);
132 message->doc = notmuch->xapian_db->get_document (doc_id);
133 } catch (const Xapian::DocNotFoundError &error) {
134 talloc_free (message);
136 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
143 /* Create a new notmuch_message_t object for a specific message ID,
144 * (which may or may not already exist in the database).
146 * The 'notmuch' database will be the talloc owner of the returned
149 * If there is already a document with message ID 'message_id' in the
150 * database, then the returned message can be used to query/modify the
151 * document. Otherwise, a new document will be inserted into the
152 * database before this function returns, (and *status will be set
153 * to NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND).
155 * If an error occurs, this function will return NULL and *status
156 * will be set as appropriate. (The status pointer argument must
160 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
161 const char *message_id,
162 notmuch_private_status_t *status_ret)
164 notmuch_message_t *message;
165 Xapian::Document doc;
166 Xapian::WritableDatabase *db;
170 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
172 message = notmuch_database_find_message (notmuch, message_id);
174 return talloc_steal (notmuch, message);
176 term = talloc_asprintf (NULL, "%s%s",
177 _find_prefix ("id"), message_id);
179 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
183 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
184 INTERNAL_ERROR ("Failure to ensure database is writable.");
186 db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
188 doc.add_term (term, 0);
191 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
193 doc_id = db->add_document (doc);
194 } catch (const Xapian::Error &error) {
195 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
196 error.get_msg().c_str());
197 notmuch->exception_reported = TRUE;
198 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
202 message = _notmuch_message_create (notmuch, notmuch,
205 /* We want to inform the caller that we had to create a new
207 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
208 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
214 notmuch_message_get_message_id (notmuch_message_t *message)
216 Xapian::TermIterator i;
218 if (message->message_id)
219 return message->message_id;
221 i = message->doc.termlist_begin ();
222 i.skip_to (_find_prefix ("id"));
224 if (i == message->doc.termlist_end ())
225 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
228 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
230 #if DEBUG_DATABASE_SANITY
233 if (i != message->doc.termlist_end () &&
234 strncmp ((*i).c_str (), _find_prefix ("id"),
235 strlen (_find_prefix ("id"))) == 0)
237 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
242 return message->message_id;
246 _notmuch_message_ensure_message_file (notmuch_message_t *message)
248 const char *filename;
250 if (message->message_file)
253 filename = notmuch_message_get_filename (message);
254 if (unlikely (filename == NULL))
257 message->message_file = _notmuch_message_file_open_ctx (message, filename);
261 notmuch_message_get_header (notmuch_message_t *message, const char *header)
263 _notmuch_message_ensure_message_file (message);
264 if (message->message_file == NULL)
267 return notmuch_message_file_get_header (message->message_file, header);
270 /* Return the message ID from the In-Reply-To header of 'message'.
272 * Returns an empty string ("") if 'message' has no In-Reply-To
275 * Returns NULL if any error occurs.
278 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
280 const char *prefix = _find_prefix ("replyto");
281 int prefix_len = strlen (prefix);
282 Xapian::TermIterator i;
283 std::string in_reply_to;
285 if (message->in_reply_to)
286 return message->in_reply_to;
288 i = message->doc.termlist_begin ();
291 if (i != message->doc.termlist_end ())
294 /* It's perfectly valid for a message to have no In-Reply-To
295 * header. For these cases, we return an empty string. */
296 if (i == message->doc.termlist_end () ||
297 strncmp (in_reply_to.c_str (), prefix, prefix_len))
299 message->in_reply_to = talloc_strdup (message, "");
300 return message->in_reply_to;
303 message->in_reply_to = talloc_strdup (message,
304 in_reply_to.c_str () + prefix_len);
306 #if DEBUG_DATABASE_SANITY
311 if (i != message->doc.termlist_end () &&
312 strncmp ((*i).c_str (), prefix, prefix_len) == 0)
314 INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
315 notmuch_message_get_message_id (message),
316 message->in_reply_to,
317 (*i).c_str () + prefix_len);
321 return message->in_reply_to;
325 notmuch_message_get_thread_id (notmuch_message_t *message)
327 const char *prefix = _find_prefix ("thread");
328 Xapian::TermIterator i;
331 /* This code is written with the assumption that "thread" has a
332 * single-character prefix. */
333 assert (strlen (prefix) == 1);
335 if (message->thread_id)
336 return message->thread_id;
338 i = message->doc.termlist_begin ();
341 if (i != message->doc.termlist_end ())
344 if (i == message->doc.termlist_end () || id[0] != *prefix)
345 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
348 message->thread_id = talloc_strdup (message, id.c_str () + 1);
350 #if DEBUG_DATABASE_SANITY
354 if (i != message->doc.termlist_end () && id[0] == *prefix)
356 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
357 notmuch_message_get_message_id (message),
363 return message->thread_id;
367 _notmuch_message_add_reply (notmuch_message_t *message,
368 notmuch_message_node_t *reply)
370 _notmuch_message_list_append (message->replies, reply);
374 notmuch_message_get_replies (notmuch_message_t *message)
376 return _notmuch_messages_create (message->replies);
379 /* Add an additional 'filename' for 'message'.
381 * This change will not be reflected in the database until the next
382 * call to _notmuch_message_set_sync. */
384 _notmuch_message_add_filename (notmuch_message_t *message,
385 const char *filename)
387 notmuch_status_t status;
388 void *local = talloc_new (message);
391 if (message->filename) {
392 talloc_free (message->filename);
393 message->filename = NULL;
396 if (filename == NULL)
397 INTERNAL_ERROR ("Message filename cannot be NULL.");
399 status = _notmuch_database_filename_to_direntry (local,
401 filename, &direntry);
405 _notmuch_message_add_term (message, "file-direntry", direntry);
409 return NOTMUCH_STATUS_SUCCESS;
413 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
415 return talloc_strdup (message, message->doc.get_data ().c_str ());
419 _notmuch_message_clear_data (notmuch_message_t *message)
421 message->doc.set_data ("");
425 notmuch_message_get_filename (notmuch_message_t *message)
427 const char *prefix = _find_prefix ("file-direntry");
428 int prefix_len = strlen (prefix);
429 Xapian::TermIterator i;
430 char *direntry, *colon;
431 const char *db_path, *directory, *basename;
432 unsigned int directory_id;
433 void *local = talloc_new (message);
435 if (message->filename)
436 return message->filename;
438 i = message->doc.termlist_begin ();
441 if (i != message->doc.termlist_end ())
442 direntry = talloc_strdup (local, (*i).c_str ());
444 if (i == message->doc.termlist_end () ||
445 strncmp (direntry, prefix, prefix_len))
447 /* A message document created by an old version of notmuch
448 * (prior to rename support) will have the filename in the
449 * data of the document rather than as a file-direntry term.
451 * It would be nice to do the upgrade of the document directly
452 * here, but the database is likely open in read-only mode. */
455 data = message->doc.get_data ().c_str ();
458 INTERNAL_ERROR ("message with no filename");
460 message->filename = talloc_strdup (message, data);
462 return message->filename;
465 direntry += prefix_len;
467 directory_id = strtol (direntry, &colon, 10);
469 if (colon == NULL || *colon != ':')
470 INTERNAL_ERROR ("malformed direntry");
472 basename = colon + 1;
476 db_path = notmuch_database_get_path (message->notmuch);
478 directory = _notmuch_database_get_directory_path (local,
482 if (strlen (directory))
483 message->filename = talloc_asprintf (message, "%s/%s/%s",
484 db_path, directory, basename);
486 message->filename = talloc_asprintf (message, "%s/%s",
488 talloc_free ((void *) directory);
492 return message->filename;
496 notmuch_message_get_flag (notmuch_message_t *message,
497 notmuch_message_flag_t flag)
499 return message->flags & (1 << flag);
503 notmuch_message_set_flag (notmuch_message_t *message,
504 notmuch_message_flag_t flag, notmuch_bool_t enable)
507 message->flags |= (1 << flag);
509 message->flags &= ~(1 << flag);
513 notmuch_message_get_date (notmuch_message_t *message)
518 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
519 } catch (Xapian::Error &error) {
520 INTERNAL_ERROR ("Failed to read timestamp value from document.");
524 return Xapian::sortable_unserialise (value);
528 notmuch_message_get_tags (notmuch_message_t *message)
530 Xapian::TermIterator i, end;
531 i = message->doc.termlist_begin();
532 end = message->doc.termlist_end();
533 return _notmuch_convert_tags(message, i, end);
537 _notmuch_message_set_date (notmuch_message_t *message,
542 /* GMime really doesn't want to see a NULL date, so protect its
544 if (date == NULL || *date == '\0')
547 time_value = g_mime_utils_header_decode_date (date, NULL);
549 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
550 Xapian::sortable_serialise (time_value));
553 /* Synchronize changes made to message->doc out into the database. */
555 _notmuch_message_sync (notmuch_message_t *message)
557 Xapian::WritableDatabase *db;
559 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
562 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
563 db->replace_document (message->doc_id, message->doc);
566 /* Ensure that 'message' is not holding any file object open. Future
567 * calls to various functions will still automatically open the
568 * message file as needed.
571 _notmuch_message_close (notmuch_message_t *message)
573 if (message->message_file) {
574 notmuch_message_file_close (message->message_file);
575 message->message_file = NULL;
579 /* Add a name:value term to 'message', (the actual term will be
580 * encoded by prefixing the value with a short prefix). See
581 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
582 * names to prefix values.
584 * This change will not be reflected in the database until the next
585 * call to _notmuch_message_set_sync. */
586 notmuch_private_status_t
587 _notmuch_message_add_term (notmuch_message_t *message,
588 const char *prefix_name,
595 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
597 term = talloc_asprintf (message, "%s%s",
598 _find_prefix (prefix_name), value);
600 if (strlen (term) > NOTMUCH_TERM_MAX)
601 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
603 message->doc.add_term (term, 0);
607 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
610 /* Parse 'text' and add a term to 'message' for each parsed word. Each
611 * term will be added both prefixed (if prefix_name is not NULL) and
612 * also unprefixed). */
613 notmuch_private_status_t
614 _notmuch_message_gen_terms (notmuch_message_t *message,
615 const char *prefix_name,
618 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
621 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
623 term_gen->set_document (message->doc);
626 const char *prefix = _find_prefix (prefix_name);
628 term_gen->index_text (text, 1, prefix);
631 term_gen->index_text (text);
633 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
636 /* Remove a name:value term from 'message', (the actual term will be
637 * encoded by prefixing the value with a short prefix). See
638 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
639 * names to prefix values.
641 * This change will not be reflected in the database until the next
642 * call to _notmuch_message_set_sync. */
643 notmuch_private_status_t
644 _notmuch_message_remove_term (notmuch_message_t *message,
645 const char *prefix_name,
651 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
653 term = talloc_asprintf (message, "%s%s",
654 _find_prefix (prefix_name), value);
656 if (strlen (term) > NOTMUCH_TERM_MAX)
657 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
660 message->doc.remove_term (term);
661 } catch (const Xapian::InvalidArgumentError) {
662 /* We'll let the philosopher's try to wrestle with the
663 * question of whether failing to remove that which was not
664 * there in the first place is failure. For us, we'll silently
665 * consider it all good. */
670 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
674 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
676 notmuch_private_status_t private_status;
677 notmuch_status_t status;
679 status = _notmuch_database_ensure_writable (message->notmuch);
684 return NOTMUCH_STATUS_NULL_POINTER;
686 if (strlen (tag) > NOTMUCH_TAG_MAX)
687 return NOTMUCH_STATUS_TAG_TOO_LONG;
689 private_status = _notmuch_message_add_term (message, "tag", tag);
690 if (private_status) {
691 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
695 if (! message->frozen)
696 _notmuch_message_sync (message);
698 return NOTMUCH_STATUS_SUCCESS;
702 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
704 notmuch_private_status_t private_status;
705 notmuch_status_t status;
707 status = _notmuch_database_ensure_writable (message->notmuch);
712 return NOTMUCH_STATUS_NULL_POINTER;
714 if (strlen (tag) > NOTMUCH_TAG_MAX)
715 return NOTMUCH_STATUS_TAG_TOO_LONG;
717 private_status = _notmuch_message_remove_term (message, "tag", tag);
718 if (private_status) {
719 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
723 if (! message->frozen)
724 _notmuch_message_sync (message);
726 return NOTMUCH_STATUS_SUCCESS;
730 notmuch_message_remove_all_tags (notmuch_message_t *message)
732 notmuch_private_status_t private_status;
733 notmuch_status_t status;
734 notmuch_tags_t *tags;
737 status = _notmuch_database_ensure_writable (message->notmuch);
741 for (tags = notmuch_message_get_tags (message);
742 notmuch_tags_has_more (tags);
743 notmuch_tags_advance (tags))
745 tag = notmuch_tags_get (tags);
747 private_status = _notmuch_message_remove_term (message, "tag", tag);
748 if (private_status) {
749 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
754 if (! message->frozen)
755 _notmuch_message_sync (message);
757 return NOTMUCH_STATUS_SUCCESS;
761 notmuch_message_freeze (notmuch_message_t *message)
763 notmuch_status_t status;
765 status = _notmuch_database_ensure_writable (message->notmuch);
771 return NOTMUCH_STATUS_SUCCESS;
775 notmuch_message_thaw (notmuch_message_t *message)
777 notmuch_status_t status;
779 status = _notmuch_database_ensure_writable (message->notmuch);
783 if (message->frozen > 0) {
785 if (message->frozen == 0)
786 _notmuch_message_sync (message);
787 return NOTMUCH_STATUS_SUCCESS;
789 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
794 notmuch_message_destroy (notmuch_message_t *message)
796 talloc_free (message);