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;
156 notmuch_message_get_filename (notmuch_message_t *message)
158 std::string filename_str;
160 if (message->filename)
161 return message->filename;
163 filename_str = message->doc.get_data ();
164 message->filename = talloc_strdup (message, filename_str.c_str ());
166 return message->filename;
169 /* We end up having to call the destructors explicitly because we had
170 * to use "placement new" in order to initialize C++ objects within a
171 * block that we allocated with talloc. So C++ is making talloc
172 * slightly less simple to use, (we wouldn't need
173 * talloc_set_destructor at all otherwise).
176 _notmuch_tags_destructor (notmuch_tags_t *tags)
178 tags->iterator.~TermIterator ();
179 tags->iterator_end.~TermIterator ();
185 notmuch_message_get_tags (notmuch_message_t *message)
187 notmuch_tags_t *tags;
189 tags = talloc (message, notmuch_tags_t);
190 if (unlikely (tags == NULL))
193 new (&tags->iterator) Xapian::TermIterator;
194 new (&tags->iterator_end) Xapian::TermIterator;
196 talloc_set_destructor (tags, _notmuch_tags_destructor);
198 tags->iterator = message->doc.termlist_begin ();
199 tags->iterator.skip_to (_find_prefix ("tag"));
200 tags->iterator_end = message->doc.termlist_end ();
205 notmuch_thread_ids_t *
206 notmuch_message_get_thread_ids (notmuch_message_t *message)
208 notmuch_thread_ids_t *thread_ids;
211 thread_ids = talloc (message, notmuch_thread_ids_t);
212 if (unlikely (thread_ids == NULL))
215 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
216 thread_ids->next = talloc_strdup (message, id_str.c_str ());
218 /* Initialize thread_ids->current and terminate first ID. */
219 notmuch_thread_ids_advance (thread_ids);
225 thread_id_generate (thread_id_t *thread_id)
227 static int seeded = 0;
234 dev_random = fopen ("/dev/random", "r");
235 if (dev_random == NULL) {
238 fread ((void *) &value, sizeof (value), 1, dev_random);
246 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
248 sprintf (s, "%08x", value);
253 /* Synchronize changes made to message->doc into the database. */
255 _notmuch_message_sync (notmuch_message_t *message)
257 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
259 db->replace_document (message->doc_id, message->doc);
262 /* Add a name:value term to 'message', (the actual term will be
263 * encoded by prefixing the value with a short prefix). See
264 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
265 * names to prefix values.
267 * This change will not be reflected in the database until the next
268 * call to _notmuch_message_set_sync. */
269 notmuch_private_status_t
270 _notmuch_message_add_term (notmuch_message_t *message,
271 const char *prefix_name,
278 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
280 term = talloc_asprintf (message, "%s%s",
281 _find_prefix (prefix_name), value);
283 if (strlen (term) > NOTMUCH_TERM_MAX)
284 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
286 message->doc.add_term (term);
290 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
293 /* Remove a name:value term from 'message', (the actual term will be
294 * encoded by prefixing the value with a short prefix). See
295 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
296 * names to prefix values.
298 * This change will not be reflected in the database until the next
299 * call to _notmuch_message_set_sync. */
300 notmuch_private_status_t
301 _notmuch_message_remove_term (notmuch_message_t *message,
302 const char *prefix_name,
308 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
310 term = talloc_asprintf (message, "%s%s",
311 _find_prefix (prefix_name), value);
313 if (strlen (term) > NOTMUCH_TERM_MAX)
314 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
316 message->doc.remove_term (term);
320 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
324 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
326 notmuch_private_status_t status;
329 return NOTMUCH_STATUS_NULL_POINTER;
331 if (strlen (tag) > NOTMUCH_TAG_MAX)
332 return NOTMUCH_STATUS_TAG_TOO_LONG;
334 status = _notmuch_message_add_term (message, "tag", tag);
336 fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
341 _notmuch_message_sync (message);
343 return NOTMUCH_STATUS_SUCCESS;
347 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
349 notmuch_private_status_t status;
352 return NOTMUCH_STATUS_NULL_POINTER;
354 if (strlen (tag) > NOTMUCH_TAG_MAX)
355 return NOTMUCH_STATUS_TAG_TOO_LONG;
357 status = _notmuch_message_remove_term (message, "tag", tag);
359 fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
364 _notmuch_message_sync (message);
366 return NOTMUCH_STATUS_SUCCESS;
370 notmuch_message_destroy (notmuch_message_t *message)
372 talloc_free (message);
376 notmuch_tags_has_more (notmuch_tags_t *tags)
380 if (tags->iterator == tags->iterator_end)
384 if (s.size () && s[0] == 'L')
391 notmuch_tags_get (notmuch_tags_t *tags)
393 return talloc_strdup (tags, (*tags->iterator).c_str () + 1);
397 notmuch_tags_advance (notmuch_tags_t *tags)
403 notmuch_tags_destroy (notmuch_tags_t *tags)
409 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
411 if (thread_ids->current == NULL || *thread_ids->current == '\0')
418 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
420 return thread_ids->current;
424 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
426 thread_ids->current = strsep (&thread_ids->next, ",");
430 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
432 talloc_free (thread_ids);