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;
34 struct _notmuch_tags {
35 Xapian::TermIterator iterator;
36 Xapian::TermIterator iterator_end;
39 struct _notmuch_thread_ids {
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 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
53 /* These prefix values are specifically chosen to be compatible
54 * with sup, (http://sup.rubyforge.org), written by
55 * William Morgan <wmorgan-sup@masanjin.net>, and released
56 * under the GNU GPL v2.
64 prefix_t NORMAL_PREFIX[] = {
67 { "from_name", "FN" },
73 prefix_t BOOLEAN_PREFIX[] = {
75 { "from_email", "FE" },
82 { "attachment_extension", "O" },
89 _find_prefix (const char *name)
93 for (i = 0; i < ARRAY_SIZE (NORMAL_PREFIX); i++)
94 if (strcmp (name, NORMAL_PREFIX[i].name) == 0)
95 return NORMAL_PREFIX[i].prefix;
97 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX); i++)
98 if (strcmp (name, BOOLEAN_PREFIX[i].name) == 0)
99 return BOOLEAN_PREFIX[i].prefix;
104 /* We end up having to call the destructor explicitly because we had
105 * to use "placement new" in order to initialize C++ objects within a
106 * block that we allocated with talloc. So C++ is making talloc
107 * slightly less simple to use, (we wouldn't need
108 * talloc_set_destructor at all otherwise).
111 _notmuch_message_destructor (notmuch_message_t *message)
113 message->doc.~Document ();
118 /* Create a new notmuch_message_t object for an existing document in
121 * Here, 'talloc owner' is an optional talloc context to which the new
122 * message will belong. This allows for the caller to not bother
123 * calling notmuch_message_destroy on the message, and no that all
124 * memory will be reclaimed with 'talloc_owner' is free. The caller
125 * still can call notmuch_message_destroy when finished with the
126 * message if desired.
128 * The 'talloc_owner' argument can also be NULL, in which case the
129 * caller *is* responsible for calling notmuch_message_destroy.
131 * If no document exists in the database with document ID of 'doc_id'
132 * then this function returns NULL.
135 _notmuch_message_create (const void *talloc_owner,
136 notmuch_database_t *notmuch,
139 notmuch_message_t *message;
141 message = talloc (talloc_owner, notmuch_message_t);
142 if (unlikely (message == NULL))
145 message->notmuch = notmuch;
146 message->doc_id = doc_id;
147 message->message_id = NULL; /* lazily created */
148 message->filename = NULL; /* lazily created */
149 new (&message->doc) Xapian::Document;
151 talloc_set_destructor (message, _notmuch_message_destructor);
154 message->doc = notmuch->xapian_db->get_document (doc_id);
155 } catch (const Xapian::DocNotFoundError &error) {
156 talloc_free (message);
163 /* Create a new notmuch_message_t object for a specific message ID,
164 * (which may or may not already exist in the databas).
166 * Here, 'talloc owner' is an optional talloc context to which the new
167 * message will belong. This allows for the caller to not bother
168 * calling notmuch_message_destroy on the message, and no that all
169 * memory will be reclaimed with 'talloc_owner' is free. The caller
170 * still can call notmuch_message_destroy when finished with the
171 * message if desired.
173 * The 'talloc_owner' argument can also be NULL, in which case the
174 * caller *is* responsible for calling notmuch_message_destroy.
176 * If there is already a document with message ID 'message_id' in the
177 * database, then the returned message can be used to query/modify the
178 * document. Otherwise, a new document will be inserted into the
179 * database before this function returns;
182 _notmuch_message_create_for_message_id (const void *talloc_owner,
183 notmuch_database_t *notmuch,
184 const char *message_id)
186 notmuch_message_t *message;
187 Xapian::Document doc;
191 message = notmuch_database_find_message (notmuch, message_id);
193 return talloc_steal (talloc_owner, message);
195 term = talloc_asprintf (NULL, "%s%s",
196 _find_prefix ("msgid"), message_id);
200 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
202 doc_id = notmuch->xapian_db->add_document (doc);
204 return _notmuch_message_create (talloc_owner, notmuch, doc_id);
208 notmuch_message_get_message_id (notmuch_message_t *message)
210 Xapian::TermIterator i;
212 if (message->message_id)
213 return message->message_id;
215 i = message->doc.termlist_begin ();
216 i.skip_to (_find_prefix ("msgid"));
218 /* XXX: This should really be an internal error, but we'll need to
219 * fix the add_message side of things first. */
220 if (i == message->doc.termlist_end ())
223 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
224 return message->message_id;
227 /* Set the filename for 'message' to 'filename'.
229 * XXX: We should still figure out what we want to do for multiple
230 * files with identical message IDs. We will probably want to store a
231 * list of filenames here, (so that this will be "add_filename"
232 * instead of "set_filename"). Which would make this very similar to
235 * This change will not be reflected in the database until the next
236 * call to _notmuch_message_set_sync. */
238 _notmuch_message_set_filename (notmuch_message_t *message,
239 const char *filename)
241 if (message->filename)
242 talloc_free (message->filename);
243 message->doc.set_data (filename);
247 notmuch_message_get_filename (notmuch_message_t *message)
249 std::string filename_str;
251 if (message->filename)
252 return message->filename;
254 filename_str = message->doc.get_data ();
255 message->filename = talloc_strdup (message, filename_str.c_str ());
257 return message->filename;
260 /* We end up having to call the destructors explicitly because we had
261 * to use "placement new" in order to initialize C++ objects within a
262 * block that we allocated with talloc. So C++ is making talloc
263 * slightly less simple to use, (we wouldn't need
264 * talloc_set_destructor at all otherwise).
267 _notmuch_tags_destructor (notmuch_tags_t *tags)
269 tags->iterator.~TermIterator ();
270 tags->iterator_end.~TermIterator ();
276 notmuch_message_get_tags (notmuch_message_t *message)
278 notmuch_tags_t *tags;
280 tags = talloc (message, notmuch_tags_t);
281 if (unlikely (tags == NULL))
284 new (&tags->iterator) Xapian::TermIterator;
285 new (&tags->iterator_end) Xapian::TermIterator;
287 talloc_set_destructor (tags, _notmuch_tags_destructor);
289 tags->iterator = message->doc.termlist_begin ();
290 tags->iterator.skip_to (_find_prefix ("tag"));
291 tags->iterator_end = message->doc.termlist_end ();
296 notmuch_thread_ids_t *
297 notmuch_message_get_thread_ids (notmuch_message_t *message)
299 notmuch_thread_ids_t *thread_ids;
302 thread_ids = talloc (message, notmuch_thread_ids_t);
303 if (unlikely (thread_ids == NULL))
306 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
307 thread_ids->next = talloc_strdup (message, id_str.c_str ());
309 /* Initialize thread_ids->current and terminate first ID. */
310 notmuch_thread_ids_advance (thread_ids);
316 _notmuch_message_set_date (notmuch_message_t *message,
321 time_value = notmuch_parse_date (date, NULL);
323 message->doc.add_value (NOTMUCH_VALUE_DATE,
324 Xapian::sortable_serialise (time_value));
328 _notmuch_message_add_thread_id (notmuch_message_t *message,
329 const char *thread_id)
333 _notmuch_message_add_term (message, "thread", thread_id);
335 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
337 if (id_str.empty ()) {
338 message->doc.add_value (NOTMUCH_VALUE_THREAD, thread_id);
342 /* Think about using a hash here if there's any performance
344 pos = id_str.find (thread_id);
345 if (pos == std::string::npos) {
347 id_str.append (thread_id);
348 message->doc.add_value (NOTMUCH_VALUE_THREAD, id_str);
354 thread_id_generate (thread_id_t *thread_id)
356 static int seeded = 0;
363 dev_random = fopen ("/dev/random", "r");
364 if (dev_random == NULL) {
367 fread ((void *) &value, sizeof (value), 1, dev_random);
375 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
377 sprintf (s, "%08x", value);
383 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
385 /* If not part of any existing thread, generate a new thread_id. */
386 thread_id_t thread_id;
388 thread_id_generate (&thread_id);
389 _notmuch_message_add_term (message, "thread", thread_id.str);
390 message->doc.add_value (NOTMUCH_VALUE_THREAD, thread_id.str);
393 /* Synchronize changes made to message->doc out into the database. */
395 _notmuch_message_sync (notmuch_message_t *message)
397 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
399 db->replace_document (message->doc_id, message->doc);
402 /* Add a name:value term to 'message', (the actual term will be
403 * encoded by prefixing the value with a short prefix). See
404 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
405 * names to prefix values.
407 * This change will not be reflected in the database until the next
408 * call to _notmuch_message_set_sync. */
409 notmuch_private_status_t
410 _notmuch_message_add_term (notmuch_message_t *message,
411 const char *prefix_name,
418 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
420 term = talloc_asprintf (message, "%s%s",
421 _find_prefix (prefix_name), value);
423 if (strlen (term) > NOTMUCH_TERM_MAX)
424 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
426 message->doc.add_term (term);
430 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
433 /* Remove a name:value term from 'message', (the actual term will be
434 * encoded by prefixing the value with a short prefix). See
435 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
436 * names to prefix values.
438 * This change will not be reflected in the database until the next
439 * call to _notmuch_message_set_sync. */
440 notmuch_private_status_t
441 _notmuch_message_remove_term (notmuch_message_t *message,
442 const char *prefix_name,
448 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
450 term = talloc_asprintf (message, "%s%s",
451 _find_prefix (prefix_name), value);
453 if (strlen (term) > NOTMUCH_TERM_MAX)
454 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
456 message->doc.remove_term (term);
460 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
464 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
466 notmuch_private_status_t status;
469 return NOTMUCH_STATUS_NULL_POINTER;
471 if (strlen (tag) > NOTMUCH_TAG_MAX)
472 return NOTMUCH_STATUS_TAG_TOO_LONG;
474 status = _notmuch_message_add_term (message, "tag", tag);
476 fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
481 _notmuch_message_sync (message);
483 return NOTMUCH_STATUS_SUCCESS;
487 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
489 notmuch_private_status_t status;
492 return NOTMUCH_STATUS_NULL_POINTER;
494 if (strlen (tag) > NOTMUCH_TAG_MAX)
495 return NOTMUCH_STATUS_TAG_TOO_LONG;
497 status = _notmuch_message_remove_term (message, "tag", tag);
499 fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
504 _notmuch_message_sync (message);
506 return NOTMUCH_STATUS_SUCCESS;
510 notmuch_message_destroy (notmuch_message_t *message)
512 talloc_free (message);
516 notmuch_tags_has_more (notmuch_tags_t *tags)
520 if (tags->iterator == tags->iterator_end)
524 if (s.size () && s[0] == 'L')
531 notmuch_tags_get (notmuch_tags_t *tags)
533 return talloc_strdup (tags, (*tags->iterator).c_str () + 1);
537 notmuch_tags_advance (notmuch_tags_t *tags)
543 notmuch_tags_destroy (notmuch_tags_t *tags)
549 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
551 if (thread_ids->current == NULL || *thread_ids->current == '\0')
558 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
560 return thread_ids->current;
564 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
566 thread_ids->current = strsep (&thread_ids->next, ",");
570 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
572 talloc_free (thread_ids);