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;
44 /* "128 bits of thread-id ought to be enough for anybody" */
45 #define NOTMUCH_THREAD_ID_BITS 128
46 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
47 typedef struct _thread_id {
48 char str[NOTMUCH_THREAD_ID_DIGITS + 1];
51 /* We end up having to call the destructor explicitly because we had
52 * to use "placement new" in order to initialize C++ objects within a
53 * block that we allocated with talloc. So C++ is making talloc
54 * slightly less simple to use, (we wouldn't need
55 * talloc_set_destructor at all otherwise).
58 _notmuch_message_destructor (notmuch_message_t *message)
60 message->doc.~Document ();
65 /* Create a new notmuch_message_t object for an existing document in
68 * Here, 'talloc owner' is an optional talloc context to which the new
69 * message will belong. This allows for the caller to not bother
70 * calling notmuch_message_destroy on the message, and no that all
71 * memory will be reclaimed with 'talloc_owner' is free. The caller
72 * still can call notmuch_message_destroy when finished with the
75 * The 'talloc_owner' argument can also be NULL, in which case the
76 * caller *is* responsible for calling notmuch_message_destroy.
78 * If no document exists in the database with document ID of 'doc_id'
79 * then this function returns NULL and optionally sets *status to
80 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
82 * This function can also fail to due lack of available memory,
83 * returning NULL and optionally setting *status to
84 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
86 * The caller can pass NULL for status if uninterested in
87 * distinguishing these two cases.
90 _notmuch_message_create (const void *talloc_owner,
91 notmuch_database_t *notmuch,
93 notmuch_private_status_t *status)
95 notmuch_message_t *message;
98 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
100 message = talloc (talloc_owner, notmuch_message_t);
101 if (unlikely (message == NULL)) {
103 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
107 message->notmuch = notmuch;
108 message->doc_id = doc_id;
112 /* Each of these will be lazily created as needed. */
113 message->message_id = NULL;
114 message->thread_id = NULL;
115 message->in_reply_to = NULL;
116 message->filename = NULL;
117 message->message_file = NULL;
119 message->replies = _notmuch_message_list_create (message);
120 if (unlikely (message->replies == NULL)) {
122 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
126 /* This is C++'s creepy "placement new", which is really just an
127 * ugly way to call a constructor for a pre-allocated object. So
128 * it's really not an error to not be checking for OUT_OF_MEMORY
129 * here, since this "new" isn't actually allocating memory. This
130 * is language-design comedy of the wrong kind. */
132 new (&message->doc) Xapian::Document;
134 talloc_set_destructor (message, _notmuch_message_destructor);
137 message->doc = notmuch->xapian_db->get_document (doc_id);
138 } catch (const Xapian::DocNotFoundError &error) {
139 talloc_free (message);
141 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
148 /* Create a new notmuch_message_t object for a specific message ID,
149 * (which may or may not already exist in the database).
151 * The 'notmuch' database will be the talloc owner of the returned
154 * If there is already a document with message ID 'message_id' in the
155 * database, then the returned message can be used to query/modify the
156 * document. Otherwise, a new document will be inserted into the
157 * database before this function returns, (and *status will be set
158 * to NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND).
160 * If an error occurs, this function will return NULL and *status
161 * will be set as appropriate. (The status pointer argument must
165 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
166 const char *message_id,
167 notmuch_private_status_t *status_ret)
169 notmuch_message_t *message;
170 Xapian::Document doc;
174 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
176 message = notmuch_database_find_message (notmuch, message_id);
178 return talloc_steal (notmuch, message);
180 term = talloc_asprintf (NULL, "%s%s",
181 _find_prefix ("id"), message_id);
183 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
191 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
193 doc_id = notmuch->xapian_db->add_document (doc);
194 } catch (const Xapian::Error &error) {
195 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
199 message = _notmuch_message_create (notmuch, notmuch,
202 /* We want to inform the caller that we had to create a new
204 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
205 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
211 notmuch_message_get_message_id (notmuch_message_t *message)
213 Xapian::TermIterator i;
215 if (message->message_id)
216 return message->message_id;
218 i = message->doc.termlist_begin ();
219 i.skip_to (_find_prefix ("id"));
221 if (i == message->doc.termlist_end ())
222 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
225 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
227 #if DEBUG_DATABASE_SANITY
230 if (i != message->doc.termlist_end () &&
231 strncmp ((*i).c_str (), _find_prefix ("id"),
232 strlen (_find_prefix ("id"))) == 0)
234 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
239 return message->message_id;
243 _notmuch_message_ensure_message_file (notmuch_message_t *message)
245 const char *filename;
247 if (message->message_file)
250 filename = notmuch_message_get_filename (message);
251 if (unlikely (filename == NULL))
254 message->message_file = _notmuch_message_file_open_ctx (message, filename);
258 notmuch_message_get_header (notmuch_message_t *message, const char *header)
260 _notmuch_message_ensure_message_file (message);
261 if (message->message_file == NULL)
264 return notmuch_message_file_get_header (message->message_file, header);
267 /* Return the message ID from the In-Reply-To header of 'message'.
269 * Returns an empty string ("") if 'message' has no In-Reply-To
272 * Returns NULL if any error occurs.
275 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
277 const char *prefix = _find_prefix ("replyto");
278 int prefix_len = strlen (prefix);
279 Xapian::TermIterator i;
280 std::string in_reply_to;
282 if (message->in_reply_to)
283 return message->in_reply_to;
285 i = message->doc.termlist_begin ();
288 if (i != message->doc.termlist_end ())
291 /* It's perfectly valid for a message to have no In-Reply-To
292 * header. For these cases, we return an empty string. */
293 if (i == message->doc.termlist_end () ||
294 strncmp (in_reply_to.c_str (), prefix, prefix_len))
296 message->in_reply_to = talloc_strdup (message, "");
297 return message->in_reply_to;
300 message->in_reply_to = talloc_strdup (message,
301 in_reply_to.c_str () + prefix_len);
303 #if DEBUG_DATABASE_SANITY
308 if (i != message->doc.termlist_end () &&
309 strncmp ((*i).c_str (), prefix, prefix_len))
311 INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n"
312 notmuch_message_get_message_id (message),
313 message->in_reply_to,
314 (*i).c_str () + prefix_len);
318 return message->in_reply_to;
322 notmuch_message_get_thread_id (notmuch_message_t *message)
324 const char *prefix = _find_prefix ("thread");
325 Xapian::TermIterator i;
328 /* This code is written with the assumption that "thread" has a
329 * single-character prefix. */
330 assert (strlen (prefix) == 1);
332 if (message->thread_id)
333 return message->thread_id;
335 i = message->doc.termlist_begin ();
338 if (i != message->doc.termlist_end ())
341 if (i == message->doc.termlist_end () || id[0] != *prefix)
342 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
345 message->thread_id = talloc_strdup (message, id.c_str () + 1);
347 #if DEBUG_DATABASE_SANITY
351 if (i != message->doc.termlist_end () && id[0] == *prefix)
353 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
354 notmuch_message_get_message_id (message),
360 return message->thread_id;
364 _notmuch_message_add_reply (notmuch_message_t *message,
365 notmuch_message_node_t *reply)
367 _notmuch_message_list_append (message->replies, reply);
371 notmuch_message_get_replies (notmuch_message_t *message)
373 return _notmuch_messages_create (message->replies);
376 /* Set the filename for 'message' to 'filename'.
378 * XXX: We should still figure out if we think it's important to store
379 * multiple filenames for email messages with identical message IDs.
381 * This change will not be reflected in the database until the next
382 * call to _notmuch_message_set_sync. */
384 _notmuch_message_set_filename (notmuch_message_t *message,
385 const char *filename)
389 unsigned int db_path_len;
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.");
401 db_path = notmuch_database_get_path (message->notmuch);
402 db_path_len = strlen (db_path);
404 if (*s == '/' && strncmp (s, db_path, db_path_len) == 0
405 && strlen (s) > db_path_len)
407 s += db_path_len + 1;
410 message->doc.set_data (s);
414 notmuch_message_get_filename (notmuch_message_t *message)
416 std::string filename_str;
419 if (message->filename)
420 return message->filename;
422 filename_str = message->doc.get_data ();
423 db_path = notmuch_database_get_path (message->notmuch);
425 if (filename_str[0] != '/')
426 message->filename = talloc_asprintf (message, "%s/%s", db_path,
427 filename_str.c_str ());
429 message->filename = talloc_strdup (message, filename_str.c_str ());
431 return message->filename;
435 notmuch_message_get_date (notmuch_message_t *message)
440 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
441 } catch (Xapian::Error &error) {
442 INTERNAL_ERROR ("Failed to read timestamp value from document.");
446 return Xapian::sortable_unserialise (value);
450 notmuch_message_get_tags (notmuch_message_t *message)
452 const char *prefix = _find_prefix ("tag");
453 Xapian::TermIterator i, end;
454 notmuch_tags_t *tags;
457 /* Currently this iteration is written with the assumption that
458 * "tag" has a single-character prefix. */
459 assert (strlen (prefix) == 1);
461 tags = _notmuch_tags_create (message);
462 if (unlikely (tags == NULL))
465 i = message->doc.termlist_begin ();
466 end = message->doc.termlist_end ();
473 if (tag.empty () || tag[0] != *prefix)
476 _notmuch_tags_add_tag (tags, tag.c_str () + 1);
481 _notmuch_tags_prepare_iterator (tags);
487 _notmuch_message_set_date (notmuch_message_t *message,
492 /* GMime really doesn't want to see a NULL date, so protect its
494 if (date == NULL || *date == '\0')
497 time_value = g_mime_utils_header_decode_date (date, NULL);
499 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
500 Xapian::sortable_serialise (time_value));
504 thread_id_generate (thread_id_t *thread_id)
506 static int seeded = 0;
513 dev_random = fopen ("/dev/random", "r");
514 if (dev_random == NULL) {
517 fread ((void *) &value, sizeof (value), 1, dev_random);
525 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
527 sprintf (s, "%08x", value);
533 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
535 /* If not part of any existing thread, generate a new thread_id. */
536 thread_id_t thread_id;
538 thread_id_generate (&thread_id);
539 _notmuch_message_add_term (message, "thread", thread_id.str);
542 /* Synchronize changes made to message->doc out into the database. */
544 _notmuch_message_sync (notmuch_message_t *message)
546 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
548 db->replace_document (message->doc_id, message->doc);
551 /* Ensure that 'message' is not holding any file object open. Future
552 * calls to various functions will still automatically open the
553 * message file as needed.
556 _notmuch_message_close (notmuch_message_t *message)
558 if (message->message_file) {
559 notmuch_message_file_close (message->message_file);
560 message->message_file = NULL;
564 /* Add a name:value term to 'message', (the actual term will be
565 * encoded by prefixing the value with a short prefix). See
566 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
567 * names to prefix values.
569 * This change will not be reflected in the database until the next
570 * call to _notmuch_message_set_sync. */
571 notmuch_private_status_t
572 _notmuch_message_add_term (notmuch_message_t *message,
573 const char *prefix_name,
580 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
582 term = talloc_asprintf (message, "%s%s",
583 _find_prefix (prefix_name), value);
585 if (strlen (term) > NOTMUCH_TERM_MAX)
586 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
588 message->doc.add_term (term);
592 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
595 /* Parse 'text' and add a term to 'message' for each parsed word. Each
596 * term will be added both prefixed (if prefix_name is not NULL) and
597 * also unprefixed). */
598 notmuch_private_status_t
599 _notmuch_message_gen_terms (notmuch_message_t *message,
600 const char *prefix_name,
603 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
606 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
608 term_gen->set_document (message->doc);
611 const char *prefix = _find_prefix (prefix_name);
613 term_gen->index_text (text, 1, prefix);
616 term_gen->index_text (text);
618 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
621 /* Remove a name:value term from 'message', (the actual term will be
622 * encoded by prefixing the value with a short prefix). See
623 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
624 * names to prefix values.
626 * This change will not be reflected in the database until the next
627 * call to _notmuch_message_set_sync. */
628 notmuch_private_status_t
629 _notmuch_message_remove_term (notmuch_message_t *message,
630 const char *prefix_name,
636 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
638 term = talloc_asprintf (message, "%s%s",
639 _find_prefix (prefix_name), value);
641 if (strlen (term) > NOTMUCH_TERM_MAX)
642 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
645 message->doc.remove_term (term);
646 } catch (const Xapian::InvalidArgumentError) {
647 /* We'll let the philosopher's try to wrestle with the
648 * question of whether failing to remove that which was not
649 * there in the first place is failure. For us, we'll silently
650 * consider it all good. */
655 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
659 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
661 notmuch_private_status_t status;
664 return NOTMUCH_STATUS_NULL_POINTER;
666 if (strlen (tag) > NOTMUCH_TAG_MAX)
667 return NOTMUCH_STATUS_TAG_TOO_LONG;
669 status = _notmuch_message_add_term (message, "tag", tag);
671 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
675 if (! message->frozen)
676 _notmuch_message_sync (message);
678 return NOTMUCH_STATUS_SUCCESS;
682 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
684 notmuch_private_status_t status;
687 return NOTMUCH_STATUS_NULL_POINTER;
689 if (strlen (tag) > NOTMUCH_TAG_MAX)
690 return NOTMUCH_STATUS_TAG_TOO_LONG;
692 status = _notmuch_message_remove_term (message, "tag", tag);
694 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
698 if (! message->frozen)
699 _notmuch_message_sync (message);
701 return NOTMUCH_STATUS_SUCCESS;
705 notmuch_message_remove_all_tags (notmuch_message_t *message)
707 notmuch_private_status_t status;
708 notmuch_tags_t *tags;
711 for (tags = notmuch_message_get_tags (message);
712 notmuch_tags_has_more (tags);
713 notmuch_tags_advance (tags))
715 tag = notmuch_tags_get (tags);
717 status = _notmuch_message_remove_term (message, "tag", tag);
719 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
724 if (! message->frozen)
725 _notmuch_message_sync (message);
729 notmuch_message_freeze (notmuch_message_t *message)
735 notmuch_message_thaw (notmuch_message_t *message)
737 if (message->frozen > 0) {
739 if (message->frozen == 0)
740 _notmuch_message_sync (message);
741 return NOTMUCH_STATUS_SUCCESS;
743 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
748 notmuch_message_destroy (notmuch_message_t *message)
750 talloc_free (message);