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 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
46 /* These prefix values are specifically chosen to be compatible
47 * with sup, (http://sup.rubyforge.org), written by
48 * William Morgan <wmorgan-sup@masanjin.net>, and released
49 * under the GNU GPL v2.
57 prefix_t NORMAL_PREFIX[] = {
60 { "from_name", "FN" },
66 prefix_t BOOLEAN_PREFIX[] = {
68 { "from_email", "FE" },
75 { "attachment_extension", "O" },
82 _find_prefix (const char *name)
86 for (i = 0; i < ARRAY_SIZE (NORMAL_PREFIX); i++)
87 if (strcmp (name, NORMAL_PREFIX[i].name) == 0)
88 return NORMAL_PREFIX[i].prefix;
90 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX); i++)
91 if (strcmp (name, BOOLEAN_PREFIX[i].name) == 0)
92 return BOOLEAN_PREFIX[i].prefix;
97 /* We end up having to call the destructor explicitly because we had
98 * to use "placement new" in order to initialize C++ objects within a
99 * block that we allocated with talloc. So C++ is making talloc
100 * slightly less simple to use, (we wouldn't need
101 * talloc_set_destructor at all otherwise).
104 _notmuch_message_destructor (notmuch_message_t *message)
106 message->doc.~Document ();
112 _notmuch_message_create (const void *talloc_owner,
113 notmuch_database_t *notmuch,
116 notmuch_message_t *message;
118 message = talloc (talloc_owner, notmuch_message_t);
119 if (unlikely (message == NULL))
122 message->notmuch = notmuch;
123 message->doc_id = doc_id;
124 message->message_id = NULL; /* lazily created */
125 message->filename = NULL; /* lazily created */
126 new (&message->doc) Xapian::Document;
128 talloc_set_destructor (message, _notmuch_message_destructor);
130 message->doc = notmuch->xapian_db->get_document (doc_id);
136 notmuch_message_get_message_id (notmuch_message_t *message)
138 Xapian::TermIterator i;
140 if (message->message_id)
141 return message->message_id;
143 i = message->doc.termlist_begin ();
144 i.skip_to (_find_prefix ("msgid"));
146 /* XXX: This should really be an internal error, but we'll need to
147 * fix the add_message side of things first. */
148 if (i == message->doc.termlist_end ())
151 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
152 return message->message_id;
155 /* Set the filename for 'message' to 'filename'.
157 * XXX: We should still figure out what we want to do for multiple
158 * files with identical message IDs. We will probably want to store a
159 * list of filenames here, (so that this will be "add_filename"
160 * instead of "set_filename"). Which would make this very similar to
163 * This change will not be reflected in the database until the next
164 * call to _notmuch_message_set_sync. */
166 _notmuch_message_set_filename (notmuch_message_t *message,
167 const char *filename)
169 if (message->filename)
170 talloc_free (message->filename);
171 message->doc.set_data (filename);
175 notmuch_message_get_filename (notmuch_message_t *message)
177 std::string filename_str;
179 if (message->filename)
180 return message->filename;
182 filename_str = message->doc.get_data ();
183 message->filename = talloc_strdup (message, filename_str.c_str ());
185 return message->filename;
188 /* We end up having to call the destructors explicitly because we had
189 * to use "placement new" in order to initialize C++ objects within a
190 * block that we allocated with talloc. So C++ is making talloc
191 * slightly less simple to use, (we wouldn't need
192 * talloc_set_destructor at all otherwise).
195 _notmuch_tags_destructor (notmuch_tags_t *tags)
197 tags->iterator.~TermIterator ();
198 tags->iterator_end.~TermIterator ();
204 notmuch_message_get_tags (notmuch_message_t *message)
206 notmuch_tags_t *tags;
208 tags = talloc (message, notmuch_tags_t);
209 if (unlikely (tags == NULL))
212 new (&tags->iterator) Xapian::TermIterator;
213 new (&tags->iterator_end) Xapian::TermIterator;
215 talloc_set_destructor (tags, _notmuch_tags_destructor);
217 tags->iterator = message->doc.termlist_begin ();
218 tags->iterator.skip_to (_find_prefix ("tag"));
219 tags->iterator_end = message->doc.termlist_end ();
224 notmuch_thread_ids_t *
225 notmuch_message_get_thread_ids (notmuch_message_t *message)
227 notmuch_thread_ids_t *thread_ids;
230 thread_ids = talloc (message, notmuch_thread_ids_t);
231 if (unlikely (thread_ids == NULL))
234 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
235 thread_ids->next = talloc_strdup (message, id_str.c_str ());
237 /* Initialize thread_ids->current and terminate first ID. */
238 notmuch_thread_ids_advance (thread_ids);
244 _notmuch_message_set_date (notmuch_message_t *message,
249 time_value = notmuch_parse_date (date, NULL);
251 message->doc.add_value (NOTMUCH_VALUE_DATE,
252 Xapian::sortable_serialise (time_value));
256 _notmuch_message_add_thread_id (notmuch_message_t *message,
257 const char *thread_id)
261 _notmuch_message_add_term (message, "thread", thread_id);
263 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
265 if (id_str.empty ()) {
266 message->doc.add_value (NOTMUCH_VALUE_THREAD, thread_id);
270 /* Think about using a hash here if there's any performance
272 pos = id_str.find (thread_id);
273 if (pos == std::string::npos) {
275 id_str.append (thread_id);
276 message->doc.add_value (NOTMUCH_VALUE_THREAD, id_str);
282 thread_id_generate (thread_id_t *thread_id)
284 static int seeded = 0;
291 dev_random = fopen ("/dev/random", "r");
292 if (dev_random == NULL) {
295 fread ((void *) &value, sizeof (value), 1, dev_random);
303 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
305 sprintf (s, "%08x", value);
311 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
313 /* If not part of any existing thread, generate a new thread_id. */
314 thread_id_t thread_id;
316 thread_id_generate (&thread_id);
317 _notmuch_message_add_term (message, "thread", thread_id.str);
318 message->doc.add_value (NOTMUCH_VALUE_THREAD, thread_id.str);
321 /* Synchronize changes made to message->doc out into the database. */
323 _notmuch_message_sync (notmuch_message_t *message)
325 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
327 db->replace_document (message->doc_id, message->doc);
330 /* Add a name:value term to 'message', (the actual term will be
331 * encoded by prefixing the value with a short prefix). See
332 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
333 * names to prefix values.
335 * This change will not be reflected in the database until the next
336 * call to _notmuch_message_set_sync. */
337 notmuch_private_status_t
338 _notmuch_message_add_term (notmuch_message_t *message,
339 const char *prefix_name,
346 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
348 term = talloc_asprintf (message, "%s%s",
349 _find_prefix (prefix_name), value);
351 if (strlen (term) > NOTMUCH_TERM_MAX)
352 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
354 message->doc.add_term (term);
358 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
361 /* Remove a name:value term from 'message', (the actual term will be
362 * encoded by prefixing the value with a short prefix). See
363 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
364 * names to prefix values.
366 * This change will not be reflected in the database until the next
367 * call to _notmuch_message_set_sync. */
368 notmuch_private_status_t
369 _notmuch_message_remove_term (notmuch_message_t *message,
370 const char *prefix_name,
376 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
378 term = talloc_asprintf (message, "%s%s",
379 _find_prefix (prefix_name), value);
381 if (strlen (term) > NOTMUCH_TERM_MAX)
382 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
384 message->doc.remove_term (term);
388 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
392 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
394 notmuch_private_status_t status;
397 return NOTMUCH_STATUS_NULL_POINTER;
399 if (strlen (tag) > NOTMUCH_TAG_MAX)
400 return NOTMUCH_STATUS_TAG_TOO_LONG;
402 status = _notmuch_message_add_term (message, "tag", tag);
404 fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
409 _notmuch_message_sync (message);
411 return NOTMUCH_STATUS_SUCCESS;
415 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
417 notmuch_private_status_t status;
420 return NOTMUCH_STATUS_NULL_POINTER;
422 if (strlen (tag) > NOTMUCH_TAG_MAX)
423 return NOTMUCH_STATUS_TAG_TOO_LONG;
425 status = _notmuch_message_remove_term (message, "tag", tag);
427 fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
432 _notmuch_message_sync (message);
434 return NOTMUCH_STATUS_SUCCESS;
438 notmuch_message_destroy (notmuch_message_t *message)
440 talloc_free (message);
444 notmuch_tags_has_more (notmuch_tags_t *tags)
448 if (tags->iterator == tags->iterator_end)
452 if (s.size () && s[0] == 'L')
459 notmuch_tags_get (notmuch_tags_t *tags)
461 return talloc_strdup (tags, (*tags->iterator).c_str () + 1);
465 notmuch_tags_advance (notmuch_tags_t *tags)
471 notmuch_tags_destroy (notmuch_tags_t *tags)
477 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
479 if (thread_ids->current == NULL || *thread_ids->current == '\0')
486 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
488 return thread_ids->current;
492 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
494 thread_ids->current = strsep (&thread_ids->next, ",");
498 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
500 talloc_free (thread_ids);