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