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