]> git.notmuchmail.org Git - notmuch/blob - lib/notmuch.h
lib: Add support for nested atomic sections.
[notmuch] / lib / notmuch.h
1 /* notmuch - Not much of an email library, (just index and search)
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 #ifndef NOTMUCH_H
22 #define NOTMUCH_H
23
24 #ifdef  __cplusplus
25 # define NOTMUCH_BEGIN_DECLS  extern "C" {
26 # define NOTMUCH_END_DECLS    }
27 #else
28 # define NOTMUCH_BEGIN_DECLS
29 # define NOTMUCH_END_DECLS
30 #endif
31
32 NOTMUCH_BEGIN_DECLS
33
34 #include <time.h>
35
36 #ifndef FALSE
37 #define FALSE 0
38 #endif
39
40 #ifndef TRUE
41 #define TRUE 1
42 #endif
43
44 typedef int notmuch_bool_t;
45
46 /* Status codes used for the return values of most functions.
47  *
48  * A zero value (NOTMUCH_STATUS_SUCCESS) indicates that the function
49  * completed without error. Any other value indicates an error as
50  * follows:
51  *
52  * NOTMUCH_STATUS_SUCCESS: No error occurred.
53  *
54  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory
55  *
56  * XXX: We don't really want to expose this lame XAPIAN_EXCEPTION
57  * value. Instead we should map to things like DATABASE_LOCKED or
58  * whatever.
59  *
60  * NOTMUCH_STATUS_READ_ONLY_DATABASE: An attempt was made to write to
61  *      a database opened in read-only mode.
62  *
63  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
64  *
65  * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to read or
66  *      write to a file (this could be file not found, permission
67  *      denied, etc.)
68  *
69  * NOTMUCH_STATUS_FILE_NOT_EMAIL: A file was presented that doesn't
70  *      appear to be an email message.
71  *
72  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: A file contains a message ID
73  *      that is identical to a message already in the database.
74  *
75  * NOTMUCH_STATUS_NULL_POINTER: The user erroneously passed a NULL
76  *      pointer to a notmuch function.
77  *
78  * NOTMUCH_STATUS_TAG_TOO_LONG: A tag value is too long (exceeds
79  *      NOTMUCH_TAG_MAX)
80  *
81  * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: The notmuch_message_thaw
82  *      function has been called more times than notmuch_message_freeze.
83  *
84  * NOTMUCH_STATUS_UNBALANCED_ATOMIC: notmuch_database_end_atomic has
85  *      been called more times than notmuch_database_begin_atomic.
86  *
87  * And finally:
88  *
89  * NOTMUCH_STATUS_LAST_STATUS: Not an actual status value. Just a way
90  *      to find out how many valid status values there are.
91  */
92 typedef enum _notmuch_status {
93     NOTMUCH_STATUS_SUCCESS = 0,
94     NOTMUCH_STATUS_OUT_OF_MEMORY,
95     NOTMUCH_STATUS_READ_ONLY_DATABASE,
96     NOTMUCH_STATUS_XAPIAN_EXCEPTION,
97     NOTMUCH_STATUS_FILE_ERROR,
98     NOTMUCH_STATUS_FILE_NOT_EMAIL,
99     NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID,
100     NOTMUCH_STATUS_NULL_POINTER,
101     NOTMUCH_STATUS_TAG_TOO_LONG,
102     NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
103     NOTMUCH_STATUS_UNBALANCED_ATOMIC,
104
105     NOTMUCH_STATUS_LAST_STATUS
106 } notmuch_status_t;
107
108 /* Get a string representation of a notmuch_status_t value.
109  *
110  * The result is read-only.
111  */
112 const char *
113 notmuch_status_to_string (notmuch_status_t status);
114
115 /* Various opaque data types. For each notmuch_<foo>_t see the various
116  * notmuch_<foo> functions below. */
117 typedef struct _notmuch_database notmuch_database_t;
118 typedef struct _notmuch_query notmuch_query_t;
119 typedef struct _notmuch_threads notmuch_threads_t;
120 typedef struct _notmuch_thread notmuch_thread_t;
121 typedef struct _notmuch_messages notmuch_messages_t;
122 typedef struct _notmuch_message notmuch_message_t;
123 typedef struct _notmuch_tags notmuch_tags_t;
124 typedef struct _notmuch_directory notmuch_directory_t;
125 typedef struct _notmuch_filenames notmuch_filenames_t;
126
127 /* Create a new, empty notmuch database located at 'path'.
128  *
129  * The path should be a top-level directory to a collection of
130  * plain-text email messages (one message per file). This call will
131  * create a new ".notmuch" directory within 'path' where notmuch will
132  * store its data.
133  *
134  * After a successful call to notmuch_database_create, the returned
135  * database will be open so the caller should call
136  * notmuch_database_close when finished with it.
137  *
138  * The database will not yet have any data in it
139  * (notmuch_database_create itself is a very cheap function). Messages
140  * contained within 'path' can be added to the database by calling
141  * notmuch_database_add_message.
142  *
143  * In case of any failure, this function returns NULL, (after printing
144  * an error message on stderr).
145  */
146 notmuch_database_t *
147 notmuch_database_create (const char *path);
148
149 typedef enum {
150     NOTMUCH_DATABASE_MODE_READ_ONLY = 0,
151     NOTMUCH_DATABASE_MODE_READ_WRITE
152 } notmuch_database_mode_t;
153
154 /* XXX: I think I'd like this to take an extra argument of
155  * notmuch_status_t* for returning a status value on failure. */
156
157 /* Open an existing notmuch database located at 'path'.
158  *
159  * The database should have been created at some time in the past,
160  * (not necessarily by this process), by calling
161  * notmuch_database_create with 'path'. By default the database should be
162  * opened for reading only. In order to write to the database you need to
163  * pass the NOTMUCH_DATABASE_MODE_READ_WRITE mode.
164  *
165  * An existing notmuch database can be identified by the presence of a
166  * directory named ".notmuch" below 'path'.
167  *
168  * The caller should call notmuch_database_close when finished with
169  * this database.
170  *
171  * In case of any failure, this function returns NULL, (after printing
172  * an error message on stderr).
173  */
174 notmuch_database_t *
175 notmuch_database_open (const char *path,
176                        notmuch_database_mode_t mode);
177
178 /* Close the given notmuch database, freeing all associated
179  * resources. See notmuch_database_open. */
180 void
181 notmuch_database_close (notmuch_database_t *database);
182
183 /* Return the database path of the given database.
184  *
185  * The return value is a string owned by notmuch so should not be
186  * modified nor freed by the caller. */
187 const char *
188 notmuch_database_get_path (notmuch_database_t *database);
189
190 /* Return the database format version of the given database. */
191 unsigned int
192 notmuch_database_get_version (notmuch_database_t *database);
193
194 /* Does this database need to be upgraded before writing to it?
195  *
196  * If this function returns TRUE then no functions that modify the
197  * database (notmuch_database_add_message, notmuch_message_add_tag,
198  * notmuch_directory_set_mtime, etc.) will work unless the function
199  * notmuch_database_upgrade is called successfully first. */
200 notmuch_bool_t
201 notmuch_database_needs_upgrade (notmuch_database_t *database);
202
203 /* Upgrade the current database.
204  *
205  * After opening a database in read-write mode, the client should
206  * check if an upgrade is needed (notmuch_database_needs_upgrade) and
207  * if so, upgrade with this function before making any modifications.
208  *
209  * The optional progress_notify callback can be used by the caller to
210  * provide progress indication to the user. If non-NULL it will be
211  * called periodically with 'progress' as a floating-point value in
212  * the range of [0.0 .. 1.0] indicating the progress made so far in
213  * the upgrade process.
214  */
215 notmuch_status_t
216 notmuch_database_upgrade (notmuch_database_t *database,
217                           void (*progress_notify) (void *closure,
218                                                    double progress),
219                           void *closure);
220
221 /* Begin an atomic database operation.
222  *
223  * Any modifications performed between a successful begin and a
224  * notmuch_database_end_atomic will be applied to the database
225  * atomically.  Note that, unlike a typical database transaction, this
226  * only ensures atomicity, not durability; neither begin nor end
227  * necessarily flush modifications to disk.
228  *
229  * Atomic sections may be nested.  begin_atomic and end_atomic must
230  * always be called in pairs.
231  *
232  * Return value:
233  *
234  * NOTMUCH_STATUS_SUCCESS: Successfully entered atomic section.
235  *
236  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
237  *      atomic section not entered.
238  */
239 notmuch_status_t
240 notmuch_database_begin_atomic (notmuch_database_t *notmuch);
241
242 /* Indicate the end of an atomic database operation.
243  *
244  * Return value:
245  *
246  * NOTMUCH_STATUS_SUCCESS: Successfully completed atomic section.
247  *
248  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
249  *      atomic section not ended.
250  *
251  * NOTMUCH_STATUS_UNBALANCED_ATOMIC: The database is not currently in
252  *      an atomic section.
253  */
254 notmuch_status_t
255 notmuch_database_end_atomic (notmuch_database_t *notmuch);
256
257 /* Retrieve a directory object from the database for 'path'.
258  *
259  * Here, 'path' should be a path relative to the path of 'database'
260  * (see notmuch_database_get_path), or else should be an absolute path
261  * with initial components that match the path of 'database'.
262  *
263  * Can return NULL if a Xapian exception occurs.
264  */
265 notmuch_directory_t *
266 notmuch_database_get_directory (notmuch_database_t *database,
267                                 const char *path);
268
269 /* Add a new message to the given notmuch database.
270  *
271  * Here,'filename' should be a path relative to the path of
272  * 'database' (see notmuch_database_get_path), or else should be an
273  * absolute filename with initial components that match the path of
274  * 'database'.
275  *
276  * The file should be a single mail message (not a multi-message mbox)
277  * that is expected to remain at its current location, (since the
278  * notmuch database will reference the filename, and will not copy the
279  * entire contents of the file.
280  *
281  * If 'message' is not NULL, then, on successful return
282  * (NOTMUCH_STATUS_SUCCESS or NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) '*message'
283  * will be initialized to a message object that can be used for things
284  * such as adding tags to the just-added message. The user should call
285  * notmuch_message_destroy when done with the message. On any failure
286  * '*message' will be set to NULL.
287  *
288  * Return value:
289  *
290  * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
291  *
292  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
293  *      message not added.
294  *
295  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message
296  *      ID as another message already in the database. The new
297  *      filename was successfully added to the message in the database
298  *      (if not already present).
299  *
300  * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
301  *      file, (such as permission denied, or file not found,
302  *      etc.). Nothing added to the database.
303  *
304  * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
305  *      like an email message. Nothing added to the database.
306  *
307  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
308  *      mode so no message can be added.
309  */
310 notmuch_status_t
311 notmuch_database_add_message (notmuch_database_t *database,
312                               const char *filename,
313                               notmuch_message_t **message);
314
315 /* Remove a message from the given notmuch database.
316  *
317  * Note that only this particular filename association is removed from
318  * the database. If the same message (as determined by the message ID)
319  * is still available via other filenames, then the message will
320  * persist in the database for those filenames. When the last filename
321  * is removed for a particular message, the database content for that
322  * message will be entirely removed.
323  *
324  * Return value:
325  *
326  * NOTMUCH_STATUS_SUCCESS: The last filename was removed and the
327  *      message was removed from the database.
328  *
329  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
330  *      message not removed.
331  *
332  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: This filename was removed but
333  *      the message persists in the database with at least one other
334  *      filename.
335  *
336  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
337  *      mode so no message can be removed.
338  */
339 notmuch_status_t
340 notmuch_database_remove_message (notmuch_database_t *database,
341                                  const char *filename);
342
343 /* Find a message with the given message_id.
344  *
345  * If the database contains a message with the given message_id, then
346  * a new notmuch_message_t object is returned. The caller should call
347  * notmuch_message_destroy when done with the message.
348  *
349  * This function returns NULL in the following situations:
350  *
351  *      * No message is found with the given message_id
352  *      * An out-of-memory situation occurs
353  *      * A Xapian exception occurs
354  */
355 notmuch_message_t *
356 notmuch_database_find_message (notmuch_database_t *database,
357                                const char *message_id);
358
359 /* Return a list of all tags found in the database.
360  *
361  * This function creates a list of all tags found in the database. The
362  * resulting list contains all tags from all messages found in the database.
363  *
364  * On error this function returns NULL.
365  */
366 notmuch_tags_t *
367 notmuch_database_get_all_tags (notmuch_database_t *db);
368
369 /* Create a new query for 'database'.
370  *
371  * Here, 'database' should be an open database, (see
372  * notmuch_database_open and notmuch_database_create).
373  *
374  * For the query string, we'll document the syntax here more
375  * completely in the future, but it's likely to be a specialized
376  * version of the general Xapian query syntax:
377  *
378  * http://xapian.org/docs/queryparser.html
379  *
380  * As a special case, passing either a length-zero string, (that is ""),
381  * or a string consisting of a single asterisk (that is "*"), will
382  * result in a query that returns all messages in the database.
383  *
384  * See notmuch_query_set_sort for controlling the order of results.
385  * See notmuch_query_search_messages and notmuch_query_search_threads
386  * to actually execute the query.
387  *
388  * User should call notmuch_query_destroy when finished with this
389  * query.
390  *
391  * Will return NULL if insufficient memory is available.
392  */
393 notmuch_query_t *
394 notmuch_query_create (notmuch_database_t *database,
395                       const char *query_string);
396
397 /* Sort values for notmuch_query_set_sort */
398 typedef enum {
399     NOTMUCH_SORT_OLDEST_FIRST,
400     NOTMUCH_SORT_NEWEST_FIRST,
401     NOTMUCH_SORT_MESSAGE_ID,
402     NOTMUCH_SORT_UNSORTED
403 } notmuch_sort_t;
404
405 /* Return the query_string of this query. See notmuch_query_create. */
406 const char *
407 notmuch_query_get_query_string (notmuch_query_t *query);
408
409 /* Specify the sorting desired for this query. */
410 void
411 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
412
413 /* Return the sort specified for this query. See notmuch_query_set_sort. */
414 notmuch_sort_t
415 notmuch_query_get_sort (notmuch_query_t *query);
416
417 /* Execute a query for threads, returning a notmuch_threads_t object
418  * which can be used to iterate over the results. The returned threads
419  * object is owned by the query and as such, will only be valid until
420  * notmuch_query_destroy.
421  *
422  * Typical usage might be:
423  *
424  *     notmuch_query_t *query;
425  *     notmuch_threads_t *threads;
426  *     notmuch_thread_t *thread;
427  *
428  *     query = notmuch_query_create (database, query_string);
429  *
430  *     for (threads = notmuch_query_search_threads (query);
431  *          notmuch_threads_valid (threads);
432  *          notmuch_threads_move_to_next (threads))
433  *     {
434  *         thread = notmuch_threads_get (threads);
435  *         ....
436  *         notmuch_thread_destroy (thread);
437  *     }
438  *
439  *     notmuch_query_destroy (query);
440  *
441  * Note: If you are finished with a thread before its containing
442  * query, you can call notmuch_thread_destroy to clean up some memory
443  * sooner (as in the above example). Otherwise, if your thread objects
444  * are long-lived, then you don't need to call notmuch_thread_destroy
445  * and all the memory will still be reclaimed when the query is
446  * destroyed.
447  *
448  * Note that there's no explicit destructor needed for the
449  * notmuch_threads_t object. (For consistency, we do provide a
450  * notmuch_threads_destroy function, but there's no good reason
451  * to call it if the query is about to be destroyed).
452  *
453  * If a Xapian exception occurs this function will return NULL.
454  */
455 notmuch_threads_t *
456 notmuch_query_search_threads (notmuch_query_t *query);
457
458 /* Execute a query for messages, returning a notmuch_messages_t object
459  * which can be used to iterate over the results. The returned
460  * messages object is owned by the query and as such, will only be
461  * valid until notmuch_query_destroy.
462  *
463  * Typical usage might be:
464  *
465  *     notmuch_query_t *query;
466  *     notmuch_messages_t *messages;
467  *     notmuch_message_t *message;
468  *
469  *     query = notmuch_query_create (database, query_string);
470  *
471  *     for (messages = notmuch_query_search_messages (query);
472  *          notmuch_messages_valid (messages);
473  *          notmuch_messages_move_to_next (messages))
474  *     {
475  *         message = notmuch_messages_get (messages);
476  *         ....
477  *         notmuch_message_destroy (message);
478  *     }
479  *
480  *     notmuch_query_destroy (query);
481  *
482  * Note: If you are finished with a message before its containing
483  * query, you can call notmuch_message_destroy to clean up some memory
484  * sooner (as in the above example). Otherwise, if your message
485  * objects are long-lived, then you don't need to call
486  * notmuch_message_destroy and all the memory will still be reclaimed
487  * when the query is destroyed.
488  *
489  * Note that there's no explicit destructor needed for the
490  * notmuch_messages_t object. (For consistency, we do provide a
491  * notmuch_messages_destroy function, but there's no good
492  * reason to call it if the query is about to be destroyed).
493  *
494  * If a Xapian exception occurs this function will return NULL.
495  */
496 notmuch_messages_t *
497 notmuch_query_search_messages (notmuch_query_t *query);
498
499 /* Destroy a notmuch_query_t along with any associated resources.
500  *
501  * This will in turn destroy any notmuch_threads_t and
502  * notmuch_messages_t objects generated by this query, (and in
503  * turn any notmuch_thread_t and notmuch_message_t objects generated
504  * from those results, etc.), if such objects haven't already been
505  * destroyed.
506  */
507 void
508 notmuch_query_destroy (notmuch_query_t *query);
509
510 /* Is the given 'threads' iterator pointing at a valid thread.
511  *
512  * When this function returns TRUE, notmuch_threads_get will return a
513  * valid object. Whereas when this function returns FALSE,
514  * notmuch_threads_get will return NULL.
515  *
516  * See the documentation of notmuch_query_search_threads for example
517  * code showing how to iterate over a notmuch_threads_t object.
518  */
519 notmuch_bool_t
520 notmuch_threads_valid (notmuch_threads_t *threads);
521
522 /* Get the current thread from 'threads' as a notmuch_thread_t.
523  *
524  * Note: The returned thread belongs to 'threads' and has a lifetime
525  * identical to it (and the query to which it belongs).
526  *
527  * See the documentation of notmuch_query_search_threads for example
528  * code showing how to iterate over a notmuch_threads_t object.
529  *
530  * If an out-of-memory situation occurs, this function will return
531  * NULL.
532  */
533 notmuch_thread_t *
534 notmuch_threads_get (notmuch_threads_t *threads);
535
536 /* Move the 'threads' iterator to the next thread.
537  *
538  * If 'threads' is already pointing at the last thread then the
539  * iterator will be moved to a point just beyond that last thread,
540  * (where notmuch_threads_valid will return FALSE and
541  * notmuch_threads_get will return NULL).
542  *
543  * See the documentation of notmuch_query_search_threads for example
544  * code showing how to iterate over a notmuch_threads_t object.
545  */
546 void
547 notmuch_threads_move_to_next (notmuch_threads_t *threads);
548
549 /* Destroy a notmuch_threads_t object.
550  *
551  * It's not strictly necessary to call this function. All memory from
552  * the notmuch_threads_t object will be reclaimed when the
553  * containing query object is destroyed.
554  */
555 void
556 notmuch_threads_destroy (notmuch_threads_t *threads);
557
558 /* Return an estimate of the number of messages matching a search
559  *
560  * This function performs a search and returns Xapian's best
561  * guess as to number of matching messages.
562  *
563  * If a Xapian exception occurs, this function may return 0 (after
564  * printing a message).
565  */
566 unsigned
567 notmuch_query_count_messages (notmuch_query_t *query);
568  
569 /* Get the thread ID of 'thread'.
570  *
571  * The returned string belongs to 'thread' and as such, should not be
572  * modified by the caller and will only be valid for as long as the
573  * thread is valid, (which is until notmuch_thread_destroy or until
574  * the query from which it derived is destroyed).
575  */
576 const char *
577 notmuch_thread_get_thread_id (notmuch_thread_t *thread);
578
579 /* Get the total number of messages in 'thread'.
580  *
581  * This count consists of all messages in the database belonging to
582  * this thread. Contrast with notmuch_thread_get_matched_messages() .
583  */
584 int
585 notmuch_thread_get_total_messages (notmuch_thread_t *thread);
586
587 /* Get a notmuch_messages_t iterator for the top-level messages in
588  * 'thread'.
589  *
590  * This iterator will not necessarily iterate over all of the messages
591  * in the thread. It will only iterate over the messages in the thread
592  * which are not replies to other messages in the thread.
593  *
594  * To iterate over all messages in the thread, the caller will need to
595  * iterate over the result of notmuch_message_get_replies for each
596  * top-level message (and do that recursively for the resulting
597  * messages, etc.).
598  */
599 notmuch_messages_t *
600 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread);
601
602 /* Get the number of messages in 'thread' that matched the search.
603  *
604  * This count includes only the messages in this thread that were
605  * matched by the search from which the thread was created. Contrast
606  * with notmuch_thread_get_total_messages() .
607  */
608 int
609 notmuch_thread_get_matched_messages (notmuch_thread_t *thread);
610
611 /* Get the authors of 'thread'
612  *
613  * The returned string is a comma-separated list of the names of the
614  * authors of mail messages in the query results that belong to this
615  * thread.
616  *
617  * The returned string belongs to 'thread' and as such, should not be
618  * modified by the caller and will only be valid for as long as the
619  * thread is valid, (which is until notmuch_thread_destroy or until
620  * the query from which it derived is destroyed).
621  */
622 const char *
623 notmuch_thread_get_authors (notmuch_thread_t *thread);
624
625 /* Get the subject of 'thread'
626  *
627  * The subject is taken from the first message (according to the query
628  * order---see notmuch_query_set_sort) in the query results that
629  * belongs to this thread.
630  *
631  * The returned string belongs to 'thread' and as such, should not be
632  * modified by the caller and will only be valid for as long as the
633  * thread is valid, (which is until notmuch_thread_destroy or until
634  * the query from which it derived is destroyed).
635  */
636 const char *
637 notmuch_thread_get_subject (notmuch_thread_t *thread);
638
639 /* Get the date of the oldest message in 'thread' as a time_t value.
640  */
641 time_t
642 notmuch_thread_get_oldest_date (notmuch_thread_t *thread);
643
644 /* Get the date of the newest message in 'thread' as a time_t value.
645  */
646 time_t
647 notmuch_thread_get_newest_date (notmuch_thread_t *thread);
648
649 /* Get the tags for 'thread', returning a notmuch_tags_t object which
650  * can be used to iterate over all tags.
651  *
652  * Note: In the Notmuch database, tags are stored on individual
653  * messages, not on threads. So the tags returned here will be all
654  * tags of the messages which matched the search and which belong to
655  * this thread.
656  *
657  * The tags object is owned by the thread and as such, will only be
658  * valid for as long as the thread is valid, (for example, until
659  * notmuch_thread_destroy or until the query from which it derived is
660  * destroyed).
661  *
662  * Typical usage might be:
663  *
664  *     notmuch_thread_t *thread;
665  *     notmuch_tags_t *tags;
666  *     const char *tag;
667  *
668  *     thread = notmuch_threads_get (threads);
669  *
670  *     for (tags = notmuch_thread_get_tags (thread);
671  *          notmuch_tags_valid (tags);
672  *          notmuch_result_move_to_next (tags))
673  *     {
674  *         tag = notmuch_tags_get (tags);
675  *         ....
676  *     }
677  *
678  *     notmuch_thread_destroy (thread);
679  *
680  * Note that there's no explicit destructor needed for the
681  * notmuch_tags_t object. (For consistency, we do provide a
682  * notmuch_tags_destroy function, but there's no good reason to call
683  * it if the message is about to be destroyed).
684  */
685 notmuch_tags_t *
686 notmuch_thread_get_tags (notmuch_thread_t *thread);
687
688 /* Destroy a notmuch_thread_t object. */
689 void
690 notmuch_thread_destroy (notmuch_thread_t *thread);
691
692 /* Is the given 'messages' iterator pointing at a valid message.
693  *
694  * When this function returns TRUE, notmuch_messages_get will return a
695  * valid object. Whereas when this function returns FALSE,
696  * notmuch_messages_get will return NULL.
697  *
698  * See the documentation of notmuch_query_search_messages for example
699  * code showing how to iterate over a notmuch_messages_t object.
700  */
701 notmuch_bool_t
702 notmuch_messages_valid (notmuch_messages_t *messages);
703
704 /* Get the current message from 'messages' as a notmuch_message_t.
705  *
706  * Note: The returned message belongs to 'messages' and has a lifetime
707  * identical to it (and the query to which it belongs).
708  *
709  * See the documentation of notmuch_query_search_messages for example
710  * code showing how to iterate over a notmuch_messages_t object.
711  *
712  * If an out-of-memory situation occurs, this function will return
713  * NULL.
714  */
715 notmuch_message_t *
716 notmuch_messages_get (notmuch_messages_t *messages);
717
718 /* Move the 'messages' iterator to the next message.
719  *
720  * If 'messages' is already pointing at the last message then the
721  * iterator will be moved to a point just beyond that last message,
722  * (where notmuch_messages_valid will return FALSE and
723  * notmuch_messages_get will return NULL).
724  *
725  * See the documentation of notmuch_query_search_messages for example
726  * code showing how to iterate over a notmuch_messages_t object.
727  */
728 void
729 notmuch_messages_move_to_next (notmuch_messages_t *messages);
730
731 /* Destroy a notmuch_messages_t object.
732  *
733  * It's not strictly necessary to call this function. All memory from
734  * the notmuch_messages_t object will be reclaimed when the containing
735  * query object is destroyed.
736  */
737 void
738 notmuch_messages_destroy (notmuch_messages_t *messages);
739
740 /* Return a list of tags from all messages.
741  *
742  * The resulting list is guaranteed not to contain duplicated tags.
743  *
744  * WARNING: You can no longer iterate over messages after calling this
745  * function, because the iterator will point at the end of the list.
746  * We do not have a function to reset the iterator yet and the only
747  * way how you can iterate over the list again is to recreate the
748  * message list.
749  *
750  * The function returns NULL on error.
751  */
752 notmuch_tags_t *
753 notmuch_messages_collect_tags (notmuch_messages_t *messages);
754
755 /* Get the message ID of 'message'.
756  *
757  * The returned string belongs to 'message' and as such, should not be
758  * modified by the caller and will only be valid for as long as the
759  * message is valid, (which is until the query from which it derived
760  * is destroyed).
761  *
762  * This function will not return NULL since Notmuch ensures that every
763  * message has a unique message ID, (Notmuch will generate an ID for a
764  * message if the original file does not contain one).
765  */
766 const char *
767 notmuch_message_get_message_id (notmuch_message_t *message);
768
769 /* Get the thread ID of 'message'.
770  *
771  * The returned string belongs to 'message' and as such, should not be
772  * modified by the caller and will only be valid for as long as the
773  * message is valid, (for example, until the user calls
774  * notmuch_message_destroy on 'message' or until a query from which it
775  * derived is destroyed).
776  *
777  * This function will not return NULL since Notmuch ensures that every
778  * message belongs to a single thread.
779  */
780 const char *
781 notmuch_message_get_thread_id (notmuch_message_t *message);
782
783 /* Get a notmuch_messages_t iterator for all of the replies to
784  * 'message'.
785  *
786  * Note: This call only makes sense if 'message' was ultimately
787  * obtained from a notmuch_thread_t object, (such as by coming
788  * directly from the result of calling notmuch_thread_get_
789  * toplevel_messages or by any number of subsequent
790  * calls to notmuch_message_get_replies).
791  *
792  * If 'message' was obtained through some non-thread means, (such as
793  * by a call to notmuch_query_search_messages), then this function
794  * will return NULL.
795  *
796  * If there are no replies to 'message', this function will return
797  * NULL. (Note that notmuch_messages_valid will accept that NULL
798  * value as legitimate, and simply return FALSE for it.)
799  */
800 notmuch_messages_t *
801 notmuch_message_get_replies (notmuch_message_t *message);
802
803 /* Get a filename for the email corresponding to 'message'.
804  *
805  * The returned filename is an absolute filename, (the initial
806  * component will match notmuch_database_get_path() ).
807  *
808  * The returned string belongs to the message so should not be
809  * modified or freed by the caller (nor should it be referenced after
810  * the message is destroyed).
811  *
812  * Note: If this message corresponds to multiple files in the mail
813  * store, (that is, multiple files contain identical message IDs),
814  * this function will arbitrarily return a single one of those
815  * filenames. See notmuch_message_get_filenames for returning the
816  * complete list of filenames.
817  */
818 const char *
819 notmuch_message_get_filename (notmuch_message_t *message);
820
821 /* Get all filenames for the email corresponding to 'message'.
822  *
823  * Returns a notmuch_filenames_t iterator listing all the filenames
824  * associated with 'message'. These files may not have identical
825  * content, but each will have the identical Message-ID.
826  *
827  * Each filename in the iterator is an absolute filename, (the initial
828  * component will match notmuch_database_get_path() ).
829  */
830 notmuch_filenames_t *
831 notmuch_message_get_filenames (notmuch_message_t *message);
832
833 /* Message flags */
834 typedef enum _notmuch_message_flag {
835     NOTMUCH_MESSAGE_FLAG_MATCH
836 } notmuch_message_flag_t;
837
838 /* Get a value of a flag for the email corresponding to 'message'. */
839 notmuch_bool_t
840 notmuch_message_get_flag (notmuch_message_t *message,
841                           notmuch_message_flag_t flag);
842
843 /* Set a value of a flag for the email corresponding to 'message'. */
844 void
845 notmuch_message_set_flag (notmuch_message_t *message,
846                           notmuch_message_flag_t flag, notmuch_bool_t value);
847
848 /* Get the date of 'message' as a time_t value.
849  *
850  * For the original textual representation of the Date header from the
851  * message call notmuch_message_get_header() with a header value of
852  * "date". */
853 time_t
854 notmuch_message_get_date  (notmuch_message_t *message);
855
856 /* Get the value of the specified header from 'message'.
857  *
858  * The value will be read from the actual message file, not from the
859  * notmuch database. The header name is case insensitive.
860  *
861  * The returned string belongs to the message so should not be
862  * modified or freed by the caller (nor should it be referenced after
863  * the message is destroyed).
864  *
865  * Returns an empty string ("") if the message does not contain a
866  * header line matching 'header'. Returns NULL if any error occurs.
867  */
868 const char *
869 notmuch_message_get_header (notmuch_message_t *message, const char *header);
870
871 /* Get the tags for 'message', returning a notmuch_tags_t object which
872  * can be used to iterate over all tags.
873  *
874  * The tags object is owned by the message and as such, will only be
875  * valid for as long as the message is valid, (which is until the
876  * query from which it derived is destroyed).
877  *
878  * Typical usage might be:
879  *
880  *     notmuch_message_t *message;
881  *     notmuch_tags_t *tags;
882  *     const char *tag;
883  *
884  *     message = notmuch_database_find_message (database, message_id);
885  *
886  *     for (tags = notmuch_message_get_tags (message);
887  *          notmuch_tags_valid (tags);
888  *          notmuch_result_move_to_next (tags))
889  *     {
890  *         tag = notmuch_tags_get (tags);
891  *         ....
892  *     }
893  *
894  *     notmuch_message_destroy (message);
895  *
896  * Note that there's no explicit destructor needed for the
897  * notmuch_tags_t object. (For consistency, we do provide a
898  * notmuch_tags_destroy function, but there's no good reason to call
899  * it if the message is about to be destroyed).
900  */
901 notmuch_tags_t *
902 notmuch_message_get_tags (notmuch_message_t *message);
903
904 /* The longest possible tag value. */
905 #define NOTMUCH_TAG_MAX 200
906
907 /* Add a tag to the given message.
908  *
909  * Return value:
910  *
911  * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
912  *
913  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
914  *
915  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
916  *      (exceeds NOTMUCH_TAG_MAX)
917  *
918  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
919  *      mode so message cannot be modified.
920  */
921 notmuch_status_t
922 notmuch_message_add_tag (notmuch_message_t *message, const char *tag);
923
924 /* Remove a tag from the given message.
925  *
926  * Return value:
927  *
928  * NOTMUCH_STATUS_SUCCESS: Tag successfully removed from message
929  *
930  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
931  *
932  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
933  *      (exceeds NOTMUCH_TAG_MAX)
934  *
935  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
936  *      mode so message cannot be modified.
937  */
938 notmuch_status_t
939 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag);
940
941 /* Remove all tags from the given message.
942  *
943  * See notmuch_message_freeze for an example showing how to safely
944  * replace tag values.
945  *
946  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
947  *      mode so message cannot be modified.
948  */
949 notmuch_status_t
950 notmuch_message_remove_all_tags (notmuch_message_t *message);
951
952 /* Add/remove tags according to maildir flags in the message filename(s)
953  *
954  * This function examines the filenames of 'message' for maildir
955  * flags, and adds or removes tags on 'message' as follows when these
956  * flags are present:
957  *
958  *      Flag    Action if present
959  *      ----    -----------------
960  *      'D'     Adds the "draft" tag to the message
961  *      'F'     Adds the "flagged" tag to the message
962  *      'P'     Adds the "passed" tag to the message
963  *      'R'     Adds the "replied" tag to the message
964  *      'S'     Removes the "unread" tag from the message
965  *
966  * For each flag that is not present, the opposite action (add/remove)
967  * is performed for the corresponding tags.
968  *
969  * Flags are identified as trailing components of the filename after a
970  * sequence of ":2,".
971  *
972  * If there are multiple filenames associated with this message, the
973  * flag is considered present if it appears in one or more
974  * filenames. (That is, the flags from the multiple filenames are
975  * combined with the logical OR operator.)
976  *
977  * A client can ensure that notmuch database tags remain synchronized
978  * with maildir flags by calling this function after each call to
979  * notmuch_database_add_message. See also
980  * notmuch_message_tags_to_maildir_flags for synchronizing tag changes
981  * back to maildir flags.
982  */
983 notmuch_status_t
984 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message);
985
986 /* Rename message filename(s) to encode tags as maildir flags
987  *
988  * Specifically, for each filename corresponding to this message:
989  *
990  * If the filename is not in a maildir directory, do nothing.  (A
991  * maildir directory is determined as a directory named "new" or
992  * "cur".) Similarly, if the filename has invalid maildir info,
993  * (repeated or outof-ASCII-order flag characters after ":2,"), then
994  * do nothing.
995  *
996  * If the filename is in a maildir directory, rename the file so that
997  * its filename ends with the sequence ":2," followed by zero or more
998  * of the following single-character flags (in ASCII order):
999  *
1000  *   'D' iff the message has the "draft" tag
1001  *   'F' iff the message has the "flagged" tag
1002  *   'P' iff the message has the "passed" tag
1003  *   'R' iff the message has the "replied" tag
1004  *   'S' iff the message does not have the "unread" tag
1005  *
1006  * Any existing flags unmentioned in the list above will be preserved
1007  * in the renaming.
1008  *
1009  * Also, if this filename is in a directory named "new", rename it to
1010  * be within the neighboring directory named "cur".
1011  *
1012  * A client can ensure that maildir filename flags remain synchronized
1013  * with notmuch database tags by calling this function after changing
1014  * tags, (after calls to notmuch_message_add_tag,
1015  * notmuch_message_remove_tag, or notmuch_message_freeze/
1016  * notmuch_message_thaw). See also notmuch_message_maildir_flags_to_tags
1017  * for synchronizing maildir flag changes back to tags.
1018  */
1019 notmuch_status_t
1020 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message);
1021
1022 /* Freeze the current state of 'message' within the database.
1023  *
1024  * This means that changes to the message state, (via
1025  * notmuch_message_add_tag, notmuch_message_remove_tag, and
1026  * notmuch_message_remove_all_tags), will not be committed to the
1027  * database until the message is thawed with notmuch_message_thaw.
1028  *
1029  * Multiple calls to freeze/thaw are valid and these calls will
1030  * "stack". That is there must be as many calls to thaw as to freeze
1031  * before a message is actually thawed.
1032  *
1033  * The ability to do freeze/thaw allows for safe transactions to
1034  * change tag values. For example, explicitly setting a message to
1035  * have a given set of tags might look like this:
1036  *
1037  *    notmuch_message_freeze (message);
1038  *
1039  *    notmuch_message_remove_all_tags (message);
1040  *
1041  *    for (i = 0; i < NUM_TAGS; i++)
1042  *        notmuch_message_add_tag (message, tags[i]);
1043  *
1044  *    notmuch_message_thaw (message);
1045  *
1046  * With freeze/thaw used like this, the message in the database is
1047  * guaranteed to have either the full set of original tag values, or
1048  * the full set of new tag values, but nothing in between.
1049  *
1050  * Imagine the example above without freeze/thaw and the operation
1051  * somehow getting interrupted. This could result in the message being
1052  * left with no tags if the interruption happened after
1053  * notmuch_message_remove_all_tags but before notmuch_message_add_tag.
1054  *
1055  * Return value:
1056  *
1057  * NOTMUCH_STATUS_SUCCESS: Message successfully frozen.
1058  *
1059  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1060  *      mode so message cannot be modified.
1061  */
1062 notmuch_status_t
1063 notmuch_message_freeze (notmuch_message_t *message);
1064
1065 /* Thaw the current 'message', synchronizing any changes that may have
1066  * occurred while 'message' was frozen into the notmuch database.
1067  *
1068  * See notmuch_message_freeze for an example of how to use this
1069  * function to safely provide tag changes.
1070  *
1071  * Multiple calls to freeze/thaw are valid and these calls with
1072  * "stack". That is there must be as many calls to thaw as to freeze
1073  * before a message is actually thawed.
1074  *
1075  * Return value:
1076  *
1077  * NOTMUCH_STATUS_SUCCESS: Message successfully thawed, (or at least
1078  *      its frozen count has successfully been reduced by 1).
1079  *
1080  * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: An attempt was made to thaw
1081  *      an unfrozen message. That is, there have been an unbalanced
1082  *      number of calls to notmuch_message_freeze and
1083  *      notmuch_message_thaw.
1084  */
1085 notmuch_status_t
1086 notmuch_message_thaw (notmuch_message_t *message);
1087
1088 /* Destroy a notmuch_message_t object.
1089  *
1090  * It can be useful to call this function in the case of a single
1091  * query object with many messages in the result, (such as iterating
1092  * over the entire database). Otherwise, it's fine to never call this
1093  * function and there will still be no memory leaks. (The memory from
1094  * the messages get reclaimed when the containing query is destroyed.)
1095  */
1096 void
1097 notmuch_message_destroy (notmuch_message_t *message);
1098
1099 /* Is the given 'tags' iterator pointing at a valid tag.
1100  *
1101  * When this function returns TRUE, notmuch_tags_get will return a
1102  * valid string. Whereas when this function returns FALSE,
1103  * notmuch_tags_get will return NULL.
1104  *
1105  * See the documentation of notmuch_message_get_tags for example code
1106  * showing how to iterate over a notmuch_tags_t object.
1107  */
1108 notmuch_bool_t
1109 notmuch_tags_valid (notmuch_tags_t *tags);
1110
1111 /* Get the current tag from 'tags' as a string.
1112  *
1113  * Note: The returned string belongs to 'tags' and has a lifetime
1114  * identical to it (and the query to which it ultimately belongs).
1115  *
1116  * See the documentation of notmuch_message_get_tags for example code
1117  * showing how to iterate over a notmuch_tags_t object.
1118  */
1119 const char *
1120 notmuch_tags_get (notmuch_tags_t *tags);
1121
1122 /* Move the 'tags' iterator to the next tag.
1123  *
1124  * If 'tags' is already pointing at the last tag then the iterator
1125  * will be moved to a point just beyond that last tag, (where
1126  * notmuch_tags_valid will return FALSE and notmuch_tags_get will
1127  * return NULL).
1128  *
1129  * See the documentation of notmuch_message_get_tags for example code
1130  * showing how to iterate over a notmuch_tags_t object.
1131  */
1132 void
1133 notmuch_tags_move_to_next (notmuch_tags_t *tags);
1134
1135 /* Destroy a notmuch_tags_t object.
1136  *
1137  * It's not strictly necessary to call this function. All memory from
1138  * the notmuch_tags_t object will be reclaimed when the containing
1139  * message or query objects are destroyed.
1140  */
1141 void
1142 notmuch_tags_destroy (notmuch_tags_t *tags);
1143
1144 /* Store an mtime within the database for 'directory'.
1145  *
1146  * The 'directory' should be an object retrieved from the database
1147  * with notmuch_database_get_directory for a particular path.
1148  *
1149  * The intention is for the caller to use the mtime to allow efficient
1150  * identification of new messages to be added to the database. The
1151  * recommended usage is as follows:
1152  *
1153  *   o Read the mtime of a directory from the filesystem
1154  *
1155  *   o Call add_message for all mail files in the directory
1156  *
1157  *   o Call notmuch_directory_set_mtime with the mtime read from the
1158  *     filesystem.
1159  *
1160  * Then, when wanting to check for updates to the directory in the
1161  * future, the client can call notmuch_directory_get_mtime and know
1162  * that it only needs to add files if the mtime of the directory and
1163  * files are newer than the stored timestamp.
1164  *
1165  * Note: The notmuch_directory_get_mtime function does not allow the
1166  * caller to distinguish a timestamp of 0 from a non-existent
1167  * timestamp. So don't store a timestamp of 0 unless you are
1168  * comfortable with that.
1169  *
1170  * Return value:
1171  *
1172  * NOTMUCH_STATUS_SUCCESS: mtime successfully stored in database.
1173  *
1174  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception
1175  *      occurred, mtime not stored.
1176  *
1177  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1178  *      mode so directory mtime cannot be modified.
1179  */
1180 notmuch_status_t
1181 notmuch_directory_set_mtime (notmuch_directory_t *directory,
1182                              time_t mtime);
1183
1184 /* Get the mtime of a directory, (as previously stored with
1185  * notmuch_directory_set_mtime).
1186  *
1187  * Returns 0 if no mtime has previously been stored for this
1188  * directory.*/
1189 time_t
1190 notmuch_directory_get_mtime (notmuch_directory_t *directory);
1191
1192 /* Get a notmuch_filenames_t iterator listing all the filenames of
1193  * messages in the database within the given directory.
1194  *
1195  * The returned filenames will be the basename-entries only (not
1196  * complete paths). */
1197 notmuch_filenames_t *
1198 notmuch_directory_get_child_files (notmuch_directory_t *directory);
1199
1200 /* Get a notmuch_filenams_t iterator listing all the filenames of
1201  * sub-directories in the database within the given directory.
1202  *
1203  * The returned filenames will be the basename-entries only (not
1204  * complete paths). */
1205 notmuch_filenames_t *
1206 notmuch_directory_get_child_directories (notmuch_directory_t *directory);
1207
1208 /* Destroy a notmuch_directory_t object. */
1209 void
1210 notmuch_directory_destroy (notmuch_directory_t *directory);
1211
1212 /* Is the given 'filenames' iterator pointing at a valid filename.
1213  *
1214  * When this function returns TRUE, notmuch_filenames_get will return
1215  * a valid string. Whereas when this function returns FALSE,
1216  * notmuch_filenames_get will return NULL.
1217  *
1218  * It is acceptable to pass NULL for 'filenames', in which case this
1219  * function will always return FALSE.
1220  */
1221 notmuch_bool_t
1222 notmuch_filenames_valid (notmuch_filenames_t *filenames);
1223
1224 /* Get the current filename from 'filenames' as a string.
1225  *
1226  * Note: The returned string belongs to 'filenames' and has a lifetime
1227  * identical to it (and the directory to which it ultimately belongs).
1228  *
1229  * It is acceptable to pass NULL for 'filenames', in which case this
1230  * function will always return NULL.
1231  */
1232 const char *
1233 notmuch_filenames_get (notmuch_filenames_t *filenames);
1234
1235 /* Move the 'filenames' iterator to the next filename.
1236  *
1237  * If 'filenames' is already pointing at the last filename then the
1238  * iterator will be moved to a point just beyond that last filename,
1239  * (where notmuch_filenames_valid will return FALSE and
1240  * notmuch_filenames_get will return NULL).
1241  *
1242  * It is acceptable to pass NULL for 'filenames', in which case this
1243  * function will do nothing.
1244  */
1245 void
1246 notmuch_filenames_move_to_next (notmuch_filenames_t *filenames);
1247
1248 /* Destroy a notmuch_filenames_t object.
1249  *
1250  * It's not strictly necessary to call this function. All memory from
1251  * the notmuch_filenames_t object will be reclaimed when the
1252  * containing directory object is destroyed.
1253  *
1254  * It is acceptable to pass NULL for 'filenames', in which case this
1255  * function will do nothing.
1256  */
1257 void
1258 notmuch_filenames_destroy (notmuch_filenames_t *filenames);
1259
1260 NOTMUCH_END_DECLS
1261
1262 #endif