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