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;
37 notmuch_message_file_t *message_file;
38 notmuch_message_list_t *replies;
43 /* "128 bits of thread-id ought to be enough for anybody" */
44 #define NOTMUCH_THREAD_ID_BITS 128
45 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
46 typedef struct _thread_id {
47 char str[NOTMUCH_THREAD_ID_DIGITS + 1];
50 /* We end up having to call the destructor explicitly because we had
51 * to use "placement new" in order to initialize C++ objects within a
52 * block that we allocated with talloc. So C++ is making talloc
53 * slightly less simple to use, (we wouldn't need
54 * talloc_set_destructor at all otherwise).
57 _notmuch_message_destructor (notmuch_message_t *message)
59 message->doc.~Document ();
64 /* Create a new notmuch_message_t object for an existing document in
67 * Here, 'talloc owner' is an optional talloc context to which the new
68 * message will belong. This allows for the caller to not bother
69 * calling notmuch_message_destroy on the message, and no that all
70 * memory will be reclaimed with 'talloc_owner' is free. The caller
71 * still can call notmuch_message_destroy when finished with the
74 * The 'talloc_owner' argument can also be NULL, in which case the
75 * caller *is* responsible for calling notmuch_message_destroy.
77 * If no document exists in the database with document ID of 'doc_id'
78 * then this function returns NULL and optionally sets *status to
79 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
81 * This function can also fail to due lack of available memory,
82 * returning NULL and optionally setting *status to
83 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
85 * The caller can pass NULL for status if uninterested in
86 * distinguishing these two cases.
89 _notmuch_message_create (const void *talloc_owner,
90 notmuch_database_t *notmuch,
92 notmuch_private_status_t *status)
94 notmuch_message_t *message;
97 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
99 message = talloc (talloc_owner, notmuch_message_t);
100 if (unlikely (message == NULL)) {
102 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
106 message->notmuch = notmuch;
107 message->doc_id = doc_id;
111 /* Each of these will be lazily created as needed. */
112 message->message_id = NULL;
113 message->thread_id = NULL;
114 message->filename = NULL;
115 message->message_file = NULL;
117 message->replies = _notmuch_message_list_create (message);
118 if (unlikely (message->replies == NULL)) {
120 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
124 /* This is C++'s creepy "placement new", which is really just an
125 * ugly way to call a constructor for a pre-allocated object. So
126 * it's really not an error to not be checking for OUT_OF_MEMORY
127 * here, since this "new" isn't actually allocating memory. This
128 * is language-design comedy of the wrong kind. */
130 new (&message->doc) Xapian::Document;
132 talloc_set_destructor (message, _notmuch_message_destructor);
135 message->doc = notmuch->xapian_db->get_document (doc_id);
136 } catch (const Xapian::DocNotFoundError &error) {
137 talloc_free (message);
139 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
146 /* Create a new notmuch_message_t object for a specific message ID,
147 * (which may or may not already exist in the databas).
149 * Here, 'talloc owner' is an optional talloc context to which the new
150 * message will belong. This allows for the caller to not bother
151 * calling notmuch_message_destroy on the message, and no that all
152 * memory will be reclaimed with 'talloc_owner' is free. The caller
153 * still can call notmuch_message_destroy when finished with the
154 * message if desired.
156 * The 'talloc_owner' argument can also be NULL, in which case the
157 * caller *is* responsible for calling notmuch_message_destroy.
159 * If there is already a document with message ID 'message_id' in the
160 * database, then the returned message can be used to query/modify the
161 * document. Otherwise, a new document will be inserted into the
162 * database before this function returns, (and *status will be set
163 * to NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND).
165 * If an error occurs, this function will return NULL and *status
166 * will be set as appropriate. (The status pointer argument must
170 _notmuch_message_create_for_message_id (const void *talloc_owner,
171 notmuch_database_t *notmuch,
172 const char *message_id,
173 notmuch_private_status_t *status_ret)
175 notmuch_message_t *message;
176 Xapian::Document doc;
180 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
182 message = notmuch_database_find_message (notmuch, message_id);
184 return talloc_steal (talloc_owner, message);
186 term = talloc_asprintf (NULL, "%s%s",
187 _find_prefix ("id"), message_id);
189 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
197 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
199 doc_id = notmuch->xapian_db->add_document (doc);
200 } catch (const Xapian::Error &error) {
201 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
205 message = _notmuch_message_create (talloc_owner, notmuch,
208 /* We want to inform the caller that we had to create a new
210 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
211 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
217 notmuch_message_get_message_id (notmuch_message_t *message)
219 Xapian::TermIterator i;
221 if (message->message_id)
222 return message->message_id;
224 i = message->doc.termlist_begin ();
225 i.skip_to (_find_prefix ("id"));
227 if (i == message->doc.termlist_end ())
228 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
231 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
233 #if DEBUG_DATABASE_SANITY
236 if (i != message->doc.termlist_end () &&
237 strncmp ((*i).c_str (), _find_prefix ("id"),
238 strlen (_find_prefix ("id"))) == 0)
240 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
245 return message->message_id;
249 _notmuch_message_ensure_message_file (notmuch_message_t *message)
251 const char *filename;
253 if (message->message_file)
256 filename = notmuch_message_get_filename (message);
257 if (unlikely (filename == NULL))
260 message->message_file = _notmuch_message_file_open_ctx (message, filename);
264 notmuch_message_get_header (notmuch_message_t *message, const char *header)
266 _notmuch_message_ensure_message_file (message);
267 if (message->message_file == NULL)
270 return notmuch_message_file_get_header (message->message_file, header);
273 /* XXX: We probably want to store the In-Reply-To header in the
274 * database (separate from the References message IDs) so that we can
275 * fetch it out again without having to go load the message file. */
277 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
279 return _parse_message_id (message,
280 notmuch_message_get_header (message,
286 notmuch_message_get_thread_id (notmuch_message_t *message)
288 Xapian::TermIterator i;
290 if (message->thread_id)
291 return message->thread_id;
293 i = message->doc.termlist_begin ();
294 i.skip_to (_find_prefix ("thread"));
296 if (i == message->doc.termlist_end ())
297 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
300 message->thread_id = talloc_strdup (message, (*i).c_str () + 1);
302 #if DEBUG_DATABASE_SANITY
305 if (i != message->doc.termlist_end () &&
306 strncmp ((*i).c_str (), _find_prefix ("thread"),
307 strlen (_find_prefix ("thread"))) == 0)
309 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
310 notmuch_message_get_message_id (message),
316 return message->thread_id;
320 _notmuch_message_add_reply (notmuch_message_t *message,
321 notmuch_message_node_t *reply)
323 _notmuch_message_list_append (message->replies, reply);
327 notmuch_message_get_replies (notmuch_message_t *message)
329 return _notmuch_messages_create (message->replies);
332 /* Set the filename for 'message' to 'filename'.
334 * XXX: We should still figure out if we think it's important to store
335 * multiple filenames for email messages with identical message IDs.
337 * This change will not be reflected in the database until the next
338 * call to _notmuch_message_set_sync. */
340 _notmuch_message_set_filename (notmuch_message_t *message,
341 const char *filename)
345 unsigned int db_path_len;
347 if (message->filename) {
348 talloc_free (message->filename);
349 message->filename = NULL;
352 if (filename == NULL)
353 INTERNAL_ERROR ("Message filename cannot be NULL.");
357 db_path = notmuch_database_get_path (message->notmuch);
358 db_path_len = strlen (db_path);
360 if (*s == '/' && strncmp (s, db_path, db_path_len) == 0
361 && strlen (s) > db_path_len)
363 s += db_path_len + 1;
366 message->doc.set_data (s);
370 notmuch_message_get_filename (notmuch_message_t *message)
372 std::string filename_str;
375 if (message->filename)
376 return message->filename;
378 filename_str = message->doc.get_data ();
379 db_path = notmuch_database_get_path (message->notmuch);
381 if (filename_str[0] != '/')
382 message->filename = talloc_asprintf (message, "%s/%s", db_path,
383 filename_str.c_str ());
385 message->filename = talloc_strdup (message, filename_str.c_str ());
387 return message->filename;
391 notmuch_message_get_date (notmuch_message_t *message)
396 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
397 } catch (Xapian::Error &error) {
398 INTERNAL_ERROR ("Failed to read timestamp value from document.");
402 return Xapian::sortable_unserialise (value);
406 notmuch_message_get_tags (notmuch_message_t *message)
408 const char *prefix = _find_prefix ("tag");
409 Xapian::TermIterator i, end;
410 notmuch_tags_t *tags;
413 /* Currently this iteration is written with the assumption that
414 * "tag" has a single-character prefix. */
415 assert (strlen (prefix) == 1);
417 tags = _notmuch_tags_create (message);
418 if (unlikely (tags == NULL))
421 i = message->doc.termlist_begin ();
422 end = message->doc.termlist_end ();
429 if (tag.empty () || tag[0] != *prefix)
432 _notmuch_tags_add_tag (tags, tag.c_str () + 1);
437 _notmuch_tags_prepare_iterator (tags);
443 _notmuch_message_set_date (notmuch_message_t *message,
448 /* GMime really doesn't want to see a NULL date, so protect its
453 time_value = g_mime_utils_header_decode_date (date, NULL);
455 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
456 Xapian::sortable_serialise (time_value));
460 thread_id_generate (thread_id_t *thread_id)
462 static int seeded = 0;
469 dev_random = fopen ("/dev/random", "r");
470 if (dev_random == NULL) {
473 fread ((void *) &value, sizeof (value), 1, dev_random);
481 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
483 sprintf (s, "%08x", value);
489 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
491 /* If not part of any existing thread, generate a new thread_id. */
492 thread_id_t thread_id;
494 thread_id_generate (&thread_id);
495 _notmuch_message_add_term (message, "thread", thread_id.str);
498 /* Synchronize changes made to message->doc out into the database. */
500 _notmuch_message_sync (notmuch_message_t *message)
502 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
504 db->replace_document (message->doc_id, message->doc);
507 /* Add a name:value term to 'message', (the actual term will be
508 * encoded by prefixing the value with a short prefix). See
509 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
510 * names to prefix values.
512 * This change will not be reflected in the database until the next
513 * call to _notmuch_message_set_sync. */
514 notmuch_private_status_t
515 _notmuch_message_add_term (notmuch_message_t *message,
516 const char *prefix_name,
523 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
525 term = talloc_asprintf (message, "%s%s",
526 _find_prefix (prefix_name), value);
528 if (strlen (term) > NOTMUCH_TERM_MAX)
529 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
531 message->doc.add_term (term);
535 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
538 /* Parse 'text' and add a term to 'message' for each parsed word. Each
539 * term will be added both prefixed (if prefix_name is not NULL) and
540 * also unprefixed). */
541 notmuch_private_status_t
542 _notmuch_message_gen_terms (notmuch_message_t *message,
543 const char *prefix_name,
546 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
549 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
551 term_gen->set_document (message->doc);
554 const char *prefix = _find_prefix (prefix_name);
556 term_gen->index_text (text, 1, prefix);
559 term_gen->index_text (text);
561 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
564 /* Remove a name:value term from '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_remove_term (notmuch_message_t *message,
573 const char *prefix_name,
579 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
581 term = talloc_asprintf (message, "%s%s",
582 _find_prefix (prefix_name), value);
584 if (strlen (term) > NOTMUCH_TERM_MAX)
585 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
588 message->doc.remove_term (term);
589 } catch (const Xapian::InvalidArgumentError) {
590 /* We'll let the philosopher's try to wrestle with the
591 * question of whether failing to remove that which was not
592 * there in the first place is failure. For us, we'll silently
593 * consider it all good. */
598 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
602 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
604 notmuch_private_status_t status;
607 return NOTMUCH_STATUS_NULL_POINTER;
609 if (strlen (tag) > NOTMUCH_TAG_MAX)
610 return NOTMUCH_STATUS_TAG_TOO_LONG;
612 status = _notmuch_message_add_term (message, "tag", tag);
614 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
618 if (! message->frozen)
619 _notmuch_message_sync (message);
621 return NOTMUCH_STATUS_SUCCESS;
625 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
627 notmuch_private_status_t status;
630 return NOTMUCH_STATUS_NULL_POINTER;
632 if (strlen (tag) > NOTMUCH_TAG_MAX)
633 return NOTMUCH_STATUS_TAG_TOO_LONG;
635 status = _notmuch_message_remove_term (message, "tag", tag);
637 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
641 if (! message->frozen)
642 _notmuch_message_sync (message);
644 return NOTMUCH_STATUS_SUCCESS;
648 notmuch_message_remove_all_tags (notmuch_message_t *message)
650 notmuch_private_status_t status;
651 notmuch_tags_t *tags;
654 for (tags = notmuch_message_get_tags (message);
655 notmuch_tags_has_more (tags);
656 notmuch_tags_advance (tags))
658 tag = notmuch_tags_get (tags);
660 status = _notmuch_message_remove_term (message, "tag", tag);
662 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
667 if (! message->frozen)
668 _notmuch_message_sync (message);
672 notmuch_message_freeze (notmuch_message_t *message)
678 notmuch_message_thaw (notmuch_message_t *message)
680 if (message->frozen > 0) {
682 if (message->frozen == 0)
683 _notmuch_message_sync (message);
684 return NOTMUCH_STATUS_SUCCESS;
686 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
691 notmuch_message_destroy (notmuch_message_t *message)
693 talloc_free (message);