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