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