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 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
179 message = notmuch_database_find_message (notmuch, message_id);
181 return talloc_steal (notmuch, message);
183 term = talloc_asprintf (NULL, "%s%s",
184 _find_prefix ("id"), message_id);
186 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
190 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
191 INTERNAL_ERROR ("Failure to ensure database is writable.");
193 db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
198 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
200 doc_id = db->add_document (doc);
201 } catch (const Xapian::Error &error) {
202 fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
203 error.get_msg().c_str());
204 notmuch->exception_reported = TRUE;
205 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
209 message = _notmuch_message_create (notmuch, notmuch,
212 /* We want to inform the caller that we had to create a new
214 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
215 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
221 notmuch_message_get_message_id (notmuch_message_t *message)
223 Xapian::TermIterator i;
225 if (message->message_id)
226 return message->message_id;
228 i = message->doc.termlist_begin ();
229 i.skip_to (_find_prefix ("id"));
231 if (i == message->doc.termlist_end ())
232 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
235 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
237 #if DEBUG_DATABASE_SANITY
240 if (i != message->doc.termlist_end () &&
241 strncmp ((*i).c_str (), _find_prefix ("id"),
242 strlen (_find_prefix ("id"))) == 0)
244 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
249 return message->message_id;
253 _notmuch_message_ensure_message_file (notmuch_message_t *message)
255 const char *filename;
257 if (message->message_file)
260 filename = notmuch_message_get_filename (message);
261 if (unlikely (filename == NULL))
264 message->message_file = _notmuch_message_file_open_ctx (message, filename);
268 notmuch_message_get_header (notmuch_message_t *message, const char *header)
270 _notmuch_message_ensure_message_file (message);
271 if (message->message_file == NULL)
274 return notmuch_message_file_get_header (message->message_file, header);
277 /* Return the message ID from the In-Reply-To header of 'message'.
279 * Returns an empty string ("") if 'message' has no In-Reply-To
282 * Returns NULL if any error occurs.
285 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
287 const char *prefix = _find_prefix ("replyto");
288 int prefix_len = strlen (prefix);
289 Xapian::TermIterator i;
290 std::string in_reply_to;
292 if (message->in_reply_to)
293 return message->in_reply_to;
295 i = message->doc.termlist_begin ();
298 if (i != message->doc.termlist_end ())
301 /* It's perfectly valid for a message to have no In-Reply-To
302 * header. For these cases, we return an empty string. */
303 if (i == message->doc.termlist_end () ||
304 strncmp (in_reply_to.c_str (), prefix, prefix_len))
306 message->in_reply_to = talloc_strdup (message, "");
307 return message->in_reply_to;
310 message->in_reply_to = talloc_strdup (message,
311 in_reply_to.c_str () + prefix_len);
313 #if DEBUG_DATABASE_SANITY
318 if (i != message->doc.termlist_end () &&
319 strncmp ((*i).c_str (), prefix, prefix_len) == 0)
321 INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
322 notmuch_message_get_message_id (message),
323 message->in_reply_to,
324 (*i).c_str () + prefix_len);
328 return message->in_reply_to;
332 notmuch_message_get_thread_id (notmuch_message_t *message)
334 const char *prefix = _find_prefix ("thread");
335 Xapian::TermIterator i;
338 /* This code is written with the assumption that "thread" has a
339 * single-character prefix. */
340 assert (strlen (prefix) == 1);
342 if (message->thread_id)
343 return message->thread_id;
345 i = message->doc.termlist_begin ();
348 if (i != message->doc.termlist_end ())
351 if (i == message->doc.termlist_end () || id[0] != *prefix)
352 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
355 message->thread_id = talloc_strdup (message, id.c_str () + 1);
357 #if DEBUG_DATABASE_SANITY
361 if (i != message->doc.termlist_end () && id[0] == *prefix)
363 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
364 notmuch_message_get_message_id (message),
370 return message->thread_id;
374 _notmuch_message_add_reply (notmuch_message_t *message,
375 notmuch_message_node_t *reply)
377 _notmuch_message_list_append (message->replies, reply);
381 notmuch_message_get_replies (notmuch_message_t *message)
383 return _notmuch_messages_create (message->replies);
386 /* Add an additional 'filename' for 'message'.
388 * This change will not be reflected in the database until the next
389 * call to _notmuch_message_set_sync. */
391 _notmuch_message_add_filename (notmuch_message_t *message,
392 const char *filename)
394 notmuch_status_t status;
395 void *local = talloc_new (message);
398 if (message->filename) {
399 talloc_free (message->filename);
400 message->filename = NULL;
403 if (filename == NULL)
404 INTERNAL_ERROR ("Message filename cannot be NULL.");
406 status = _notmuch_database_filename_to_direntry (local,
408 filename, &direntry);
412 _notmuch_message_add_term (message, "file-direntry", direntry);
416 return NOTMUCH_STATUS_SUCCESS;
420 notmuch_message_get_filename (notmuch_message_t *message)
422 const char *prefix = _find_prefix ("file-direntry");
423 int prefix_len = strlen (prefix);
424 Xapian::TermIterator i;
425 char *direntry, *colon;
426 const char *db_path, *directory, *basename;
427 unsigned int directory_id;
428 void *local = talloc_new (message);
430 if (message->filename)
431 return message->filename;
433 i = message->doc.termlist_begin ();
436 if (i != message->doc.termlist_end ())
437 direntry = talloc_strdup (local, (*i).c_str ());
439 if (i == message->doc.termlist_end () ||
440 strncmp (direntry, prefix, prefix_len))
442 /* A message document created by an old version of notmuch
443 * (prior to rename support) will have the filename in the
444 * data of the document rather than as a file-direntry term. */
447 data = message->doc.get_data ().c_str ();
450 INTERNAL_ERROR ("message with no filename");
452 message->filename = talloc_strdup (message, data);
454 return message->filename;
457 direntry += prefix_len;
459 directory_id = strtol (direntry, &colon, 10);
461 if (colon == NULL || *colon != ':')
462 INTERNAL_ERROR ("malformed direntry");
464 basename = colon + 1;
468 db_path = notmuch_database_get_path (message->notmuch);
470 directory = _notmuch_database_get_directory_path (local,
474 if (strlen (directory))
475 message->filename = talloc_asprintf (message, "%s/%s/%s",
476 db_path, directory, basename);
478 message->filename = talloc_asprintf (message, "%s/%s",
480 talloc_free ((void *) directory);
484 return message->filename;
488 notmuch_message_get_flag (notmuch_message_t *message,
489 notmuch_message_flag_t flag)
491 return message->flags & (1 << flag);
495 notmuch_message_set_flag (notmuch_message_t *message,
496 notmuch_message_flag_t flag, notmuch_bool_t enable)
499 message->flags |= (1 << flag);
501 message->flags &= ~(1 << flag);
505 notmuch_message_get_date (notmuch_message_t *message)
510 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
511 } catch (Xapian::Error &error) {
512 INTERNAL_ERROR ("Failed to read timestamp value from document.");
516 return Xapian::sortable_unserialise (value);
520 notmuch_message_get_tags (notmuch_message_t *message)
522 Xapian::TermIterator i, end;
523 i = message->doc.termlist_begin();
524 end = message->doc.termlist_end();
525 return _notmuch_convert_tags(message, i, end);
529 _notmuch_message_set_date (notmuch_message_t *message,
534 /* GMime really doesn't want to see a NULL date, so protect its
536 if (date == NULL || *date == '\0')
539 time_value = g_mime_utils_header_decode_date (date, NULL);
541 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
542 Xapian::sortable_serialise (time_value));
546 thread_id_generate (thread_id_t *thread_id)
548 static int seeded = 0;
555 dev_random = fopen ("/dev/random", "r");
556 if (dev_random == NULL) {
559 fread ((void *) &value, sizeof (value), 1, dev_random);
567 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
569 sprintf (s, "%08x", value);
575 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
577 /* If not part of any existing thread, generate a new thread_id. */
578 thread_id_t thread_id;
580 thread_id_generate (&thread_id);
581 _notmuch_message_add_term (message, "thread", thread_id.str);
584 /* Synchronize changes made to message->doc out into the database. */
586 _notmuch_message_sync (notmuch_message_t *message)
588 Xapian::WritableDatabase *db;
590 if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
593 db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
594 db->replace_document (message->doc_id, message->doc);
597 /* Ensure that 'message' is not holding any file object open. Future
598 * calls to various functions will still automatically open the
599 * message file as needed.
602 _notmuch_message_close (notmuch_message_t *message)
604 if (message->message_file) {
605 notmuch_message_file_close (message->message_file);
606 message->message_file = NULL;
610 /* Add a name:value term to 'message', (the actual term will be
611 * encoded by prefixing the value with a short prefix). See
612 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
613 * names to prefix values.
615 * This change will not be reflected in the database until the next
616 * call to _notmuch_message_set_sync. */
617 notmuch_private_status_t
618 _notmuch_message_add_term (notmuch_message_t *message,
619 const char *prefix_name,
626 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
628 term = talloc_asprintf (message, "%s%s",
629 _find_prefix (prefix_name), value);
631 if (strlen (term) > NOTMUCH_TERM_MAX)
632 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
634 message->doc.add_term (term);
638 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
641 /* Parse 'text' and add a term to 'message' for each parsed word. Each
642 * term will be added both prefixed (if prefix_name is not NULL) and
643 * also unprefixed). */
644 notmuch_private_status_t
645 _notmuch_message_gen_terms (notmuch_message_t *message,
646 const char *prefix_name,
649 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
652 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
654 term_gen->set_document (message->doc);
657 const char *prefix = _find_prefix (prefix_name);
659 term_gen->index_text (text, 1, prefix);
662 term_gen->index_text (text);
664 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
667 /* Remove a name:value term from 'message', (the actual term will be
668 * encoded by prefixing the value with a short prefix). See
669 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
670 * names to prefix values.
672 * This change will not be reflected in the database until the next
673 * call to _notmuch_message_set_sync. */
674 notmuch_private_status_t
675 _notmuch_message_remove_term (notmuch_message_t *message,
676 const char *prefix_name,
682 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
684 term = talloc_asprintf (message, "%s%s",
685 _find_prefix (prefix_name), value);
687 if (strlen (term) > NOTMUCH_TERM_MAX)
688 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
691 message->doc.remove_term (term);
692 } catch (const Xapian::InvalidArgumentError) {
693 /* We'll let the philosopher's try to wrestle with the
694 * question of whether failing to remove that which was not
695 * there in the first place is failure. For us, we'll silently
696 * consider it all good. */
701 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
705 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
707 notmuch_private_status_t private_status;
708 notmuch_status_t status;
710 status = _notmuch_database_ensure_writable (message->notmuch);
715 return NOTMUCH_STATUS_NULL_POINTER;
717 if (strlen (tag) > NOTMUCH_TAG_MAX)
718 return NOTMUCH_STATUS_TAG_TOO_LONG;
720 private_status = _notmuch_message_add_term (message, "tag", tag);
721 if (private_status) {
722 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
726 if (! message->frozen)
727 _notmuch_message_sync (message);
729 return NOTMUCH_STATUS_SUCCESS;
733 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
735 notmuch_private_status_t private_status;
736 notmuch_status_t status;
738 status = _notmuch_database_ensure_writable (message->notmuch);
743 return NOTMUCH_STATUS_NULL_POINTER;
745 if (strlen (tag) > NOTMUCH_TAG_MAX)
746 return NOTMUCH_STATUS_TAG_TOO_LONG;
748 private_status = _notmuch_message_remove_term (message, "tag", tag);
749 if (private_status) {
750 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_remove_all_tags (notmuch_message_t *message)
763 notmuch_private_status_t private_status;
764 notmuch_status_t status;
765 notmuch_tags_t *tags;
768 status = _notmuch_database_ensure_writable (message->notmuch);
772 for (tags = notmuch_message_get_tags (message);
773 notmuch_tags_has_more (tags);
774 notmuch_tags_advance (tags))
776 tag = notmuch_tags_get (tags);
778 private_status = _notmuch_message_remove_term (message, "tag", tag);
779 if (private_status) {
780 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
785 if (! message->frozen)
786 _notmuch_message_sync (message);
788 return NOTMUCH_STATUS_SUCCESS;
792 notmuch_message_freeze (notmuch_message_t *message)
794 notmuch_status_t status;
796 status = _notmuch_database_ensure_writable (message->notmuch);
802 return NOTMUCH_STATUS_SUCCESS;
806 notmuch_message_thaw (notmuch_message_t *message)
808 notmuch_status_t status;
810 status = _notmuch_database_ensure_writable (message->notmuch);
814 if (message->frozen > 0) {
816 if (message->frozen == 0)
817 _notmuch_message_sync (message);
818 return NOTMUCH_STATUS_SUCCESS;
820 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
825 notmuch_message_destroy (notmuch_message_t *message)
827 talloc_free (message);