]> git.notmuchmail.org Git - notmuch/blob - message.cc
Add an initial implementation of a notmuch_thread_t object.
[notmuch] / message.cc
1 /* message.cc - Results of message-based searches from a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22 #include "database-private.h"
23
24 #include <xapian.h>
25
26 struct _notmuch_message {
27     notmuch_database_t *notmuch;
28     Xapian::docid doc_id;
29     char *message_id;
30     char *thread_id;
31     char *filename;
32     Xapian::Document doc;
33 };
34
35 typedef struct _notmuch_terms {
36     char prefix_char;
37     Xapian::TermIterator iterator;
38     Xapian::TermIterator iterator_end;
39 } notmuch_terms_t;
40
41 struct _notmuch_tags {
42     notmuch_terms_t terms;
43 };
44
45 /* "128 bits of thread-id ought to be enough for anybody" */
46 #define NOTMUCH_THREAD_ID_BITS   128
47 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
48 typedef struct _thread_id {
49     char str[NOTMUCH_THREAD_ID_DIGITS + 1];
50 } thread_id_t;
51
52 /* We end up having to call the destructor explicitly because we had
53  * to use "placement new" in order to initialize C++ objects within a
54  * block that we allocated with talloc. So C++ is making talloc
55  * slightly less simple to use, (we wouldn't need
56  * talloc_set_destructor at all otherwise).
57  */
58 static int
59 _notmuch_message_destructor (notmuch_message_t *message)
60 {
61     message->doc.~Document ();
62
63     return 0;
64 }
65
66 /* Create a new notmuch_message_t object for an existing document in
67  * the database.
68  *
69  * Here, 'talloc owner' is an optional talloc context to which the new
70  * message will belong. This allows for the caller to not bother
71  * calling notmuch_message_destroy on the message, and no that all
72  * memory will be reclaimed with 'talloc_owner' is free. The caller
73  * still can call notmuch_message_destroy when finished with the
74  * message if desired.
75  *
76  * The 'talloc_owner' argument can also be NULL, in which case the
77  * caller *is* responsible for calling notmuch_message_destroy.
78  *
79  * If no document exists in the database with document ID of 'doc_id'
80  * then this function returns NULL and optionally sets *status to
81  * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
82  *
83  * This function can also fail to due lack of available memory,
84  * returning NULL and optionally setting *status to
85  * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
86  *
87  * The caller can pass NULL for status if uninterested in
88  * distinguishing these two cases.
89  */
90 notmuch_message_t *
91 _notmuch_message_create (const void *talloc_owner,
92                          notmuch_database_t *notmuch,
93                          unsigned int doc_id,
94                          notmuch_private_status_t *status)
95 {
96     notmuch_message_t *message;
97
98     if (status)
99         *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
100
101     message = talloc (talloc_owner, notmuch_message_t);
102     if (unlikely (message == NULL)) {
103         if (status)
104             *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
105         return NULL;
106     }
107
108     message->notmuch = notmuch;
109     message->doc_id = doc_id;
110     message->message_id = NULL; /* lazily created */
111     message->thread_id = NULL; /* lazily created */
112     message->filename = NULL; /* lazily created */
113
114     /* This is C++'s creepy "placement new", which is really just an
115      * ugly way to call a constructor for a pre-allocated object. So
116      * it's really not an error to not be checking for OUT_OF_MEMORY
117      * here, since this "new" isn't actually allocating memory. This
118      * is language-design comedy of the wrong kind. */
119
120     new (&message->doc) Xapian::Document;
121
122     talloc_set_destructor (message, _notmuch_message_destructor);
123
124     try {
125         message->doc = notmuch->xapian_db->get_document (doc_id);
126     } catch (const Xapian::DocNotFoundError &error) {
127         talloc_free (message);
128         if (status)
129             *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
130         return NULL;
131     }
132
133     return message;
134 }
135
136 /* Create a new notmuch_message_t object for a specific message ID,
137  * (which may or may not already exist in the databas).
138  *
139  * Here, 'talloc owner' is an optional talloc context to which the new
140  * message will belong. This allows for the caller to not bother
141  * calling notmuch_message_destroy on the message, and no that all
142  * memory will be reclaimed with 'talloc_owner' is free. The caller
143  * still can call notmuch_message_destroy when finished with the
144  * message if desired.
145  *
146  * The 'talloc_owner' argument can also be NULL, in which case the
147  * caller *is* responsible for calling notmuch_message_destroy.
148  *
149  * If there is already a document with message ID 'message_id' in the
150  * database, then the returned message can be used to query/modify the
151  * document. Otherwise, a new document will be inserted into the
152  * database before this function returns.
153  *
154  * If an error occurs, this function will return NULL and *status
155  * will be set as appropriate. (The status pointer argument must
156  * not be NULL.)
157  */
158 notmuch_message_t *
159 _notmuch_message_create_for_message_id (const void *talloc_owner,
160                                         notmuch_database_t *notmuch,
161                                         const char *message_id,
162                                         notmuch_status_t *status)
163 {
164     notmuch_private_status_t private_status;
165     notmuch_message_t *message;
166     Xapian::Document doc;
167     unsigned int doc_id;
168     char *term;
169
170     *status = NOTMUCH_STATUS_SUCCESS;
171
172     message = notmuch_database_find_message (notmuch, message_id);
173     if (message)
174         return talloc_steal (talloc_owner, message);
175
176     term = talloc_asprintf (NULL, "%s%s",
177                             _find_prefix ("id"), message_id);
178     if (term == NULL) {
179         *status = NOTMUCH_STATUS_OUT_OF_MEMORY;
180         return NULL;
181     }
182
183     try {
184         doc.add_term (term);
185         talloc_free (term);
186
187         doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
188
189         doc_id = notmuch->xapian_db->add_document (doc);
190     } catch (const Xapian::Error &error) {
191         *status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
192         return NULL;
193     }
194
195     message = _notmuch_message_create (talloc_owner, notmuch,
196                                        doc_id, &private_status);
197
198     *status = COERCE_STATUS (private_status,
199                              "Failed to find dcocument after inserting it.");
200
201     return message;
202 }
203
204 const char *
205 notmuch_message_get_message_id (notmuch_message_t *message)
206 {
207     Xapian::TermIterator i;
208
209     if (message->message_id)
210         return message->message_id;
211
212     i = message->doc.termlist_begin ();
213     i.skip_to (_find_prefix ("id"));
214
215     if (i == message->doc.termlist_end ())
216         INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
217                         message->doc_id);
218
219     message->message_id = talloc_strdup (message, (*i).c_str () + 1);
220
221 #if DEBUG_DATABASE_SANITY
222     i++;
223
224     if (i != message->doc.termlist_end () &&
225         strncmp ((*i).c_str (), _find_prefix ("id"),
226                  strlen (_find_prefix ("id"))) == 0)
227     {
228         INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
229                         message->doc_id);
230     }
231 #endif
232
233     return message->message_id;
234 }
235
236 const char *
237 notmuch_message_get_thread_id (notmuch_message_t *message)
238 {
239     Xapian::TermIterator i;
240
241     if (message->thread_id)
242         return message->thread_id;
243
244     i = message->doc.termlist_begin ();
245     i.skip_to (_find_prefix ("thread"));
246
247     if (i == message->doc.termlist_end ())
248         INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
249                         message->doc_id);
250
251     message->thread_id = talloc_strdup (message, (*i).c_str () + 1);
252
253 #if DEBUG_DATABASE_SANITY
254     i++;
255
256     if (i != message->doc.termlist_end () &&
257         strncmp ((*i).c_str (), _find_prefix ("thread"),
258                  strlen (_find_prefix ("thread"))) == 0)
259     {
260         INTERNAL_ERROR ("Message with document ID of %d has duplicate thread IDs.\n",
261                         message->doc_id);
262     }
263 #endif
264
265     return message->thread_id;
266 }
267
268 /* Set the filename for 'message' to 'filename'.
269  *
270  * XXX: We should still figure out if we think it's important to store
271  * multiple filenames for email messages with identical message IDs.
272  *
273  * This change will not be reflected in the database until the next
274  * call to _notmuch_message_set_sync. */
275 void
276 _notmuch_message_set_filename (notmuch_message_t *message,
277                                const char *filename)
278 {
279     if (message->filename)
280         talloc_free (message->filename);
281     message->doc.set_data (filename);
282 }
283
284 const char *
285 notmuch_message_get_filename (notmuch_message_t *message)
286 {
287     std::string filename_str;
288
289     if (message->filename)
290         return message->filename;
291
292     filename_str = message->doc.get_data ();
293     message->filename = talloc_strdup (message, filename_str.c_str ());
294
295     return message->filename;
296 }
297
298 /* We end up having to call the destructors explicitly because we had
299  * to use "placement new" in order to initialize C++ objects within a
300  * block that we allocated with talloc. So C++ is making talloc
301  * slightly less simple to use, (we wouldn't need
302  * talloc_set_destructor at all otherwise).
303  */
304 static int
305 _notmuch_terms_destructor (notmuch_terms_t *terms)
306 {
307     terms->iterator.~TermIterator ();
308     terms->iterator_end.~TermIterator ();
309
310     return 0;
311 }
312
313 static notmuch_terms_t *
314 _notmuch_terms_create (void *ctx,
315                        Xapian::Document doc,
316                        const char *prefix_name)
317 {
318     notmuch_terms_t *terms;
319     const char *prefix = _find_prefix (prefix_name);
320
321     /* Currently, notmuch_terms_t is written with the assumption that
322      * any prefix its derivatives use will be only a single
323      * character. */
324     assert (strlen (prefix) == 1);
325
326     terms = talloc (ctx, notmuch_terms_t);
327     if (unlikely (terms == NULL))
328         return NULL;
329
330     terms->prefix_char = *prefix;
331     new (&terms->iterator) Xapian::TermIterator;
332     new (&terms->iterator_end) Xapian::TermIterator;
333
334     talloc_set_destructor (terms, _notmuch_terms_destructor);
335
336     terms->iterator = doc.termlist_begin ();
337     terms->iterator.skip_to (prefix);
338     terms->iterator_end = doc.termlist_end ();
339
340     return terms;
341 }
342
343 /* The assertion is to ensure that 'type' is a derivative of
344  * notmuch_terms_t in that it contains a notmuch_terms_t as its first
345  * member. We do this by name of 'terms' as opposed to type, because
346  * that's as clever as I've been so far. */
347 #define _notmuch_terms_create_type(ctx, doc, prefix_name, type) \
348     (COMPILE_TIME_ASSERT(offsetof(type, terms) == 0),           \
349      (type *) _notmuch_terms_create (ctx, doc, prefix_name))
350
351 notmuch_tags_t *
352 notmuch_message_get_tags (notmuch_message_t *message)
353 {
354     return _notmuch_terms_create_type (message, message->doc, "tag",
355                                        notmuch_tags_t);
356 }
357
358 void
359 _notmuch_message_set_date (notmuch_message_t *message,
360                            const char *date)
361 {
362     time_t time_value;
363
364     time_value = notmuch_parse_date (date, NULL);
365
366     message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
367                             Xapian::sortable_serialise (time_value));
368 }
369
370 static void
371 thread_id_generate (thread_id_t *thread_id)
372 {
373     static int seeded = 0;
374     FILE *dev_random;
375     uint32_t value;
376     char *s;
377     int i;
378
379     if (! seeded) {
380         dev_random = fopen ("/dev/random", "r");
381         if (dev_random == NULL) {
382             srand (time (NULL));
383         } else {
384             fread ((void *) &value, sizeof (value), 1, dev_random);
385             srand (value);
386             fclose (dev_random);
387         }
388         seeded = 1;
389     }
390
391     s = thread_id->str;
392     for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
393         value = rand ();
394         sprintf (s, "%08x", value);
395         s += 8;
396     }
397 }
398
399 void
400 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
401 {
402     /* If not part of any existing thread, generate a new thread_id. */
403     thread_id_t thread_id;
404
405     thread_id_generate (&thread_id);
406     _notmuch_message_add_term (message, "thread", thread_id.str);
407 }
408
409 /* Synchronize changes made to message->doc out into the database. */
410 void
411 _notmuch_message_sync (notmuch_message_t *message)
412 {
413     Xapian::WritableDatabase *db = message->notmuch->xapian_db;
414
415     db->replace_document (message->doc_id, message->doc);
416 }
417
418 /* Add a name:value term to 'message', (the actual term will be
419  * encoded by prefixing the value with a short prefix). See
420  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
421  * names to prefix values.
422  *
423  * This change will not be reflected in the database until the next
424  * call to _notmuch_message_set_sync. */
425 notmuch_private_status_t
426 _notmuch_message_add_term (notmuch_message_t *message,
427                            const char *prefix_name,
428                            const char *value)
429 {
430
431     char *term;
432
433     if (value == NULL)
434         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
435
436     term = talloc_asprintf (message, "%s%s",
437                             _find_prefix (prefix_name), value);
438
439     if (strlen (term) > NOTMUCH_TERM_MAX)
440         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
441
442     message->doc.add_term (term);
443
444     talloc_free (term);
445
446     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
447 }
448
449 /* Remove a name:value term from 'message', (the actual term will be
450  * encoded by prefixing the value with a short prefix). See
451  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
452  * names to prefix values.
453  *
454  * This change will not be reflected in the database until the next
455  * call to _notmuch_message_set_sync. */
456 notmuch_private_status_t
457 _notmuch_message_remove_term (notmuch_message_t *message,
458                               const char *prefix_name,
459                               const char *value)
460 {
461     char *term;
462
463     if (value == NULL)
464         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
465
466     term = talloc_asprintf (message, "%s%s",
467                             _find_prefix (prefix_name), value);
468
469     if (strlen (term) > NOTMUCH_TERM_MAX)
470         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
471
472     message->doc.remove_term (term);
473
474     talloc_free (term);
475
476     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
477 }
478
479 notmuch_status_t
480 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
481 {
482     notmuch_private_status_t status;
483
484     if (tag == NULL)
485         return NOTMUCH_STATUS_NULL_POINTER;
486
487     if (strlen (tag) > NOTMUCH_TAG_MAX)
488         return NOTMUCH_STATUS_TAG_TOO_LONG;
489
490     status = _notmuch_message_add_term (message, "tag", tag);
491     if (status) {
492         INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
493                         status);
494     }
495
496     _notmuch_message_sync (message);
497
498     return NOTMUCH_STATUS_SUCCESS;
499 }
500
501 notmuch_status_t
502 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
503 {
504     notmuch_private_status_t status;
505
506     if (tag == NULL)
507         return NOTMUCH_STATUS_NULL_POINTER;
508
509     if (strlen (tag) > NOTMUCH_TAG_MAX)
510         return NOTMUCH_STATUS_TAG_TOO_LONG;
511
512     status = _notmuch_message_remove_term (message, "tag", tag);
513     if (status) {
514         INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
515                         status);
516     }
517
518     _notmuch_message_sync (message);
519
520     return NOTMUCH_STATUS_SUCCESS;
521 }
522
523 void
524 notmuch_message_destroy (notmuch_message_t *message)
525 {
526     talloc_free (message);
527 }
528
529 static notmuch_bool_t
530 _notmuch_terms_has_more (notmuch_terms_t *terms)
531 {
532     std::string s;
533
534     if (terms->iterator == terms->iterator_end)
535         return FALSE;
536
537     s = *terms->iterator;
538     if (! s.empty () && s[0] == terms->prefix_char)
539         return TRUE;
540     else
541         return FALSE;
542 }
543
544 static const char *
545 _notmuch_terms_get (notmuch_terms_t *terms)
546 {
547     return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
548 }
549
550 static void
551 _notmuch_terms_advance (notmuch_terms_t *terms)
552 {
553     terms->iterator++;
554 }
555
556 static void
557 _notmuch_terms_destroy (notmuch_terms_t *terms)
558 {
559     talloc_free (terms);
560 }
561
562 notmuch_bool_t
563 notmuch_tags_has_more (notmuch_tags_t *tags)
564 {
565     return _notmuch_terms_has_more (&tags->terms);
566 }
567
568 const char *
569 notmuch_tags_get (notmuch_tags_t *tags)
570 {
571     return _notmuch_terms_get (&tags->terms);
572 }
573
574 void
575 notmuch_tags_advance (notmuch_tags_t *tags)
576 {
577     return _notmuch_terms_advance (&tags->terms);
578 }
579
580 void
581 notmuch_tags_destroy (notmuch_tags_t *tags)
582 {
583     return _notmuch_terms_destroy (&tags->terms);
584 }