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 /* "128 bits of thread-id ought to be enough for anybody" */
46 #define NOTMUCH_THREAD_ID_BITS 128
47 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
48 typedef struct _thread_id {
49 char str[NOTMUCH_THREAD_ID_DIGITS + 1];
52 /* We end up having to call the destructor explicitly because we had
53 * to use "placement new" in order to initialize C++ objects within a
54 * block that we allocated with talloc. So C++ is making talloc
55 * slightly less simple to use, (we wouldn't need
56 * talloc_set_destructor at all otherwise).
59 _notmuch_message_destructor (notmuch_message_t *message)
61 message->doc.~Document ();
66 /* Create a new notmuch_message_t object for an existing document in
69 * Here, 'talloc owner' is an optional talloc context to which the new
70 * message will belong. This allows for the caller to not bother
71 * calling notmuch_message_destroy on the message, and no that all
72 * memory will be reclaimed with 'talloc_owner' is free. The caller
73 * still can call notmuch_message_destroy when finished with the
76 * The 'talloc_owner' argument can also be NULL, in which case the
77 * caller *is* responsible for calling notmuch_message_destroy.
79 * If no document exists in the database with document ID of 'doc_id'
80 * then this function returns NULL and optionally sets *status to
81 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
83 * This function can also fail to due lack of available memory,
84 * returning NULL and optionally setting *status to
85 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
87 * The caller can pass NULL for status if uninterested in
88 * distinguishing these two cases.
91 _notmuch_message_create (const void *talloc_owner,
92 notmuch_database_t *notmuch,
94 notmuch_private_status_t *status)
96 notmuch_message_t *message;
99 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
101 message = talloc (talloc_owner, notmuch_message_t);
102 if (unlikely (message == NULL)) {
104 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
108 message->notmuch = notmuch;
109 message->doc_id = doc_id;
114 /* Each of these will be lazily created as needed. */
115 message->message_id = NULL;
116 message->thread_id = NULL;
117 message->in_reply_to = NULL;
118 message->filename = NULL;
119 message->message_file = NULL;
121 message->replies = _notmuch_message_list_create (message);
122 if (unlikely (message->replies == NULL)) {
124 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
128 /* This is C++'s creepy "placement new", which is really just an
129 * ugly way to call a constructor for a pre-allocated object. So
130 * it's really not an error to not be checking for OUT_OF_MEMORY
131 * here, since this "new" isn't actually allocating memory. This
132 * is language-design comedy of the wrong kind. */
134 new (&message->doc) Xapian::Document;
136 talloc_set_destructor (message, _notmuch_message_destructor);
139 message->doc = notmuch->xapian_db->get_document (doc_id);
140 } catch (const Xapian::DocNotFoundError &error) {
141 talloc_free (message);
143 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
150 /* Create a new notmuch_message_t object for a specific message ID,
151 * (which may or may not already exist in the database).
153 * The 'notmuch' database will be the talloc owner of the returned
156 * If there is already a document with message ID 'message_id' in the
157 * database, then the returned message can be used to query/modify the
158 * document. Otherwise, a new document will be inserted into the
159 * database before this function returns, (and *status will be set
160 * to NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND).
162 * If an error occurs, this function will return NULL and *status
163 * will be set as appropriate. (The status pointer argument must
167 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
168 const char *message_id,
169 notmuch_private_status_t *status_ret)
171 notmuch_message_t *message;
172 Xapian::Document doc;
173 Xapian::WritableDatabase *db;
177 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
178 *status_ret = NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE;
182 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
184 message = notmuch_database_find_message (notmuch, message_id);
186 return talloc_steal (notmuch, message);
188 term = talloc_asprintf (NULL, "%s%s",
189 _find_prefix ("id"), message_id);
191 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
195 db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
200 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
202 doc_id = db->add_document (doc);
203 } catch (const Xapian::Error &error) {
204 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
205 error.get_msg().c_str());
206 notmuch->exception_reported = TRUE;
207 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
211 message = _notmuch_message_create (notmuch, notmuch,
214 /* We want to inform the caller that we had to create a new
216 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
217 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
223 notmuch_message_get_message_id (notmuch_message_t *message)
225 Xapian::TermIterator i;
227 if (message->message_id)
228 return message->message_id;
230 i = message->doc.termlist_begin ();
231 i.skip_to (_find_prefix ("id"));
233 if (i == message->doc.termlist_end ())
234 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
237 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
239 #if DEBUG_DATABASE_SANITY
242 if (i != message->doc.termlist_end () &&
243 strncmp ((*i).c_str (), _find_prefix ("id"),
244 strlen (_find_prefix ("id"))) == 0)
246 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
251 return message->message_id;
255 _notmuch_message_ensure_message_file (notmuch_message_t *message)
257 const char *filename;
259 if (message->message_file)
262 filename = notmuch_message_get_filename (message);
263 if (unlikely (filename == NULL))
266 message->message_file = _notmuch_message_file_open_ctx (message, filename);
270 notmuch_message_get_header (notmuch_message_t *message, const char *header)
272 _notmuch_message_ensure_message_file (message);
273 if (message->message_file == NULL)
276 return notmuch_message_file_get_header (message->message_file, header);
279 /* Return the message ID from the In-Reply-To header of 'message'.
281 * Returns an empty string ("") if 'message' has no In-Reply-To
284 * Returns NULL if any error occurs.
287 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
289 const char *prefix = _find_prefix ("replyto");
290 int prefix_len = strlen (prefix);
291 Xapian::TermIterator i;
292 std::string in_reply_to;
294 if (message->in_reply_to)
295 return message->in_reply_to;
297 i = message->doc.termlist_begin ();
300 if (i != message->doc.termlist_end ())
303 /* It's perfectly valid for a message to have no In-Reply-To
304 * header. For these cases, we return an empty string. */
305 if (i == message->doc.termlist_end () ||
306 strncmp (in_reply_to.c_str (), prefix, prefix_len))
308 message->in_reply_to = talloc_strdup (message, "");
309 return message->in_reply_to;
312 message->in_reply_to = talloc_strdup (message,
313 in_reply_to.c_str () + prefix_len);
315 #if DEBUG_DATABASE_SANITY
320 if (i != message->doc.termlist_end () &&
321 strncmp ((*i).c_str (), prefix, prefix_len) == 0)
323 INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
324 notmuch_message_get_message_id (message),
325 message->in_reply_to,
326 (*i).c_str () + prefix_len);
330 return message->in_reply_to;
334 notmuch_message_get_thread_id (notmuch_message_t *message)
336 const char *prefix = _find_prefix ("thread");
337 Xapian::TermIterator i;
340 /* This code is written with the assumption that "thread" has a
341 * single-character prefix. */
342 assert (strlen (prefix) == 1);
344 if (message->thread_id)
345 return message->thread_id;
347 i = message->doc.termlist_begin ();
350 if (i != message->doc.termlist_end ())
353 if (i == message->doc.termlist_end () || id[0] != *prefix)
354 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
357 message->thread_id = talloc_strdup (message, id.c_str () + 1);
359 #if DEBUG_DATABASE_SANITY
363 if (i != message->doc.termlist_end () && id[0] == *prefix)
365 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
366 notmuch_message_get_message_id (message),
372 return message->thread_id;
376 _notmuch_message_add_reply (notmuch_message_t *message,
377 notmuch_message_node_t *reply)
379 _notmuch_message_list_append (message->replies, reply);
383 notmuch_message_get_replies (notmuch_message_t *message)
385 return _notmuch_messages_create (message->replies);
388 /* Set the filename for 'message' to 'filename'.
390 * XXX: We should still figure out if we think it's important to store
391 * multiple filenames for email messages with identical message IDs.
393 * This change will not be reflected in the database until the next
394 * call to _notmuch_message_set_sync. */
396 _notmuch_message_set_filename (notmuch_message_t *message,
397 const char *filename)
399 const char *relative, *directory, *basename;
401 Xapian::docid directory_id;
402 notmuch_status_t status;
403 void *local = talloc_new (message);
405 if (message->filename) {
406 talloc_free (message->filename);
407 message->filename = NULL;
410 if (filename == NULL)
411 INTERNAL_ERROR ("Message filename cannot be NULL.");
413 relative = _notmuch_database_relative_path (message->notmuch, filename);
415 status = _notmuch_database_split_path (local, relative,
416 &directory, &basename);
420 status = _notmuch_database_find_directory_id (message->notmuch, directory,
425 term = talloc_asprintf (local, "%s%u:%s",
426 _find_prefix ("direntry"), directory_id, basename);
428 message->doc.add_term (term);
432 return NOTMUCH_STATUS_SUCCESS;
436 notmuch_message_get_filename (notmuch_message_t *message)
438 const char *prefix = _find_prefix ("direntry");
439 int prefix_len = strlen (prefix);
440 Xapian::TermIterator i;
441 char *direntry, *colon;
442 const char *db_path, *directory, *basename;
443 unsigned int directory_id;
444 void *local = talloc_new (message);
446 if (message->filename)
447 return message->filename;
449 i = message->doc.termlist_begin ();
452 if (i != message->doc.termlist_end ())
453 direntry = talloc_strdup (local, (*i).c_str ());
455 if (i == message->doc.termlist_end () ||
456 strncmp (direntry, prefix, prefix_len))
458 INTERNAL_ERROR ("message with no filename");
461 direntry += prefix_len;
463 directory_id = strtol (direntry, &colon, 10);
465 if (colon == NULL || *colon != ':')
466 INTERNAL_ERROR ("malformed direntry");
468 basename = colon + 1;
472 db_path = notmuch_database_get_path (message->notmuch);
474 directory = _notmuch_database_get_directory_path (local,
478 if (strlen (directory))
479 message->filename = talloc_asprintf (message, "%s/%s/%s",
480 db_path, directory, basename);
482 message->filename = talloc_asprintf (message, "%s/%s",
484 talloc_free ((void *) directory);
488 return message->filename;
492 notmuch_message_get_flag (notmuch_message_t *message,
493 notmuch_message_flag_t flag)
495 return message->flags & (1 << flag);
499 notmuch_message_set_flag (notmuch_message_t *message,
500 notmuch_message_flag_t flag, notmuch_bool_t enable)
503 message->flags |= (1 << flag);
505 message->flags &= ~(1 << flag);
509 notmuch_message_get_date (notmuch_message_t *message)
514 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
515 } catch (Xapian::Error &error) {
516 INTERNAL_ERROR ("Failed to read timestamp value from document.");
520 return Xapian::sortable_unserialise (value);
524 notmuch_message_get_tags (notmuch_message_t *message)
526 Xapian::TermIterator i, end;
527 i = message->doc.termlist_begin();
528 end = message->doc.termlist_end();
529 return _notmuch_convert_tags(message, i, end);
533 _notmuch_message_set_date (notmuch_message_t *message,
538 /* GMime really doesn't want to see a NULL date, so protect its
540 if (date == NULL || *date == '\0')
543 time_value = g_mime_utils_header_decode_date (date, NULL);
545 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
546 Xapian::sortable_serialise (time_value));
550 thread_id_generate (thread_id_t *thread_id)
552 static int seeded = 0;
559 dev_random = fopen ("/dev/random", "r");
560 if (dev_random == NULL) {
563 fread ((void *) &value, sizeof (value), 1, dev_random);
571 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
573 sprintf (s, "%08x", value);
579 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
581 /* If not part of any existing thread, generate a new thread_id. */
582 thread_id_t thread_id;
584 thread_id_generate (&thread_id);
585 _notmuch_message_add_term (message, "thread", thread_id.str);
588 /* Synchronize changes made to message->doc out into the database. */
590 _notmuch_message_sync (notmuch_message_t *message)
592 Xapian::WritableDatabase *db;
594 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
597 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
598 db->replace_document (message->doc_id, message->doc);
601 /* Ensure that 'message' is not holding any file object open. Future
602 * calls to various functions will still automatically open the
603 * message file as needed.
606 _notmuch_message_close (notmuch_message_t *message)
608 if (message->message_file) {
609 notmuch_message_file_close (message->message_file);
610 message->message_file = NULL;
614 /* Add a name:value term to 'message', (the actual term will be
615 * encoded by prefixing the value with a short prefix). See
616 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
617 * names to prefix values.
619 * This change will not be reflected in the database until the next
620 * call to _notmuch_message_set_sync. */
621 notmuch_private_status_t
622 _notmuch_message_add_term (notmuch_message_t *message,
623 const char *prefix_name,
630 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
632 term = talloc_asprintf (message, "%s%s",
633 _find_prefix (prefix_name), value);
635 if (strlen (term) > NOTMUCH_TERM_MAX)
636 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
638 message->doc.add_term (term);
642 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
645 /* Parse 'text' and add a term to 'message' for each parsed word. Each
646 * term will be added both prefixed (if prefix_name is not NULL) and
647 * also unprefixed). */
648 notmuch_private_status_t
649 _notmuch_message_gen_terms (notmuch_message_t *message,
650 const char *prefix_name,
653 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
656 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
658 term_gen->set_document (message->doc);
661 const char *prefix = _find_prefix (prefix_name);
663 term_gen->index_text (text, 1, prefix);
666 term_gen->index_text (text);
668 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
671 /* Remove a name:value term from 'message', (the actual term will be
672 * encoded by prefixing the value with a short prefix). See
673 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
674 * names to prefix values.
676 * This change will not be reflected in the database until the next
677 * call to _notmuch_message_set_sync. */
678 notmuch_private_status_t
679 _notmuch_message_remove_term (notmuch_message_t *message,
680 const char *prefix_name,
686 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
688 term = talloc_asprintf (message, "%s%s",
689 _find_prefix (prefix_name), value);
691 if (strlen (term) > NOTMUCH_TERM_MAX)
692 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
695 message->doc.remove_term (term);
696 } catch (const Xapian::InvalidArgumentError) {
697 /* We'll let the philosopher's try to wrestle with the
698 * question of whether failing to remove that which was not
699 * there in the first place is failure. For us, we'll silently
700 * consider it all good. */
705 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
709 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
711 notmuch_private_status_t status;
714 return NOTMUCH_STATUS_NULL_POINTER;
716 if (strlen (tag) > NOTMUCH_TAG_MAX)
717 return NOTMUCH_STATUS_TAG_TOO_LONG;
719 status = _notmuch_message_add_term (message, "tag", tag);
721 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
725 if (! message->frozen)
726 _notmuch_message_sync (message);
728 return NOTMUCH_STATUS_SUCCESS;
732 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
734 notmuch_private_status_t status;
737 return NOTMUCH_STATUS_NULL_POINTER;
739 if (strlen (tag) > NOTMUCH_TAG_MAX)
740 return NOTMUCH_STATUS_TAG_TOO_LONG;
742 status = _notmuch_message_remove_term (message, "tag", tag);
744 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
748 if (! message->frozen)
749 _notmuch_message_sync (message);
751 return NOTMUCH_STATUS_SUCCESS;
755 notmuch_message_remove_all_tags (notmuch_message_t *message)
757 notmuch_private_status_t status;
758 notmuch_tags_t *tags;
761 for (tags = notmuch_message_get_tags (message);
762 notmuch_tags_has_more (tags);
763 notmuch_tags_advance (tags))
765 tag = notmuch_tags_get (tags);
767 status = _notmuch_message_remove_term (message, "tag", tag);
769 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
774 if (! message->frozen)
775 _notmuch_message_sync (message);
779 notmuch_message_freeze (notmuch_message_t *message)
785 notmuch_message_thaw (notmuch_message_t *message)
787 if (message->frozen > 0) {
789 if (message->frozen == 0)
790 _notmuch_message_sync (message);
791 return NOTMUCH_STATUS_SUCCESS;
793 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
798 notmuch_message_destroy (notmuch_message_t *message)
800 talloc_free (message);