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