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 struct _notmuch_message {
27 notmuch_database_t *notmuch;
33 notmuch_message_file_t *message_file;
37 /* "128 bits of thread-id ought to be enough for anybody" */
38 #define NOTMUCH_THREAD_ID_BITS 128
39 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
40 typedef struct _thread_id {
41 char str[NOTMUCH_THREAD_ID_DIGITS + 1];
44 /* We end up having to call the destructor explicitly because we had
45 * to use "placement new" in order to initialize C++ objects within a
46 * block that we allocated with talloc. So C++ is making talloc
47 * slightly less simple to use, (we wouldn't need
48 * talloc_set_destructor at all otherwise).
51 _notmuch_message_destructor (notmuch_message_t *message)
53 message->doc.~Document ();
58 /* Create a new notmuch_message_t object for an existing document in
61 * Here, 'talloc owner' is an optional talloc context to which the new
62 * message will belong. This allows for the caller to not bother
63 * calling notmuch_message_destroy on the message, and no that all
64 * memory will be reclaimed with 'talloc_owner' is free. The caller
65 * still can call notmuch_message_destroy when finished with the
68 * The 'talloc_owner' argument can also be NULL, in which case the
69 * caller *is* responsible for calling notmuch_message_destroy.
71 * If no document exists in the database with document ID of 'doc_id'
72 * then this function returns NULL and optionally sets *status to
73 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
75 * This function can also fail to due lack of available memory,
76 * returning NULL and optionally setting *status to
77 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
79 * The caller can pass NULL for status if uninterested in
80 * distinguishing these two cases.
83 _notmuch_message_create (const void *talloc_owner,
84 notmuch_database_t *notmuch,
86 notmuch_private_status_t *status)
88 notmuch_message_t *message;
91 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
93 message = talloc (talloc_owner, notmuch_message_t);
94 if (unlikely (message == NULL)) {
96 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
100 message->notmuch = notmuch;
101 message->doc_id = doc_id;
105 /* Each of these will be lazily created as needed. */
106 message->message_id = NULL;
107 message->thread_id = NULL;
108 message->filename = NULL;
109 message->message_file = NULL;
111 /* This is C++'s creepy "placement new", which is really just an
112 * ugly way to call a constructor for a pre-allocated object. So
113 * it's really not an error to not be checking for OUT_OF_MEMORY
114 * here, since this "new" isn't actually allocating memory. This
115 * is language-design comedy of the wrong kind. */
117 new (&message->doc) Xapian::Document;
119 talloc_set_destructor (message, _notmuch_message_destructor);
122 message->doc = notmuch->xapian_db->get_document (doc_id);
123 } catch (const Xapian::DocNotFoundError &error) {
124 talloc_free (message);
126 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
133 /* Create a new notmuch_message_t object for a specific message ID,
134 * (which may or may not already exist in the databas).
136 * Here, 'talloc owner' is an optional talloc context to which the new
137 * message will belong. This allows for the caller to not bother
138 * calling notmuch_message_destroy on the message, and no that all
139 * memory will be reclaimed with 'talloc_owner' is free. The caller
140 * still can call notmuch_message_destroy when finished with the
141 * message if desired.
143 * The 'talloc_owner' argument can also be NULL, in which case the
144 * caller *is* responsible for calling notmuch_message_destroy.
146 * If there is already a document with message ID 'message_id' in the
147 * database, then the returned message can be used to query/modify the
148 * document. Otherwise, a new document will be inserted into the
149 * database before this function returns, (and *status will be set
150 * to NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND).
152 * If an error occurs, this function will return NULL and *status
153 * will be set as appropriate. (The status pointer argument must
157 _notmuch_message_create_for_message_id (const void *talloc_owner,
158 notmuch_database_t *notmuch,
159 const char *message_id,
160 notmuch_private_status_t *status_ret)
162 notmuch_message_t *message;
163 Xapian::Document doc;
167 *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
169 message = notmuch_database_find_message (notmuch, message_id);
171 return talloc_steal (talloc_owner, message);
173 term = talloc_asprintf (NULL, "%s%s",
174 _find_prefix ("id"), message_id);
176 *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
184 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
186 doc_id = notmuch->xapian_db->add_document (doc);
187 } catch (const Xapian::Error &error) {
188 *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
192 message = _notmuch_message_create (talloc_owner, notmuch,
195 /* We want to inform the caller that we had to create a new
197 if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
198 *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
204 notmuch_message_get_message_id (notmuch_message_t *message)
206 Xapian::TermIterator i;
208 if (message->message_id)
209 return message->message_id;
211 i = message->doc.termlist_begin ();
212 i.skip_to (_find_prefix ("id"));
214 if (i == message->doc.termlist_end ())
215 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
218 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
220 #if DEBUG_DATABASE_SANITY
223 if (i != message->doc.termlist_end () &&
224 strncmp ((*i).c_str (), _find_prefix ("id"),
225 strlen (_find_prefix ("id"))) == 0)
227 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
232 return message->message_id;
236 _notmuch_message_get_subject (notmuch_message_t *message)
238 if (! message->message_file) {
239 notmuch_message_file_t *message_file;
240 const char *filename;
242 filename = notmuch_message_get_filename (message);
243 if (unlikely (filename == NULL))
246 message_file = _notmuch_message_file_open_ctx (message, filename);
247 if (unlikely (message_file == NULL))
250 message->message_file = message_file;
253 return notmuch_message_file_get_header (message->message_file,
258 notmuch_message_get_thread_id (notmuch_message_t *message)
260 Xapian::TermIterator i;
262 if (message->thread_id)
263 return message->thread_id;
265 i = message->doc.termlist_begin ();
266 i.skip_to (_find_prefix ("thread"));
268 if (i == message->doc.termlist_end ())
269 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
272 message->thread_id = talloc_strdup (message, (*i).c_str () + 1);
274 #if DEBUG_DATABASE_SANITY
277 if (i != message->doc.termlist_end () &&
278 strncmp ((*i).c_str (), _find_prefix ("thread"),
279 strlen (_find_prefix ("thread"))) == 0)
281 INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
282 notmuch_message_get_message_id (message),
288 return message->thread_id;
291 /* Set the filename for 'message' to 'filename'.
293 * XXX: We should still figure out if we think it's important to store
294 * multiple filenames for email messages with identical message IDs.
296 * This change will not be reflected in the database until the next
297 * call to _notmuch_message_set_sync. */
299 _notmuch_message_set_filename (notmuch_message_t *message,
300 const char *filename)
304 unsigned int db_path_len;
306 if (message->filename) {
307 talloc_free (message->filename);
308 message->filename = NULL;
311 if (filename == NULL)
312 INTERNAL_ERROR ("Message filename cannot be NULL.");
316 db_path = notmuch_database_get_path (message->notmuch);
317 db_path_len = strlen (db_path);
319 if (*s == '/' && strncmp (s, db_path, db_path_len) == 0
320 && strlen (s) > db_path_len)
322 s += db_path_len + 1;
325 message->doc.set_data (s);
329 notmuch_message_get_filename (notmuch_message_t *message)
331 std::string filename_str;
334 if (message->filename)
335 return message->filename;
337 filename_str = message->doc.get_data ();
338 db_path = notmuch_database_get_path (message->notmuch);
340 if (filename_str[0] != '/')
341 message->filename = talloc_asprintf (message, "%s/%s", db_path,
342 filename_str.c_str ());
344 message->filename = talloc_strdup (message, filename_str.c_str ());
346 return message->filename;
350 notmuch_message_get_tags (notmuch_message_t *message)
352 const char *prefix = _find_prefix ("tag");
353 Xapian::TermIterator i, end;
354 notmuch_tags_t *tags;
357 /* Currently this iteration is written with the assumption that
358 * "tag" has a single-character prefix. */
359 assert (strlen (prefix) == 1);
361 tags = _notmuch_tags_create (message);
362 if (unlikely (tags == NULL))
365 i = message->doc.termlist_begin ();
366 end = message->doc.termlist_end ();
373 if (tag.empty () || tag[0] != *prefix)
376 _notmuch_tags_add_tag (tags, tag.c_str () + 1);
381 _notmuch_tags_prepare_iterator (tags);
387 _notmuch_message_set_date (notmuch_message_t *message,
392 time_value = notmuch_parse_date (date, NULL);
394 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
395 Xapian::sortable_serialise (time_value));
399 thread_id_generate (thread_id_t *thread_id)
401 static int seeded = 0;
408 dev_random = fopen ("/dev/random", "r");
409 if (dev_random == NULL) {
412 fread ((void *) &value, sizeof (value), 1, dev_random);
420 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
422 sprintf (s, "%08x", value);
428 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
430 /* If not part of any existing thread, generate a new thread_id. */
431 thread_id_t thread_id;
433 thread_id_generate (&thread_id);
434 _notmuch_message_add_term (message, "thread", thread_id.str);
437 /* Synchronize changes made to message->doc out into the database. */
439 _notmuch_message_sync (notmuch_message_t *message)
441 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
443 db->replace_document (message->doc_id, message->doc);
446 /* Add a name:value term to 'message', (the actual term will be
447 * encoded by prefixing the value with a short prefix). See
448 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
449 * names to prefix values.
451 * This change will not be reflected in the database until the next
452 * call to _notmuch_message_set_sync. */
453 notmuch_private_status_t
454 _notmuch_message_add_term (notmuch_message_t *message,
455 const char *prefix_name,
462 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
464 term = talloc_asprintf (message, "%s%s",
465 _find_prefix (prefix_name), value);
467 if (strlen (term) > NOTMUCH_TERM_MAX)
468 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
470 message->doc.add_term (term);
474 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
477 /* Parse 'text' and add a term to 'message' for each parsed word. Each
478 * term will be added both prefixed (if prefix_name is not NULL) and
479 * also unprefixed). */
480 notmuch_private_status_t
481 _notmuch_message_gen_terms (notmuch_message_t *message,
482 const char *prefix_name,
485 Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
488 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
490 term_gen->set_document (message->doc);
493 const char *prefix = _find_prefix (prefix_name);
495 term_gen->index_text (text, 1, prefix);
498 term_gen->index_text (text);
500 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
503 /* Remove a name:value term from 'message', (the actual term will be
504 * encoded by prefixing the value with a short prefix). See
505 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
506 * names to prefix values.
508 * This change will not be reflected in the database until the next
509 * call to _notmuch_message_set_sync. */
510 notmuch_private_status_t
511 _notmuch_message_remove_term (notmuch_message_t *message,
512 const char *prefix_name,
518 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
520 term = talloc_asprintf (message, "%s%s",
521 _find_prefix (prefix_name), value);
523 if (strlen (term) > NOTMUCH_TERM_MAX)
524 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
527 message->doc.remove_term (term);
528 } catch (const Xapian::InvalidArgumentError) {
529 /* We'll let the philosopher's try to wrestle with the
530 * question of whether failing to remove that which was not
531 * there in the first place is failure. For us, we'll silently
532 * consider it all good. */
537 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
541 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
543 notmuch_private_status_t status;
546 return NOTMUCH_STATUS_NULL_POINTER;
548 if (strlen (tag) > NOTMUCH_TAG_MAX)
549 return NOTMUCH_STATUS_TAG_TOO_LONG;
551 status = _notmuch_message_add_term (message, "tag", tag);
553 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
557 if (! message->frozen)
558 _notmuch_message_sync (message);
560 return NOTMUCH_STATUS_SUCCESS;
564 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
566 notmuch_private_status_t status;
569 return NOTMUCH_STATUS_NULL_POINTER;
571 if (strlen (tag) > NOTMUCH_TAG_MAX)
572 return NOTMUCH_STATUS_TAG_TOO_LONG;
574 status = _notmuch_message_remove_term (message, "tag", tag);
576 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
580 if (! message->frozen)
581 _notmuch_message_sync (message);
583 return NOTMUCH_STATUS_SUCCESS;
587 notmuch_message_remove_all_tags (notmuch_message_t *message)
589 notmuch_private_status_t status;
590 notmuch_tags_t *tags;
593 for (tags = notmuch_message_get_tags (message);
594 notmuch_tags_has_more (tags);
595 notmuch_tags_advance (tags))
597 tag = notmuch_tags_get (tags);
599 status = _notmuch_message_remove_term (message, "tag", tag);
601 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
606 if (! message->frozen)
607 _notmuch_message_sync (message);
611 notmuch_message_freeze (notmuch_message_t *message)
617 notmuch_message_thaw (notmuch_message_t *message)
619 if (message->frozen > 0) {
621 if (message->frozen == 0)
622 _notmuch_message_sync (message);
623 return NOTMUCH_STATUS_SUCCESS;
625 return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
630 notmuch_message_destroy (notmuch_message_t *message)
632 talloc_free (message);