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