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;
36 /* "128 bits of thread-id ought to be enough for anybody" */
37 #define NOTMUCH_THREAD_ID_BITS 128
38 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
39 typedef struct _thread_id {
40 char str[NOTMUCH_THREAD_ID_DIGITS + 1];
43 /* We end up having to call the destructor explicitly because we had
44 * to use "placement new" in order to initialize C++ objects within a
45 * block that we allocated with talloc. So C++ is making talloc
46 * slightly less simple to use, (we wouldn't need
47 * talloc_set_destructor at all otherwise).
50 _notmuch_message_destructor (notmuch_message_t *message)
52 message->doc.~Document ();
57 /* Create a new notmuch_message_t object for an existing document in
60 * Here, 'talloc owner' is an optional talloc context to which the new
61 * message will belong. This allows for the caller to not bother
62 * calling notmuch_message_destroy on the message, and no that all
63 * memory will be reclaimed with 'talloc_owner' is free. The caller
64 * still can call notmuch_message_destroy when finished with the
67 * The 'talloc_owner' argument can also be NULL, in which case the
68 * caller *is* responsible for calling notmuch_message_destroy.
70 * If no document exists in the database with document ID of 'doc_id'
71 * then this function returns NULL and optionally sets *status to
72 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
74 * This function can also fail to due lack of available memory,
75 * returning NULL and optionally setting *status to
76 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
78 * The caller can pass NULL for status if uninterested in
79 * distinguishing these two cases.
82 _notmuch_message_create (const void *talloc_owner,
83 notmuch_database_t *notmuch,
85 notmuch_private_status_t *status)
87 notmuch_message_t *message;
90 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
92 message = talloc (talloc_owner, notmuch_message_t);
93 if (unlikely (message == NULL)) {
95 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
99 message->notmuch = notmuch;
100 message->doc_id = doc_id;
101 message->message_id = NULL; /* lazily created */
102 message->thread_id = NULL; /* lazily created */
103 message->filename = NULL; /* lazily created */
105 /* This is C++'s creepy "placement new", which is really just an
106 * ugly way to call a constructor for a pre-allocated object. So
107 * it's really not an error to not be checking for OUT_OF_MEMORY
108 * here, since this "new" isn't actually allocating memory. This
109 * is language-design comedy of the wrong kind. */
111 new (&message->doc) Xapian::Document;
113 talloc_set_destructor (message, _notmuch_message_destructor);
116 message->doc = notmuch->xapian_db->get_document (doc_id);
117 } catch (const Xapian::DocNotFoundError &error) {
118 talloc_free (message);
120 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
127 /* Create a new notmuch_message_t object for a specific message ID,
128 * (which may or may not already exist in the databas).
130 * Here, 'talloc owner' is an optional talloc context to which the new
131 * message will belong. This allows for the caller to not bother
132 * calling notmuch_message_destroy on the message, and no that all
133 * memory will be reclaimed with 'talloc_owner' is free. The caller
134 * still can call notmuch_message_destroy when finished with the
135 * message if desired.
137 * The 'talloc_owner' argument can also be NULL, in which case the
138 * caller *is* responsible for calling notmuch_message_destroy.
140 * If there is already a document with message ID 'message_id' in the
141 * database, then the returned message can be used to query/modify the
142 * document. Otherwise, a new document will be inserted into the
143 * database before this function returns.
145 * If an error occurs, this function will return NULL and *status
146 * will be set as appropriate. (The status pointer argument must
150 _notmuch_message_create_for_message_id (const void *talloc_owner,
151 notmuch_database_t *notmuch,
152 const char *message_id,
153 notmuch_status_t *status)
155 notmuch_private_status_t private_status;
156 notmuch_message_t *message;
157 Xapian::Document doc;
161 *status = NOTMUCH_STATUS_SUCCESS;
163 message = notmuch_database_find_message (notmuch, message_id);
165 return talloc_steal (talloc_owner, message);
167 term = talloc_asprintf (NULL, "%s%s",
168 _find_prefix ("id"), message_id);
170 *status = NOTMUCH_STATUS_OUT_OF_MEMORY;
178 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
180 doc_id = notmuch->xapian_db->add_document (doc);
181 } catch (const Xapian::Error &error) {
182 *status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
186 message = _notmuch_message_create (talloc_owner, notmuch,
187 doc_id, &private_status);
189 *status = COERCE_STATUS (private_status,
190 "Failed to find dcocument after inserting it.");
196 notmuch_message_get_message_id (notmuch_message_t *message)
198 Xapian::TermIterator i;
200 if (message->message_id)
201 return message->message_id;
203 i = message->doc.termlist_begin ();
204 i.skip_to (_find_prefix ("id"));
206 if (i == message->doc.termlist_end ())
207 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
210 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
212 #if DEBUG_DATABASE_SANITY
215 if (i != message->doc.termlist_end () &&
216 strncmp ((*i).c_str (), _find_prefix ("id"),
217 strlen (_find_prefix ("id"))) == 0)
219 INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
224 return message->message_id;
228 notmuch_message_get_thread_id (notmuch_message_t *message)
230 Xapian::TermIterator i;
232 if (message->thread_id)
233 return message->thread_id;
235 i = message->doc.termlist_begin ();
236 i.skip_to (_find_prefix ("thread"));
238 if (i == message->doc.termlist_end ())
239 INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
242 message->thread_id = talloc_strdup (message, (*i).c_str () + 1);
244 #if DEBUG_DATABASE_SANITY
247 if (i != message->doc.termlist_end () &&
248 strncmp ((*i).c_str (), _find_prefix ("thread"),
249 strlen (_find_prefix ("thread"))) == 0)
251 INTERNAL_ERROR ("Message with document ID of %d has duplicate thread IDs.\n",
256 return message->thread_id;
259 /* Set the filename for 'message' to 'filename'.
261 * XXX: We should still figure out if we think it's important to store
262 * multiple filenames for email messages with identical message IDs.
264 * This change will not be reflected in the database until the next
265 * call to _notmuch_message_set_sync. */
267 _notmuch_message_set_filename (notmuch_message_t *message,
268 const char *filename)
270 if (message->filename)
271 talloc_free (message->filename);
272 message->doc.set_data (filename);
276 notmuch_message_get_filename (notmuch_message_t *message)
278 std::string filename_str;
280 if (message->filename)
281 return message->filename;
283 filename_str = message->doc.get_data ();
284 message->filename = talloc_strdup (message, filename_str.c_str ());
286 return message->filename;
290 notmuch_message_get_tags (notmuch_message_t *message)
292 const char *prefix = _find_prefix ("tag");
293 Xapian::TermIterator i, end;
294 notmuch_tags_t *tags;
297 /* Currently this iteration is written with the assumption that
298 * "tag" has a single-character prefix. */
299 assert (strlen (prefix) == 1);
301 tags = _notmuch_tags_create (message);
302 if (unlikely (tags == NULL))
305 i = message->doc.termlist_begin ();
306 end = message->doc.termlist_end ();
313 if (tag.empty () || tag[0] != *prefix)
316 _notmuch_tags_add_tag (tags, tag.c_str () + 1);
321 _notmuch_tags_sort (tags);
323 _notmuch_tags_reset (tags);
329 _notmuch_message_set_date (notmuch_message_t *message,
334 time_value = notmuch_parse_date (date, NULL);
336 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
337 Xapian::sortable_serialise (time_value));
341 thread_id_generate (thread_id_t *thread_id)
343 static int seeded = 0;
350 dev_random = fopen ("/dev/random", "r");
351 if (dev_random == NULL) {
354 fread ((void *) &value, sizeof (value), 1, dev_random);
362 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
364 sprintf (s, "%08x", value);
370 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
372 /* If not part of any existing thread, generate a new thread_id. */
373 thread_id_t thread_id;
375 thread_id_generate (&thread_id);
376 _notmuch_message_add_term (message, "thread", thread_id.str);
379 /* Synchronize changes made to message->doc out into the database. */
381 _notmuch_message_sync (notmuch_message_t *message)
383 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
385 db->replace_document (message->doc_id, message->doc);
388 /* Add a name:value term to 'message', (the actual term will be
389 * encoded by prefixing the value with a short prefix). See
390 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
391 * names to prefix values.
393 * This change will not be reflected in the database until the next
394 * call to _notmuch_message_set_sync. */
395 notmuch_private_status_t
396 _notmuch_message_add_term (notmuch_message_t *message,
397 const char *prefix_name,
404 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
406 term = talloc_asprintf (message, "%s%s",
407 _find_prefix (prefix_name), value);
409 if (strlen (term) > NOTMUCH_TERM_MAX)
410 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
412 message->doc.add_term (term);
416 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
419 /* Remove a name:value term from 'message', (the actual term will be
420 * encoded by prefixing the value with a short prefix). See
421 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
422 * names to prefix values.
424 * This change will not be reflected in the database until the next
425 * call to _notmuch_message_set_sync. */
426 notmuch_private_status_t
427 _notmuch_message_remove_term (notmuch_message_t *message,
428 const char *prefix_name,
434 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
436 term = talloc_asprintf (message, "%s%s",
437 _find_prefix (prefix_name), value);
439 if (strlen (term) > NOTMUCH_TERM_MAX)
440 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
442 message->doc.remove_term (term);
446 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
450 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
452 notmuch_private_status_t status;
455 return NOTMUCH_STATUS_NULL_POINTER;
457 if (strlen (tag) > NOTMUCH_TAG_MAX)
458 return NOTMUCH_STATUS_TAG_TOO_LONG;
460 status = _notmuch_message_add_term (message, "tag", tag);
462 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
466 _notmuch_message_sync (message);
468 return NOTMUCH_STATUS_SUCCESS;
472 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
474 notmuch_private_status_t status;
477 return NOTMUCH_STATUS_NULL_POINTER;
479 if (strlen (tag) > NOTMUCH_TAG_MAX)
480 return NOTMUCH_STATUS_TAG_TOO_LONG;
482 status = _notmuch_message_remove_term (message, "tag", tag);
484 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
488 _notmuch_message_sync (message);
490 return NOTMUCH_STATUS_SUCCESS;
494 notmuch_message_destroy (notmuch_message_t *message)
496 talloc_free (message);