]> git.cworth.org Git - notmuch/blob - lib/notmuch.h
4f384e58881faa7f1a0bc08a40550f45585ac808
[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 #pragma GCC visibility push(default)
47
48 #ifndef FALSE
49 #define FALSE 0
50 #endif
51
52 #ifndef TRUE
53 #define TRUE 1
54 #endif
55
56 /*
57  * The library version number.  This must agree with the soname
58  * version in Makefile.local.
59  */
60 #define LIBNOTMUCH_MAJOR_VERSION        5
61 #define LIBNOTMUCH_MINOR_VERSION        3
62 #define LIBNOTMUCH_MICRO_VERSION        0
63
64
65 #if defined (__clang_major__) && __clang_major__ >= 3 \
66     || defined (__GNUC__) && __GNUC__ >= 5 \
67     || defined (__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ >= 5
68 #define NOTMUCH_DEPRECATED(major, minor) \
69     __attribute__ ((deprecated ("function deprecated as of libnotmuch " #major "." #minor)))
70 #else
71 #define NOTMUCH_DEPRECATED(major, minor) __attribute__ ((deprecated))
72 #endif
73
74
75 #endif /* __DOXYGEN__ */
76
77 /**
78  * Check the version of the notmuch library being compiled against.
79  *
80  * Return true if the library being compiled against is of the
81  * specified version or above. For example:
82  *
83  * @code
84  * #if LIBNOTMUCH_CHECK_VERSION(3, 1, 0)
85  *     (code requiring libnotmuch 3.1.0 or above)
86  * #endif
87  * @endcode
88  *
89  * LIBNOTMUCH_CHECK_VERSION has been defined since version 3.1.0; to
90  * check for versions prior to that, use:
91  *
92  * @code
93  * #if !defined(NOTMUCH_CHECK_VERSION)
94  *     (code requiring libnotmuch prior to 3.1.0)
95  * #endif
96  * @endcode
97  */
98 #define LIBNOTMUCH_CHECK_VERSION(major, minor, micro)                   \
99     (LIBNOTMUCH_MAJOR_VERSION > (major) ||                                      \
100      (LIBNOTMUCH_MAJOR_VERSION == (major) && LIBNOTMUCH_MINOR_VERSION > (minor)) || \
101      (LIBNOTMUCH_MAJOR_VERSION == (major) && LIBNOTMUCH_MINOR_VERSION == (minor) && \
102       LIBNOTMUCH_MICRO_VERSION >= (micro)))
103
104 /**
105  * Notmuch boolean type.
106  */
107 typedef int notmuch_bool_t;
108
109 /**
110  * Status codes used for the return values of most functions.
111  *
112  * A zero value (NOTMUCH_STATUS_SUCCESS) indicates that the function
113  * completed without error. Any other value indicates an error.
114  */
115 typedef enum _notmuch_status {
116     /**
117      * No error occurred.
118      */
119     NOTMUCH_STATUS_SUCCESS = 0,
120     /**
121      * Out of memory.
122      */
123     NOTMUCH_STATUS_OUT_OF_MEMORY,
124     /**
125      * An attempt was made to write to a database opened in read-only
126      * mode.
127      */
128     NOTMUCH_STATUS_READ_ONLY_DATABASE,
129     /**
130      * A Xapian exception occurred.
131      *
132      * @todo We don't really want to expose this lame XAPIAN_EXCEPTION
133      * value. Instead we should map to things like DATABASE_LOCKED or
134      * whatever.
135      */
136     NOTMUCH_STATUS_XAPIAN_EXCEPTION,
137     /**
138      * An error occurred trying to read or write to a file (this could
139      * be file not found, permission denied, etc.)
140      */
141     NOTMUCH_STATUS_FILE_ERROR,
142     /**
143      * A file was presented that doesn't appear to be an email
144      * message.
145      */
146     NOTMUCH_STATUS_FILE_NOT_EMAIL,
147     /**
148      * A file contains a message ID that is identical to a message
149      * already in the database.
150      */
151     NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID,
152     /**
153      * The user erroneously passed a NULL pointer to a notmuch
154      * function.
155      */
156     NOTMUCH_STATUS_NULL_POINTER,
157     /**
158      * A tag value is too long (exceeds NOTMUCH_TAG_MAX).
159      */
160     NOTMUCH_STATUS_TAG_TOO_LONG,
161     /**
162      * The notmuch_message_thaw function has been called more times
163      * than notmuch_message_freeze.
164      */
165     NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
166     /**
167      * notmuch_database_end_atomic has been called more times than
168      * notmuch_database_begin_atomic.
169      */
170     NOTMUCH_STATUS_UNBALANCED_ATOMIC,
171     /**
172      * The operation is not supported.
173      */
174     NOTMUCH_STATUS_UNSUPPORTED_OPERATION,
175     /**
176      * The operation requires a database upgrade.
177      */
178     NOTMUCH_STATUS_UPGRADE_REQUIRED,
179     /**
180      * There is a problem with the proposed path, e.g. a relative path
181      * passed to a function expecting an absolute path.
182      */
183     NOTMUCH_STATUS_PATH_ERROR,
184     /**
185      * The requested operation was ignored. Depending on the function,
186      * this may not be an actual error.
187      */
188     NOTMUCH_STATUS_IGNORED,
189     /**
190      * One of the arguments violates the preconditions for the
191      * function, in a way not covered by a more specific argument.
192      */
193     NOTMUCH_STATUS_ILLEGAL_ARGUMENT,
194     /**
195      * A MIME object claimed to have cryptographic protection which
196      * notmuch tried to handle, but the protocol was not specified in
197      * an intelligible way.
198      */
199     NOTMUCH_STATUS_MALFORMED_CRYPTO_PROTOCOL,
200     /**
201      * Notmuch attempted to do crypto processing, but could not
202      * initialize the engine needed to do so.
203      */
204     NOTMUCH_STATUS_FAILED_CRYPTO_CONTEXT_CREATION,
205     /**
206      * A MIME object claimed to have cryptographic protection, and
207      * notmuch attempted to process it, but the specific protocol was
208      * something that notmuch doesn't know how to handle.
209      */
210     NOTMUCH_STATUS_UNKNOWN_CRYPTO_PROTOCOL,
211     /**
212      * Unable to load a config file
213      */
214     NOTMUCH_STATUS_NO_CONFIG,
215     /**
216      * Not an actual status value. Just a way to find out how many
217      * valid status values there are.
218      */
219     NOTMUCH_STATUS_LAST_STATUS
220 } notmuch_status_t;
221
222 /**
223  * Get a string representation of a notmuch_status_t value.
224  *
225  * The result is read-only.
226  */
227 const char *
228 notmuch_status_to_string (notmuch_status_t status);
229
230 /* Various opaque data types. For each notmuch_<foo>_t see the various
231  * notmuch_<foo> functions below. */
232 #ifndef __DOXYGEN__
233 typedef struct _notmuch_database notmuch_database_t;
234 typedef struct _notmuch_query notmuch_query_t;
235 typedef struct _notmuch_threads notmuch_threads_t;
236 typedef struct _notmuch_thread notmuch_thread_t;
237 typedef struct _notmuch_messages notmuch_messages_t;
238 typedef struct _notmuch_message notmuch_message_t;
239 typedef struct _notmuch_tags notmuch_tags_t;
240 typedef struct _notmuch_directory notmuch_directory_t;
241 typedef struct _notmuch_filenames notmuch_filenames_t;
242 typedef struct _notmuch_config_list notmuch_config_list_t;
243 typedef struct _notmuch_config_values notmuch_config_values_t;
244 typedef struct _notmuch_indexopts notmuch_indexopts_t;
245 #endif /* __DOXYGEN__ */
246
247 /**
248  * Create a new, empty notmuch database located at 'path'.
249  *
250  * The path should be a top-level directory to a collection of
251  * plain-text email messages (one message per file). This call will
252  * create a new ".notmuch" directory within 'path' where notmuch will
253  * store its data.
254  *
255  * After a successful call to notmuch_database_create, the returned
256  * database will be open so the caller should call
257  * notmuch_database_destroy when finished with it.
258  *
259  * The database will not yet have any data in it
260  * (notmuch_database_create itself is a very cheap function). Messages
261  * contained within 'path' can be added to the database by calling
262  * notmuch_database_index_file.
263  *
264  * In case of any failure, this function returns an error status and
265  * sets *database to NULL (after printing an error message on stderr).
266  *
267  * Return value:
268  *
269  * NOTMUCH_STATUS_SUCCESS: Successfully created the database.
270  *
271  * NOTMUCH_STATUS_NULL_POINTER: The given 'path' argument is NULL.
272  *
273  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.
274  *
275  * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to create the
276  *      database file (such as permission denied, or file not found,
277  *      etc.), or the database already exists.
278  *
279  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.
280  */
281 notmuch_status_t
282 notmuch_database_create (const char *path, notmuch_database_t **database);
283
284 /**
285  * Like notmuch_database_create, except optionally return an error
286  * message. This message is allocated by malloc and should be freed by
287  * the caller.
288  */
289 notmuch_status_t
290 notmuch_database_create_verbose (const char *path,
291                                  notmuch_database_t **database,
292                                  char **error_message);
293
294 /**
295  * Database open mode for notmuch_database_open.
296  */
297 typedef enum {
298     /**
299      * Open database for reading only.
300      */
301     NOTMUCH_DATABASE_MODE_READ_ONLY = 0,
302     /**
303      * Open database for reading and writing.
304      */
305     NOTMUCH_DATABASE_MODE_READ_WRITE
306 } notmuch_database_mode_t;
307
308 /**
309  * Deprecated alias for notmuch_database_open_with_config with
310  * config_path=error_message=NULL
311  * @deprecated Deprecated as of libnotmuch 5.4 (notmuch 0.32)
312  */
313 /* NOTMUCH_DEPRECATED(5, 4) */
314 notmuch_status_t
315 notmuch_database_open (const char *path,
316                        notmuch_database_mode_t mode,
317                        notmuch_database_t **database);
318 /**
319  * Deprecated alias for notmuch_database_open_with_config with
320  * config_path=NULL
321  *
322  * @deprecated Deprecated as of libnotmuch 5.4 (notmuch 0.32)
323  *
324  */
325 /* NOTMUCH_DEPRECATED(5, 4) */
326 notmuch_status_t
327 notmuch_database_open_verbose (const char *path,
328                                notmuch_database_mode_t mode,
329                                notmuch_database_t **database,
330                                char **error_message);
331
332 /**
333  * Open an existing notmuch database located at 'database_path', using
334  * configuration in 'config_path'.
335  *
336  * @param[in]   database_path
337  * @parblock
338  * Path to existing database.
339  *
340  * A notmuch database is a Xapian database containing appropriate
341  * metadata.
342  *
343  * The database should have been created at some time in the past,
344  * (not necessarily by this process), by calling
345  * notmuch_database_create.
346  *
347  * If 'database_path' is NULL, use the location specified
348  *
349  * - in the environment variable NOTMUCH_DATABASE, if non-empty
350  *
351  * - in a configuration file, located as described under 'config_path'
352  *
353  * - by $XDG_DATA_HOME/notmuch/$PROFILE where XDG_DATA_HOME defaults
354  *   to "$HOME/.local/share" and PROFILE as as discussed in
355  *   'profile'
356  *
357  * If 'database_path' is non-NULL, but does not appear to be a Xapian
358  * database, check for a directory '.notmuch/xapian' below
359  * 'database_path' (this is the behavior of
360  * notmuch_database_open_verbose pre-0.32).
361  *
362  * @endparblock
363  * @param[in]   mode
364  * @parblock
365  * Mode to open database. Use one of #NOTMUCH_DATABASE_MODE_READ_WRITE
366  * or #NOTMUCH_DATABASE_MODE_READ_ONLY
367  *
368  * @endparblock
369  * @param[in]  config_path
370  * @parblock
371  * Path to config file.
372  *
373  * Config file is key-value, with mandatory sections. See
374  * <em>notmuch-config(5)</em> for more information. The key-value pair
375  * overrides the corresponding configuration data stored in the
376  * database (see <em>notmuch_database_get_config</em>)
377  *
378  * If <em>config_path</em> is NULL use the path specified
379  *
380  * - in environment variable <em>NOTMUCH_CONFIG</em>, if non-empty
381  *
382  * - by  <em>XDG_CONFIG_HOME</em>/notmuch/ where
383  *   XDG_CONFIG_HOME defaults to "$HOME/.config".
384  *
385  * - by $HOME/.notmuch-config
386  *
387  * If <em>config_path</em> is "" (empty string) then do not
388  * open any configuration file.
389  * @endparblock
390  * @param[in] profile:
391  * @parblock
392  * Name of profile (configuration/database variant).
393  *
394  * If non-NULL, append to the directory / file path determined for
395  * <em>config_path</em> and <em>database_path</em>.
396  *
397  * If NULL then use
398  * - environment variable NOTMUCH_PROFILE if defined,
399  * - otherwise "default" for directories and "" (empty string) for paths.
400  *
401  * @endparblock
402  * @param[out] database
403  * @parblock
404  * Pointer to database object. May not be NULL.
405  *
406  * The caller should call notmuch_database_destroy when finished with
407  * this database.
408  *
409  * In case of any failure, this function returns an error status and
410  * sets *database to NULL.
411  *
412  * @endparblock
413  * @param[out] error_message
414  * If non-NULL, store error message from opening the database.
415  * Any such message is allocated by \a malloc(3) and should be freed
416  * by the caller.
417  *
418  * @retval NOTMUCH_STATUS_SUCCESS: Successfully opened the database.
419  *
420  * @retval NOTMUCH_STATUS_NULL_POINTER: The given \a database
421  * argument is NULL.
422  *
423  * @retval NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.
424  *
425  * @retval NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to open the
426  *      database or config file (such as permission denied, or file not found,
427  *      etc.), or the database version is unknown.
428  *
429  * @retval NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.
430  *
431  * @since libnotmuch 5.4 (notmuch 0.32)
432  */
433
434 notmuch_status_t
435 notmuch_database_open_with_config (const char *database_path,
436                                    notmuch_database_mode_t mode,
437                                    const char *config_path,
438                                    const char *profile,
439                                    notmuch_database_t **database,
440                                    char **error_message);
441
442 /**
443  * Retrieve last status string for given database.
444  *
445  */
446 const char *
447 notmuch_database_status_string (const notmuch_database_t *notmuch);
448
449 /**
450  * Commit changes and close the given notmuch database.
451  *
452  * After notmuch_database_close has been called, calls to other
453  * functions on objects derived from this database may either behave
454  * as if the database had not been closed (e.g., if the required data
455  * has been cached) or may fail with a
456  * NOTMUCH_STATUS_XAPIAN_EXCEPTION. The only further operation
457  * permitted on the database itself is to call
458  * notmuch_database_destroy.
459  *
460  * notmuch_database_close can be called multiple times.  Later calls
461  * have no effect.
462  *
463  * For writable databases, notmuch_database_close commits all changes
464  * to disk before closing the database.  If the caller is currently in
465  * an atomic section (there was a notmuch_database_begin_atomic
466  * without a matching notmuch_database_end_atomic), this will discard
467  * changes made in that atomic section (but still commit changes made
468  * prior to entering the atomic section).
469  *
470  * Return value:
471  *
472  * NOTMUCH_STATUS_SUCCESS: Successfully closed the database.
473  *
474  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred; the
475  *      database has been closed but there are no guarantees the
476  *      changes to the database, if any, have been flushed to disk.
477  */
478 notmuch_status_t
479 notmuch_database_close (notmuch_database_t *database);
480
481 /**
482  * A callback invoked by notmuch_database_compact to notify the user
483  * of the progress of the compaction process.
484  */
485 typedef void (*notmuch_compact_status_cb_t)(const char *message, void *closure);
486
487 /**
488  * Compact a notmuch database, backing up the original database to the
489  * given path.
490  *
491  * The database will be opened with NOTMUCH_DATABASE_MODE_READ_WRITE
492  * during the compaction process to ensure no writes are made.
493  *
494  * If the optional callback function 'status_cb' is non-NULL, it will
495  * be called with diagnostic and informational messages. The argument
496  * 'closure' is passed verbatim to any callback invoked.
497  */
498 notmuch_status_t
499 notmuch_database_compact (const char *path,
500                           const char *backup_path,
501                           notmuch_compact_status_cb_t status_cb,
502                           void *closure);
503
504 /**
505  * Like notmuch_database_compact, but take an open database as a
506  * parameter.
507  *
508  * @since libnnotmuch 5.4 (notmuch 0.32)
509  */
510 notmuch_status_t
511 notmuch_database_compact_db (notmuch_database_t *database,
512                              const char *backup_path,
513                              notmuch_compact_status_cb_t status_cb,
514                              void *closure);
515
516 /**
517  * Destroy the notmuch database, closing it if necessary and freeing
518  * all associated resources.
519  *
520  * Return value as in notmuch_database_close if the database was open;
521  * notmuch_database_destroy itself has no failure modes.
522  */
523 notmuch_status_t
524 notmuch_database_destroy (notmuch_database_t *database);
525
526 /**
527  * Return the database path of the given database.
528  *
529  * The return value is a string owned by notmuch so should not be
530  * modified nor freed by the caller.
531  */
532 const char *
533 notmuch_database_get_path (notmuch_database_t *database);
534
535 /**
536  * Return the database format version of the given database.
537  *
538  * @retval 0 on error
539  */
540 unsigned int
541 notmuch_database_get_version (notmuch_database_t *database);
542
543 /**
544  * Can the database be upgraded to a newer database version?
545  *
546  * If this function returns TRUE, then the caller may call
547  * notmuch_database_upgrade to upgrade the database.  If the caller
548  * does not upgrade an out-of-date database, then some functions may
549  * fail with NOTMUCH_STATUS_UPGRADE_REQUIRED.  This always returns
550  * FALSE for a read-only database because there's no way to upgrade a
551  * read-only database.
552  *
553  * Also returns FALSE if an error occurs accessing the database.
554  *
555  */
556 notmuch_bool_t
557 notmuch_database_needs_upgrade (notmuch_database_t *database);
558
559 /**
560  * Upgrade the current database to the latest supported version.
561  *
562  * This ensures that all current notmuch functionality will be
563  * available on the database.  After opening a database in read-write
564  * mode, it is recommended that clients check if an upgrade is needed
565  * (notmuch_database_needs_upgrade) and if so, upgrade with this
566  * function before making any modifications.  If
567  * notmuch_database_needs_upgrade returns FALSE, this will be a no-op.
568  *
569  * The optional progress_notify callback can be used by the caller to
570  * provide progress indication to the user. If non-NULL it will be
571  * called periodically with 'progress' as a floating-point value in
572  * the range of [0.0 .. 1.0] indicating the progress made so far in
573  * the upgrade process.  The argument 'closure' is passed verbatim to
574  * any callback invoked.
575  */
576 notmuch_status_t
577 notmuch_database_upgrade (notmuch_database_t *database,
578                           void (*progress_notify)(void *closure,
579                                                   double progress),
580                           void *closure);
581
582 /**
583  * Begin an atomic database operation.
584  *
585  * Any modifications performed between a successful begin and a
586  * notmuch_database_end_atomic will be applied to the database
587  * atomically.  Note that, unlike a typical database transaction, this
588  * only ensures atomicity, not durability; neither begin nor end
589  * necessarily flush modifications to disk.
590  *
591  * Atomic sections may be nested.  begin_atomic and end_atomic must
592  * always be called in pairs.
593  *
594  * Return value:
595  *
596  * NOTMUCH_STATUS_SUCCESS: Successfully entered atomic section.
597  *
598  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
599  *      atomic section not entered.
600  */
601 notmuch_status_t
602 notmuch_database_begin_atomic (notmuch_database_t *notmuch);
603
604 /**
605  * Indicate the end of an atomic database operation.
606  *
607  * Return value:
608  *
609  * NOTMUCH_STATUS_SUCCESS: Successfully completed atomic section.
610  *
611  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
612  *      atomic section not ended.
613  *
614  * NOTMUCH_STATUS_UNBALANCED_ATOMIC: The database is not currently in
615  *      an atomic section.
616  */
617 notmuch_status_t
618 notmuch_database_end_atomic (notmuch_database_t *notmuch);
619
620 /**
621  * Return the committed database revision and UUID.
622  *
623  * The database revision number increases monotonically with each
624  * commit to the database.  Hence, all messages and message changes
625  * committed to the database (that is, visible to readers) have a last
626  * modification revision <= the committed database revision.  Any
627  * messages committed in the future will be assigned a modification
628  * revision > the committed database revision.
629  *
630  * The UUID is a NUL-terminated opaque string that uniquely identifies
631  * this database.  Two revision numbers are only comparable if they
632  * have the same database UUID.
633  */
634 unsigned long
635 notmuch_database_get_revision (notmuch_database_t *notmuch,
636                                const char **uuid);
637
638 /**
639  * Retrieve a directory object from the database for 'path'.
640  *
641  * Here, 'path' should be a path relative to the path of 'database'
642  * (see notmuch_database_get_path), or else should be an absolute path
643  * with initial components that match the path of 'database'.
644  *
645  * If this directory object does not exist in the database, this
646  * returns NOTMUCH_STATUS_SUCCESS and sets *directory to NULL.
647  *
648  * Otherwise the returned directory object is owned by the database
649  * and as such, will only be valid until notmuch_database_destroy is
650  * called.
651  *
652  * Return value:
653  *
654  * NOTMUCH_STATUS_SUCCESS: Successfully retrieved directory.
655  *
656  * NOTMUCH_STATUS_NULL_POINTER: The given 'directory' argument is NULL.
657  *
658  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
659  *      directory not retrieved.
660  *
661  * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the
662  *      database to use this function.
663  */
664 notmuch_status_t
665 notmuch_database_get_directory (notmuch_database_t *database,
666                                 const char *path,
667                                 notmuch_directory_t **directory);
668
669 /**
670  * Add a message file to a database, indexing it for retrieval by
671  * future searches.  If a message already exists with the same message
672  * ID as the specified file, their indexes will be merged, and this
673  * new filename will also be associated with the existing message.
674  *
675  * Here, 'filename' should be a path relative to the path of
676  * 'database' (see notmuch_database_get_path), or else should be an
677  * absolute filename with initial components that match the path of
678  * 'database'.
679  *
680  * The file should be a single mail message (not a multi-message mbox)
681  * that is expected to remain at its current location, (since the
682  * notmuch database will reference the filename, and will not copy the
683  * entire contents of the file.
684  *
685  * If another message with the same message ID already exists in the
686  * database, rather than creating a new message, this adds the search
687  * terms from the identified file to the existing message's index, and
688  * adds 'filename' to the list of filenames known for the message.
689  *
690  * The 'indexopts' parameter can be NULL (meaning, use the indexing
691  * defaults from the database), or can be an explicit choice of
692  * indexing options that should govern the indexing of this specific
693  * 'filename'.
694  *
695  * If 'message' is not NULL, then, on successful return
696  * (NOTMUCH_STATUS_SUCCESS or NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) '*message'
697  * will be initialized to a message object that can be used for things
698  * such as adding tags to the just-added message. The user should call
699  * notmuch_message_destroy when done with the message. On any failure
700  * '*message' will be set to NULL.
701  *
702  * Return value:
703  *
704  * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
705  *
706  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
707  *      message not added.
708  *
709  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message
710  *      ID as another message already in the database. The new
711  *      filename was successfully added to the message in the database
712  *      (if not already present) and the existing message is returned.
713  *
714  * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
715  *      file, (such as permission denied, or file not found,
716  *      etc.). Nothing added to the database.
717  *
718  * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
719  *      like an email message. Nothing added to the database.
720  *
721  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
722  *      mode so no message can be added.
723  *
724  * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the
725  *      database to use this function.
726  *
727  * @since libnotmuch 5.1 (notmuch 0.26)
728  */
729 notmuch_status_t
730 notmuch_database_index_file (notmuch_database_t *database,
731                              const char *filename,
732                              notmuch_indexopts_t *indexopts,
733                              notmuch_message_t **message);
734
735 /**
736  * Deprecated alias for notmuch_database_index_file called with
737  * NULL indexopts.
738  *
739  * @deprecated Deprecated as of libnotmuch 5.1 (notmuch 0.26). Please
740  * use notmuch_database_index_file instead.
741  *
742  */
743 NOTMUCH_DEPRECATED (5, 1)
744 notmuch_status_t
745 notmuch_database_add_message (notmuch_database_t *database,
746                               const char *filename,
747                               notmuch_message_t **message);
748
749 /**
750  * Remove a message filename from the given notmuch database. If the
751  * message has no more filenames, remove the message.
752  *
753  * If the same message (as determined by the message ID) is still
754  * available via other filenames, then the message will persist in the
755  * database for those filenames. When the last filename is removed for
756  * a particular message, the database content for that message will be
757  * entirely removed.
758  *
759  * Return value:
760  *
761  * NOTMUCH_STATUS_SUCCESS: The last filename was removed and the
762  *      message was removed from the database.
763  *
764  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
765  *      message not removed.
766  *
767  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: This filename was removed but
768  *      the message persists in the database with at least one other
769  *      filename.
770  *
771  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
772  *      mode so no message can be removed.
773  *
774  * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the
775  *      database to use this function.
776  */
777 notmuch_status_t
778 notmuch_database_remove_message (notmuch_database_t *database,
779                                  const char *filename);
780
781 /**
782  * Find a message with the given message_id.
783  *
784  * If a message with the given message_id is found then, on successful return
785  * (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to a message
786  * object.  The caller should call notmuch_message_destroy when done with the
787  * message.
788  *
789  * On any failure or when the message is not found, this function initializes
790  * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
791  * caller is supposed to check '*message' for NULL to find out whether the
792  * message with the given message_id was found.
793  *
794  * Return value:
795  *
796  * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'.
797  *
798  * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
799  *
800  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating message object
801  *
802  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
803  */
804 notmuch_status_t
805 notmuch_database_find_message (notmuch_database_t *database,
806                                const char *message_id,
807                                notmuch_message_t **message);
808
809 /**
810  * Find a message with the given filename.
811  *
812  * If the database contains a message with the given filename then, on
813  * successful return (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to
814  * a message object. The caller should call notmuch_message_destroy when done
815  * with the message.
816  *
817  * On any failure or when the message is not found, this function initializes
818  * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
819  * caller is supposed to check '*message' for NULL to find out whether the
820  * message with the given filename is found.
821  *
822  * Return value:
823  *
824  * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'
825  *
826  * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
827  *
828  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating the message object
829  *
830  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
831  *
832  * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the
833  *      database to use this function.
834  */
835 notmuch_status_t
836 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
837                                            const char *filename,
838                                            notmuch_message_t **message);
839
840 /**
841  * Return a list of all tags found in the database.
842  *
843  * This function creates a list of all tags found in the database. The
844  * resulting list contains all tags from all messages found in the database.
845  *
846  * On error this function returns NULL.
847  */
848 notmuch_tags_t *
849 notmuch_database_get_all_tags (notmuch_database_t *db);
850
851 /**
852  * Create a new query for 'database'.
853  *
854  * Here, 'database' should be an open database, (see
855  * notmuch_database_open and notmuch_database_create).
856  *
857  * For the query string, we'll document the syntax here more
858  * completely in the future, but it's likely to be a specialized
859  * version of the general Xapian query syntax:
860  *
861  * https://xapian.org/docs/queryparser.html
862  *
863  * As a special case, passing either a length-zero string, (that is ""),
864  * or a string consisting of a single asterisk (that is "*"), will
865  * result in a query that returns all messages in the database.
866  *
867  * See notmuch_query_set_sort for controlling the order of results.
868  * See notmuch_query_search_messages and notmuch_query_search_threads
869  * to actually execute the query.
870  *
871  * User should call notmuch_query_destroy when finished with this
872  * query.
873  *
874  * Will return NULL if insufficient memory is available.
875  */
876 notmuch_query_t *
877 notmuch_query_create (notmuch_database_t *database,
878                       const char *query_string);
879
880 /**
881  * Sort values for notmuch_query_set_sort.
882  */
883 typedef enum {
884     /**
885      * Oldest first.
886      */
887     NOTMUCH_SORT_OLDEST_FIRST,
888     /**
889      * Newest first.
890      */
891     NOTMUCH_SORT_NEWEST_FIRST,
892     /**
893      * Sort by message-id.
894      */
895     NOTMUCH_SORT_MESSAGE_ID,
896     /**
897      * Do not sort.
898      */
899     NOTMUCH_SORT_UNSORTED
900 } notmuch_sort_t;
901
902 /**
903  * Return the query_string of this query. See notmuch_query_create.
904  */
905 const char *
906 notmuch_query_get_query_string (const notmuch_query_t *query);
907
908 /**
909  * Return the notmuch database of this query. See notmuch_query_create.
910  */
911 notmuch_database_t *
912 notmuch_query_get_database (const notmuch_query_t *query);
913
914 /**
915  * Exclude values for notmuch_query_set_omit_excluded. The strange
916  * order is to maintain backward compatibility: the old FALSE/TRUE
917  * options correspond to the new
918  * NOTMUCH_EXCLUDE_FLAG/NOTMUCH_EXCLUDE_TRUE options.
919  */
920 typedef enum {
921     NOTMUCH_EXCLUDE_FLAG,
922     NOTMUCH_EXCLUDE_TRUE,
923     NOTMUCH_EXCLUDE_FALSE,
924     NOTMUCH_EXCLUDE_ALL
925 } notmuch_exclude_t;
926
927 /**
928  * Specify whether to omit excluded results or simply flag them.  By
929  * default, this is set to TRUE.
930  *
931  * If set to TRUE or ALL, notmuch_query_search_messages will omit excluded
932  * messages from the results, and notmuch_query_search_threads will omit
933  * threads that match only in excluded messages.  If set to TRUE,
934  * notmuch_query_search_threads will include all messages in threads that
935  * match in at least one non-excluded message.  Otherwise, if set to ALL,
936  * notmuch_query_search_threads will omit excluded messages from all threads.
937  *
938  * If set to FALSE or FLAG then both notmuch_query_search_messages and
939  * notmuch_query_search_threads will return all matching
940  * messages/threads regardless of exclude status. If set to FLAG then
941  * the exclude flag will be set for any excluded message that is
942  * returned by notmuch_query_search_messages, and the thread counts
943  * for threads returned by notmuch_query_search_threads will be the
944  * number of non-excluded messages/matches. Otherwise, if set to
945  * FALSE, then the exclude status is completely ignored.
946  *
947  * The performance difference when calling
948  * notmuch_query_search_messages should be relatively small (and both
949  * should be very fast).  However, in some cases,
950  * notmuch_query_search_threads is very much faster when omitting
951  * excluded messages as it does not need to construct the threads that
952  * only match in excluded messages.
953  */
954 void
955 notmuch_query_set_omit_excluded (notmuch_query_t *query,
956                                  notmuch_exclude_t omit_excluded);
957
958 /**
959  * Specify the sorting desired for this query.
960  */
961 void
962 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
963
964 /**
965  * Return the sort specified for this query. See
966  * notmuch_query_set_sort.
967  */
968 notmuch_sort_t
969 notmuch_query_get_sort (const notmuch_query_t *query);
970
971 /**
972  * Add a tag that will be excluded from the query results by default.
973  * This exclusion will be ignored if this tag appears explicitly in
974  * the query.
975  *
976  * @returns
977  *
978  * NOTMUCH_STATUS_SUCCESS: excluded was added successfully.
979  *
980  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occurred.
981  *      Most likely a problem lazily parsing the query string.
982  *
983  * NOTMUCH_STATUS_IGNORED: tag is explicitly present in the query, so
984  *              not excluded.
985  */
986 notmuch_status_t
987 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag);
988
989 /**
990  * Execute a query for threads, returning a notmuch_threads_t object
991  * which can be used to iterate over the results. The returned threads
992  * object is owned by the query and as such, will only be valid until
993  * notmuch_query_destroy.
994  *
995  * Typical usage might be:
996  *
997  *     notmuch_query_t *query;
998  *     notmuch_threads_t *threads;
999  *     notmuch_thread_t *thread;
1000  *     notmuch_status_t stat;
1001  *
1002  *     query = notmuch_query_create (database, query_string);
1003  *
1004  *     for (stat = notmuch_query_search_threads (query, &threads);
1005  *          stat == NOTMUCH_STATUS_SUCCESS &&
1006  *          notmuch_threads_valid (threads);
1007  *          notmuch_threads_move_to_next (threads))
1008  *     {
1009  *         thread = notmuch_threads_get (threads);
1010  *         ....
1011  *         notmuch_thread_destroy (thread);
1012  *     }
1013  *
1014  *     notmuch_query_destroy (query);
1015  *
1016  * Note: If you are finished with a thread before its containing
1017  * query, you can call notmuch_thread_destroy to clean up some memory
1018  * sooner (as in the above example). Otherwise, if your thread objects
1019  * are long-lived, then you don't need to call notmuch_thread_destroy
1020  * and all the memory will still be reclaimed when the query is
1021  * destroyed.
1022  *
1023  * Note that there's no explicit destructor needed for the
1024  * notmuch_threads_t object. (For consistency, we do provide a
1025  * notmuch_threads_destroy function, but there's no good reason
1026  * to call it if the query is about to be destroyed).
1027  *
1028  * @since libnotmuch 5.0 (notmuch 0.25)
1029  */
1030 notmuch_status_t
1031 notmuch_query_search_threads (notmuch_query_t *query,
1032                               notmuch_threads_t **out);
1033
1034 /**
1035  * Deprecated alias for notmuch_query_search_threads.
1036  *
1037  * @deprecated Deprecated as of libnotmuch 5 (notmuch 0.25). Please
1038  * use notmuch_query_search_threads instead.
1039  *
1040  */
1041 NOTMUCH_DEPRECATED (5, 0)
1042 notmuch_status_t
1043 notmuch_query_search_threads_st (notmuch_query_t *query, notmuch_threads_t **out);
1044
1045 /**
1046  * Execute a query for messages, returning a notmuch_messages_t object
1047  * which can be used to iterate over the results. The returned
1048  * messages object is owned by the query and as such, will only be
1049  * valid until notmuch_query_destroy.
1050  *
1051  * Typical usage might be:
1052  *
1053  *     notmuch_query_t *query;
1054  *     notmuch_messages_t *messages;
1055  *     notmuch_message_t *message;
1056  *
1057  *     query = notmuch_query_create (database, query_string);
1058  *
1059  *     for (messages = notmuch_query_search_messages (query);
1060  *          notmuch_messages_valid (messages);
1061  *          notmuch_messages_move_to_next (messages))
1062  *     {
1063  *         message = notmuch_messages_get (messages);
1064  *         ....
1065  *         notmuch_message_destroy (message);
1066  *     }
1067  *
1068  *     notmuch_query_destroy (query);
1069  *
1070  * Note: If you are finished with a message before its containing
1071  * query, you can call notmuch_message_destroy to clean up some memory
1072  * sooner (as in the above example). Otherwise, if your message
1073  * objects are long-lived, then you don't need to call
1074  * notmuch_message_destroy and all the memory will still be reclaimed
1075  * when the query is destroyed.
1076  *
1077  * Note that there's no explicit destructor needed for the
1078  * notmuch_messages_t object. (For consistency, we do provide a
1079  * notmuch_messages_destroy function, but there's no good
1080  * reason to call it if the query is about to be destroyed).
1081  *
1082  * If a Xapian exception occurs this function will return NULL.
1083  *
1084  * @since libnotmuch 5 (notmuch 0.25)
1085  */
1086 notmuch_status_t
1087 notmuch_query_search_messages (notmuch_query_t *query,
1088                                notmuch_messages_t **out);
1089 /**
1090  * Deprecated alias for notmuch_query_search_messages
1091  *
1092  * @deprecated Deprecated as of libnotmuch 5 (notmuch 0.25). Please use
1093  * notmuch_query_search_messages instead.
1094  *
1095  */
1096
1097 NOTMUCH_DEPRECATED (5, 0)
1098 notmuch_status_t
1099 notmuch_query_search_messages_st (notmuch_query_t *query,
1100                                   notmuch_messages_t **out);
1101
1102 /**
1103  * Destroy a notmuch_query_t along with any associated resources.
1104  *
1105  * This will in turn destroy any notmuch_threads_t and
1106  * notmuch_messages_t objects generated by this query, (and in
1107  * turn any notmuch_thread_t and notmuch_message_t objects generated
1108  * from those results, etc.), if such objects haven't already been
1109  * destroyed.
1110  */
1111 void
1112 notmuch_query_destroy (notmuch_query_t *query);
1113
1114 /**
1115  * Is the given 'threads' iterator pointing at a valid thread.
1116  *
1117  * When this function returns TRUE, notmuch_threads_get will return a
1118  * valid object. Whereas when this function returns FALSE,
1119  * notmuch_threads_get will return NULL.
1120  *
1121  * If passed a NULL pointer, this function returns FALSE
1122  *
1123  * See the documentation of notmuch_query_search_threads for example
1124  * code showing how to iterate over a notmuch_threads_t object.
1125  */
1126 notmuch_bool_t
1127 notmuch_threads_valid (notmuch_threads_t *threads);
1128
1129 /**
1130  * Get the current thread from 'threads' as a notmuch_thread_t.
1131  *
1132  * Note: The returned thread belongs to 'threads' and has a lifetime
1133  * identical to it (and the query to which it belongs).
1134  *
1135  * See the documentation of notmuch_query_search_threads for example
1136  * code showing how to iterate over a notmuch_threads_t object.
1137  *
1138  * If an out-of-memory situation occurs, this function will return
1139  * NULL.
1140  */
1141 notmuch_thread_t *
1142 notmuch_threads_get (notmuch_threads_t *threads);
1143
1144 /**
1145  * Move the 'threads' iterator to the next thread.
1146  *
1147  * If 'threads' is already pointing at the last thread then the
1148  * iterator will be moved to a point just beyond that last thread,
1149  * (where notmuch_threads_valid will return FALSE and
1150  * notmuch_threads_get will return NULL).
1151  *
1152  * See the documentation of notmuch_query_search_threads for example
1153  * code showing how to iterate over a notmuch_threads_t object.
1154  */
1155 void
1156 notmuch_threads_move_to_next (notmuch_threads_t *threads);
1157
1158 /**
1159  * Destroy a notmuch_threads_t object.
1160  *
1161  * It's not strictly necessary to call this function. All memory from
1162  * the notmuch_threads_t object will be reclaimed when the
1163  * containing query object is destroyed.
1164  */
1165 void
1166 notmuch_threads_destroy (notmuch_threads_t *threads);
1167
1168 /**
1169  * Return the number of messages matching a search.
1170  *
1171  * This function performs a search and returns the number of matching
1172  * messages.
1173  *
1174  * @returns
1175  *
1176  * NOTMUCH_STATUS_SUCCESS: query completed successfully.
1177  *
1178  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occurred. The
1179  *      value of *count is not defined.
1180  *
1181  * @since libnotmuch 5 (notmuch 0.25)
1182  */
1183 notmuch_status_t
1184 notmuch_query_count_messages (notmuch_query_t *query, unsigned int *count);
1185
1186 /**
1187  * Deprecated alias for notmuch_query_count_messages
1188  *
1189  *
1190  * @deprecated Deprecated since libnotmuch 5.0 (notmuch 0.25). Please
1191  * use notmuch_query_count_messages instead.
1192  */
1193 NOTMUCH_DEPRECATED (5, 0)
1194 notmuch_status_t
1195 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned int *count);
1196
1197 /**
1198  * Return the number of threads matching a search.
1199  *
1200  * This function performs a search and returns the number of unique thread IDs
1201  * in the matching messages. This is the same as number of threads matching a
1202  * search.
1203  *
1204  * Note that this is a significantly heavier operation than
1205  * notmuch_query_count_messages{_st}().
1206  *
1207  * @returns
1208  *
1209  * NOTMUCH_STATUS_OUT_OF_MEMORY: Memory allocation failed. The value
1210  *      of *count is not defined
1211  *
1212  * NOTMUCH_STATUS_SUCCESS: query completed successfully.
1213  *
1214  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occurred. The
1215  *      value of *count is not defined.
1216  *
1217  * @since libnotmuch 5 (notmuch 0.25)
1218  */
1219 notmuch_status_t
1220 notmuch_query_count_threads (notmuch_query_t *query, unsigned *count);
1221
1222 /**
1223  * Deprecated alias for notmuch_query_count_threads
1224  *
1225  * @deprecated Deprecated as of libnotmuch 5.0 (notmuch 0.25). Please
1226  * use notmuch_query_count_threads_st instead.
1227  */
1228 NOTMUCH_DEPRECATED (5, 0)
1229 notmuch_status_t
1230 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count);
1231
1232 /**
1233  * Get the thread ID of 'thread'.
1234  *
1235  * The returned string belongs to 'thread' and as such, should not be
1236  * modified by the caller and will only be valid for as long as the
1237  * thread is valid, (which is until notmuch_thread_destroy or until
1238  * the query from which it derived is destroyed).
1239  */
1240 const char *
1241 notmuch_thread_get_thread_id (notmuch_thread_t *thread);
1242
1243 /**
1244  * Get the total number of messages in 'thread'.
1245  *
1246  * This count consists of all messages in the database belonging to
1247  * this thread. Contrast with notmuch_thread_get_matched_messages() .
1248  */
1249 int
1250 notmuch_thread_get_total_messages (notmuch_thread_t *thread);
1251
1252 /**
1253  * Get the total number of files in 'thread'.
1254  *
1255  * This sums notmuch_message_count_files over all messages in the
1256  * thread
1257  * @returns Non-negative integer
1258  * @since libnotmuch 5.0 (notmuch 0.25)
1259  */
1260
1261 int
1262 notmuch_thread_get_total_files (notmuch_thread_t *thread);
1263
1264 /**
1265  * Get a notmuch_messages_t iterator for the top-level messages in
1266  * 'thread' in oldest-first order.
1267  *
1268  * This iterator will not necessarily iterate over all of the messages
1269  * in the thread. It will only iterate over the messages in the thread
1270  * which are not replies to other messages in the thread.
1271  *
1272  * The returned list will be destroyed when the thread is destroyed.
1273  */
1274 notmuch_messages_t *
1275 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread);
1276
1277 /**
1278  * Get a notmuch_thread_t iterator for all messages in 'thread' in
1279  * oldest-first order.
1280  *
1281  * The returned list will be destroyed when the thread is destroyed.
1282  */
1283 notmuch_messages_t *
1284 notmuch_thread_get_messages (notmuch_thread_t *thread);
1285
1286 /**
1287  * Get the number of messages in 'thread' that matched the search.
1288  *
1289  * This count includes only the messages in this thread that were
1290  * matched by the search from which the thread was created and were
1291  * not excluded by any exclude tags passed in with the query (see
1292  * notmuch_query_add_tag_exclude). Contrast with
1293  * notmuch_thread_get_total_messages() .
1294  */
1295 int
1296 notmuch_thread_get_matched_messages (notmuch_thread_t *thread);
1297
1298 /**
1299  * Get the authors of 'thread' as a UTF-8 string.
1300  *
1301  * The returned string is a comma-separated list of the names of the
1302  * authors of mail messages in the query results that belong to this
1303  * thread.
1304  *
1305  * The string contains authors of messages matching the query first, then
1306  * non-matched authors (with the two groups separated by '|'). Within
1307  * each group, authors are ordered by date.
1308  *
1309  * The returned string belongs to 'thread' and as such, should not be
1310  * modified by the caller and will only be valid for as long as the
1311  * thread is valid, (which is until notmuch_thread_destroy or until
1312  * the query from which it derived is destroyed).
1313  */
1314 const char *
1315 notmuch_thread_get_authors (notmuch_thread_t *thread);
1316
1317 /**
1318  * Get the subject of 'thread' as a UTF-8 string.
1319  *
1320  * The subject is taken from the first message (according to the query
1321  * order---see notmuch_query_set_sort) in the query results that
1322  * belongs to this thread.
1323  *
1324  * The returned string belongs to 'thread' and as such, should not be
1325  * modified by the caller and will only be valid for as long as the
1326  * thread is valid, (which is until notmuch_thread_destroy or until
1327  * the query from which it derived is destroyed).
1328  */
1329 const char *
1330 notmuch_thread_get_subject (notmuch_thread_t *thread);
1331
1332 /**
1333  * Get the date of the oldest message in 'thread' as a time_t value.
1334  */
1335 time_t
1336 notmuch_thread_get_oldest_date (notmuch_thread_t *thread);
1337
1338 /**
1339  * Get the date of the newest message in 'thread' as a time_t value.
1340  */
1341 time_t
1342 notmuch_thread_get_newest_date (notmuch_thread_t *thread);
1343
1344 /**
1345  * Get the tags for 'thread', returning a notmuch_tags_t object which
1346  * can be used to iterate over all tags.
1347  *
1348  * Note: In the Notmuch database, tags are stored on individual
1349  * messages, not on threads. So the tags returned here will be all
1350  * tags of the messages which matched the search and which belong to
1351  * this thread.
1352  *
1353  * The tags object is owned by the thread and as such, will only be
1354  * valid for as long as the thread is valid, (for example, until
1355  * notmuch_thread_destroy or until the query from which it derived is
1356  * destroyed).
1357  *
1358  * Typical usage might be:
1359  *
1360  *     notmuch_thread_t *thread;
1361  *     notmuch_tags_t *tags;
1362  *     const char *tag;
1363  *
1364  *     thread = notmuch_threads_get (threads);
1365  *
1366  *     for (tags = notmuch_thread_get_tags (thread);
1367  *          notmuch_tags_valid (tags);
1368  *          notmuch_tags_move_to_next (tags))
1369  *     {
1370  *         tag = notmuch_tags_get (tags);
1371  *         ....
1372  *     }
1373  *
1374  *     notmuch_thread_destroy (thread);
1375  *
1376  * Note that there's no explicit destructor needed for the
1377  * notmuch_tags_t object. (For consistency, we do provide a
1378  * notmuch_tags_destroy function, but there's no good reason to call
1379  * it if the message is about to be destroyed).
1380  */
1381 notmuch_tags_t *
1382 notmuch_thread_get_tags (notmuch_thread_t *thread);
1383
1384 /**
1385  * Destroy a notmuch_thread_t object.
1386  */
1387 void
1388 notmuch_thread_destroy (notmuch_thread_t *thread);
1389
1390 /**
1391  * Is the given 'messages' iterator pointing at a valid message.
1392  *
1393  * When this function returns TRUE, notmuch_messages_get will return a
1394  * valid object. Whereas when this function returns FALSE,
1395  * notmuch_messages_get will return NULL.
1396  *
1397  * See the documentation of notmuch_query_search_messages for example
1398  * code showing how to iterate over a notmuch_messages_t object.
1399  */
1400 notmuch_bool_t
1401 notmuch_messages_valid (notmuch_messages_t *messages);
1402
1403 /**
1404  * Get the current message from 'messages' as a notmuch_message_t.
1405  *
1406  * Note: The returned message belongs to 'messages' and has a lifetime
1407  * identical to it (and the query to which it belongs).
1408  *
1409  * See the documentation of notmuch_query_search_messages for example
1410  * code showing how to iterate over a notmuch_messages_t object.
1411  *
1412  * If an out-of-memory situation occurs, this function will return
1413  * NULL.
1414  */
1415 notmuch_message_t *
1416 notmuch_messages_get (notmuch_messages_t *messages);
1417
1418 /**
1419  * Move the 'messages' iterator to the next message.
1420  *
1421  * If 'messages' is already pointing at the last message then the
1422  * iterator will be moved to a point just beyond that last message,
1423  * (where notmuch_messages_valid will return FALSE and
1424  * notmuch_messages_get will return NULL).
1425  *
1426  * See the documentation of notmuch_query_search_messages for example
1427  * code showing how to iterate over a notmuch_messages_t object.
1428  */
1429 void
1430 notmuch_messages_move_to_next (notmuch_messages_t *messages);
1431
1432 /**
1433  * Destroy a notmuch_messages_t object.
1434  *
1435  * It's not strictly necessary to call this function. All memory from
1436  * the notmuch_messages_t object will be reclaimed when the containing
1437  * query object is destroyed.
1438  */
1439 void
1440 notmuch_messages_destroy (notmuch_messages_t *messages);
1441
1442 /**
1443  * Return a list of tags from all messages.
1444  *
1445  * The resulting list is guaranteed not to contain duplicated tags.
1446  *
1447  * WARNING: You can no longer iterate over messages after calling this
1448  * function, because the iterator will point at the end of the list.
1449  * We do not have a function to reset the iterator yet and the only
1450  * way how you can iterate over the list again is to recreate the
1451  * message list.
1452  *
1453  * The function returns NULL on error.
1454  */
1455 notmuch_tags_t *
1456 notmuch_messages_collect_tags (notmuch_messages_t *messages);
1457
1458 /**
1459  * Get the database associated with this message.
1460  *
1461  * @since libnotmuch 5.2 (notmuch 0.27)
1462  */
1463 notmuch_database_t *
1464 notmuch_message_get_database (const notmuch_message_t *message);
1465
1466 /**
1467  * Get the message ID of 'message'.
1468  *
1469  * The returned string belongs to 'message' and as such, should not be
1470  * modified by the caller and will only be valid for as long as the
1471  * message is valid, (which is until the query from which it derived
1472  * is destroyed).
1473  *
1474  * This function will return NULL if triggers an unhandled Xapian
1475  * exception.
1476  */
1477 const char *
1478 notmuch_message_get_message_id (notmuch_message_t *message);
1479
1480 /**
1481  * Get the thread ID of 'message'.
1482  *
1483  * The returned string belongs to 'message' and as such, should not be
1484  * modified by the caller and will only be valid for as long as the
1485  * message is valid, (for example, until the user calls
1486  * notmuch_message_destroy on 'message' or until a query from which it
1487  * derived is destroyed).
1488  *
1489  * This function will return NULL if triggers an unhandled Xapian
1490  * exception.
1491  */
1492 const char *
1493 notmuch_message_get_thread_id (notmuch_message_t *message);
1494
1495 /**
1496  * Get a notmuch_messages_t iterator for all of the replies to
1497  * 'message'.
1498  *
1499  * Note: This call only makes sense if 'message' was ultimately
1500  * obtained from a notmuch_thread_t object, (such as by coming
1501  * directly from the result of calling notmuch_thread_get_
1502  * toplevel_messages or by any number of subsequent
1503  * calls to notmuch_message_get_replies).
1504  *
1505  * If 'message' was obtained through some non-thread means, (such as
1506  * by a call to notmuch_query_search_messages), then this function
1507  * will return NULL.
1508  *
1509  * If there are no replies to 'message', this function will return
1510  * NULL. (Note that notmuch_messages_valid will accept that NULL
1511  * value as legitimate, and simply return FALSE for it.)
1512  *
1513  * This function also returns NULL if it triggers a Xapian exception.
1514  *
1515  * The returned list will be destroyed when the thread is
1516  * destroyed.
1517  */
1518 notmuch_messages_t *
1519 notmuch_message_get_replies (notmuch_message_t *message);
1520
1521 /**
1522  * Get the total number of files associated with a message.
1523  * @returns Non-negative integer for file count.
1524  * @returns Negative integer for error.
1525  * @since libnotmuch 5.0 (notmuch 0.25)
1526  */
1527 int
1528 notmuch_message_count_files (notmuch_message_t *message);
1529
1530 /**
1531  * Get a filename for the email corresponding to 'message'.
1532  *
1533  * The returned filename is an absolute filename, (the initial
1534  * component will match notmuch_database_get_path() ).
1535  *
1536  * The returned string belongs to the message so should not be
1537  * modified or freed by the caller (nor should it be referenced after
1538  * the message is destroyed).
1539  *
1540  * Note: If this message corresponds to multiple files in the mail
1541  * store, (that is, multiple files contain identical message IDs),
1542  * this function will arbitrarily return a single one of those
1543  * filenames. See notmuch_message_get_filenames for returning the
1544  * complete list of filenames.
1545  *
1546  * This function returns NULL if it triggers a Xapian exception.
1547  */
1548 const char *
1549 notmuch_message_get_filename (notmuch_message_t *message);
1550
1551 /**
1552  * Get all filenames for the email corresponding to 'message'.
1553  *
1554  * Returns a notmuch_filenames_t iterator listing all the filenames
1555  * associated with 'message'. These files may not have identical
1556  * content, but each will have the identical Message-ID.
1557  *
1558  * Each filename in the iterator is an absolute filename, (the initial
1559  * component will match notmuch_database_get_path() ).
1560  *
1561  * This function returns NULL if it triggers a Xapian exception.
1562  */
1563 notmuch_filenames_t *
1564 notmuch_message_get_filenames (notmuch_message_t *message);
1565
1566 /**
1567  * Re-index the e-mail corresponding to 'message' using the supplied index options
1568  *
1569  * Returns the status of the re-index operation.  (see the return
1570  * codes documented in notmuch_database_index_file)
1571  *
1572  * After reindexing, the user should discard the message object passed
1573  * in here by calling notmuch_message_destroy, since it refers to the
1574  * original message, not to the reindexed message.
1575  */
1576 notmuch_status_t
1577 notmuch_message_reindex (notmuch_message_t *message,
1578                          notmuch_indexopts_t *indexopts);
1579
1580 /**
1581  * Message flags.
1582  */
1583 typedef enum _notmuch_message_flag {
1584     NOTMUCH_MESSAGE_FLAG_MATCH,
1585     NOTMUCH_MESSAGE_FLAG_EXCLUDED,
1586
1587     /* This message is a "ghost message", meaning it has no filenames
1588      * or content, but we know it exists because it was referenced by
1589      * some other message.  A ghost message has only a message ID and
1590      * thread ID.
1591      */
1592     NOTMUCH_MESSAGE_FLAG_GHOST,
1593 } notmuch_message_flag_t;
1594
1595 /**
1596  * Get a value of a flag for the email corresponding to 'message'.
1597  *
1598  * returns FALSE in case of errors.
1599  *
1600  * @deprecated Deprecated as of libnotmuch 5.3 (notmuch 0.31). Please
1601  * use notmuch_message_get_flag_st instead.
1602  */
1603 NOTMUCH_DEPRECATED(5,3)
1604 notmuch_bool_t
1605 notmuch_message_get_flag (notmuch_message_t *message,
1606                           notmuch_message_flag_t flag);
1607
1608 /**
1609  * Get a value of a flag for the email corresponding to 'message'.
1610  *
1611  * @param message a message object
1612  * @param flag flag to check
1613  * @param is_set pointer to boolean to store flag value.
1614  *
1615  * @retval #NOTMUCH_STATUS_SUCCESS
1616  * @retval #NOTMUCH_STATUS_NULL_POINTER is_set is NULL
1617  * @retval #NOTMUCH_STATUS_XAPIAN_EXCEPTION Accessing the database
1618  * triggered an exception.
1619  *
1620  * @since libnotmuch 5.3 (notmuch 0.31)
1621  */
1622 notmuch_status_t
1623 notmuch_message_get_flag_st (notmuch_message_t *message,
1624                              notmuch_message_flag_t flag,
1625                              notmuch_bool_t *is_set);
1626
1627 /**
1628  * Set a value of a flag for the email corresponding to 'message'.
1629  */
1630 void
1631 notmuch_message_set_flag (notmuch_message_t *message,
1632                           notmuch_message_flag_t flag, notmuch_bool_t value);
1633
1634 /**
1635  * Get the date of 'message' as a time_t value.
1636  *
1637  * For the original textual representation of the Date header from the
1638  * message call notmuch_message_get_header() with a header value of
1639  * "date".
1640  *
1641  * Returns 0 in case of error.
1642  */
1643 time_t
1644 notmuch_message_get_date (notmuch_message_t *message);
1645
1646 /**
1647  * Get the value of the specified header from 'message' as a UTF-8 string.
1648  *
1649  * Common headers are stored in the database when the message is
1650  * indexed and will be returned from the database.  Other headers will
1651  * be read from the actual message file.
1652  *
1653  * The header name is case insensitive.
1654  *
1655  * The returned string belongs to the message so should not be
1656  * modified or freed by the caller (nor should it be referenced after
1657  * the message is destroyed).
1658  *
1659  * Returns an empty string ("") if the message does not contain a
1660  * header line matching 'header'. Returns NULL if any error occurs.
1661  */
1662 const char *
1663 notmuch_message_get_header (notmuch_message_t *message, const char *header);
1664
1665 /**
1666  * Get the tags for 'message', returning a notmuch_tags_t object which
1667  * can be used to iterate over all tags.
1668  *
1669  * The tags object is owned by the message and as such, will only be
1670  * valid for as long as the message is valid, (which is until the
1671  * query from which it derived is destroyed).
1672  *
1673  * Typical usage might be:
1674  *
1675  *     notmuch_message_t *message;
1676  *     notmuch_tags_t *tags;
1677  *     const char *tag;
1678  *
1679  *     message = notmuch_database_find_message (database, message_id);
1680  *
1681  *     for (tags = notmuch_message_get_tags (message);
1682  *          notmuch_tags_valid (tags);
1683  *          notmuch_tags_move_to_next (tags))
1684  *     {
1685  *         tag = notmuch_tags_get (tags);
1686  *         ....
1687  *     }
1688  *
1689  *     notmuch_message_destroy (message);
1690  *
1691  * Note that there's no explicit destructor needed for the
1692  * notmuch_tags_t object. (For consistency, we do provide a
1693  * notmuch_tags_destroy function, but there's no good reason to call
1694  * it if the message is about to be destroyed).
1695  */
1696 notmuch_tags_t *
1697 notmuch_message_get_tags (notmuch_message_t *message);
1698
1699 /**
1700  * The longest possible tag value.
1701  */
1702 #define NOTMUCH_TAG_MAX 200
1703
1704 /**
1705  * Add a tag to the given message.
1706  *
1707  * Return value:
1708  *
1709  * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
1710  *
1711  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
1712  *
1713  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
1714  *      (exceeds NOTMUCH_TAG_MAX)
1715  *
1716  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1717  *      mode so message cannot be modified.
1718  */
1719 notmuch_status_t
1720 notmuch_message_add_tag (notmuch_message_t *message, const char *tag);
1721
1722 /**
1723  * Remove a tag from the given message.
1724  *
1725  * Return value:
1726  *
1727  * NOTMUCH_STATUS_SUCCESS: Tag successfully removed from message
1728  *
1729  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
1730  *
1731  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
1732  *      (exceeds NOTMUCH_TAG_MAX)
1733  *
1734  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1735  *      mode so message cannot be modified.
1736  */
1737 notmuch_status_t
1738 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag);
1739
1740 /**
1741  * Remove all tags from the given message.
1742  *
1743  * See notmuch_message_freeze for an example showing how to safely
1744  * replace tag values.
1745  *
1746  * @retval #NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in
1747  *      read-only mode so message cannot be modified.
1748  * @retval #NOTMUCH_STATUS_XAPIAN_EXCEPTION: an exception was thrown
1749  *      accessing the database.
1750  */
1751 notmuch_status_t
1752 notmuch_message_remove_all_tags (notmuch_message_t *message);
1753
1754 /**
1755  * Add/remove tags according to maildir flags in the message filename(s).
1756  *
1757  * This function examines the filenames of 'message' for maildir
1758  * flags, and adds or removes tags on 'message' as follows when these
1759  * flags are present:
1760  *
1761  *      Flag    Action if present
1762  *      ----    -----------------
1763  *      'D'     Adds the "draft" tag to the message
1764  *      'F'     Adds the "flagged" tag to the message
1765  *      'P'     Adds the "passed" tag to the message
1766  *      'R'     Adds the "replied" tag to the message
1767  *      'S'     Removes the "unread" tag from the message
1768  *
1769  * For each flag that is not present, the opposite action (add/remove)
1770  * is performed for the corresponding tags.
1771  *
1772  * Flags are identified as trailing components of the filename after a
1773  * sequence of ":2,".
1774  *
1775  * If there are multiple filenames associated with this message, the
1776  * flag is considered present if it appears in one or more
1777  * filenames. (That is, the flags from the multiple filenames are
1778  * combined with the logical OR operator.)
1779  *
1780  * A client can ensure that notmuch database tags remain synchronized
1781  * with maildir flags by calling this function after each call to
1782  * notmuch_database_index_file. See also
1783  * notmuch_message_tags_to_maildir_flags for synchronizing tag changes
1784  * back to maildir flags.
1785  */
1786 notmuch_status_t
1787 notmuch_message_maildir_flags_to_tags (notmuch_message_t *message);
1788
1789 /**
1790  * return TRUE if any filename of 'message' has maildir flag 'flag',
1791  * FALSE otherwise.
1792  *
1793  * Deprecated wrapper for notmuch_message_has_maildir_flag_st
1794  *
1795  * @returns FALSE in case of error
1796  * @deprecated libnotmuch 5.3 (notmuch 0.31)
1797  */
1798 NOTMUCH_DEPRECATED(5, 3)
1799 notmuch_bool_t
1800 notmuch_message_has_maildir_flag (notmuch_message_t *message, char flag);
1801
1802 /**
1803  * check message for maildir flag
1804  *
1805  * @param [in,out]      message message to check
1806  * @param [in] flag     flag to check for
1807  * @param [out] is_set  pointer to boolean
1808  *
1809  * @retval #NOTMUCH_STATUS_SUCCESS
1810  * @retval #NOTMUCH_STATUS_NULL_POINTER is_set is NULL
1811  * @retval #NOTMUCH_STATUS_XAPIAN_EXCEPTION Accessing the database
1812  * triggered an exception.
1813  */
1814 notmuch_status_t
1815 notmuch_message_has_maildir_flag_st (notmuch_message_t *message,
1816                                      char flag,
1817                                      notmuch_bool_t *is_set);
1818
1819 /**
1820  * Rename message filename(s) to encode tags as maildir flags.
1821  *
1822  * Specifically, for each filename corresponding to this message:
1823  *
1824  * If the filename is not in a maildir directory, do nothing.  (A
1825  * maildir directory is determined as a directory named "new" or
1826  * "cur".) Similarly, if the filename has invalid maildir info,
1827  * (repeated or outof-ASCII-order flag characters after ":2,"), then
1828  * do nothing.
1829  *
1830  * If the filename is in a maildir directory, rename the file so that
1831  * its filename ends with the sequence ":2," followed by zero or more
1832  * of the following single-character flags (in ASCII order):
1833  *
1834  *   * flag 'D' iff the message has the "draft" tag
1835  *   * flag 'F' iff the message has the "flagged" tag
1836  *   * flag 'P' iff the message has the "passed" tag
1837  *   * flag 'R' iff the message has the "replied" tag
1838  *   * flag 'S' iff the message does not have the "unread" tag
1839  *
1840  * Any existing flags unmentioned in the list above will be preserved
1841  * in the renaming.
1842  *
1843  * Also, if this filename is in a directory named "new", rename it to
1844  * be within the neighboring directory named "cur".
1845  *
1846  * A client can ensure that maildir filename flags remain synchronized
1847  * with notmuch database tags by calling this function after changing
1848  * tags, (after calls to notmuch_message_add_tag,
1849  * notmuch_message_remove_tag, or notmuch_message_freeze/
1850  * notmuch_message_thaw). See also notmuch_message_maildir_flags_to_tags
1851  * for synchronizing maildir flag changes back to tags.
1852  */
1853 notmuch_status_t
1854 notmuch_message_tags_to_maildir_flags (notmuch_message_t *message);
1855
1856 /**
1857  * Freeze the current state of 'message' within the database.
1858  *
1859  * This means that changes to the message state, (via
1860  * notmuch_message_add_tag, notmuch_message_remove_tag, and
1861  * notmuch_message_remove_all_tags), will not be committed to the
1862  * database until the message is thawed with notmuch_message_thaw.
1863  *
1864  * Multiple calls to freeze/thaw are valid and these calls will
1865  * "stack". That is there must be as many calls to thaw as to freeze
1866  * before a message is actually thawed.
1867  *
1868  * The ability to do freeze/thaw allows for safe transactions to
1869  * change tag values. For example, explicitly setting a message to
1870  * have a given set of tags might look like this:
1871  *
1872  *    notmuch_message_freeze (message);
1873  *
1874  *    notmuch_message_remove_all_tags (message);
1875  *
1876  *    for (i = 0; i < NUM_TAGS; i++)
1877  *        notmuch_message_add_tag (message, tags[i]);
1878  *
1879  *    notmuch_message_thaw (message);
1880  *
1881  * With freeze/thaw used like this, the message in the database is
1882  * guaranteed to have either the full set of original tag values, or
1883  * the full set of new tag values, but nothing in between.
1884  *
1885  * Imagine the example above without freeze/thaw and the operation
1886  * somehow getting interrupted. This could result in the message being
1887  * left with no tags if the interruption happened after
1888  * notmuch_message_remove_all_tags but before notmuch_message_add_tag.
1889  *
1890  * Return value:
1891  *
1892  * NOTMUCH_STATUS_SUCCESS: Message successfully frozen.
1893  *
1894  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
1895  *      mode so message cannot be modified.
1896  */
1897 notmuch_status_t
1898 notmuch_message_freeze (notmuch_message_t *message);
1899
1900 /**
1901  * Thaw the current 'message', synchronizing any changes that may have
1902  * occurred while 'message' was frozen into the notmuch database.
1903  *
1904  * See notmuch_message_freeze for an example of how to use this
1905  * function to safely provide tag changes.
1906  *
1907  * Multiple calls to freeze/thaw are valid and these calls with
1908  * "stack". That is there must be as many calls to thaw as to freeze
1909  * before a message is actually thawed.
1910  *
1911  * Return value:
1912  *
1913  * NOTMUCH_STATUS_SUCCESS: Message successfully thawed, (or at least
1914  *      its frozen count has successfully been reduced by 1).
1915  *
1916  * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: An attempt was made to thaw
1917  *      an unfrozen message. That is, there have been an unbalanced
1918  *      number of calls to notmuch_message_freeze and
1919  *      notmuch_message_thaw.
1920  */
1921 notmuch_status_t
1922 notmuch_message_thaw (notmuch_message_t *message);
1923
1924 /**
1925  * Destroy a notmuch_message_t object.
1926  *
1927  * It can be useful to call this function in the case of a single
1928  * query object with many messages in the result, (such as iterating
1929  * over the entire database). Otherwise, it's fine to never call this
1930  * function and there will still be no memory leaks. (The memory from
1931  * the messages get reclaimed when the containing query is destroyed.)
1932  */
1933 void
1934 notmuch_message_destroy (notmuch_message_t *message);
1935
1936 /**
1937  * @name Message Properties
1938  *
1939  * This interface provides the ability to attach arbitrary (key,value)
1940  * string pairs to a message, to remove such pairs, and to iterate
1941  * over them.  The caller should take some care as to what keys they
1942  * add or delete values for, as other subsystems or extensions may
1943  * depend on these properties.
1944  *
1945  * Please see notmuch-properties(7) for more details about specific
1946  * properties and conventions around their use.
1947  *
1948  */
1949 /**@{*/
1950 /**
1951  * Retrieve the value for a single property key
1952  *
1953  * *value* is set to a string owned by the message or NULL if there is
1954  * no such key. In the case of multiple values for the given key, the
1955  * first one is retrieved.
1956  *
1957  * @returns
1958  * - NOTMUCH_STATUS_NULL_POINTER: *value* may not be NULL.
1959  * - NOTMUCH_STATUS_SUCCESS: No error occurred.
1960  * @since libnotmuch 4.4 (notmuch 0.23)
1961  */
1962 notmuch_status_t
1963 notmuch_message_get_property (notmuch_message_t *message, const char *key, const char **value);
1964
1965 /**
1966  * Add a (key,value) pair to a message
1967  *
1968  * @returns
1969  * - NOTMUCH_STATUS_ILLEGAL_ARGUMENT: *key* may not contain an '=' character.
1970  * - NOTMUCH_STATUS_NULL_POINTER: Neither *key* nor *value* may be NULL.
1971  * - NOTMUCH_STATUS_SUCCESS: No error occurred.
1972  * @since libnotmuch 4.4 (notmuch 0.23)
1973  */
1974 notmuch_status_t
1975 notmuch_message_add_property (notmuch_message_t *message, const char *key, const char *value);
1976
1977 /**
1978  * Remove a (key,value) pair from a message.
1979  *
1980  * It is not an error to remove a non-existent (key,value) pair
1981  *
1982  * @returns
1983  * - NOTMUCH_STATUS_ILLEGAL_ARGUMENT: *key* may not contain an '=' character.
1984  * - NOTMUCH_STATUS_NULL_POINTER: Neither *key* nor *value* may be NULL.
1985  * - NOTMUCH_STATUS_SUCCESS: No error occurred.
1986  * @since libnotmuch 4.4 (notmuch 0.23)
1987  */
1988 notmuch_status_t
1989 notmuch_message_remove_property (notmuch_message_t *message, const char *key, const char *value);
1990
1991 /**
1992  * Remove all (key,value) pairs from the given message.
1993  *
1994  * @param[in,out] message  message to operate on.
1995  * @param[in]     key      key to delete properties for. If NULL, delete
1996  *                         properties for all keys
1997  * @returns
1998  * - NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in
1999  *   read-only mode so message cannot be modified.
2000  * - NOTMUCH_STATUS_SUCCESS: No error occurred.
2001  *
2002  * @since libnotmuch 4.4 (notmuch 0.23)
2003  */
2004 notmuch_status_t
2005 notmuch_message_remove_all_properties (notmuch_message_t *message, const char *key);
2006
2007 /**
2008  * Remove all (prefix*,value) pairs from the given message
2009  *
2010  * @param[in,out] message  message to operate on.
2011  * @param[in]     prefix   delete properties with keys that start with prefix.
2012  *                         If NULL, delete all properties
2013  * @returns
2014  * - NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in
2015  *   read-only mode so message cannot be modified.
2016  * - NOTMUCH_STATUS_SUCCESS: No error occurred.
2017  *
2018  * @since libnotmuch 5.1 (notmuch 0.26)
2019  */
2020 notmuch_status_t
2021 notmuch_message_remove_all_properties_with_prefix (notmuch_message_t *message, const char *prefix);
2022
2023 /**
2024  * Opaque message property iterator
2025  */
2026 typedef struct _notmuch_string_map_iterator notmuch_message_properties_t;
2027
2028 /**
2029  * Get the properties for *message*, returning a
2030  * notmuch_message_properties_t object which can be used to iterate
2031  * over all properties.
2032  *
2033  * The notmuch_message_properties_t object is owned by the message and
2034  * as such, will only be valid for as long as the message is valid,
2035  * (which is until the query from which it derived is destroyed).
2036  *
2037  * @param[in] message  The message to examine
2038  * @param[in] key      key or key prefix
2039  * @param[in] exact    if TRUE, require exact match with key. Otherwise
2040  *                     treat as prefix.
2041  *
2042  * Typical usage might be:
2043  *
2044  *     notmuch_message_properties_t *list;
2045  *
2046  *     for (list = notmuch_message_get_properties (message, "testkey1", TRUE);
2047  *          notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
2048  *        printf("%s\n", notmuch_message_properties_value(list));
2049  *     }
2050  *
2051  *     notmuch_message_properties_destroy (list);
2052  *
2053  * Note that there's no explicit destructor needed for the
2054  * notmuch_message_properties_t object. (For consistency, we do
2055  * provide a notmuch_message_properities_destroy function, but there's
2056  * no good reason to call it if the message is about to be destroyed).
2057  *
2058  * @since libnotmuch 4.4 (notmuch 0.23)
2059  */
2060 notmuch_message_properties_t *
2061 notmuch_message_get_properties (notmuch_message_t *message, const char *key, notmuch_bool_t exact);
2062
2063 /**
2064  * Return the number of properties named "key" belonging to the specific message.
2065  *
2066  * @param[in] message  The message to examine
2067  * @param[in] key      key to count
2068  * @param[out] count   The number of matching properties associated with this message.
2069  *
2070  * @returns
2071  *
2072  * NOTMUCH_STATUS_SUCCESS: successful count, possibly some other error.
2073  *
2074  * @since libnotmuch 5.2 (notmuch 0.27)
2075  */
2076 notmuch_status_t
2077 notmuch_message_count_properties (notmuch_message_t *message, const char *key, unsigned int *count);
2078
2079 /**
2080  * Is the given *properties* iterator pointing at a valid (key,value)
2081  * pair.
2082  *
2083  * When this function returns TRUE,
2084  * notmuch_message_properties_{key,value} will return a valid string,
2085  * and notmuch_message_properties_move_to_next will do what it
2086  * says. Whereas when this function returns FALSE, calling any of
2087  * these functions results in undefined behaviour.
2088  *
2089  * See the documentation of notmuch_message_get_properties for example
2090  * code showing how to iterate over a notmuch_message_properties_t
2091  * object.
2092  *
2093  * @since libnotmuch 4.4 (notmuch 0.23)
2094  */
2095 notmuch_bool_t
2096 notmuch_message_properties_valid (notmuch_message_properties_t *properties);
2097
2098 /**
2099  * Move the *properties* iterator to the next (key,value) pair
2100  *
2101  * If *properties* is already pointing at the last pair then the iterator
2102  * will be moved to a point just beyond that last pair, (where
2103  * notmuch_message_properties_valid will return FALSE).
2104  *
2105  * See the documentation of notmuch_message_get_properties for example
2106  * code showing how to iterate over a notmuch_message_properties_t object.
2107  *
2108  * @since libnotmuch 4.4 (notmuch 0.23)
2109  */
2110 void
2111 notmuch_message_properties_move_to_next (notmuch_message_properties_t *properties);
2112
2113 /**
2114  * Return the key from the current (key,value) pair.
2115  *
2116  * this could be useful if iterating for a prefix
2117  *
2118  * @since libnotmuch 4.4 (notmuch 0.23)
2119  */
2120 const char *
2121 notmuch_message_properties_key (notmuch_message_properties_t *properties);
2122
2123 /**
2124  * Return the value from the current (key,value) pair.
2125  *
2126  * This could be useful if iterating for a prefix.
2127  *
2128  * @since libnotmuch 4.4 (notmuch 0.23)
2129  */
2130 const char *
2131 notmuch_message_properties_value (notmuch_message_properties_t *properties);
2132
2133
2134 /**
2135  * Destroy a notmuch_message_properties_t object.
2136  *
2137  * It's not strictly necessary to call this function. All memory from
2138  * the notmuch_message_properties_t object will be reclaimed when the
2139  * containing message object is destroyed.
2140  *
2141  * @since libnotmuch 4.4 (notmuch 0.23)
2142  */
2143 void
2144 notmuch_message_properties_destroy (notmuch_message_properties_t *properties);
2145
2146 /**@}*/
2147
2148 /**
2149  * Is the given 'tags' iterator pointing at a valid tag.
2150  *
2151  * When this function returns TRUE, notmuch_tags_get will return a
2152  * valid string. Whereas when this function returns FALSE,
2153  * notmuch_tags_get will return NULL.
2154  *
2155  * See the documentation of notmuch_message_get_tags for example code
2156  * showing how to iterate over a notmuch_tags_t object.
2157  */
2158 notmuch_bool_t
2159 notmuch_tags_valid (notmuch_tags_t *tags);
2160
2161 /**
2162  * Get the current tag from 'tags' as a string.
2163  *
2164  * Note: The returned string belongs to 'tags' and has a lifetime
2165  * identical to it (and the query to which it ultimately belongs).
2166  *
2167  * See the documentation of notmuch_message_get_tags for example code
2168  * showing how to iterate over a notmuch_tags_t object.
2169  */
2170 const char *
2171 notmuch_tags_get (notmuch_tags_t *tags);
2172
2173 /**
2174  * Move the 'tags' iterator to the next tag.
2175  *
2176  * If 'tags' is already pointing at the last tag then the iterator
2177  * will be moved to a point just beyond that last tag, (where
2178  * notmuch_tags_valid will return FALSE and notmuch_tags_get will
2179  * return NULL).
2180  *
2181  * See the documentation of notmuch_message_get_tags for example code
2182  * showing how to iterate over a notmuch_tags_t object.
2183  */
2184 void
2185 notmuch_tags_move_to_next (notmuch_tags_t *tags);
2186
2187 /**
2188  * Destroy a notmuch_tags_t object.
2189  *
2190  * It's not strictly necessary to call this function. All memory from
2191  * the notmuch_tags_t object will be reclaimed when the containing
2192  * message or query objects are destroyed.
2193  */
2194 void
2195 notmuch_tags_destroy (notmuch_tags_t *tags);
2196
2197 /**
2198  * Store an mtime within the database for 'directory'.
2199  *
2200  * The 'directory' should be an object retrieved from the database
2201  * with notmuch_database_get_directory for a particular path.
2202  *
2203  * The intention is for the caller to use the mtime to allow efficient
2204  * identification of new messages to be added to the database. The
2205  * recommended usage is as follows:
2206  *
2207  *   o Read the mtime of a directory from the filesystem
2208  *
2209  *   o Call index_file for all mail files in the directory
2210  *
2211  *   o Call notmuch_directory_set_mtime with the mtime read from the
2212  *     filesystem.
2213  *
2214  * Then, when wanting to check for updates to the directory in the
2215  * future, the client can call notmuch_directory_get_mtime and know
2216  * that it only needs to add files if the mtime of the directory and
2217  * files are newer than the stored timestamp.
2218  *
2219  * Note: The notmuch_directory_get_mtime function does not allow the
2220  * caller to distinguish a timestamp of 0 from a non-existent
2221  * timestamp. So don't store a timestamp of 0 unless you are
2222  * comfortable with that.
2223  *
2224  * Return value:
2225  *
2226  * NOTMUCH_STATUS_SUCCESS: mtime successfully stored in database.
2227  *
2228  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception
2229  *      occurred, mtime not stored.
2230  *
2231  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
2232  *      mode so directory mtime cannot be modified.
2233  */
2234 notmuch_status_t
2235 notmuch_directory_set_mtime (notmuch_directory_t *directory,
2236                              time_t mtime);
2237
2238 /**
2239  * Get the mtime of a directory, (as previously stored with
2240  * notmuch_directory_set_mtime).
2241  *
2242  * Returns 0 if no mtime has previously been stored for this
2243  * directory.
2244  */
2245 time_t
2246 notmuch_directory_get_mtime (notmuch_directory_t *directory);
2247
2248 /**
2249  * Get a notmuch_filenames_t iterator listing all the filenames of
2250  * messages in the database within the given directory.
2251  *
2252  * The returned filenames will be the basename-entries only (not
2253  * complete paths).
2254  *
2255  * Returns NULL if it triggers a Xapian exception
2256  */
2257 notmuch_filenames_t *
2258 notmuch_directory_get_child_files (notmuch_directory_t *directory);
2259
2260 /**
2261  * Get a notmuch_filenames_t iterator listing all the filenames of
2262  * sub-directories in the database within the given directory.
2263  *
2264  * The returned filenames will be the basename-entries only (not
2265  * complete paths).
2266  *
2267  * Returns NULL if it triggers a Xapian exception
2268  */
2269 notmuch_filenames_t *
2270 notmuch_directory_get_child_directories (notmuch_directory_t *directory);
2271
2272 /**
2273  * Delete directory document from the database, and destroy the
2274  * notmuch_directory_t object. Assumes any child directories and files
2275  * have been deleted by the caller.
2276  *
2277  * @since libnotmuch 4.3 (notmuch 0.21)
2278  */
2279 notmuch_status_t
2280 notmuch_directory_delete (notmuch_directory_t *directory);
2281
2282 /**
2283  * Destroy a notmuch_directory_t object.
2284  */
2285 void
2286 notmuch_directory_destroy (notmuch_directory_t *directory);
2287
2288 /**
2289  * Is the given 'filenames' iterator pointing at a valid filename.
2290  *
2291  * When this function returns TRUE, notmuch_filenames_get will return
2292  * a valid string. Whereas when this function returns FALSE,
2293  * notmuch_filenames_get will return NULL.
2294  *
2295  * It is acceptable to pass NULL for 'filenames', in which case this
2296  * function will always return FALSE.
2297  */
2298 notmuch_bool_t
2299 notmuch_filenames_valid (notmuch_filenames_t *filenames);
2300
2301 /**
2302  * Get the current filename from 'filenames' as a string.
2303  *
2304  * Note: The returned string belongs to 'filenames' and has a lifetime
2305  * identical to it (and the directory to which it ultimately belongs).
2306  *
2307  * It is acceptable to pass NULL for 'filenames', in which case this
2308  * function will always return NULL.
2309  */
2310 const char *
2311 notmuch_filenames_get (notmuch_filenames_t *filenames);
2312
2313 /**
2314  * Move the 'filenames' iterator to the next filename.
2315  *
2316  * If 'filenames' is already pointing at the last filename then the
2317  * iterator will be moved to a point just beyond that last filename,
2318  * (where notmuch_filenames_valid will return FALSE and
2319  * notmuch_filenames_get will return NULL).
2320  *
2321  * It is acceptable to pass NULL for 'filenames', in which case this
2322  * function will do nothing.
2323  */
2324 void
2325 notmuch_filenames_move_to_next (notmuch_filenames_t *filenames);
2326
2327 /**
2328  * Destroy a notmuch_filenames_t object.
2329  *
2330  * It's not strictly necessary to call this function. All memory from
2331  * the notmuch_filenames_t object will be reclaimed when the
2332  * containing directory object is destroyed.
2333  *
2334  * It is acceptable to pass NULL for 'filenames', in which case this
2335  * function will do nothing.
2336  */
2337 void
2338 notmuch_filenames_destroy (notmuch_filenames_t *filenames);
2339
2340
2341 /**
2342  * set config 'key' to 'value'
2343  *
2344  * @since libnotmuch 4.4 (notmuch 0.23)
2345  * @retval #NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in
2346  *      read-only mode so message cannot be modified.
2347  * @retval #NOTMUCH_STATUS_XAPIAN_EXCEPTION: an exception was thrown
2348  *      accessing the database.
2349  * @retval #NOTMUCH_STATUS_SUCCESS
2350  */
2351 notmuch_status_t
2352 notmuch_database_set_config (notmuch_database_t *db, const char *key, const char *value);
2353
2354 /**
2355  * retrieve config item 'key', assign to  'value'
2356  *
2357  * keys which have not been previously set with n_d_set_config will
2358  * return an empty string.
2359  *
2360  * return value is allocated by malloc and should be freed by the
2361  * caller.
2362  *
2363  * @since libnotmuch 4.4 (notmuch 0.23)
2364  *
2365  */
2366 notmuch_status_t
2367 notmuch_database_get_config (notmuch_database_t *db, const char *key, char **value);
2368
2369 /**
2370  * Create an iterator for all config items with keys matching a given prefix
2371  *
2372  * @since libnotmuch 4.4 (notmuch 0.23)
2373  */
2374 notmuch_status_t
2375 notmuch_database_get_config_list (notmuch_database_t *db, const char *prefix, notmuch_config_list_t **out);
2376
2377 /**
2378  * Is 'config_list' iterator valid (i.e. _key, _value, _move_to_next can be called).
2379  *
2380  * @since libnotmuch 4.4 (notmuch 0.23)
2381  */
2382 notmuch_bool_t
2383 notmuch_config_list_valid (notmuch_config_list_t *config_list);
2384
2385 /**
2386  * return key for current config pair
2387  *
2388  * return value is owned by the iterator, and will be destroyed by the
2389  * next call to notmuch_config_list_key or notmuch_config_list_destroy.
2390  *
2391  * @since libnotmuch 4.4 (notmuch 0.23)
2392  */
2393 const char *
2394 notmuch_config_list_key (notmuch_config_list_t *config_list);
2395
2396 /**
2397  * return 'value' for current config pair
2398  *
2399  * return value is owned by the iterator, and will be destroyed by the
2400  * next call to notmuch_config_list_value or notmuch config_list_destroy
2401  *
2402  * @since libnotmuch 4.4 (notmuch 0.23)
2403  * @retval NULL for errors
2404  */
2405 const char *
2406 notmuch_config_list_value (notmuch_config_list_t *config_list);
2407
2408
2409 /**
2410  * move 'config_list' iterator to the next pair
2411  *
2412  * @since libnotmuch 4.4 (notmuch 0.23)
2413  */
2414 void
2415 notmuch_config_list_move_to_next (notmuch_config_list_t *config_list);
2416
2417 /**
2418  * free any resources held by 'config_list'
2419  *
2420  * @since libnotmuch 4.4 (notmuch 0.23)
2421  */
2422 void
2423 notmuch_config_list_destroy (notmuch_config_list_t *config_list);
2424
2425
2426 /**
2427  * Configuration keys known to libnotmuch
2428  */
2429 typedef enum _notmuch_config_key {
2430     NOTMUCH_CONFIG_FIRST,
2431     NOTMUCH_CONFIG_DATABASE_PATH = NOTMUCH_CONFIG_FIRST,
2432     NOTMUCH_CONFIG_EXCLUDE_TAGS,
2433     NOTMUCH_CONFIG_NEW_TAGS,
2434     NOTMUCH_CONFIG_NEW_IGNORE,
2435     NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS,
2436     NOTMUCH_CONFIG_PRIMARY_EMAIL,
2437     NOTMUCH_CONFIG_OTHER_EMAIL,
2438     NOTMUCH_CONFIG_USER_NAME,
2439     NOTMUCH_CONFIG_LAST
2440 } notmuch_config_key_t;
2441
2442 /**
2443  * get a configuration value from an open database.
2444  *
2445  * This value reflects all configuration information given at the time
2446  * the database was opened.
2447  *
2448  * @param[in] notmuch database
2449  * @param[in] key configuration key
2450  *
2451  * @since libnotmuch 5.4 (notmuch 0.32)
2452  *
2453  * @retval NULL if 'key' unknown or if no value is known for
2454  *         'key'.  Otherwise returns a string owned by notmuch which should
2455  *         not be modified nor freed by the caller.
2456  */
2457 const char *
2458 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key);
2459
2460 /**
2461  * set a configuration value from in an open database.
2462  *
2463  * This value reflects all configuration information given at the time
2464  * the database was opened.
2465  *
2466  * @param[in,out] notmuch database open read/write
2467  * @param[in] key configuration key
2468  * @param[in] val configuration value
2469  *
2470  * @since libnotmuch 5.4 (notmuch 0.32)
2471  *
2472  * @retval returns any return value for notmuch_database_set_config.
2473  */
2474 notmuch_status_t
2475 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val);
2476
2477 /**
2478  * Returns an iterator for a ';'-delimited list of configuration values
2479  *
2480  * These values reflect all configuration information given at the
2481  * time the database was opened.
2482  *
2483  * @param[in] notmuch database
2484  * @param[in] key configuration key
2485  *
2486  * @since libnotmuch 5.4 (notmuch 0.32)
2487  *
2488  * @retval NULL in case of error.
2489  */
2490 notmuch_config_values_t *
2491 notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key);
2492
2493 /**
2494  * Is the given 'config_values' iterator pointing at a valid element.
2495  *
2496  * @param[in] values iterator
2497  *
2498  * @since libnotmuch 5.4 (notmuch 0.32)
2499  *
2500  * @retval FALSE if passed a NULL pointer, or the iterator is exhausted.
2501  *
2502  */
2503 notmuch_bool_t
2504 notmuch_config_values_valid (notmuch_config_values_t *values);
2505
2506 /**
2507  * Get the current value from the 'values' iterator
2508  *
2509  * @param[in] values iterator
2510  *
2511  * @since libnotmuch 5.4 (notmuch 0.32)
2512  *
2513  * @retval a string with the same lifetime as the iterator
2514  */
2515 const char *
2516 notmuch_config_values_get (notmuch_config_values_t *values);
2517
2518 /**
2519  * Move the 'values' iterator to the next element
2520  *
2521  * @param[in,out] values iterator
2522  *
2523  * @since libnotmuch 5.4 (notmuch 0.32)
2524  *
2525  */
2526 void
2527 notmuch_config_values_move_to_next (notmuch_config_values_t *values);
2528
2529
2530 /**
2531  * reset the 'values' iterator to the first element
2532  *
2533  * @param[in,out] values iterator. A NULL value is ignored.
2534  *
2535  * @since libnotmuch 5.4 (notmuch 0.32)
2536  *
2537  */
2538 void
2539 notmuch_config_values_start (notmuch_config_values_t *values);
2540
2541 /**
2542  * Destroy a config values iterator, along with any associated
2543  * resources.
2544  *
2545  * @param[in,out] values iterator
2546  *
2547  * @since libnotmuch 5.4 (notmuch 0.32)
2548  */
2549 void
2550 notmuch_config_values_destroy (notmuch_config_values_t *values);
2551
2552 /**
2553  * get a configuration value from an open database as Boolean
2554  *
2555  * This value reflects all configuration information given at the time
2556  * the database was opened.
2557  *
2558  * @param[in] notmuch database
2559  * @param[in] key configuration key
2560  * @param[out] val configuration value, converted to Boolean
2561  *
2562  * @since libnotmuch 5.4 (notmuch 0.32)
2563  *
2564  * @retval #NOTMUCH_STATUS_ILLEGAL_ARGUMENT if either key is unknown
2565  * or the corresponding value does not convert to Boolean.
2566  */
2567 notmuch_status_t
2568 notmuch_config_get_bool (notmuch_database_t *notmuch,
2569                          notmuch_config_key_t key,
2570                          notmuch_bool_t *val);
2571 /**
2572  * get the current default indexing options for a given database.
2573  *
2574  * This object will survive until the database itself is destroyed,
2575  * but the caller may also release it earlier with
2576  * notmuch_indexopts_destroy.
2577  *
2578  * This object represents a set of options on how a message can be
2579  * added to the index.  At the moment it is a featureless stub.
2580  *
2581  * @since libnotmuch 5.1 (notmuch 0.26)
2582  * @retval NULL in case of error
2583  */
2584 notmuch_indexopts_t *
2585 notmuch_database_get_default_indexopts (notmuch_database_t *db);
2586
2587 /**
2588  * Stating a policy about how to decrypt messages.
2589  *
2590  * See index.decrypt in notmuch-config(1) for more details.
2591  */
2592 typedef enum {
2593     NOTMUCH_DECRYPT_FALSE,
2594     NOTMUCH_DECRYPT_TRUE,
2595     NOTMUCH_DECRYPT_AUTO,
2596     NOTMUCH_DECRYPT_NOSTASH,
2597 } notmuch_decryption_policy_t;
2598
2599 /**
2600  * Specify whether to decrypt encrypted parts while indexing.
2601  *
2602  * Be aware that the index is likely sufficient to reconstruct the
2603  * cleartext of the message itself, so please ensure that the notmuch
2604  * message index is adequately protected. DO NOT SET THIS FLAG TO TRUE
2605  * without considering the security of your index.
2606  *
2607  * @since libnotmuch 5.1 (notmuch 0.26)
2608  */
2609 notmuch_status_t
2610 notmuch_indexopts_set_decrypt_policy (notmuch_indexopts_t *indexopts,
2611                                       notmuch_decryption_policy_t decrypt_policy);
2612
2613 /**
2614  * Return whether to decrypt encrypted parts while indexing.
2615  * see notmuch_indexopts_set_decrypt_policy.
2616  *
2617  * @since libnotmuch 5.1 (notmuch 0.26)
2618  */
2619 notmuch_decryption_policy_t
2620 notmuch_indexopts_get_decrypt_policy (const notmuch_indexopts_t *indexopts);
2621
2622 /**
2623  * Destroy a notmuch_indexopts_t object.
2624  *
2625  * @since libnotmuch 5.1 (notmuch 0.26)
2626  */
2627 void
2628 notmuch_indexopts_destroy (notmuch_indexopts_t *options);
2629
2630
2631 /**
2632  * interrogate the library for compile time features
2633  *
2634  * @since libnotmuch 4.4 (notmuch 0.23)
2635  */
2636 notmuch_bool_t
2637 notmuch_built_with (const char *name);
2638 /**@}*/
2639
2640 #pragma GCC visibility pop
2641
2642 NOTMUCH_END_DECLS
2643
2644 #endif