]> git.notmuchmail.org Git - notmuch/blob - message.cc
Add notmuch_message_get_filename
[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 const char *
156 notmuch_message_get_filename (notmuch_message_t *message)
157 {
158     std::string filename_str;
159
160     if (message->filename)
161         return message->filename;
162
163     filename_str = message->doc.get_data ();
164     message->filename = talloc_strdup (message, filename_str.c_str ());
165
166     return message->filename;
167 }
168
169 /* We end up having to call the destructors explicitly because we had
170  * to use "placement new" in order to initialize C++ objects within a
171  * block that we allocated with talloc. So C++ is making talloc
172  * slightly less simple to use, (we wouldn't need
173  * talloc_set_destructor at all otherwise).
174  */
175 static int
176 _notmuch_tags_destructor (notmuch_tags_t *tags)
177 {
178     tags->iterator.~TermIterator ();
179     tags->iterator_end.~TermIterator ();
180
181     return 0;
182 }
183
184 notmuch_tags_t *
185 notmuch_message_get_tags (notmuch_message_t *message)
186 {
187     notmuch_tags_t *tags;
188
189     tags = talloc (message, notmuch_tags_t);
190     if (unlikely (tags == NULL))
191         return NULL;
192
193     new (&tags->iterator) Xapian::TermIterator;
194     new (&tags->iterator_end) Xapian::TermIterator;
195
196     talloc_set_destructor (tags, _notmuch_tags_destructor);
197
198     tags->iterator = message->doc.termlist_begin ();
199     tags->iterator.skip_to (_find_prefix ("tag"));
200     tags->iterator_end = message->doc.termlist_end ();
201
202     return tags;
203 }
204
205 notmuch_thread_ids_t *
206 notmuch_message_get_thread_ids (notmuch_message_t *message)
207 {
208     notmuch_thread_ids_t *thread_ids;
209     std::string id_str;
210
211     thread_ids = talloc (message, notmuch_thread_ids_t);
212     if (unlikely (thread_ids == NULL))
213         return NULL;
214
215     id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
216     thread_ids->next = talloc_strdup (message, id_str.c_str ());
217
218     /* Initialize thread_ids->current and terminate first ID. */
219     notmuch_thread_ids_advance (thread_ids);
220
221     return thread_ids;
222 }
223
224 void
225 thread_id_generate (thread_id_t *thread_id)
226 {
227     static int seeded = 0;
228     FILE *dev_random;
229     uint32_t value;
230     char *s;
231     int i;
232
233     if (! seeded) {
234         dev_random = fopen ("/dev/random", "r");
235         if (dev_random == NULL) {
236             srand (time (NULL));
237         } else {
238             fread ((void *) &value, sizeof (value), 1, dev_random);
239             srand (value);
240             fclose (dev_random);
241         }
242         seeded = 1;
243     }
244
245     s = thread_id->str;
246     for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
247         value = rand ();
248         sprintf (s, "%08x", value);
249         s += 8;
250     }
251 }
252
253 /* Synchronize changes made to message->doc into the database. */
254 static void
255 _notmuch_message_sync (notmuch_message_t *message)
256 {
257     Xapian::WritableDatabase *db = message->notmuch->xapian_db;
258
259     db->replace_document (message->doc_id, message->doc);
260 }
261
262 /* Add a name:value term to 'message', (the actual term will be
263  * encoded by prefixing the value with a short prefix). See
264  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
265  * names to prefix values.
266  *
267  * This change will not be reflected in the database until the next
268  * call to _notmuch_message_set_sync. */
269 notmuch_private_status_t
270 _notmuch_message_add_term (notmuch_message_t *message,
271                            const char *prefix_name,
272                            const char *value)
273 {
274
275     char *term;
276
277     if (value == NULL)
278         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
279
280     term = talloc_asprintf (message, "%s%s",
281                             _find_prefix (prefix_name), value);
282
283     if (strlen (term) > NOTMUCH_TERM_MAX)
284         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
285
286     message->doc.add_term (term);
287
288     talloc_free (term);
289
290     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
291 }
292
293 /* Remove a name:value term from 'message', (the actual term will be
294  * encoded by prefixing the value with a short prefix). See
295  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
296  * names to prefix values.
297  *
298  * This change will not be reflected in the database until the next
299  * call to _notmuch_message_set_sync. */
300 notmuch_private_status_t
301 _notmuch_message_remove_term (notmuch_message_t *message,
302                               const char *prefix_name,
303                               const char *value)
304 {
305     char *term;
306
307     if (value == NULL)
308         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
309
310     term = talloc_asprintf (message, "%s%s",
311                             _find_prefix (prefix_name), value);
312
313     if (strlen (term) > NOTMUCH_TERM_MAX)
314         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
315
316     message->doc.remove_term (term);
317
318     talloc_free (term);
319
320     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
321 }
322
323 notmuch_status_t
324 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
325 {
326     notmuch_private_status_t status;
327
328     if (tag == NULL)
329         return NOTMUCH_STATUS_NULL_POINTER;
330
331     if (strlen (tag) > NOTMUCH_TAG_MAX)
332         return NOTMUCH_STATUS_TAG_TOO_LONG;
333
334     status = _notmuch_message_add_term (message, "tag", tag);
335     if (status) {
336         fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
337                  status);
338         exit (1);
339     }
340
341     _notmuch_message_sync (message);
342
343     return NOTMUCH_STATUS_SUCCESS;
344 }
345
346 notmuch_status_t
347 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
348 {
349     notmuch_private_status_t status;
350
351     if (tag == NULL)
352         return NOTMUCH_STATUS_NULL_POINTER;
353
354     if (strlen (tag) > NOTMUCH_TAG_MAX)
355         return NOTMUCH_STATUS_TAG_TOO_LONG;
356
357     status = _notmuch_message_remove_term (message, "tag", tag);
358     if (status) {
359         fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
360                  status);
361         exit (1);
362     }
363
364     _notmuch_message_sync (message);
365
366     return NOTMUCH_STATUS_SUCCESS;
367 }
368
369 void
370 notmuch_message_destroy (notmuch_message_t *message)
371 {
372     talloc_free (message);
373 }
374
375 notmuch_bool_t
376 notmuch_tags_has_more (notmuch_tags_t *tags)
377 {
378     std::string s;
379
380     if (tags->iterator == tags->iterator_end)
381         return FALSE;
382
383     s = *tags->iterator;
384     if (s.size () && s[0] == 'L')
385         return TRUE;
386     else
387         return FALSE;
388 }
389
390 const char *
391 notmuch_tags_get (notmuch_tags_t *tags)
392 {
393     return talloc_strdup (tags, (*tags->iterator).c_str () + 1);
394 }
395
396 void
397 notmuch_tags_advance (notmuch_tags_t *tags)
398 {
399     tags->iterator++;
400 }
401
402 void
403 notmuch_tags_destroy (notmuch_tags_t *tags)
404 {
405     talloc_free (tags);
406 }
407
408 notmuch_bool_t
409 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
410 {
411     if (thread_ids->current == NULL || *thread_ids->current == '\0')
412         return FALSE;
413     else
414         return TRUE;
415 }
416
417 const char *
418 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
419 {
420     return thread_ids->current;
421 }
422
423 void
424 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
425 {
426     thread_ids->current = strsep (&thread_ids->next, ",");
427 }
428
429 void
430 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
431 {
432     talloc_free (thread_ids);
433 }