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