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" },
86 { "timestamp", "KTS" },
90 _find_prefix (const char *name)
94 for (i = 0; i < ARRAY_SIZE (NORMAL_PREFIX); i++)
95 if (strcmp (name, NORMAL_PREFIX[i].name) == 0)
96 return NORMAL_PREFIX[i].prefix;
98 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX); i++)
99 if (strcmp (name, BOOLEAN_PREFIX[i].name) == 0)
100 return BOOLEAN_PREFIX[i].prefix;
102 fprintf (stderr, "Internal error: No prefix exists for '%s'\n", name);
108 /* We end up having to call the destructor explicitly because we had
109 * to use "placement new" in order to initialize C++ objects within a
110 * block that we allocated with talloc. So C++ is making talloc
111 * slightly less simple to use, (we wouldn't need
112 * talloc_set_destructor at all otherwise).
115 _notmuch_message_destructor (notmuch_message_t *message)
117 message->doc.~Document ();
122 /* Create a new notmuch_message_t object for an existing document in
125 * Here, 'talloc owner' is an optional talloc context to which the new
126 * message will belong. This allows for the caller to not bother
127 * calling notmuch_message_destroy on the message, and no that all
128 * memory will be reclaimed with 'talloc_owner' is free. The caller
129 * still can call notmuch_message_destroy when finished with the
130 * message if desired.
132 * The 'talloc_owner' argument can also be NULL, in which case the
133 * caller *is* responsible for calling notmuch_message_destroy.
135 * If no document exists in the database with document ID of 'doc_id'
136 * then this function returns NULL.
139 _notmuch_message_create (const void *talloc_owner,
140 notmuch_database_t *notmuch,
143 notmuch_message_t *message;
145 message = talloc (talloc_owner, notmuch_message_t);
146 if (unlikely (message == NULL))
149 message->notmuch = notmuch;
150 message->doc_id = doc_id;
151 message->message_id = NULL; /* lazily created */
152 message->filename = NULL; /* lazily created */
153 new (&message->doc) Xapian::Document;
155 talloc_set_destructor (message, _notmuch_message_destructor);
158 message->doc = notmuch->xapian_db->get_document (doc_id);
159 } catch (const Xapian::DocNotFoundError &error) {
160 talloc_free (message);
167 /* Create a new notmuch_message_t object for a specific message ID,
168 * (which may or may not already exist in the databas).
170 * Here, 'talloc owner' is an optional talloc context to which the new
171 * message will belong. This allows for the caller to not bother
172 * calling notmuch_message_destroy on the message, and no that all
173 * memory will be reclaimed with 'talloc_owner' is free. The caller
174 * still can call notmuch_message_destroy when finished with the
175 * message if desired.
177 * The 'talloc_owner' argument can also be NULL, in which case the
178 * caller *is* responsible for calling notmuch_message_destroy.
180 * If there is already a document with message ID 'message_id' in the
181 * database, then the returned message can be used to query/modify the
182 * document. Otherwise, a new document will be inserted into the
183 * database before this function returns;
186 _notmuch_message_create_for_message_id (const void *talloc_owner,
187 notmuch_database_t *notmuch,
188 const char *message_id)
190 notmuch_message_t *message;
191 Xapian::Document doc;
195 message = notmuch_database_find_message (notmuch, message_id);
197 return talloc_steal (talloc_owner, message);
199 term = talloc_asprintf (NULL, "%s%s",
200 _find_prefix ("msgid"), message_id);
204 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
206 doc_id = notmuch->xapian_db->add_document (doc);
208 return _notmuch_message_create (talloc_owner, notmuch, doc_id);
212 notmuch_message_get_message_id (notmuch_message_t *message)
214 Xapian::TermIterator i;
216 if (message->message_id)
217 return message->message_id;
219 i = message->doc.termlist_begin ();
220 i.skip_to (_find_prefix ("msgid"));
222 if (i == message->doc.termlist_end ()) {
223 fprintf (stderr, "Internal error: Message with document ID of %d has no message ID.\n",
228 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
229 return message->message_id;
232 /* Set the filename for 'message' to 'filename'.
234 * XXX: We should still figure out what we want to do for multiple
235 * files with identical message IDs. We will probably want to store a
236 * list of filenames here, (so that this will be "add_filename"
237 * instead of "set_filename"). Which would make this very similar to
240 * This change will not be reflected in the database until the next
241 * call to _notmuch_message_set_sync. */
243 _notmuch_message_set_filename (notmuch_message_t *message,
244 const char *filename)
246 if (message->filename)
247 talloc_free (message->filename);
248 message->doc.set_data (filename);
252 notmuch_message_get_filename (notmuch_message_t *message)
254 std::string filename_str;
256 if (message->filename)
257 return message->filename;
259 filename_str = message->doc.get_data ();
260 message->filename = talloc_strdup (message, filename_str.c_str ());
262 return message->filename;
265 /* We end up having to call the destructors explicitly because we had
266 * to use "placement new" in order to initialize C++ objects within a
267 * block that we allocated with talloc. So C++ is making talloc
268 * slightly less simple to use, (we wouldn't need
269 * talloc_set_destructor at all otherwise).
272 _notmuch_tags_destructor (notmuch_tags_t *tags)
274 tags->iterator.~TermIterator ();
275 tags->iterator_end.~TermIterator ();
281 notmuch_message_get_tags (notmuch_message_t *message)
283 notmuch_tags_t *tags;
285 tags = talloc (message, notmuch_tags_t);
286 if (unlikely (tags == NULL))
289 new (&tags->iterator) Xapian::TermIterator;
290 new (&tags->iterator_end) Xapian::TermIterator;
292 talloc_set_destructor (tags, _notmuch_tags_destructor);
294 tags->iterator = message->doc.termlist_begin ();
295 tags->iterator.skip_to (_find_prefix ("tag"));
296 tags->iterator_end = message->doc.termlist_end ();
301 notmuch_thread_ids_t *
302 notmuch_message_get_thread_ids (notmuch_message_t *message)
304 notmuch_thread_ids_t *thread_ids;
307 thread_ids = talloc (message, notmuch_thread_ids_t);
308 if (unlikely (thread_ids == NULL))
311 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
312 thread_ids->next = talloc_strdup (message, id_str.c_str ());
314 /* Initialize thread_ids->current and terminate first ID. */
315 notmuch_thread_ids_advance (thread_ids);
321 _notmuch_message_set_date (notmuch_message_t *message,
326 time_value = notmuch_parse_date (date, NULL);
328 message->doc.add_value (NOTMUCH_VALUE_DATE,
329 Xapian::sortable_serialise (time_value));
333 _notmuch_message_add_thread_id (notmuch_message_t *message,
334 const char *thread_id)
338 _notmuch_message_add_term (message, "thread", thread_id);
340 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
342 if (id_str.empty ()) {
343 message->doc.add_value (NOTMUCH_VALUE_THREAD, thread_id);
347 /* Think about using a hash here if there's any performance
349 pos = id_str.find (thread_id);
350 if (pos == std::string::npos) {
352 id_str.append (thread_id);
353 message->doc.add_value (NOTMUCH_VALUE_THREAD, id_str);
359 thread_id_generate (thread_id_t *thread_id)
361 static int seeded = 0;
368 dev_random = fopen ("/dev/random", "r");
369 if (dev_random == NULL) {
372 fread ((void *) &value, sizeof (value), 1, dev_random);
380 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
382 sprintf (s, "%08x", value);
388 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
390 /* If not part of any existing thread, generate a new thread_id. */
391 thread_id_t thread_id;
393 thread_id_generate (&thread_id);
394 _notmuch_message_add_term (message, "thread", thread_id.str);
395 message->doc.add_value (NOTMUCH_VALUE_THREAD, thread_id.str);
398 /* Synchronize changes made to message->doc out into the database. */
400 _notmuch_message_sync (notmuch_message_t *message)
402 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
404 db->replace_document (message->doc_id, message->doc);
407 /* Add a name:value term to 'message', (the actual term will be
408 * encoded by prefixing the value with a short prefix). See
409 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
410 * names to prefix values.
412 * This change will not be reflected in the database until the next
413 * call to _notmuch_message_set_sync. */
414 notmuch_private_status_t
415 _notmuch_message_add_term (notmuch_message_t *message,
416 const char *prefix_name,
423 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
425 term = talloc_asprintf (message, "%s%s",
426 _find_prefix (prefix_name), value);
428 if (strlen (term) > NOTMUCH_TERM_MAX)
429 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
431 message->doc.add_term (term);
435 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
438 /* Remove a name:value term from 'message', (the actual term will be
439 * encoded by prefixing the value with a short prefix). See
440 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
441 * names to prefix values.
443 * This change will not be reflected in the database until the next
444 * call to _notmuch_message_set_sync. */
445 notmuch_private_status_t
446 _notmuch_message_remove_term (notmuch_message_t *message,
447 const char *prefix_name,
453 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
455 term = talloc_asprintf (message, "%s%s",
456 _find_prefix (prefix_name), value);
458 if (strlen (term) > NOTMUCH_TERM_MAX)
459 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
461 message->doc.remove_term (term);
465 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
469 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
471 notmuch_private_status_t status;
474 return NOTMUCH_STATUS_NULL_POINTER;
476 if (strlen (tag) > NOTMUCH_TAG_MAX)
477 return NOTMUCH_STATUS_TAG_TOO_LONG;
479 status = _notmuch_message_add_term (message, "tag", tag);
481 fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
486 _notmuch_message_sync (message);
488 return NOTMUCH_STATUS_SUCCESS;
492 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
494 notmuch_private_status_t status;
497 return NOTMUCH_STATUS_NULL_POINTER;
499 if (strlen (tag) > NOTMUCH_TAG_MAX)
500 return NOTMUCH_STATUS_TAG_TOO_LONG;
502 status = _notmuch_message_remove_term (message, "tag", tag);
504 fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
509 _notmuch_message_sync (message);
511 return NOTMUCH_STATUS_SUCCESS;
515 notmuch_message_destroy (notmuch_message_t *message)
517 talloc_free (message);
521 notmuch_tags_has_more (notmuch_tags_t *tags)
525 if (tags->iterator == tags->iterator_end)
529 if (! s.empty () && s[0] == 'L')
536 notmuch_tags_get (notmuch_tags_t *tags)
538 return talloc_strdup (tags, (*tags->iterator).c_str () + 1);
542 notmuch_tags_advance (notmuch_tags_t *tags)
548 notmuch_tags_destroy (notmuch_tags_t *tags)
554 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
556 if (thread_ids->current == NULL || *thread_ids->current == '\0')
563 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
565 return thread_ids->current;
569 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
571 thread_ids->current = strsep (&thread_ids->next, ",");
575 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
577 talloc_free (thread_ids);