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