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