]> git.notmuchmail.org Git - notmuch/blob - message.cc
add_message: Re-order the code a bit (find message-id first).
[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     Xapian::Document doc;
31 };
32
33 struct _notmuch_tags {
34     Xapian::TermIterator iterator;
35     Xapian::TermIterator iterator_end;
36 };
37
38 struct _notmuch_thread_ids {
39     char *current;
40     char *next;
41 };
42
43 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
44
45 /* These prefix values are specifically chosen to be compatible
46  * with sup, (http://sup.rubyforge.org), written by
47  * William Morgan <wmorgan-sup@masanjin.net>, and released
48  * under the GNU GPL v2.
49  */
50
51 typedef struct {
52     const char *name;
53     const char *prefix;
54 } prefix_t;
55
56 prefix_t NORMAL_PREFIX[] = {
57     { "subject", "S" },
58     { "body", "B" },
59     { "from_name", "FN" },
60     { "to_name", "TN" },
61     { "name", "N" },
62     { "attachment", "A" }
63 };
64
65 prefix_t BOOLEAN_PREFIX[] = {
66     { "type", "K" },
67     { "from_email", "FE" },
68     { "to_email", "TE" },
69     { "email", "E" },
70     { "date", "D" },
71     { "label", "L" },
72     { "tag", "L" },
73     { "source_id", "I" },
74     { "attachment_extension", "O" },
75     { "msgid", "Q" },
76     { "thread", "H" },
77     { "ref", "R" }
78 };
79
80 const char *
81 _find_prefix (const char *name)
82 {
83     unsigned int i;
84
85     for (i = 0; i < ARRAY_SIZE (NORMAL_PREFIX); i++)
86         if (strcmp (name, NORMAL_PREFIX[i].name) == 0)
87             return NORMAL_PREFIX[i].prefix;
88
89     for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX); i++)
90         if (strcmp (name, BOOLEAN_PREFIX[i].name) == 0)
91             return BOOLEAN_PREFIX[i].prefix;
92
93     return "";
94 }
95
96 /* We end up having to call the destructor explicitly because we had
97  * to use "placement new" in order to initialize C++ objects within a
98  * block that we allocated with talloc. So C++ is making talloc
99  * slightly less simple to use, (we wouldn't need
100  * talloc_set_destructor at all otherwise).
101  */
102 static int
103 _notmuch_message_destructor (notmuch_message_t *message)
104 {
105     message->doc.~Document ();
106
107     return 0;
108 }
109
110 notmuch_message_t *
111 _notmuch_message_create (const void *talloc_owner,
112                          notmuch_database_t *notmuch,
113                          unsigned int doc_id)
114 {
115     notmuch_message_t *message;
116
117     message = talloc (talloc_owner, notmuch_message_t);
118     if (unlikely (message == NULL))
119         return NULL;
120
121     message->notmuch = notmuch;
122     message->doc_id = doc_id;
123     message->message_id = NULL; /* lazily created */
124     new (&message->doc) Xapian::Document;
125
126     talloc_set_destructor (message, _notmuch_message_destructor);
127
128     message->doc = notmuch->xapian_db->get_document (doc_id);
129
130     return message;
131 }
132
133 const char *
134 notmuch_message_get_message_id (notmuch_message_t *message)
135 {
136     Xapian::TermIterator i;
137
138     if (message->message_id)
139         return message->message_id;
140
141     i = message->doc.termlist_begin ();
142     i.skip_to (_find_prefix ("msgid"));
143
144     /* XXX: This should really be an internal error, but we'll need to
145      * fix the add_message side of things first. */
146     if (i == message->doc.termlist_end ())
147         return NULL;
148
149     message->message_id = talloc_strdup (message, (*i).c_str () + 1);
150     return message->message_id;
151 }
152
153 /* We end up having to call the destructors explicitly because we had
154  * to use "placement new" in order to initialize C++ objects within a
155  * block that we allocated with talloc. So C++ is making talloc
156  * slightly less simple to use, (we wouldn't need
157  * talloc_set_destructor at all otherwise).
158  */
159 static int
160 _notmuch_tags_destructor (notmuch_tags_t *tags)
161 {
162     tags->iterator.~TermIterator ();
163     tags->iterator_end.~TermIterator ();
164
165     return 0;
166 }
167
168 notmuch_tags_t *
169 notmuch_message_get_tags (notmuch_message_t *message)
170 {
171     notmuch_tags_t *tags;
172
173     tags = talloc (message, notmuch_tags_t);
174     if (unlikely (tags == NULL))
175         return NULL;
176
177     new (&tags->iterator) Xapian::TermIterator;
178     new (&tags->iterator_end) Xapian::TermIterator;
179
180     talloc_set_destructor (tags, _notmuch_tags_destructor);
181
182     tags->iterator = message->doc.termlist_begin ();
183     tags->iterator.skip_to (_find_prefix ("tag"));
184     tags->iterator_end = message->doc.termlist_end ();
185
186     return tags;
187 }
188
189 notmuch_thread_ids_t *
190 notmuch_message_get_thread_ids (notmuch_message_t *message)
191 {
192     notmuch_thread_ids_t *thread_ids;
193     std::string id_str;
194
195     thread_ids = talloc (message, notmuch_thread_ids_t);
196     if (unlikely (thread_ids == NULL))
197         return NULL;
198
199     id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
200     thread_ids->next = talloc_strdup (message, id_str.c_str ());
201
202     /* Initialize thread_ids->current and terminate first ID. */
203     notmuch_thread_ids_advance (thread_ids);
204
205     return thread_ids;
206 }
207
208 void
209 thread_id_generate (thread_id_t *thread_id)
210 {
211     static int seeded = 0;
212     FILE *dev_random;
213     uint32_t value;
214     char *s;
215     int i;
216
217     if (! seeded) {
218         dev_random = fopen ("/dev/random", "r");
219         if (dev_random == NULL) {
220             srand (time (NULL));
221         } else {
222             fread ((void *) &value, sizeof (value), 1, dev_random);
223             srand (value);
224             fclose (dev_random);
225         }
226         seeded = 1;
227     }
228
229     s = thread_id->str;
230     for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
231         value = rand ();
232         sprintf (s, "%08x", value);
233         s += 8;
234     }
235 }
236
237 /* Synchronize changes made to message->doc into the database. */
238 static void
239 _notmuch_message_sync (notmuch_message_t *message)
240 {
241     Xapian::WritableDatabase *db = message->notmuch->xapian_db;
242
243     db->replace_document (message->doc_id, message->doc);
244 }
245
246 /* Add a name:value term to 'message', (the actual term will be
247  * encoded by prefixing the value with a short prefix). See
248  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
249  * names to prefix values.
250  *
251  * This change will not be reflected in the database until the next
252  * call to _notmuch_message_set_sync. */
253 notmuch_private_status_t
254 _notmuch_message_add_term (notmuch_message_t *message,
255                            const char *prefix_name,
256                            const char *value)
257 {
258
259     char *term;
260
261     if (value == NULL)
262         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
263
264     term = talloc_asprintf (message, "%s%s",
265                             _find_prefix (prefix_name), value);
266
267     if (strlen (term) > NOTMUCH_TERM_MAX)
268         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
269
270     message->doc.add_term (term);
271
272     talloc_free (term);
273
274     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
275 }
276
277 /* Remove a name:value term from 'message', (the actual term will be
278  * encoded by prefixing the value with a short prefix). See
279  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
280  * names to prefix values.
281  *
282  * This change will not be reflected in the database until the next
283  * call to _notmuch_message_set_sync. */
284 notmuch_private_status_t
285 _notmuch_message_remove_term (notmuch_message_t *message,
286                               const char *prefix_name,
287                               const char *value)
288 {
289     char *term;
290
291     if (value == NULL)
292         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
293
294     term = talloc_asprintf (message, "%s%s",
295                             _find_prefix (prefix_name), value);
296
297     if (strlen (term) > NOTMUCH_TERM_MAX)
298         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
299
300     message->doc.remove_term (term);
301
302     talloc_free (term);
303
304     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
305 }
306
307 notmuch_status_t
308 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
309 {
310     notmuch_private_status_t status;
311
312     if (tag == NULL)
313         return NOTMUCH_STATUS_NULL_POINTER;
314
315     if (strlen (tag) > NOTMUCH_TAG_MAX)
316         return NOTMUCH_STATUS_TAG_TOO_LONG;
317
318     status = _notmuch_message_add_term (message, "tag", tag);
319     if (status) {
320         fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
321                  status);
322         exit (1);
323     }
324
325     _notmuch_message_sync (message);
326
327     return NOTMUCH_STATUS_SUCCESS;
328 }
329
330 notmuch_status_t
331 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
332 {
333     notmuch_private_status_t status;
334
335     if (tag == NULL)
336         return NOTMUCH_STATUS_NULL_POINTER;
337
338     if (strlen (tag) > NOTMUCH_TAG_MAX)
339         return NOTMUCH_STATUS_TAG_TOO_LONG;
340
341     status = _notmuch_message_remove_term (message, "tag", tag);
342     if (status) {
343         fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
344                  status);
345         exit (1);
346     }
347
348     _notmuch_message_sync (message);
349
350     return NOTMUCH_STATUS_SUCCESS;
351 }
352
353 void
354 notmuch_message_destroy (notmuch_message_t *message)
355 {
356     talloc_free (message);
357 }
358
359 notmuch_bool_t
360 notmuch_tags_has_more (notmuch_tags_t *tags)
361 {
362     std::string s;
363
364     if (tags->iterator == tags->iterator_end)
365         return FALSE;
366
367     s = *tags->iterator;
368     if (s.size () && s[0] == 'L')
369         return TRUE;
370     else
371         return FALSE;
372 }
373
374 const char *
375 notmuch_tags_get (notmuch_tags_t *tags)
376 {
377     return talloc_strdup (tags, (*tags->iterator).c_str () + 1);
378 }
379
380 void
381 notmuch_tags_advance (notmuch_tags_t *tags)
382 {
383     tags->iterator++;
384 }
385
386 void
387 notmuch_tags_destroy (notmuch_tags_t *tags)
388 {
389     talloc_free (tags);
390 }
391
392 notmuch_bool_t
393 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
394 {
395     if (thread_ids->current == NULL || *thread_ids->current == '\0')
396         return FALSE;
397     else
398         return TRUE;
399 }
400
401 const char *
402 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
403 {
404     return thread_ids->current;
405 }
406
407 void
408 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
409 {
410     thread_ids->current = strsep (&thread_ids->next, ",");
411 }
412
413 void
414 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
415 {
416     talloc_free (thread_ids);
417 }