]> git.notmuchmail.org Git - notmuch/blob - notmuch.h
Fix relative date formatting to not split one day into two formats.
[notmuch] / 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 #ifndef NOTMUCH_H
22 #define NOTMUCH_H
23
24 #ifdef  __cplusplus
25 # define NOTMUCH_BEGIN_DECLS  extern "C" {
26 # define NOTMUCH_END_DECLS    }
27 #else
28 # define NOTMUCH_BEGIN_DECLS
29 # define NOTMUCH_END_DECLS
30 #endif
31
32 NOTMUCH_BEGIN_DECLS
33
34 #include <time.h>
35
36 #ifndef FALSE
37 #define FALSE 0
38 #endif
39
40 #ifndef TRUE
41 #define TRUE 1
42 #endif
43
44 typedef int notmuch_bool_t;
45
46 /* Status codes used for the return values of most functions.
47  *
48  * A zero value (NOTMUCH_STATUS_SUCCESS) indicates that the function
49  * completed without error. Any other value indicates an error as
50  * follows:
51  *
52  * NOTMUCH_STATUS_SUCCESS: No error occurred.
53  *
54  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory
55  *
56  * XXX: We don't really want to expose this lame XAPIAN_EXCEPTION
57  * value. Instead we should map to things like DATABASE_LOCKED or
58  * whatever.
59  *
60  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
61  *
62  * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to read or
63  *      write to a file (this could be file not found, permission
64  *      denied, etc.)
65  *
66  * NOTMUCH_STATUS_FILE_NOT_EMAIL: A file was presented that doesn't
67  *      appear to be an email message.
68  *
69  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: A file contains a message ID
70  *      that is identical to a message already in the database.
71  *
72  * NOTMUCH_STATUS_NULL_POINTER: The user erroneously passed a NULL
73  *      pointer to a notmuch function.
74  *
75  * NOTMUCH_STATUS_TAG_TOO_LONG: A tag value is too long (exceeds
76  *      NOTMUCH_TAG_MAX)
77  *
78  * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: The notmuch_message_thaw
79  *      function has been called more times than notmuch_message_freeze.
80  *
81  * And finally:
82  *
83  * NOTMUCH_STATUS_LAST_STATUS: Not an actual status value. Just a way
84  *      to find out how many valid status values there are.
85  */
86 typedef enum _notmuch_status {
87     NOTMUCH_STATUS_SUCCESS = 0,
88     NOTMUCH_STATUS_OUT_OF_MEMORY,
89     NOTMUCH_STATUS_XAPIAN_EXCEPTION,
90     NOTMUCH_STATUS_FILE_ERROR,
91     NOTMUCH_STATUS_FILE_NOT_EMAIL,
92     NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID,
93     NOTMUCH_STATUS_NULL_POINTER,
94     NOTMUCH_STATUS_TAG_TOO_LONG,
95     NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
96
97     NOTMUCH_STATUS_LAST_STATUS
98 } notmuch_status_t;
99
100 /* Get a string representation of a notmuch_status_t value.
101  *
102  * The result is readonly.
103  */
104 const char *
105 notmuch_status_to_string (notmuch_status_t status);
106
107 /* Various opaque data types. For each notmuch_<foo>_t see the various
108  * notmuch_<foo> functions below. */
109 typedef struct _notmuch_database notmuch_database_t;
110 typedef struct _notmuch_query notmuch_query_t;
111 typedef struct _notmuch_thread_results notmuch_thread_results_t;
112 typedef struct _notmuch_thread notmuch_thread_t;
113 typedef struct _notmuch_message_results notmuch_message_results_t;
114 typedef struct _notmuch_message notmuch_message_t;
115 typedef struct _notmuch_tags notmuch_tags_t;
116
117 /* Lookup the default database path.
118  *
119  * This is the path that will be used by notmuch_database_create and
120  * notmuch_database_open if given a NULL path. Specifically it will be
121  * the value of the NOTMUCH_BASE environment variable if set,
122  * otherwise ${HOME}/mail
123  *
124  * Returns a newly allocated string which the caller should free()
125  * when finished with it.
126  */
127 char *
128 notmuch_database_default_path (void);
129
130 /* Create a new, empty notmuch database located at 'path'.
131  *
132  * The path should be a top-level directory to a collection of
133  * plain-text email messages (one message per file). This call will
134  * create a new ".notmuch" directory within 'path' where notmuch will
135  * store its data.
136  *
137  * Passing a value of NULL for 'path' will cause notmuch to open the
138  * default database. The default database path can be specified by the
139  * NOTMUCH_BASE environment variable, and is equivalent to
140  * ${HOME}/mail if NOTMUCH_BASE is not set.
141  *
142  * After a successful call to notmuch_database_create, the returned
143  * database will be open so the caller should call
144  * notmuch_database_close when finished with it.
145  *
146  * The database will not yet have any data in it
147  * (notmuch_database_create itself is a very cheap function). Messages
148  * contained within 'path' can be added to the database by calling
149  * notmuch_database_add_message.
150  *
151  * In case of any failure, this function returns NULL, (after printing
152  * an error message on stderr).
153  */
154 notmuch_database_t *
155 notmuch_database_create (const char *path);
156
157 /* XXX: I think I'd like this to take an extra argument of
158  * notmuch_status_t* for returning a status value on failure. */
159
160 /* Open a an existing notmuch database located at 'path'.
161  *
162  * The database should have been created at some time in the past,
163  * (not necessarily by this process), by calling
164  * notmuch_database_create with 'path'.
165  *
166  * An existing notmuch database can be identified by the presence of a
167  * directory named ".notmuch" below 'path'.
168  *
169  * Passing a value of NULL for 'path' will cause notmuch to open the
170  * default database. The default database path can be specified by the
171  * NOTMUCH_BASE environment variable, and is equivalent to
172  * ${HOME}/mail if NOTMUCH_BASE is not set.
173  *
174  * The caller should call notmuch_database_close when finished with
175  * this database.
176  *
177  * In case of any failure, this function returns NULL, (after printing
178  * an error message on stderr).
179  */
180 notmuch_database_t *
181 notmuch_database_open (const char *path);
182
183 /* Close the given notmuch database, freeing all associated
184  * resources. See notmuch_database_open. */
185 void
186 notmuch_database_close (notmuch_database_t *database);
187
188 /* Return the database path of the given database.
189  *
190  * The return value is a string owned by notmuch so should not be
191  * modified nor freed by the caller. */
192 const char *
193 notmuch_database_get_path (notmuch_database_t *database);
194
195 /* Store a timestamp within the database.
196  *
197  * The Notmuch database will not interpret this key nor the timestamp
198  * values at all. It will merely store them together and return the
199  * timestamp when notmuch_database_get_timestamp is called with the
200  * same value for 'key'.
201  *
202  * The intention is for the caller to use the timestamp to allow
203  * efficient identification of new messages to be added to the
204  * database. The recommended usage is as follows:
205  *
206  *   o Read the mtime of a directory from the filesystem
207  *
208  *   o Call add_message for all mail files in the directory
209  *
210  *   o Call notmuch_database_set_timestamp with the path of the
211  *     directory as 'key' and the originally read mtime as 'value'.
212  *
213  * Then, when wanting to check for updates to the directory in the
214  * future, the client can call notmuch_database_get_timestamp and know
215  * that it only needs to add files if the mtime of the directory and
216  * files are newer than the stored timestamp.
217  *
218  * Note: The notmuch_database_get_timestamp function does not allow
219  * the caller to distinguish a timestamp of 0 from a non-existent
220  * timestamp. So don't store a timestamp of 0 unless you are
221  * comfortable with that.
222  *
223  * Return value:
224  *
225  * NOTMUCH_STATUS_SUCCESS: Timestamp successfully stored in database.
226  *
227  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception
228  *      occurred. Timestamp not stored.
229  */
230 notmuch_status_t
231 notmuch_database_set_timestamp (notmuch_database_t *database,
232                                 const char *key, time_t timestamp);
233
234 /* Retrieve a timestamp from the database.
235  *
236  * Returns the timestamp value previously stored by calling
237  * notmuch_database_set_timestamp with the same value for 'key'.
238  *
239  * Returns 0 if no timestamp is stored for 'key' or if any error
240  * occurred querying the database.
241  */
242 time_t
243 notmuch_database_get_timestamp (notmuch_database_t *database,
244                                 const char *key);
245
246 /* Add a new message to the given notmuch database.
247  *
248  * Here,'filename' should be a path relative to the the path of
249  * 'database' (see notmuch_database_get_path), or else should be an
250  * absolute filename with initial components that match the path of
251  * 'database'.
252  *
253  * The file should be a single mail message (not a multi-message mbox)
254  * that is expected to remain at its current location, (since the
255  * notmuch database will reference the filename, and will not copy the
256  * entire contents of the file.
257  *
258  * If 'message' is not NULL, then, on successful return '*message'
259  * will be initialized to a message object that can be used for things
260  * such as adding tags to the just-added message. The user should call
261  * notmuch_message_destroy when done with the message. On any failure
262  * '*message' will be set to NULL.
263  *
264  * Return value:
265  *
266  * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
267  *
268  * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message
269  *      ID as another message already in the database. Nothing added
270  *      to the database.
271  *
272  * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
273  *      file, (such as permission denied, or file not found,
274  *      etc.). Nothing added to the database.
275  *
276  * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
277  *      like an email message. Nothing added to the database.
278  */
279 notmuch_status_t
280 notmuch_database_add_message (notmuch_database_t *database,
281                               const char *filename,
282                               notmuch_message_t **message);
283
284 /* Find a message with the given messsage_id.
285  *
286  * If the database contains a message with the given message_id, then
287  * a new notmuch_message_t object is returned. The caller should call
288  * notmuch_message_destroy when done with the message.
289  *
290  * If no message is found with the given message_id or if an
291  * out-of-memory situation occurs, this function returns NULL.
292  */
293 notmuch_message_t *
294 notmuch_database_find_message (notmuch_database_t *database,
295                                const char *message_id);
296
297 /* Create a new query for 'database'.
298  *
299  * Here, 'database' should be an open database, (see
300  * notmuch_database_open and notmuch_database_create).
301  *
302  * For the query string, we'll document the syntax here more
303  * completely in the future, but it's likely to be a specialized
304  * version of the general Xapian query syntax:
305  *
306  * http://xapian.org/docs/queryparser.html
307  *
308  * As a special case, passing a length-zero string, (that is ""), will
309  * result in a query that returns all messages in the database.
310  *
311  * See notmuch_query_set_sort for controlling the order of results and
312  * notmuch_query_search to actually execute the query.
313  *
314  * User should call notmuch_query_destroy when finished with this
315  * query.
316  *
317  * Will return NULL if insufficient memory is available.
318  */
319 notmuch_query_t *
320 notmuch_query_create (notmuch_database_t *database,
321                       const char *query_string);
322
323 /* Sort values for notmuch_query_set_sort */
324 typedef enum {
325     NOTMUCH_SORT_DATE_OLDEST_FIRST,
326     NOTMUCH_SORT_DATE_NEWEST_FIRST,
327     NOTMUCH_SORT_MESSAGE_ID
328 } notmuch_sort_t;
329
330 /* Specify the sorting desired for this query. */
331 void
332 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
333
334 /* Execute a query for threads, returning a notmuch_thread_results_t
335  * object which can be used to iterate over the results. The results
336  * object is owned by the query and as such, will only be valid until
337  * notmuch_query_destroy.
338  *
339  * Typical usage might be:
340  *
341  *     notmuch_query_t *query;
342  *     notmuch_thread_results_t *results;
343  *     notmuch_thread_t *thread;
344  *
345  *     query = notmuch_query_create (database, query_string);
346  *
347  *     for (results = notmuch_query_search_threads (query);
348  *          notmuch_thread_results_has_more (results);
349  *          notmuch_thread_results_advance (results))
350  *     {
351  *         thread = notmuch_thread_results_get (results);
352  *         ....
353  *         notmuch_thread_destroy (thread);
354  *     }
355  *
356  *     notmuch_query_destroy (query);
357  *
358  * Note: If you are finished with a thread before its containing
359  * query, you can call notmuch_thread_destroy to clean up some memory
360  * sooner (as in the above example). Otherwise, if your thread objects
361  * are long-lived, then you don't need to call notmuch_thread_destroy
362  * and all the memory will still be reclaimed when the query is
363  * destroyed.
364  *
365  * Note that there's no explicit destructor needed for the
366  * notmuch_thread_results_t object. (For consistency, we do provide a
367  * notmuch_thread_results_destroy function, but there's no good reason
368  * to call it if the query is about to be destroyed).
369  */
370 notmuch_thread_results_t *
371 notmuch_query_search_threads (notmuch_query_t *query);
372
373 /* Execute a query for messages, returning a notmuch_message_results_t
374  * object which can be used to iterate over the results. The results
375  * object is owned by the query and as such, will only be valid until
376  * notmuch_query_destroy.
377  *
378  * Typical usage might be:
379  *
380  *     notmuch_query_t *query;
381  *     notmuch_message_results_t *results;
382  *     notmuch_message_t *message;
383  *
384  *     query = notmuch_query_create (database, query_string);
385  *
386  *     for (results = notmuch_query_search_messages (query);
387  *          notmuch_message_results_has_more (results);
388  *          notmuch_message_results_advance (results))
389  *     {
390  *         message = notmuch_message_results_get (results);
391  *         ....
392  *         notmuch_message_destroy (message);
393  *     }
394  *
395  *     notmuch_query_destroy (query);
396  *
397  * Note: If you are finished with a message before its containing
398  * query, you can call notmuch_message_destroy to clean up some memory
399  * sooner (as in the above example). Otherwise, if your message
400  * objects are long-lived, then you don't need to call
401  * notmuch_message_destroy and all the memory will still be reclaimed
402  * when the query is destroyed.
403  *
404  * Note that there's no explicit destructor needed for the
405  * notmuch_message_results_t object. (For consistency, we do provide a
406  * notmuch_message_results_destroy function, but there's no good
407  * reason to call it if the query is about to be destroyed).
408  */
409 notmuch_message_results_t *
410 notmuch_query_search_messages (notmuch_query_t *query);
411
412 /* Destroy a notmuch_query_t along with any associated resources.
413  *
414  * This will in turn destroy any notmuch_thread_results_t and
415  * notmuch_message_results_t objects generated by this query, (and in
416  * turn any notmuch_thrad_t and notmuch_message_t objects generated
417  * from those results, etc.), if such objects haven't already been
418  * destroyed.
419  */
420 void
421 notmuch_query_destroy (notmuch_query_t *query);
422
423 /* Does the given notmuch_thread_results_t object contain any more
424  * results.
425  *
426  * When this function returns TRUE, notmuch_thread_results_get will
427  * return a valid object. Whereas when this function returns FALSE,
428  * notmuch_thread_results_get will return NULL.
429  *
430  * See the documentation of notmuch_query_search_threads for example
431  * code showing how to iterate over a notmuch_thread_results_t object.
432  */
433 notmuch_bool_t
434 notmuch_thread_results_has_more (notmuch_thread_results_t *results);
435
436 /* Get the current result from 'results' as a notmuch_thread_t.
437  *
438  * Note: The returned thread belongs to 'results' and has a lifetime
439  * identical to it (and the query to which it belongs).
440  *
441  * See the documentation of notmuch_query_search_threads for example
442  * code showing how to iterate over a notmuch_thread_results_t object.
443  *
444  * If an out-of-memory situation occurs, this function will return
445  * NULL.
446  */
447 notmuch_thread_t *
448 notmuch_thread_results_get (notmuch_thread_results_t *results);
449
450 /* Advance the 'results' iterator to the next result.
451  *
452  * See the documentation of notmuch_query_search_threads for example
453  * code showing how to iterate over a notmuch_thread_results_t object.
454  */
455 void
456 notmuch_thread_results_advance (notmuch_thread_results_t *results);
457
458 /* Destroy a notmuch_thread_results_t object.
459  *
460  * It's not strictly necessary to call this function. All memory from
461  * the notmuch_thread_results_t object will be reclaimed when the
462  * containg query object is destroyed.
463  */
464 void
465 notmuch_thread_results_destroy (notmuch_thread_results_t *results);
466
467 /* Get the thread ID of 'thread'.
468  *
469  * The returned string belongs to 'thread' and as such, should not be
470  * modified by the caller and will only be valid for as long as the
471  * thread is valid, (which is until notmuch_thread_destroy or until
472  * the query from which it derived is destroyed).
473  */
474 const char *
475 notmuch_thread_get_thread_id (notmuch_thread_t *thread);
476
477 /* Get the subject of 'thread'
478  *
479  * The subject is taken from the first message (according to the query
480  * order---see notmuch_query_set_sort) in the query results that
481  * belongs to this thread.
482  *
483  * The returned string belongs to 'thread' and as such, should not be
484  * modified by the caller and will only be valid for as long as the
485  * thread is valid, (which is until notmuch_thread_destroy or until
486  * the query from which it derived is destroyed).
487  */
488 const char *
489 notmuch_thread_get_subject (notmuch_thread_t *thread);
490
491 /* Get the date of the oldest message in 'thread' as a time_t value.
492  */
493 time_t
494 notmuch_thread_get_oldest_date (notmuch_thread_t *thread);
495
496 /* Get the date of the oldest message in 'thread' as a time_t value.
497  */
498 time_t
499 notmuch_thread_get_newest_date (notmuch_thread_t *thread);
500
501 /* Get the tags for 'thread', returning a notmuch_tags_t object which
502  * can be used to iterate over all tags.
503  *
504  * Note: In the Notmuch database, tags are stored on individual
505  * messages, not on threads. So the tags returned here will be all
506  * tags of the messages which matched the search and which belong to
507  * this thread.
508  *
509  * The tags object is owned by the thread and as such, will only be
510  * valid for as long as the thread is valid, (for example, until
511  * notmuch_thread_destroy or until the query from which it derived is
512  * destroyed).
513  *
514  * Typical usage might be:
515  *
516  *     notmuch_thread_t *thread;
517  *     notmuch_tags_t *tags;
518  *     const char *tag;
519  *
520  *     thread = notmuch_thread_results_get (thread_results);
521  *
522  *     for (tags = notmuch_thread_get_tags (thread);
523  *          notmuch_tags_has_more (tags);
524  *          notmuch_result_advance (tags))
525  *     {
526  *         tag = notmuch_tags_get (tags);
527  *         ....
528  *     }
529  *
530  *     notmuch_thread_destroy (thread);
531  *
532  * Note that there's no explicit destructor needed for the
533  * notmuch_tags_t object. (For consistency, we do provide a
534  * notmuch_tags_destroy function, but there's no good reason to call
535  * it if the message is about to be destroyed).
536  */
537 notmuch_tags_t *
538 notmuch_thread_get_tags (notmuch_thread_t *thread);
539
540 /* Destroy a notmuch_thread_t object. */
541 void
542 notmuch_thread_destroy (notmuch_thread_t *thread);
543
544 /* Does the given notmuch_message_results_t object contain any more
545  * results.
546  *
547  * When this function returns TRUE, notmuch_message_results_get will
548  * return a valid object. Whereas when this function returns FALSE,
549  * notmuch_message_results_get will return NULL.
550  *
551  * See the documentation of notmuch_query_search_messages for example
552  * code showing how to iterate over a notmuch_message_results_t
553  * object.
554  */
555 notmuch_bool_t
556 notmuch_message_results_has_more (notmuch_message_results_t *results);
557
558 /* Get the current result from 'results' as a notmuch_message_t.
559  *
560  * Note: The returned message belongs to 'results' and has a lifetime
561  * identical to it (and the query to which it belongs).
562  *
563  * See the documentation of notmuch_query_search_messages for example
564  * code showing how to iterate over a notmuch_message_results_t
565  * object.
566  *
567  * If an out-of-memory situation occurs, this function will return
568  * NULL.
569  */
570 notmuch_message_t *
571 notmuch_message_results_get (notmuch_message_results_t *results);
572
573 /* Advance the 'results' iterator to the next result.
574  *
575  * See the documentation of notmuch_query_search_messages for example
576  * code showing how to iterate over a notmuch_message_results_t
577  * object.
578  */
579 void
580 notmuch_message_results_advance (notmuch_message_results_t *results);
581
582 /* Destroy a notmuch_message_results_t object.
583  *
584  * It's not strictly necessary to call this function. All memory from
585  * the notmuch_message_results_t object will be reclaimed when the
586  * containg query object is destroyed.
587  */
588 void
589 notmuch_message_results_destroy (notmuch_message_results_t *results);
590
591 /* Get the message ID of 'message'.
592  *
593  * The returned string belongs to 'message' and as such, should not be
594  * modified by the caller and will only be valid for as long as the
595  * message is valid, (which is until the query from which it derived
596  * is destroyed).
597  *
598  * This function will not return NULL since Notmuch ensures that every
599  * message has a unique message ID, (Notmuch will generate an ID for a
600  * message if the original file does not contain one).
601  */
602 const char *
603 notmuch_message_get_message_id (notmuch_message_t *message);
604
605 /* Get the thread ID of 'message'.
606  *
607  * The returned string belongs to 'message' and as such, should not be
608  * modified by the caller and will only be valid for as long as the
609  * message is valid, (for example, until the user calls
610  * notmuch_message_destroy on 'message' or until a query from which it
611  * derived is destroyed).
612  *
613  * This function will not return NULL since Notmuch ensures that every
614  * message belongs to a single thread.
615  */
616 const char *
617 notmuch_message_get_thread_id (notmuch_message_t *message);
618
619 /* Get the filename for the email corresponding to 'message'.
620  *
621  * The returned filename is an absolute filename, (the initial
622  * component will match notmuch_database_get_path() ).
623  *
624  * The returned string belongs to the message so should not be
625  * modified or freed by the caller (nor should it be referenced after
626  * the message is destroyed). */
627 const char *
628 notmuch_message_get_filename (notmuch_message_t *message);
629
630 /* Get the date of 'message' as a time_t value.
631  *
632  * For the original textual representation of the Date header from the
633  * message call notmuch_message_get_header() with a header value of
634  * "date". */
635 time_t
636 notmuch_message_get_date  (notmuch_message_t *message);
637
638 /* Get the size in bytes of the full header section of 'message'.
639  *
640  * This is useful in conjunction with notmuch_message_get_filename
641  * for separately parsing the message header and content.
642  *
643  * Returns 0 in the case of any error.
644  */
645 size_t
646 notmuch_message_get_header_size (notmuch_message_t *message);
647
648 /* Get the value of the specified header from 'message'.
649  *
650  * The value will be read from the actual message file, not from the
651  * notmuch database. The header name is case insensitive.
652  *
653  * The returned string belongs to the message so should not be
654  * modified or freed by the caller (nor should it be referenced after
655  * the message is destroyed).
656  *
657  * Returns NULL if the message does not contain a header line matching
658  * 'header' of if any error occurs.
659  */
660 const char *
661 notmuch_message_get_header (notmuch_message_t *message, const char *header);
662
663 /* Get the entire set of headers from an email message as a string.
664  *
665  * The value will be read from the actual message file, not from the
666  * notmuch database.
667  *
668  * The returned value is owned by the notmuch message and is valid
669  * only until the message is closed. The caller should copy it if
670  * needing to modify the value or to hold onto it for longer.
671  *
672  * Returns NULL in the case of any error.
673  */
674 const char *
675 notmuch_message_get_all_headers (notmuch_message_t *message);
676
677 /* Get the tags for 'message', returning a notmuch_tags_t object which
678  * can be used to iterate over all tags.
679  *
680  * The tags object is owned by the message and as such, will only be
681  * valid for as long as the message is valid, (which is until the
682  * query from which it derived is destroyed).
683  *
684  * Typical usage might be:
685  *
686  *     notmuch_message_t *message;
687  *     notmuch_tags_t *tags;
688  *     const char *tag;
689  *
690  *     message = notmuch_database_find_message (database, message_id);
691  *
692  *     for (tags = notmuch_message_get_tags (message);
693  *          notmuch_tags_has_more (tags);
694  *          notmuch_result_advance (tags))
695  *     {
696  *         tag = notmuch_tags_get (tags);
697  *         ....
698  *     }
699  *
700  *     notmuch_message_destroy (message);
701  *
702  * Note that there's no explicit destructor needed for the
703  * notmuch_tags_t object. (For consistency, we do provide a
704  * notmuch_tags_destroy function, but there's no good reason to call
705  * it if the message is about to be destroyed).
706  */
707 notmuch_tags_t *
708 notmuch_message_get_tags (notmuch_message_t *message);
709
710 /* The longest possible tag value. */
711 #define NOTMUCH_TAG_MAX 200
712
713 /* Add a tag to the given message.
714  *
715  * Return value:
716  *
717  * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
718  *
719  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
720  *
721  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
722  *      (exceeds NOTMUCH_TAG_MAX)
723  */
724 notmuch_status_t
725 notmuch_message_add_tag (notmuch_message_t *message, const char *tag);
726
727 /* Remove a tag from the given message.
728  *
729  * Return value:
730  *
731  * NOTMUCH_STATUS_SUCCESS: Tag successfully removed from message
732  *
733  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
734  *
735  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is too long
736  *      (exceeds NOTMUCH_TAG_MAX)
737  */
738 notmuch_status_t
739 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag);
740
741 /* Remove all tags from the given message.
742  *
743  * See notmuch_message_freeze for an example showing how to safely
744  * replace tag values.
745  */
746 void
747 notmuch_message_remove_all_tags (notmuch_message_t *message);
748
749 /* Freeze the current state of 'message' within the database.
750  *
751  * This means that changes to the message state, (via
752  * notmuch_message_add_tag, notmuch_message_remove_tag, and
753  * notmuch_message_remove_all_tags), will not be committed to the
754  * database until the message is thawed with notmuch_message_thaw.
755  *
756  * Multiple calls to freeze/thaw are valid and these calls with
757  * "stack". That is there must be as many calls to thaw as to freeze
758  * before a message is actually thawed.
759  *
760  * The ability to do freeze/thaw allows for safe transactions to
761  * change tag values. For example, explicitly setting a message to
762  * have a given set of tags might look like this:
763  *
764  *    notmuch_message_freeze (message);
765  *
766  *    notmuch_message_remove_all_tags (message);
767  *
768  *    for (i = 0; i < NUM_TAGS; i++)
769  *        notmuch_message_add_tag (message, tags[i]);
770  *
771  *    notmuch_message_thaw (message);
772  *
773  * With freeze/thaw used like this, the message in the database is
774  * guaranteed to have either the full set of original tag value, or
775  * the full set of new tag values, but nothing in between.
776  *
777  * Imagine the example above without freeze/thaw and the operation
778  * somehow getting interrupted. This could result in the message being
779  * left with no tags if the interruption happened after
780  * notmuch_message_remove_all_tags but before notmuch_message_add_tag.
781  */
782 void
783 notmuch_message_freeze (notmuch_message_t *message);
784
785 /* Thaw the current 'message', synchronizing any changes that may have
786  * occurred while 'message' was frozen into the notmuch database.
787  *
788  * See notmuch_message_freeze for an example of how to use this
789  * function to safely provide tag changes.
790  *
791  * Multiple calls to freeze/thaw are valid and these calls with
792  * "stack". That is there must be as many calls to thaw as to freeze
793  * before a message is actually thawed.
794  *
795  * Return value:
796  *
797  * NOTMUCH_STATUS_SUCCESS: Message successfully thawed, (or at least
798  *      its frozen count has successfully been reduced by 1).
799  *
800  * NOTMUCH_STATUS_UNBALANCE_FREEZE_THAW: An attempt was made to thaw
801  *      an unfrozen message. That is, there have been an unbalanced
802  *      number of calls to notmuch_message_freeze and
803  *      notmuch_message_thaw.
804  */
805 notmuch_status_t
806 notmuch_message_thaw (notmuch_message_t *message);
807
808 /* Destroy a notmuch_message_t object.
809  *
810  * It can be useful to call this function in the case of a single
811  * query object with many messages in the result, (such as iterating
812  * over the entire database). Otherwise, it's fine to never call this
813  * function and there will still be no memory leaks. (The memory from
814  * the messages get reclaimed when the containing query is destroyed.)
815  */
816 void
817 notmuch_message_destroy (notmuch_message_t *message);
818
819 /* Does the given notmuch_tags_t object contain any more tags.
820  *
821  * When this function returns TRUE, notmuch_tags_get will return a
822  * valid string. Whereas when this function returns FALSE,
823  * notmuch_tags_get will return NULL.
824  *
825  * See the documentation of notmuch_message_get_tags for example code
826  * showing how to iterate over a notmuch_tags_t object.
827  */
828 notmuch_bool_t
829 notmuch_tags_has_more (notmuch_tags_t *tags);
830
831 /* Get the current tag from 'tags' as a string.
832  *
833  * Note: The returned string belongs to 'tags' and has a lifetime
834  * identical to it (and the query to which it utlimately belongs).
835  *
836  * See the documentation of notmuch_message_get_tags for example code
837  * showing how to iterate over a notmuch_tags_t object.
838  */
839 const char *
840 notmuch_tags_get (notmuch_tags_t *tags);
841
842 /* Advance the 'tags' iterator to the next tag.
843  *
844  * See the documentation of notmuch_message_get_tags for example code
845  * showing how to iterate over a notmuch_tags_t object.
846  */
847 void
848 notmuch_tags_advance (notmuch_tags_t *tags);
849
850 /* Destroy a notmuch_tags_t object.
851  *
852  * It's not strictly necessary to call this function. All memory from
853  * the notmuch_tags_t object will be reclaimed when the containg
854  * message or query objects are destroyed.
855  */
856 void
857 notmuch_tags_destroy (notmuch_tags_t *tags);
858
859 NOTMUCH_END_DECLS
860
861 #endif