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