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