]> git.notmuchmail.org Git - notmuch/blob - lib/notmuch.h
doc: forbid further operations on a closed database
[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 /**
22  * @defgroup notmuch The notmuch API
23  *
24  * Not much of an email library, (just index and search)
25  *
26  * @{
27  */
28
29 #ifndef NOTMUCH_H
30 #define NOTMUCH_H
31
32 #ifndef __DOXYGEN__
33
34 #ifdef  __cplusplus
35 # define NOTMUCH_BEGIN_DECLS  extern "C" {
36 # define NOTMUCH_END_DECLS    }
37 #else
38 # define NOTMUCH_BEGIN_DECLS
39 # define NOTMUCH_END_DECLS
40 #endif
41
42 NOTMUCH_BEGIN_DECLS
43
44 #include <time.h>
45
46 #ifndef FALSE
47 #define FALSE 0
48 #endif
49
50 #ifndef TRUE
51 #define TRUE 1
52 #endif
53
54 /*
55  * The library version number.  This must agree with the soname
56  * version in Makefile.local.
57  */
58 #define LIBNOTMUCH_MAJOR_VERSION        4
59 #define LIBNOTMUCH_MINOR_VERSION        3
60 #define LIBNOTMUCH_MICRO_VERSION        0
61
62
63 #if defined (__clang_major__) && __clang_major__ >= 3 \
64     || defined (__GNUC__) && __GNUC__ >= 5 \
65     || defined (__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ >= 5
66 #define NOTMUCH_DEPRECATED(major,minor) \
67     __attribute__ ((deprecated ("function deprecated as of libnotmuch " #major "." #minor)))
68 #else
69 #define NOTMUCH_DEPRECATED(major,minor) __attribute__ ((deprecated))
70 #endif
71
72
73 #endif /* __DOXYGEN__ */
74
75 /**
76  * Check the version of the notmuch library being compiled against.
77  *
78  * Return true if the library being compiled against is of the
79  * specified version or above. For example:
80  *
81  * @code
82  * #if LIBNOTMUCH_CHECK_VERSION(3, 1, 0)
83  *     (code requiring libnotmuch 3.1.0 or above)
84  * #endif
85  * @endcode
86  *
87  * LIBNOTMUCH_CHECK_VERSION has been defined since version 3.1.0; to
88  * check for versions prior to that, use:
89  *
90  * @code
91  * #if !defined(NOTMUCH_CHECK_VERSION)
92  *     (code requiring libnotmuch prior to 3.1.0)
93  * #endif
94  * @endcode
95  */
96 #define LIBNOTMUCH_CHECK_VERSION (major, minor, micro)                  \
97     (LIBNOTMUCH_MAJOR_VERSION > (major) ||                                      \
98      (LIBNOTMUCH_MAJOR_VERSION == (major) && LIBNOTMUCH_MINOR_VERSION > (minor)) || \
99      (LIBNOTMUCH_MAJOR_VERSION == (major) && LIBNOTMUCH_MINOR_VERSION == (minor) && \
100       LIBNOTMUCH_MICRO_VERSION >= (micro)))
101
102 /**
103  * Notmuch boolean type.
104  */
105 typedef int notmuch_bool_t;
106
107 /**
108  * Status codes used for the return values of most functions.
109  *
110  * A zero value (NOTMUCH_STATUS_SUCCESS) indicates that the function
111  * completed without error. Any other value indicates an error.
112  */
113 typedef enum _notmuch_status {
114     /**
115      * No error occurred.
116      */
117     NOTMUCH_STATUS_SUCCESS = 0,
118     /**
119      * Out of memory.
120      */
121     NOTMUCH_STATUS_OUT_OF_MEMORY,
122     /**
123      * An attempt was made to write to a database opened in read-only
124      * mode.
125      */
126     NOTMUCH_STATUS_READ_ONLY_DATABASE,
127     /**
128      * A Xapian exception occurred.
129      */
130     NOTMUCH_STATUS_XAPIAN_EXCEPTION,
131     /**
132      * An error occurred trying to read or write to a file (this could
133      * be file not found, permission denied, etc.)
134      *
135      * @todo We don't really want to expose this lame XAPIAN_EXCEPTION
136      * value. Instead we should map to things like DATABASE_LOCKED or
137      * whatever.
138      */
139     NOTMUCH_STATUS_FILE_ERROR,
140     /**
141      * A file was presented that doesn't appear to be an email
142      * message.
143      */
144     NOTMUCH_STATUS_FILE_NOT_EMAIL,
145     /**
146      * A file contains a message ID that is identical to a message
147      * already in the database.
148      */
149     NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID,
150     /**
151      * The user erroneously passed a NULL pointer to a notmuch
152      * function.
153      */
154     NOTMUCH_STATUS_NULL_POINTER,
155     /**
156      * A tag value is too long (exceeds NOTMUCH_TAG_MAX).
157      */
158     NOTMUCH_STATUS_TAG_TOO_LONG,
159     /**
160      * The notmuch_message_thaw function has been called more times
161      * than notmuch_message_freeze.
162      */
163     NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
164     /**
165      * notmuch_database_end_atomic has been called more times than
166      * notmuch_database_begin_atomic.
167      */
168     NOTMUCH_STATUS_UNBALANCED_ATOMIC,
169     /**
170      * The operation is not supported.
171      */
172     NOTMUCH_STATUS_UNSUPPORTED_OPERATION,
173     /**
174      * The operation requires a database upgrade.
175      */
176     NOTMUCH_STATUS_UPGRADE_REQUIRED,
177     /**
178      * There is a problem with the proposed path, e.g. a relative path
179      * passed to a function expecting an absolute path.
180      */
181     NOTMUCH_STATUS_PATH_ERROR,
182     /**
183      * Not an actual status value. Just a way to find out how many
184      * valid status values there are.
185      */
186     NOTMUCH_STATUS_LAST_STATUS
187 } notmuch_status_t;
188
189 /**
190  * Get a string representation of a notmuch_status_t value.
191  *
192  * The result is read-only.
193  */
194 const char *
195 notmuch_status_to_string (notmuch_status_t status);
196
197 /* Various opaque data types. For each notmuch_<foo>_t see the various
198  * notmuch_<foo> functions below. */
199 #ifndef __DOXYGEN__
200 typedef struct _notmuch_database notmuch_database_t;
201 typedef struct _notmuch_query notmuch_query_t;
202 typedef struct _notmuch_threads notmuch_threads_t;
203 typedef struct _notmuch_thread notmuch_thread_t;
204 typedef struct _notmuch_messages notmuch_messages_t;
205 typedef struct _notmuch_message notmuch_message_t;
206 typedef struct _notmuch_tags notmuch_tags_t;
207 typedef struct _notmuch_directory notmuch_directory_t;
208 typedef struct _notmuch_filenames notmuch_filenames_t;
209 #endif /* __DOXYGEN__ */
210
211 /**
212  * Create a new, empty notmuch database located at 'path'.
213  *
214  * The path should be a top-level directory to a collection of
215  * plain-text email messages (one message per file). This call will
216  * create a new ".notmuch" directory within 'path' where notmuch will
217  * store its data.
218  *
219  * After a successful call to notmuch_database_create, the returned
220  * database will be open so the caller should call
221  * notmuch_database_destroy when finished with it.
222  *
223  * The database will not yet have any data in it
224  * (notmuch_database_create itself is a very cheap function). Messages
225  * contained within 'path' can be added to the database by calling
226  * notmuch_database_add_message.
227  *
228  * In case of any failure, this function returns an error status and
229  * sets *database to NULL (after printing an error message on stderr).
230  *
231  * Return value:
232  *
233  * NOTMUCH_STATUS_SUCCESS: Successfully created the database.
234  *
235  * NOTMUCH_STATUS_NULL_POINTER: The given 'path' argument is NULL.
236  *
237  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.
238  *
239  * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to create the
240  *      database file (such as permission denied, or file not found,
241  *      etc.), or the database already exists.
242  *
243  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.
244  */
245 notmuch_status_t
246 notmuch_database_create (const char *path, notmuch_database_t **database);
247
248 /**
249  * Like notmuch_database_create, except optionally return an error
250  * message. This message is allocated by malloc and should be freed by
251  * the caller.
252  */
253 notmuch_status_t
254 notmuch_database_create_verbose (const char *path,
255                                  notmuch_database_t **database,
256                                  char **error_message);
257
258 /**
259  * Database open mode for notmuch_database_open.
260  */
261 typedef enum {
262     /**
263      * Open database for reading only.
264      */
265     NOTMUCH_DATABASE_MODE_READ_ONLY = 0,
266     /**
267      * Open database for reading and writing.
268      */
269     NOTMUCH_DATABASE_MODE_READ_WRITE
270 } notmuch_database_mode_t;
271
272 /**
273  * Open an existing notmuch database located at 'path'.
274  *
275  * The database should have been created at some time in the past,
276  * (not necessarily by this process), by calling
277  * notmuch_database_create with 'path'. By default the database should be
278  * opened for reading only. In order to write to the database you need to
279  * pass the NOTMUCH_DATABASE_MODE_READ_WRITE mode.
280  *
281  * An existing notmuch database can be identified by the presence of a
282  * directory named ".notmuch" below 'path'.
283  *
284  * The caller should call notmuch_database_destroy when finished with
285  * this database.
286  *
287  * In case of any failure, this function returns an error status and
288  * sets *database to NULL (after printing an error message on stderr).
289  *
290  * Return value:
291  *
292  * NOTMUCH_STATUS_SUCCESS: Successfully opened the database.
293  *
294  * NOTMUCH_STATUS_NULL_POINTER: The given 'path' argument is NULL.
295  *
296  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.
297  *
298  * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to open the
299  *      database file (such as permission denied, or file not found,
300  *      etc.), or the database version is unknown.
301  *
302  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.
303  */
304 notmuch_status_t
305 notmuch_database_open (const char *path,
306                        notmuch_database_mode_t mode,
307                        notmuch_database_t **database);
308 /**
309  * Like notmuch_database_open, except optionally return an error
310  * message. This message is allocated by malloc and should be freed by
311  * the caller.
312  */
313
314 notmuch_status_t
315 notmuch_database_open_verbose (const char *path,
316                                notmuch_database_mode_t mode,
317                                notmuch_database_t **database,
318                                char **error_message);
319
320 /**
321  * Retrieve last status string for given database.
322  *
323  */
324 const char *
325 notmuch_database_status_string (const notmuch_database_t *notmuch);
326
327 /**
328  * Commit changes and close the given notmuch database.
329  *
330  * After notmuch_database_close has been called, calls to other
331  * functions on objects derived from this database may either behave
332  * as if the database had not been closed (e.g., if the required data
333  * has been cached) or may fail with a
334  * NOTMUCH_STATUS_XAPIAN_EXCEPTION. The only further operation
335  * permitted on the database itself is to call
336  * notmuch_database_destroy.
337  *
338  * notmuch_database_close can be called multiple times.  Later calls
339  * have no effect.
340  *
341  * For writable databases, notmuch_database_close commits all changes
342  * to disk before closing the database.  If the caller is currently in
343  * an atomic section (there was a notmuch_database_begin_atomic
344  * without a matching notmuch_database_end_atomic), this will discard
345  * changes made in that atomic section (but still commit changes made
346  * prior to entering the atomic section).
347  *
348  * Return value:
349  *
350  * NOTMUCH_STATUS_SUCCESS: Successfully closed the database.
351  *
352  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred; the
353  *      database has been closed but there are no guarantees the
354  *      changes to the database, if any, have been flushed to disk.
355  */
356 notmuch_status_t
357 notmuch_database_close (notmuch_database_t *database);
358
359 /**
360  * A callback invoked by notmuch_database_compact to notify the user
361  * of the progress of the compaction process.
362  */
363 typedef void (*notmuch_compact_status_cb_t)(const char *message, void *closure);
364
365 /**
366  * Compact a notmuch database, backing up the original database to the
367  * given path.
368  *
369  * The database will be opened with NOTMUCH_DATABASE_MODE_READ_WRITE
370  * during the compaction process to ensure no writes are made.
371  *
372  * If the optional callback function 'status_cb' is non-NULL, it will
373  * be called with diagnostic and informational messages. The argument
374  * 'closure' is passed verbatim to any callback invoked.
375  */
376 notmuch_status_t
377 notmuch_database_compact (const char* path,
378                           const char* backup_path,
379                           notmuch_compact_status_cb_t status_cb,
380                           void *closure);
381
382 /**
383  * Destroy the notmuch database, closing it if necessary and freeing
384  * all associated resources.
385  *
386  * Return value as in notmuch_database_close if the database was open;
387  * notmuch_database_destroy itself has no failure modes.
388  */
389 notmuch_status_t
390 notmuch_database_destroy (notmuch_database_t *database);
391
392 /**
393  * Return the database path of the given database.
394  *
395  * The return value is a string owned by notmuch so should not be
396  * modified nor freed by the caller.
397  */
398 const char *
399 notmuch_database_get_path (notmuch_database_t *database);
400
401 /**
402  * Return the database format version of the given database.
403  */
404 unsigned int
405 notmuch_database_get_version (notmuch_database_t *database);
406
407 /**
408  * Can the database be upgraded to a newer database version?
409  *
410  * If this function returns TRUE, then the caller may call
411  * notmuch_database_upgrade to upgrade the database.  If the caller
412  * does not upgrade an out-of-date database, then some functions may
413  * fail with NOTMUCH_STATUS_UPGRADE_REQUIRED.  This always returns
414  * FALSE for a read-only database because there's no way to upgrade a
415  * read-only database.
416  */
417 notmuch_bool_t
418 notmuch_database_needs_upgrade (notmuch_database_t *database);
419
420 /**
421  * Upgrade the current database to the latest supported version.
422  *
423  * This ensures that all current notmuch functionality will be
424  * available on the database.  After opening a database in read-write
425  * mode, it is recommended that clients check if an upgrade is needed
426  * (notmuch_database_needs_upgrade) and if so, upgrade with this
427  * function before making any modifications.  If
428  * notmuch_database_needs_upgrade returns FALSE, this will be a no-op.
429  *
430  * The optional progress_notify callback can be used by the caller to
431  * provide progress indication to the user. If non-NULL it will be
432  * called periodically with 'progress' as a floating-point value in
433  * the range of [0.0 .. 1.0] indicating the progress made so far in
434  * the upgrade process.  The argument 'closure' is passed verbatim to
435  * any callback invoked.
436  */
437 notmuch_status_t
438 notmuch_database_upgrade (notmuch_database_t *database,
439                           void (*progress_notify) (void *closure,
440                                                    double progress),
441                           void *closure);
442
443 /**
444  * Begin an atomic database operation.
445  *
446  * Any modifications performed between a successful begin and a
447  * notmuch_database_end_atomic will be applied to the database
448  * atomically.  Note that, unlike a typical database transaction, this
449  * only ensures atomicity, not durability; neither begin nor end
450  * necessarily flush modifications to disk.
451  *
452  * Atomic sections may be nested.  begin_atomic and end_atomic must
453  * always be called in pairs.
454  *
455  * Return value:
456  *
457  * NOTMUCH_STATUS_SUCCESS: Successfully entered atomic section.
458  *
459  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
460  *      atomic section not entered.
461  */
462 notmuch_status_t
463 notmuch_database_begin_atomic (notmuch_database_t *notmuch);
464
465 /**
466  * Indicate the end of an atomic database operation.
467  *
468  * Return value:
469  *
470  * NOTMUCH_STATUS_SUCCESS: Successfully completed atomic section.
471  *
472  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
473  *      atomic section not ended.
474  *
475  * NOTMUCH_STATUS_UNBALANCED_ATOMIC: The database is not currently in
476  *      an atomic section.
477  */
478 notmuch_status_t
479 notmuch_database_end_atomic (notmuch_database_t *notmuch);
480
481 /**
482  * Return the committed database revision and UUID.
483  *
484  * The database revision number increases monotonically with each
485  * commit to the database.  Hence, all messages and message changes
486  * committed to the database (that is, visible to readers) have a last
487  * modification revision <= the committed database revision.  Any
488  * messages committed in the future will be assigned a modification
489  * revision > the committed database revision.
490  *
491  * The UUID is a NUL-terminated opaque string that uniquely identifies
492  * this database.  Two revision numbers are only comparable if they
493  * have the same database UUID.
494  */
495 unsigned long
496 notmuch_database_get_revision (notmuch_database_t *notmuch,
497                                 const char **uuid);
498
499 /**
500  * Retrieve a directory object from the database for 'path'.
501  *
502  * Here, 'path' should be a path relative to the path of 'database'
503  * (see notmuch_database_get_path), or else should be an absolute path
504  * with initial components that match the path of 'database'.
505  *
506  * If this directory object does not exist in the database, this
507  * returns NOTMUCH_STATUS_SUCCESS and sets *directory to NULL.
508  *
509  * Otherwise the returned directory object is owned by the database
510  * and as such, will only be valid until notmuch_database_destroy is
511  * called.
512  *
513  * Return value:
514  *
515  * NOTMUCH_STATUS_SUCCESS: Successfully retrieved directory.
516  *
517  * NOTMUCH_STATUS_NULL_POINTER: The given 'directory' argument is NULL.
518  *
519  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
520  *      directory not retrieved.
521  *
522  * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the
523  *      database to use this function.
524  */
525 notmuch_status_t
526 notmuch_database_get_directory (notmuch_database_t *database,
527                                 const char *path,
528                                 notmuch_directory_t **directory);
529
530 /**
531  * Add a new message to the given notmuch database or associate an
532  * additional filename with an existing message.
533  *
534  * Here, 'filename' should be a path relative to the path of
535  * 'database' (see notmuch_database_get_path), or else should be an
536  * absolute filename with initial components that match the path of
537  * 'database'.
538  *
539  * The file should be a single mail message (not a multi-message mbox)
540  * that is expected to remain at its current location, (since the
541  * notmuch database will reference the filename, and will not copy the
542  * entire contents of the file.
543  *
544  * If another message with the same message ID already exists in the
545  * database, rather than creating a new message, this adds 'filename'
546  * to the list of the filenames for the existing message.
547  *
548  * If 'message' is not NULL, then, on successful return
549  * (NOTMUCH_STATUS_SUCCESS or NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) '*message'
550  * will be initialized to a message object that can be used for things
551  * such as adding tags to the just-added message. The user should call
552  * notmuch_message_destroy when done with the message. On any failure
553  * '*message' will be set to NULL.
554  *
555  * Return value:
556  *
557  * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
558  *
559  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
560  *      message not added.
561  *
562  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message
563  *      ID as another message already in the database. The new
564  *      filename was successfully added to the message in the database
565  *      (if not already present) and the existing message is returned.
566  *
567  * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
568  *      file, (such as permission denied, or file not found,
569  *      etc.). Nothing added to the database.
570  *
571  * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
572  *      like an email message. Nothing added to the database.
573  *
574  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
575  *      mode so no message can be added.
576  *
577  * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the
578  *      database to use this function.
579  */
580 notmuch_status_t
581 notmuch_database_add_message (notmuch_database_t *database,
582                               const char *filename,
583                               notmuch_message_t **message);
584
585 /**
586  * Remove a message filename from the given notmuch database. If the
587  * message has no more filenames, remove the message.
588  *
589  * If the same message (as determined by the message ID) is still
590  * available via other filenames, then the message will persist in the
591  * database for those filenames. When the last filename is removed for
592  * a particular message, the database content for that message will be
593  * entirely removed.
594  *
595  * Return value:
596  *
597  * NOTMUCH_STATUS_SUCCESS: The last filename was removed and the
598  *      message was removed from the database.
599  *
600  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
601  *      message not removed.
602  *
603  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: This filename was removed but
604  *      the message persists in the database with at least one other
605  *      filename.
606  *
607  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
608  *      mode so no message can be removed.
609  *
610  * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the
611  *      database to use this function.
612  */
613 notmuch_status_t
614 notmuch_database_remove_message (notmuch_database_t *database,
615                                  const char *filename);
616
617 /**
618  * Find a message with the given message_id.
619  *
620  * If a message with the given message_id is found then, on successful return
621  * (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to a message
622  * object.  The caller should call notmuch_message_destroy when done with the
623  * message.
624  *
625  * On any failure or when the message is not found, this function initializes
626  * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
627  * caller is supposed to check '*message' for NULL to find out whether the
628  * message with the given message_id was found.
629  *
630  * Return value:
631  *
632  * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'.
633  *
634  * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
635  *
636  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating message object
637  *
638  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
639  */
640 notmuch_status_t
641 notmuch_database_find_message (notmuch_database_t *database,
642                                const char *message_id,
643                                notmuch_message_t **message);
644
645 /**
646  * Find a message with the given filename.
647  *
648  * If the database contains a message with the given filename then, on
649  * successful return (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to
650  * a message object. The caller should call notmuch_message_destroy when done
651  * with the message.
652  *
653  * On any failure or when the message is not found, this function initializes
654  * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
655  * caller is supposed to check '*message' for NULL to find out whether the
656  * message with the given filename is found.
657  *
658  * Return value:
659  *
660  * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'
661  *
662  * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
663  *
664  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating the message object
665  *
666  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
667  *
668  * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the
669  *      database to use this function.
670  */
671 notmuch_status_t
672 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
673                                            const char *filename,
674                                            notmuch_message_t **message);
675
676 /**
677  * Return a list of all tags found in the database.
678  *
679  * This function creates a list of all tags found in the database. The
680  * resulting list contains all tags from all messages found in the database.
681  *
682  * On error this function returns NULL.
683  */
684 notmuch_tags_t *
685 notmuch_database_get_all_tags (notmuch_database_t *db);
686
687 /**
688  * Create a new query for 'database'.
689  *
690  * Here, 'database' should be an open database, (see
691  * notmuch_database_open and notmuch_database_create).
692  *
693  * For the query string, we'll document the syntax here more
694  * completely in the future, but it's likely to be a specialized
695  * version of the general Xapian query syntax:
696  *
697  * http://xapian.org/docs/queryparser.html
698  *
699  * As a special case, passing either a length-zero string, (that is ""),
700  * or a string consisting of a single asterisk (that is "*"), will
701  * result in a query that returns all messages in the database.
702  *
703  * See notmuch_query_set_sort for controlling the order of results.
704  * See notmuch_query_search_messages and notmuch_query_search_threads
705  * to actually execute the query.
706  *
707  * User should call notmuch_query_destroy when finished with this
708  * query.
709  *
710  * Will return NULL if insufficient memory is available.
711  */
712 notmuch_query_t *
713 notmuch_query_create (notmuch_database_t *database,
714                       const char *query_string);
715
716 /**
717  * Sort values for notmuch_query_set_sort.
718  */
719 typedef enum {
720     /**
721      * Oldest first.
722      */
723     NOTMUCH_SORT_OLDEST_FIRST,
724     /**
725      * Newest first.
726      */
727     NOTMUCH_SORT_NEWEST_FIRST,
728     /**
729      * Sort by message-id.
730      */
731     NOTMUCH_SORT_MESSAGE_ID,
732     /**
733      * Do not sort.
734      */
735     NOTMUCH_SORT_UNSORTED
736 } notmuch_sort_t;
737
738 /**
739  * Return the query_string of this query. See notmuch_query_create.
740  */
741 const char *
742 notmuch_query_get_query_string (const notmuch_query_t *query);
743
744 /**
745  * Return the notmuch database of this query. See notmuch_query_create.
746  */
747 notmuch_database_t *
748 notmuch_query_get_database (const notmuch_query_t *query);
749
750 /**
751  * Exclude values for notmuch_query_set_omit_excluded. The strange
752  * order is to maintain backward compatibility: the old FALSE/TRUE
753  * options correspond to the new
754  * NOTMUCH_EXCLUDE_FLAG/NOTMUCH_EXCLUDE_TRUE options.
755  */
756 typedef enum {
757     NOTMUCH_EXCLUDE_FLAG,
758     NOTMUCH_EXCLUDE_TRUE,
759     NOTMUCH_EXCLUDE_FALSE,
760     NOTMUCH_EXCLUDE_ALL
761 } notmuch_exclude_t;
762
763 /**
764  * Specify whether to omit excluded results or simply flag them.  By
765  * default, this is set to TRUE.
766  *
767  * If set to TRUE or ALL, notmuch_query_search_messages will omit excluded
768  * messages from the results, and notmuch_query_search_threads will omit
769  * threads that match only in excluded messages.  If set to TRUE,
770  * notmuch_query_search_threads will include all messages in threads that
771  * match in at least one non-excluded message.  Otherwise, if set to ALL,
772  * notmuch_query_search_threads will omit excluded messages from all threads.
773  *
774  * If set to FALSE or FLAG then both notmuch_query_search_messages and
775  * notmuch_query_search_threads will return all matching
776  * messages/threads regardless of exclude status. If set to FLAG then
777  * the exclude flag will be set for any excluded message that is
778  * returned by notmuch_query_search_messages, and the thread counts
779  * for threads returned by notmuch_query_search_threads will be the
780  * number of non-excluded messages/matches. Otherwise, if set to
781  * FALSE, then the exclude status is completely ignored.
782  *
783  * The performance difference when calling
784  * notmuch_query_search_messages should be relatively small (and both
785  * should be very fast).  However, in some cases,
786  * notmuch_query_search_threads is very much faster when omitting
787  * excluded messages as it does not need to construct the threads that
788  * only match in excluded messages.
789  */
790 void
791 notmuch_query_set_omit_excluded (notmuch_query_t *query,
792                                  notmuch_exclude_t omit_excluded);
793
794 /**
795  * Specify the sorting desired for this query.
796  */
797 void
798 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
799
800 /**
801  * Return the sort specified for this query. See
802  * notmuch_query_set_sort.
803  */
804 notmuch_sort_t
805 notmuch_query_get_sort (const notmuch_query_t *query);
806
807 /**
808  * Add a tag that will be excluded from the query results by default.
809  * This exclusion will be overridden if this tag appears explicitly in
810  * the query.
811  */
812 void
813 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag);
814
815 /**
816  * Execute a query for threads, returning a notmuch_threads_t object
817  * which can be used to iterate over the results. The returned threads
818  * object is owned by the query and as such, will only be valid until
819  * notmuch_query_destroy.
820  *
821  * Typical usage might be:
822  *
823  *     notmuch_query_t *query;
824  *     notmuch_threads_t *threads;
825  *     notmuch_thread_t *thread;
826  *
827  *     query = notmuch_query_create (database, query_string);
828  *
829  *     for (threads = notmuch_query_search_threads (query);
830  *          notmuch_threads_valid (threads);
831  *          notmuch_threads_move_to_next (threads))
832  *     {
833  *         thread = notmuch_threads_get (threads);
834  *         ....
835  *         notmuch_thread_destroy (thread);
836  *     }
837  *
838  *     notmuch_query_destroy (query);
839  *
840  * Note: If you are finished with a thread before its containing
841  * query, you can call notmuch_thread_destroy to clean up some memory
842  * sooner (as in the above example). Otherwise, if your thread objects
843  * are long-lived, then you don't need to call notmuch_thread_destroy
844  * and all the memory will still be reclaimed when the query is
845  * destroyed.
846  *
847  * Note that there's no explicit destructor needed for the
848  * notmuch_threads_t object. (For consistency, we do provide a
849  * notmuch_threads_destroy function, but there's no good reason
850  * to call it if the query is about to be destroyed).
851  *
852  * @since libnotmuch 4.2 (notmuch 0.20)
853  */
854 notmuch_status_t
855 notmuch_query_search_threads_st (notmuch_query_t *query,
856                                  notmuch_threads_t **out);
857
858 /**
859  * Like notmuch_query_search_threads_st, but without a status return.
860  *
861  * If a Xapian exception occurs this function will return NULL.
862  *
863  * @deprecated Deprecated as of libnotmuch 4.3 (notmuch 0.21). Please
864  * use notmuch_query_search_threads_st instead.
865  *
866  */
867 NOTMUCH_DEPRECATED(4,3)
868 notmuch_threads_t *
869 notmuch_query_search_threads (notmuch_query_t *query);
870
871 /**
872  * Execute a query for messages, returning a notmuch_messages_t object
873  * which can be used to iterate over the results. The returned
874  * messages object is owned by the query and as such, will only be
875  * valid until notmuch_query_destroy.
876  *
877  * Typical usage might be:
878  *
879  *     notmuch_query_t *query;
880  *     notmuch_messages_t *messages;
881  *     notmuch_message_t *message;
882  *
883  *     query = notmuch_query_create (database, query_string);
884  *
885  *     for (messages = notmuch_query_search_messages (query);
886  *          notmuch_messages_valid (messages);
887  *          notmuch_messages_move_to_next (messages))
888  *     {
889  *         message = notmuch_messages_get (messages);
890  *         ....
891  *         notmuch_message_destroy (message);
892  *     }
893  *
894  *     notmuch_query_destroy (query);
895  *
896  * Note: If you are finished with a message before its containing
897  * query, you can call notmuch_message_destroy to clean up some memory
898  * sooner (as in the above example). Otherwise, if your message
899  * objects are long-lived, then you don't need to call
900  * notmuch_message_destroy and all the memory will still be reclaimed
901  * when the query is destroyed.
902  *
903  * Note that there's no explicit destructor needed for the
904  * notmuch_messages_t object. (For consistency, we do provide a
905  * notmuch_messages_destroy function, but there's no good
906  * reason to call it if the query is about to be destroyed).
907  *
908  * If a Xapian exception occurs this function will return NULL.
909  *
910  * @since libnotmuch 4.2 (notmuch 0.20)
911  */
912 notmuch_status_t
913 notmuch_query_search_messages_st (notmuch_query_t *query,
914                                   notmuch_messages_t **out);
915 /**
916  * Like notmuch_query_search_messages, but without a status return.
917  *
918  * If a Xapian exception occurs this function will return NULL.
919  *
920  * @deprecated Deprecated as of libnotmuch 4.3 (notmuch 0.21). Please use
921  * notmuch_query_search_messages_st instead.
922  *
923  */
924 NOTMUCH_DEPRECATED(4,3)
925 notmuch_messages_t *
926 notmuch_query_search_messages (notmuch_query_t *query);
927
928 /**
929  * Destroy a notmuch_query_t along with any associated resources.
930  *
931  * This will in turn destroy any notmuch_threads_t and
932  * notmuch_messages_t objects generated by this query, (and in
933  * turn any notmuch_thread_t and notmuch_message_t objects generated
934  * from those results, etc.), if such objects haven't already been
935  * destroyed.
936  */
937 void
938 notmuch_query_destroy (notmuch_query_t *query);
939
940 /**
941  * Is the given 'threads' iterator pointing at a valid thread.
942  *
943  * When this function returns TRUE, notmuch_threads_get will return a
944  * valid object. Whereas when this function returns FALSE,
945  * notmuch_threads_get will return NULL.
946  *
947  * If passed a NULL pointer, this function returns FALSE
948  *
949  * See the documentation of notmuch_query_search_threads for example
950  * code showing how to iterate over a notmuch_threads_t object.
951  */
952 notmuch_bool_t
953 notmuch_threads_valid (notmuch_threads_t *threads);
954
955 /**
956  * Get the current thread from 'threads' as a notmuch_thread_t.
957  *
958  * Note: The returned thread belongs to 'threads' and has a lifetime
959  * identical to it (and the query to which it belongs).
960  *
961  * See the documentation of notmuch_query_search_threads for example
962  * code showing how to iterate over a notmuch_threads_t object.
963  *
964  * If an out-of-memory situation occurs, this function will return
965  * NULL.
966  */
967 notmuch_thread_t *
968 notmuch_threads_get (notmuch_threads_t *threads);
969
970 /**
971  * Move the 'threads' iterator to the next thread.
972  *
973  * If 'threads' is already pointing at the last thread then the
974  * iterator will be moved to a point just beyond that last thread,
975  * (where notmuch_threads_valid will return FALSE and
976  * notmuch_threads_get will return NULL).
977  *
978  * See the documentation of notmuch_query_search_threads for example
979  * code showing how to iterate over a notmuch_threads_t object.
980  */
981 void
982 notmuch_threads_move_to_next (notmuch_threads_t *threads);
983
984 /**
985  * Destroy a notmuch_threads_t object.
986  *
987  * It's not strictly necessary to call this function. All memory from
988  * the notmuch_threads_t object will be reclaimed when the
989  * containing query object is destroyed.
990  */
991 void
992 notmuch_threads_destroy (notmuch_threads_t *threads);
993
994 /**
995  * Return the number of messages matching a search.
996  *
997  * This function performs a search and returns the number of matching
998  * messages.
999  *
1000  * @returns
1001  *
1002  * NOTMUCH_STATUS_SUCCESS: query completed successfully.
1003  *
1004  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occured. The
1005  *      value of *count is not defined.
1006  *
1007  * @since libnotmuch 4.3 (notmuch 0.21)
1008  */
1009 notmuch_status_t
1010 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned int *count);
1011
1012 /**
1013  * like notmuch_query_count_messages_st, but without a status return.
1014  *
1015  * May return 0 in the case of errors.
1016  *
1017  * @deprecated Deprecated since libnotmuch 4.3 (notmuch 0.21). Please
1018  * use notmuch_query_count_messages_st instead.
1019  */
1020 NOTMUCH_DEPRECATED(4,3)
1021 unsigned int
1022 notmuch_query_count_messages (notmuch_query_t *query);
1023
1024 /**
1025  * Return the number of threads matching a search.
1026  *
1027  * This function performs a search and returns the number of unique thread IDs
1028  * in the matching messages. This is the same as number of threads matching a
1029  * search.
1030  *
1031  * Note that this is a significantly heavier operation than
1032  * notmuch_query_count_messages{_st}().
1033  *
1034  * @returns
1035  *
1036  * NOTMUCH_STATUS_OUT_OF_MEMORY: Memory allocation failed. The value
1037  *      of *count is not defined
1038
1039  * NOTMUCH_STATUS_SUCCESS: query completed successfully.
1040  *
1041  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occured. The
1042  *      value of *count is not defined.
1043  *
1044  * @since libnotmuch 4.3 (notmuch 0.21)
1045  */
1046 notmuch_status_t
1047 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count);
1048
1049 /**
1050  * like notmuch_query_count_threads, but without a status return.
1051  *
1052  * May return 0 in case of errors.
1053  *
1054  * @deprecated Deprecated as of libnotmuch 4.3 (notmuch 0.21). Please
1055  * use notmuch_query_count_threads_st instead.
1056  */
1057 NOTMUCH_DEPRECATED(4,3)
1058 unsigned int
1059 notmuch_query_count_threads (notmuch_query_t *query);
1060
1061 /**
1062  * Get the thread ID of 'thread'.
1063  *
1064  * The returned string belongs to 'thread' and as such, should not be
1065  * modified by the caller and will only be valid for as long as the
1066  * thread is valid, (which is until notmuch_thread_destroy or until
1067  * the query from which it derived is destroyed).
1068  */
1069 const char *
1070 notmuch_thread_get_thread_id (notmuch_thread_t *thread);
1071
1072 /**
1073  * Get the total number of messages in 'thread'.
1074  *
1075  * This count consists of all messages in the database belonging to
1076  * this thread. Contrast with notmuch_thread_get_matched_messages() .
1077  */
1078 int
1079 notmuch_thread_get_total_messages (notmuch_thread_t *thread);
1080
1081 /**
1082  * Get a notmuch_messages_t iterator for the top-level messages in
1083  * 'thread' in oldest-first order.
1084  *
1085  * This iterator will not necessarily iterate over all of the messages
1086  * in the thread. It will only iterate over the messages in the thread
1087  * which are not replies to other messages in the thread.
1088  *
1089  * The returned list will be destroyed when the thread is destroyed.
1090  */
1091 notmuch_messages_t *
1092 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread);
1093
1094 /**
1095  * Get a notmuch_thread_t iterator for all messages in 'thread' in
1096  * oldest-first order.
1097  *
1098  * The returned list will be destroyed when the thread is destroyed.
1099  */
1100 notmuch_messages_t *
1101 notmuch_thread_get_messages (notmuch_thread_t *thread);
1102
1103 /**
1104  * Get the number of messages in 'thread' that matched the search.
1105  *
1106  * This count includes only the messages in this thread that were
1107  * matched by the search from which the thread was created and were
1108  * not excluded by any exclude tags passed in with the query (see
1109  * notmuch_query_add_tag_exclude). Contrast with
1110  * notmuch_thread_get_total_messages() .
1111  */
1112 int
1113 notmuch_thread_get_matched_messages (notmuch_thread_t *thread);
1114
1115 /**
1116  * Get the authors of 'thread' as a UTF-8 string.
1117  *
1118  * The returned string is a comma-separated list of the names of the
1119  * authors of mail messages in the query results that belong to this
1120  * thread.
1121  *
1122  * The string contains authors of messages matching the query first, then
1123  * non-matched authors (with the two groups separated by '|'). Within
1124  * each group, authors are ordered by date.
1125  *
1126  * The returned string belongs to 'thread' and as such, should not be
1127  * modified by the caller and will only be valid for as long as the
1128  * thread is valid, (which is until notmuch_thread_destroy or until
1129  * the query from which it derived is destroyed).
1130  */
1131 const char *
1132 notmuch_thread_get_authors (notmuch_thread_t *thread);
1133
1134 /**
1135  * Get the subject of 'thread' as a UTF-8 string.
1136  *
1137  * The subject is taken from the first message (according to the query
1138  * order---see notmuch_query_set_sort) in the query results that
1139  * belongs to this thread.
1140  *
1141  * The returned string belongs to 'thread' and as such, should not be
1142  * modified by the caller and will only be valid for as long as the
1143  * thread is valid, (which is until notmuch_thread_destroy or until
1144  * the query from which it derived is destroyed).
1145  */
1146 const char *
1147 notmuch_thread_get_subject (notmuch_thread_t *thread);
1148
1149 /**
1150  * Get the date of the oldest message in 'thread' as a time_t value.
1151  */
1152 time_t
1153 notmuch_thread_get_oldest_date (notmuch_thread_t *thread);
1154
1155 /**
1156  * Get the date of the newest message in 'thread' as a time_t value.
1157  */
1158 time_t
1159 notmuch_thread_get_newest_date (notmuch_thread_t *thread);
1160
1161 /**
1162  * Get the tags for 'thread', returning a notmuch_tags_t object which
1163  * can be used to iterate over all tags.
1164  *
1165  * Note: In the Notmuch database, tags are stored on individual
1166  * messages, not on threads. So the tags returned here will be all
1167  * tags of the messages which matched the search and which belong to
1168  * this thread.
1169  *
1170  * The tags object is owned by the thread and as such, will only be
1171  * valid for as long as the thread is valid, (for example, until
1172  * notmuch_thread_destroy or until the query from which it derived is
1173  * destroyed).
1174  *
1175  * Typical usage might be:
1176  *
1177  *     notmuch_thread_t *thread;
1178  *     notmuch_tags_t *tags;
1179  *     const char *tag;
1180  *
1181  *     thread = notmuch_threads_get (threads);
1182  *
1183  *     for (tags = notmuch_thread_get_tags (thread);
1184  *          notmuch_tags_valid (tags);
1185  *          notmuch_tags_move_to_next (tags))
1186  *     {
1187  *         tag = notmuch_tags_get (tags);
1188  *         ....
1189  *     }
1190  *
1191  *     notmuch_thread_destroy (thread);
1192  *
1193  * Note that there's no explicit destructor needed for the
1194  * notmuch_tags_t object. (For consistency, we do provide a
1195  * notmuch_tags_destroy function, but there's no good reason to call
1196  * it if the message is about to be destroyed).
1197  */
1198 notmuch_tags_t *
1199 notmuch_thread_get_tags (notmuch_thread_t *thread);
1200
1201 /**
1202  * Destroy a notmuch_thread_t object.
1203  */
1204 void
1205 notmuch_thread_destroy (notmuch_thread_t *thread);
1206
1207 /**
1208  * Is the given 'messages' iterator pointing at a valid message.
1209  *
1210  * When this function returns TRUE, notmuch_messages_get will return a
1211  * valid object. Whereas when this function returns FALSE,
1212  * notmuch_messages_get will return NULL.
1213  *
1214  * See the documentation of notmuch_query_search_messages for example
1215  * code showing how to iterate over a notmuch_messages_t object.
1216  */
1217 notmuch_bool_t
1218 notmuch_messages_valid (notmuch_messages_t *messages);
1219
1220 /**
1221  * Get the current message from 'messages' as a notmuch_message_t.
1222  *
1223  * Note: The returned message belongs to 'messages' and has a lifetime
1224  * identical to it (and the query to which it belongs).
1225  *
1226  * See the documentation of notmuch_query_search_messages for example
1227  * code showing how to iterate over a notmuch_messages_t object.
1228  *
1229  * If an out-of-memory situation occurs, this function will return
1230  * NULL.
1231  */
1232 notmuch_message_t *
1233 notmuch_messages_get (notmuch_messages_t *messages);
1234
1235 /**
1236  * Move the 'messages' iterator to the next message.
1237  *
1238  * If 'messages' is already pointing at the last message then the
1239  * iterator will be moved to a point just beyond that last message,
1240  * (where notmuch_messages_valid will return FALSE and
1241  * notmuch_messages_get will return NULL).
1242  *
1243  * See the documentation of notmuch_query_search_messages for example
1244  * code showing how to iterate over a notmuch_messages_t object.
1245  */
1246 void
1247 notmuch_messages_move_to_next (notmuch_messages_t *messages);
1248
1249 /**
1250  * Destroy a notmuch_messages_t object.
1251  *
1252  * It's not strictly necessary to call this function. All memory from
1253  * the notmuch_messages_t object will be reclaimed when the containing
1254  * query object is destroyed.
1255  */
1256 void
1257 notmuch_messages_destroy (notmuch_messages_t *messages);
1258
1259 /**
1260  * Return a list of tags from all messages.
1261  *
1262  * The resulting list is guaranteed not to contain duplicated tags.
1263  *
1264  * WARNING: You can no longer iterate over messages after calling this
1265  * function, because the iterator will point at the end of the list.
1266  * We do not have a function to reset the iterator yet and the only
1267  * way how you can iterate over the list again is to recreate the
1268  * message list.
1269  *
1270  * The function returns NULL on error.
1271  */
1272 notmuch_tags_t *
1273 notmuch_messages_collect_tags (notmuch_messages_t *messages);
1274
1275 /**
1276  * Get the message ID of 'message'.
1277  *
1278  * The returned string belongs to 'message' and as such, should not be
1279  * modified by the caller and will only be valid for as long as the
1280  * message is valid, (which is until the query from which it derived
1281  * is destroyed).
1282  *
1283  * This function will not return NULL since Notmuch ensures that every
1284  * message has a unique message ID, (Notmuch will generate an ID for a
1285  * message if the original file does not contain one).
1286  */
1287 const char *
1288 notmuch_message_get_message_id (notmuch_message_t *message);
1289
1290 /**
1291  * Get the thread ID of 'message'.
1292  *
1293  * The returned string belongs to 'message' and as such, should not be
1294  * modified by the caller and will only be valid for as long as the
1295  * message is valid, (for example, until the user calls
1296  * notmuch_message_destroy on 'message' or until a query from which it
1297  * derived is destroyed).
1298  *
1299  * This function will not return NULL since Notmuch ensures that every
1300  * message belongs to a single thread.
1301  */
1302 const char *
1303 notmuch_message_get_thread_id (notmuch_message_t *message);
1304
1305 /**
1306  * Get a notmuch_messages_t iterator for all of the replies to
1307  * 'message'.
1308  *
1309  * Note: This call only makes sense if 'message' was ultimately
1310  * obtained from a notmuch_thread_t object, (such as by coming
1311  * directly from the result of calling notmuch_thread_get_
1312  * toplevel_messages or by any number of subsequent
1313  * calls to notmuch_message_get_replies).
1314  *
1315  * If 'message' was obtained through some non-thread means, (such as
1316  * by a call to notmuch_query_search_messages), then this function
1317  * will return NULL.
1318  *
1319  * If there are no replies to 'message', this function will return
1320  * NULL. (Note that notmuch_messages_valid will accept that NULL
1321  * value as legitimate, and simply return FALSE for it.)
1322  */
1323 notmuch_messages_t *
1324 notmuch_message_get_replies (notmuch_message_t *message);
1325
1326 /**
1327  * Get a filename for the email corresponding to 'message'.
1328  *
1329  * The returned filename is an absolute filename, (the initial
1330  * component will match notmuch_database_get_path() ).
1331  *
1332  * The returned string belongs to the message so should not be
1333  * modified or freed by the caller (nor should it be referenced after
1334  * the message is destroyed).
1335  *
1336  * Note: If this message corresponds to multiple files in the mail
1337  * store, (that is, multiple files contain identical message IDs),
1338  * this function will arbitrarily return a single one of those
1339  * filenames. See notmuch_message_get_filenames for returning the
1340  * complete list of filenames.
1341  */
1342 const char *
1343 notmuch_message_get_filename (notmuch_message_t *message);
1344
1345 /**
1346  * Get all filenames for the email corresponding to 'message'.
1347  *
1348  * Returns a notmuch_filenames_t iterator listing all the filenames
1349  * associated with 'message'. These files may not have identical
1350  * content, but each will have the identical Message-ID.
1351  *
1352  * Each filename in the iterator is an absolute filename, (the initial
1353  * component will match notmuch_database_get_path() ).
1354  */
1355 notmuch_filenames_t *
1356 notmuch_message_get_filenames (notmuch_message_t *message);
1357
1358 /**
1359  * Message flags.
1360  */
1361 typedef enum _notmuch_message_flag {
1362     NOTMUCH_MESSAGE_FLAG_MATCH,
1363     NOTMUCH_MESSAGE_FLAG_EXCLUDED,
1364
1365     /* This message is a "ghost message", meaning it has no filenames
1366      * or content, but we know it exists because it was referenced by
1367      * some other message.  A ghost message has only a message ID and
1368      * thread ID.
1369      */
1370     NOTMUCH_MESSAGE_FLAG_GHOST,
1371 } notmuch_message_flag_t;
1372
1373 /**
1374  * Get a value of a flag for the email corresponding to 'message'.
1375  */
1376 notmuch_bool_t
1377 notmuch_message_get_flag (notmuch_message_t *message,
1378                           notmuch_message_flag_t flag);
1379
1380 /**
1381  * Set a value of a flag for the email corresponding to 'message'.
1382  */
1383 void
1384 notmuch_message_set_flag (notmuch_message_t *message,
1385                           notmuch_message_flag_t flag, notmuch_bool_t value);
1386
1387 /**
1388  * Get the date of 'message' as a time_t value.
1389  *
1390  * For the original textual representation of the Date header from the
1391  * message call notmuch_message_get_header() with a header value of
1392  * "date".
1393  */
1394 time_t
1395 notmuch_message_get_date  (notmuch_message_t *message);
1396
1397 /**
1398  * Get the value of the specified header from 'message' as a UTF-8 string.
1399  *
1400  * Common headers are stored in the database when the message is
1401  * indexed and will be returned from the database.  Other headers will
1402  * be read from the actual message file.
1403  *
1404  * The header name is case insensitive.
1405  *
1406  * The returned string belongs to the message so should not be
1407  * modified or freed by the caller (nor should it be referenced after
1408  * the message is destroyed).
1409  *
1410  * Returns an empty string ("") if the message does not contain a
1411  * header line matching 'header'. Returns NULL if any error occurs.
1412  */
1413 const char *
1414 notmuch_message_get_header (notmuch_message_t *message, const char *header);
1415
1416 /**
1417  * Get the tags for 'message', returning a notmuch_tags_t object which
1418  * can be used to iterate over all tags.
1419  *
1420  * The tags object is owned by the message and as such, will only be
1421  * valid for as long as the message is valid, (which is until the
1422  * query from which it derived is destroyed).
1423  *
1424  * Typical usage might be:
1425  *
1426  *     notmuch_message_t *message;
1427  *     notmuch_tags_t *tags;
1428  *     const char *tag;
1429  *
1430  *     message = notmuch_database_find_message (database, message_id);
1431  *
1432  *     for (tags = notmuch_message_get_tags (message);
1433  *          notmuch_tags_valid (tags);
1434  *          notmuch_tags_move_to_next (tags))
1435  *     {
1436  *         tag = notmuch_tags_get (tags);
1437  *         ....
1438  *     }
1439  *
1440  *     notmuch_message_destroy (message);
1441  *
1442  * Note that there's no explicit destructor needed for the
1443  * notmuch_tags_t object. (For consistency, we do provide a
1444  * notmuch_tags_destroy function, but there's no good reason to call
1445  * it if the message is about to be destroyed).
1446  */
1447 notmuch_tags_t *
1448 notmuch_message_get_tags (notmuch_message_t *message);
1449
1450 /**
1451  * The longest possible tag value.
1452  */
1453 #define NOTMUCH_TAG_MAX 200
1454
1455 /**
1456  * Add a tag to the given message.
1457  *
1458  * Return value:
1459  *
1460  * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
1461  *
1462  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
1463  *
1464  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
1465  *      (exceeds NOTMUCH_TAG_MAX)
1466  *
1467  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1468  *      mode so message cannot be modified.
1469  */
1470 notmuch_status_t
1471 notmuch_message_add_tag (notmuch_message_t *message, const char *tag);
1472
1473 /**
1474  * Remove a tag from the given message.
1475  *
1476  * Return value:
1477  *
1478  * NOTMUCH_STATUS_SUCCESS: Tag successfully removed from message
1479  *
1480  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
1481  *
1482  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
1483  *      (exceeds NOTMUCH_TAG_MAX)
1484  *
1485  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1486  *      mode so message cannot be modified.
1487  */
1488 notmuch_status_t
1489 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag);
1490
1491 /**
1492  * Remove all tags from the given message.
1493  *
1494  * See notmuch_message_freeze for an example showing how to safely
1495  * replace tag values.
1496  *
1497  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1498  *      mode so message cannot be modified.
1499  */
1500 notmuch_status_t
1501 notmuch_message_remove_all_tags (notmuch_message_t *message);
1502
1503 /**
1504  * Add/remove tags according to maildir flags in the message filename(s).
1505  *
1506  * This function examines the filenames of 'message' for maildir
1507  * flags, and adds or removes tags on 'message' as follows when these
1508  * flags are present:
1509  *
1510  *      Flag    Action if present
1511  *      ----    -----------------
1512  *      'D'     Adds the "draft" tag to the message
1513  *      'F'     Adds the "flagged" tag to the message
1514  *      'P'     Adds the "passed" tag to the message
1515  *      'R'     Adds the "replied" tag to the message
1516  *      'S'     Removes the "unread" tag from the message
1517  *
1518  * For each flag that is not present, the opposite action (add/remove)
1519  * is performed for the corresponding tags.
1520  *
1521  * Flags are identified as trailing components of the filename after a
1522  * sequence of ":2,".
1523  *
1524  * If there are multiple filenames associated with this message, the
1525  * flag is considered present if it appears in one or more
1526  * filenames. (That is, the flags from the multiple filenames are
1527  * combined with the logical OR operator.)
1528  *
1529  * A client can ensure that notmuch database tags remain synchronized
1530  * with maildir flags by calling this function after each call to
1531  * notmuch_database_add_message. See also
1532  * notmuch_message_tags_to_maildir_flags for synchronizing tag changes
1533  * back to maildir flags.
1534  */
1535 notmuch_status_t
1536 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message);
1537
1538 /**
1539  * Rename message filename(s) to encode tags as maildir flags.
1540  *
1541  * Specifically, for each filename corresponding to this message:
1542  *
1543  * If the filename is not in a maildir directory, do nothing.  (A
1544  * maildir directory is determined as a directory named "new" or
1545  * "cur".) Similarly, if the filename has invalid maildir info,
1546  * (repeated or outof-ASCII-order flag characters after ":2,"), then
1547  * do nothing.
1548  *
1549  * If the filename is in a maildir directory, rename the file so that
1550  * its filename ends with the sequence ":2," followed by zero or more
1551  * of the following single-character flags (in ASCII order):
1552  *
1553  *   'D' iff the message has the "draft" tag
1554  *   'F' iff the message has the "flagged" tag
1555  *   'P' iff the message has the "passed" tag
1556  *   'R' iff the message has the "replied" tag
1557  *   'S' iff the message does not have the "unread" tag
1558  *
1559  * Any existing flags unmentioned in the list above will be preserved
1560  * in the renaming.
1561  *
1562  * Also, if this filename is in a directory named "new", rename it to
1563  * be within the neighboring directory named "cur".
1564  *
1565  * A client can ensure that maildir filename flags remain synchronized
1566  * with notmuch database tags by calling this function after changing
1567  * tags, (after calls to notmuch_message_add_tag,
1568  * notmuch_message_remove_tag, or notmuch_message_freeze/
1569  * notmuch_message_thaw). See also notmuch_message_maildir_flags_to_tags
1570  * for synchronizing maildir flag changes back to tags.
1571  */
1572 notmuch_status_t
1573 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message);
1574
1575 /**
1576  * Freeze the current state of 'message' within the database.
1577  *
1578  * This means that changes to the message state, (via
1579  * notmuch_message_add_tag, notmuch_message_remove_tag, and
1580  * notmuch_message_remove_all_tags), will not be committed to the
1581  * database until the message is thawed with notmuch_message_thaw.
1582  *
1583  * Multiple calls to freeze/thaw are valid and these calls will
1584  * "stack". That is there must be as many calls to thaw as to freeze
1585  * before a message is actually thawed.
1586  *
1587  * The ability to do freeze/thaw allows for safe transactions to
1588  * change tag values. For example, explicitly setting a message to
1589  * have a given set of tags might look like this:
1590  *
1591  *    notmuch_message_freeze (message);
1592  *
1593  *    notmuch_message_remove_all_tags (message);
1594  *
1595  *    for (i = 0; i < NUM_TAGS; i++)
1596  *        notmuch_message_add_tag (message, tags[i]);
1597  *
1598  *    notmuch_message_thaw (message);
1599  *
1600  * With freeze/thaw used like this, the message in the database is
1601  * guaranteed to have either the full set of original tag values, or
1602  * the full set of new tag values, but nothing in between.
1603  *
1604  * Imagine the example above without freeze/thaw and the operation
1605  * somehow getting interrupted. This could result in the message being
1606  * left with no tags if the interruption happened after
1607  * notmuch_message_remove_all_tags but before notmuch_message_add_tag.
1608  *
1609  * Return value:
1610  *
1611  * NOTMUCH_STATUS_SUCCESS: Message successfully frozen.
1612  *
1613  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1614  *      mode so message cannot be modified.
1615  */
1616 notmuch_status_t
1617 notmuch_message_freeze (notmuch_message_t *message);
1618
1619 /**
1620  * Thaw the current 'message', synchronizing any changes that may have
1621  * occurred while 'message' was frozen into the notmuch database.
1622  *
1623  * See notmuch_message_freeze for an example of how to use this
1624  * function to safely provide tag changes.
1625  *
1626  * Multiple calls to freeze/thaw are valid and these calls with
1627  * "stack". That is there must be as many calls to thaw as to freeze
1628  * before a message is actually thawed.
1629  *
1630  * Return value:
1631  *
1632  * NOTMUCH_STATUS_SUCCESS: Message successfully thawed, (or at least
1633  *      its frozen count has successfully been reduced by 1).
1634  *
1635  * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: An attempt was made to thaw
1636  *      an unfrozen message. That is, there have been an unbalanced
1637  *      number of calls to notmuch_message_freeze and
1638  *      notmuch_message_thaw.
1639  */
1640 notmuch_status_t
1641 notmuch_message_thaw (notmuch_message_t *message);
1642
1643 /**
1644  * Destroy a notmuch_message_t object.
1645  *
1646  * It can be useful to call this function in the case of a single
1647  * query object with many messages in the result, (such as iterating
1648  * over the entire database). Otherwise, it's fine to never call this
1649  * function and there will still be no memory leaks. (The memory from
1650  * the messages get reclaimed when the containing query is destroyed.)
1651  */
1652 void
1653 notmuch_message_destroy (notmuch_message_t *message);
1654
1655 /**
1656  * Is the given 'tags' iterator pointing at a valid tag.
1657  *
1658  * When this function returns TRUE, notmuch_tags_get will return a
1659  * valid string. Whereas when this function returns FALSE,
1660  * notmuch_tags_get will return NULL.
1661  *
1662  * See the documentation of notmuch_message_get_tags for example code
1663  * showing how to iterate over a notmuch_tags_t object.
1664  */
1665 notmuch_bool_t
1666 notmuch_tags_valid (notmuch_tags_t *tags);
1667
1668 /**
1669  * Get the current tag from 'tags' as a string.
1670  *
1671  * Note: The returned string belongs to 'tags' and has a lifetime
1672  * identical to it (and the query to which it ultimately belongs).
1673  *
1674  * See the documentation of notmuch_message_get_tags for example code
1675  * showing how to iterate over a notmuch_tags_t object.
1676  */
1677 const char *
1678 notmuch_tags_get (notmuch_tags_t *tags);
1679
1680 /**
1681  * Move the 'tags' iterator to the next tag.
1682  *
1683  * If 'tags' is already pointing at the last tag then the iterator
1684  * will be moved to a point just beyond that last tag, (where
1685  * notmuch_tags_valid will return FALSE and notmuch_tags_get will
1686  * return NULL).
1687  *
1688  * See the documentation of notmuch_message_get_tags for example code
1689  * showing how to iterate over a notmuch_tags_t object.
1690  */
1691 void
1692 notmuch_tags_move_to_next (notmuch_tags_t *tags);
1693
1694 /**
1695  * Destroy a notmuch_tags_t object.
1696  *
1697  * It's not strictly necessary to call this function. All memory from
1698  * the notmuch_tags_t object will be reclaimed when the containing
1699  * message or query objects are destroyed.
1700  */
1701 void
1702 notmuch_tags_destroy (notmuch_tags_t *tags);
1703
1704 /**
1705  * Store an mtime within the database for 'directory'.
1706  *
1707  * The 'directory' should be an object retrieved from the database
1708  * with notmuch_database_get_directory for a particular path.
1709  *
1710  * The intention is for the caller to use the mtime to allow efficient
1711  * identification of new messages to be added to the database. The
1712  * recommended usage is as follows:
1713  *
1714  *   o Read the mtime of a directory from the filesystem
1715  *
1716  *   o Call add_message for all mail files in the directory
1717  *
1718  *   o Call notmuch_directory_set_mtime with the mtime read from the
1719  *     filesystem.
1720  *
1721  * Then, when wanting to check for updates to the directory in the
1722  * future, the client can call notmuch_directory_get_mtime and know
1723  * that it only needs to add files if the mtime of the directory and
1724  * files are newer than the stored timestamp.
1725  *
1726  * Note: The notmuch_directory_get_mtime function does not allow the
1727  * caller to distinguish a timestamp of 0 from a non-existent
1728  * timestamp. So don't store a timestamp of 0 unless you are
1729  * comfortable with that.
1730  *
1731  * Return value:
1732  *
1733  * NOTMUCH_STATUS_SUCCESS: mtime successfully stored in database.
1734  *
1735  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception
1736  *      occurred, mtime not stored.
1737  *
1738  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1739  *      mode so directory mtime cannot be modified.
1740  */
1741 notmuch_status_t
1742 notmuch_directory_set_mtime (notmuch_directory_t *directory,
1743                              time_t mtime);
1744
1745 /**
1746  * Get the mtime of a directory, (as previously stored with
1747  * notmuch_directory_set_mtime).
1748  *
1749  * Returns 0 if no mtime has previously been stored for this
1750  * directory.
1751  */
1752 time_t
1753 notmuch_directory_get_mtime (notmuch_directory_t *directory);
1754
1755 /**
1756  * Get a notmuch_filenames_t iterator listing all the filenames of
1757  * messages in the database within the given directory.
1758  *
1759  * The returned filenames will be the basename-entries only (not
1760  * complete paths).
1761  */
1762 notmuch_filenames_t *
1763 notmuch_directory_get_child_files (notmuch_directory_t *directory);
1764
1765 /**
1766  * Get a notmuch_filenames_t iterator listing all the filenames of
1767  * sub-directories in the database within the given directory.
1768  *
1769  * The returned filenames will be the basename-entries only (not
1770  * complete paths).
1771  */
1772 notmuch_filenames_t *
1773 notmuch_directory_get_child_directories (notmuch_directory_t *directory);
1774
1775 /**
1776  * Delete directory document from the database, and destroy the
1777  * notmuch_directory_t object. Assumes any child directories and files
1778  * have been deleted by the caller.
1779  *
1780  * @since libnotmuch 4.3 (notmuch 0.21)
1781  */
1782 notmuch_status_t
1783 notmuch_directory_delete (notmuch_directory_t *directory);
1784
1785 /**
1786  * Destroy a notmuch_directory_t object.
1787  */
1788 void
1789 notmuch_directory_destroy (notmuch_directory_t *directory);
1790
1791 /**
1792  * Is the given 'filenames' iterator pointing at a valid filename.
1793  *
1794  * When this function returns TRUE, notmuch_filenames_get will return
1795  * a valid string. Whereas when this function returns FALSE,
1796  * notmuch_filenames_get will return NULL.
1797  *
1798  * It is acceptable to pass NULL for 'filenames', in which case this
1799  * function will always return FALSE.
1800  */
1801 notmuch_bool_t
1802 notmuch_filenames_valid (notmuch_filenames_t *filenames);
1803
1804 /**
1805  * Get the current filename from 'filenames' as a string.
1806  *
1807  * Note: The returned string belongs to 'filenames' and has a lifetime
1808  * identical to it (and the directory to which it ultimately belongs).
1809  *
1810  * It is acceptable to pass NULL for 'filenames', in which case this
1811  * function will always return NULL.
1812  */
1813 const char *
1814 notmuch_filenames_get (notmuch_filenames_t *filenames);
1815
1816 /**
1817  * Move the 'filenames' iterator to the next filename.
1818  *
1819  * If 'filenames' is already pointing at the last filename then the
1820  * iterator will be moved to a point just beyond that last filename,
1821  * (where notmuch_filenames_valid will return FALSE and
1822  * notmuch_filenames_get will return NULL).
1823  *
1824  * It is acceptable to pass NULL for 'filenames', in which case this
1825  * function will do nothing.
1826  */
1827 void
1828 notmuch_filenames_move_to_next (notmuch_filenames_t *filenames);
1829
1830 /**
1831  * Destroy a notmuch_filenames_t object.
1832  *
1833  * It's not strictly necessary to call this function. All memory from
1834  * the notmuch_filenames_t object will be reclaimed when the
1835  * containing directory object is destroyed.
1836  *
1837  * It is acceptable to pass NULL for 'filenames', in which case this
1838  * function will do nothing.
1839  */
1840 void
1841 notmuch_filenames_destroy (notmuch_filenames_t *filenames);
1842
1843 /* @} */
1844
1845 NOTMUCH_END_DECLS
1846
1847 #endif