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"
24 #include <gmime/gmime.h>
28 struct _notmuch_message {
29 notmuch_database_t *notmuch;
35 notmuch_message_file_t *message_file;
39 /* "128 bits of thread-id ought to be enough for anybody" */
40 #define NOTMUCH_THREAD_ID_BITS 128
41 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
42 typedef struct _thread_id {
43 char str[NOTMUCH_THREAD_ID_DIGITS + 1];
46 /* We end up having to call the destructor explicitly because we had
47 * to use "placement new" in order to initialize C++ objects within a
48 * block that we allocated with talloc. So C++ is making talloc
49 * slightly less simple to use, (we wouldn't need
50 * talloc_set_destructor at all otherwise).
53 _notmuch_message_destructor (notmuch_message_t *message)
55 message->doc.~Document ();
60 /* Create a new notmuch_message_t object for an existing document in
63 * Here, 'talloc owner' is an optional talloc context to which the new
64 * message will belong. This allows for the caller to not bother
65 * calling notmuch_message_destroy on the message, and no that all
66 * memory will be reclaimed with 'talloc_owner' is free. The caller
67 * still can call notmuch_message_destroy when finished with the
70 * The 'talloc_owner' argument can also be NULL, in which case the
71 * caller *is* responsible for calling notmuch_message_destroy.
73 * If no document exists in the database with document ID of 'doc_id'
74 * then this function returns NULL and optionally sets *status to
75 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
77 * This function can also fail to due lack of available memory,
78 * returning NULL and optionally setting *status to
79 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
81 * The caller can pass NULL for status if uninterested in
82 * distinguishing these two cases.
85 _notmuch_message_create (const void *talloc_owner,
86 notmuch_database_t *notmuch,
88 notmuch_private_status_t *status)
90 notmuch_message_t *message;
93 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
95 message = talloc (talloc_owner, notmuch_message_t);
96 if (unlikely (message == NULL)) {
98 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
102 message->notmuch = notmuch;
103 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->filename = NULL;
111 message->message_file = NULL;
113 /* This is C++'s creepy "placement new", which is really just an
114 * ugly way to call a constructor for a pre-allocated object. So
115 * it's really not an error to not be checking for OUT_OF_MEMORY
116 * here, since this "new" isn't actually allocating memory. This
117 * is language-design comedy of the wrong kind. */
119 new (&message->doc) Xapian::Document;
121 talloc_set_destructor (message, _notmuch_message_destructor);
124 message->doc = notmuch->xapian_db->get_document (doc_id);
125 } catch (const Xapian::DocNotFoundError &error) {
126 talloc_free (message);
128 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
135 /* Create a new notmuch_message_t object for a specific message ID,
136 * (which may or may not already exist in the databas).
138 * Here, 'talloc owner' is an optional talloc context to which the new
139 * message will belong. This allows for the caller to not bother
140 * calling notmuch_message_destroy on the message, and no that all
141 * memory will be reclaimed with 'talloc_owner' is free. The caller
142 * still can call notmuch_message_destroy when finished with the
143 * message if desired.
145 * The 'talloc_owner' argument can also be NULL, in which case the
146 * caller *is* responsible for calling notmuch_message_destroy.
148 * If there is already a document with message ID 'message_id' in the
149 * database, then the returned message can be used to query/modify the
150 * document. Otherwise, a new document will be inserted into the
151 * database before this function returns, (and *status will be set
152 * to NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND).
154 * If an error occurs, this function will return NULL and *status
155 * will be set as appropriate. (The status pointer argument must
159 _notmuch_message_create_for_message_id (const void *talloc_owner,
160 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;
169 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
171 message = notmuch_database_find_message (notmuch, message_id);
173 return talloc_steal (talloc_owner, message);
175 term = talloc_asprintf (NULL, "%s%s",
176 _find_prefix ("id"), message_id);
178 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
186 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
188 doc_id = notmuch->xapian_db->add_document (doc);
189 } catch (const Xapian::Error &error) {
190 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
194 message = _notmuch_message_create (talloc_owner, notmuch,
197 /* We want to inform the caller that we had to create a new
199 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
200 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
206 notmuch_message_get_message_id (notmuch_message_t *message)
208 Xapian::TermIterator i;
210 if (message->message_id)
211 return message->message_id;
213 i = message->doc.termlist_begin ();
214 i.skip_to (_find_prefix ("id"));
216 if (i == message->doc.termlist_end ())
217 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
220 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
222 #if DEBUG_DATABASE_SANITY
225 if (i != message->doc.termlist_end () &&
226 strncmp ((*i).c_str (), _find_prefix ("id"),
227 strlen (_find_prefix ("id"))) == 0)
229 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
234 return message->message_id;
238 _notmuch_message_ensure_message_file (notmuch_message_t *message)
240 const char *filename;
242 if (message->message_file)
245 filename = notmuch_message_get_filename (message);
246 if (unlikely (filename == NULL))
249 message->message_file = _notmuch_message_file_open_ctx (message, filename);
253 notmuch_message_get_header (notmuch_message_t *message, const char *header)
255 _notmuch_message_ensure_message_file (message);
256 if (message->message_file == NULL)
259 return notmuch_message_file_get_header (message->message_file, header);
262 /* XXX: We probably want to store the In-Reply-To header in the
263 * database (separate from the References message IDs) so that we can
264 * fetch it out again without having to go load the message file. */
266 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
268 return _parse_message_id (message,
269 notmuch_message_get_header (message,
275 notmuch_message_get_thread_id (notmuch_message_t *message)
277 Xapian::TermIterator i;
279 if (message->thread_id)
280 return message->thread_id;
282 i = message->doc.termlist_begin ();
283 i.skip_to (_find_prefix ("thread"));
285 if (i == message->doc.termlist_end ())
286 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
289 message->thread_id = talloc_strdup (message, (*i).c_str () + 1);
291 #if DEBUG_DATABASE_SANITY
294 if (i != message->doc.termlist_end () &&
295 strncmp ((*i).c_str (), _find_prefix ("thread"),
296 strlen (_find_prefix ("thread"))) == 0)
298 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
299 notmuch_message_get_message_id (message),
305 return message->thread_id;
308 /* Set the filename for 'message' to 'filename'.
310 * XXX: We should still figure out if we think it's important to store
311 * multiple filenames for email messages with identical message IDs.
313 * This change will not be reflected in the database until the next
314 * call to _notmuch_message_set_sync. */
316 _notmuch_message_set_filename (notmuch_message_t *message,
317 const char *filename)
321 unsigned int db_path_len;
323 if (message->filename) {
324 talloc_free (message->filename);
325 message->filename = NULL;
328 if (filename == NULL)
329 INTERNAL_ERROR ("Message filename cannot be NULL.");
333 db_path = notmuch_database_get_path (message->notmuch);
334 db_path_len = strlen (db_path);
336 if (*s == '/' && strncmp (s, db_path, db_path_len) == 0
337 && strlen (s) > db_path_len)
339 s += db_path_len + 1;
342 message->doc.set_data (s);
346 notmuch_message_get_filename (notmuch_message_t *message)
348 std::string filename_str;
351 if (message->filename)
352 return message->filename;
354 filename_str = message->doc.get_data ();
355 db_path = notmuch_database_get_path (message->notmuch);
357 if (filename_str[0] != '/')
358 message->filename = talloc_asprintf (message, "%s/%s", db_path,
359 filename_str.c_str ());
361 message->filename = talloc_strdup (message, filename_str.c_str ());
363 return message->filename;
367 notmuch_message_get_date (notmuch_message_t *message)
372 value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
373 } catch (Xapian::Error &error) {
374 INTERNAL_ERROR ("Failed to read timestamp value from document.");
378 return Xapian::sortable_unserialise (value);
382 notmuch_message_get_tags (notmuch_message_t *message)
384 const char *prefix = _find_prefix ("tag");
385 Xapian::TermIterator i, end;
386 notmuch_tags_t *tags;
389 /* Currently this iteration is written with the assumption that
390 * "tag" has a single-character prefix. */
391 assert (strlen (prefix) == 1);
393 tags = _notmuch_tags_create (message);
394 if (unlikely (tags == NULL))
397 i = message->doc.termlist_begin ();
398 end = message->doc.termlist_end ();
405 if (tag.empty () || tag[0] != *prefix)
408 _notmuch_tags_add_tag (tags, tag.c_str () + 1);
413 _notmuch_tags_prepare_iterator (tags);
419 _notmuch_message_set_date (notmuch_message_t *message,
424 /* GMime really doesn't want to see a NULL date, so protect its
429 time_value = g_mime_utils_header_decode_date (date, NULL);
431 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
432 Xapian::sortable_serialise (time_value));
436 thread_id_generate (thread_id_t *thread_id)
438 static int seeded = 0;
445 dev_random = fopen ("/dev/random", "r");
446 if (dev_random == NULL) {
449 fread ((void *) &value, sizeof (value), 1, dev_random);
457 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
459 sprintf (s, "%08x", value);
465 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
467 /* If not part of any existing thread, generate a new thread_id. */
468 thread_id_t thread_id;
470 thread_id_generate (&thread_id);
471 _notmuch_message_add_term (message, "thread", thread_id.str);
474 /* Synchronize changes made to message->doc out into the database. */
476 _notmuch_message_sync (notmuch_message_t *message)
478 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
480 db->replace_document (message->doc_id, message->doc);
483 /* Add a name:value term to 'message', (the actual term will be
484 * encoded by prefixing the value with a short prefix). See
485 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
486 * names to prefix values.
488 * This change will not be reflected in the database until the next
489 * call to _notmuch_message_set_sync. */
490 notmuch_private_status_t
491 _notmuch_message_add_term (notmuch_message_t *message,
492 const char *prefix_name,
499 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
501 term = talloc_asprintf (message, "%s%s",
502 _find_prefix (prefix_name), value);
504 if (strlen (term) > NOTMUCH_TERM_MAX)
505 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
507 message->doc.add_term (term);
511 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
514 /* Parse 'text' and add a term to 'message' for each parsed word. Each
515 * term will be added both prefixed (if prefix_name is not NULL) and
516 * also unprefixed). */
517 notmuch_private_status_t
518 _notmuch_message_gen_terms (notmuch_message_t *message,
519 const char *prefix_name,
522 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
525 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
527 term_gen->set_document (message->doc);
530 const char *prefix = _find_prefix (prefix_name);
532 term_gen->index_text (text, 1, prefix);
535 term_gen->index_text (text);
537 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
540 /* Remove a name:value term from 'message', (the actual term will be
541 * encoded by prefixing the value with a short prefix). See
542 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
543 * names to prefix values.
545 * This change will not be reflected in the database until the next
546 * call to _notmuch_message_set_sync. */
547 notmuch_private_status_t
548 _notmuch_message_remove_term (notmuch_message_t *message,
549 const char *prefix_name,
555 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
557 term = talloc_asprintf (message, "%s%s",
558 _find_prefix (prefix_name), value);
560 if (strlen (term) > NOTMUCH_TERM_MAX)
561 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
564 message->doc.remove_term (term);
565 } catch (const Xapian::InvalidArgumentError) {
566 /* We'll let the philosopher's try to wrestle with the
567 * question of whether failing to remove that which was not
568 * there in the first place is failure. For us, we'll silently
569 * consider it all good. */
574 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
578 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
580 notmuch_private_status_t status;
583 return NOTMUCH_STATUS_NULL_POINTER;
585 if (strlen (tag) > NOTMUCH_TAG_MAX)
586 return NOTMUCH_STATUS_TAG_TOO_LONG;
588 status = _notmuch_message_add_term (message, "tag", tag);
590 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
594 if (! message->frozen)
595 _notmuch_message_sync (message);
597 return NOTMUCH_STATUS_SUCCESS;
601 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
603 notmuch_private_status_t status;
606 return NOTMUCH_STATUS_NULL_POINTER;
608 if (strlen (tag) > NOTMUCH_TAG_MAX)
609 return NOTMUCH_STATUS_TAG_TOO_LONG;
611 status = _notmuch_message_remove_term (message, "tag", tag);
613 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
617 if (! message->frozen)
618 _notmuch_message_sync (message);
620 return NOTMUCH_STATUS_SUCCESS;
624 notmuch_message_remove_all_tags (notmuch_message_t *message)
626 notmuch_private_status_t status;
627 notmuch_tags_t *tags;
630 for (tags = notmuch_message_get_tags (message);
631 notmuch_tags_has_more (tags);
632 notmuch_tags_advance (tags))
634 tag = notmuch_tags_get (tags);
636 status = _notmuch_message_remove_term (message, "tag", tag);
638 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
643 if (! message->frozen)
644 _notmuch_message_sync (message);
648 notmuch_message_freeze (notmuch_message_t *message)
654 notmuch_message_thaw (notmuch_message_t *message)
656 if (message->frozen > 0) {
658 if (message->frozen == 0)
659 _notmuch_message_sync (message);
660 return NOTMUCH_STATUS_SUCCESS;
662 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
667 notmuch_message_destroy (notmuch_message_t *message)
669 talloc_free (message);