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