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