]> git.notmuchmail.org Git - notmuch/blob - notmuch.h
notmuch restore: Print names of tags that cannot be applied
[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 #ifndef FALSE
35 #define FALSE 0
36 #endif
37
38 #ifndef TRUE
39 #define TRUE 1
40 #endif
41
42 typedef int notmuch_bool_t;
43
44 /* Status codes used for the return values of most functions.
45  *
46  * A zero value (NOTMUCH_STATUS_SUCCESS) indicates that the function
47  * completed without error. Any other value indicates an error as
48  * follows:
49  *
50  * NOTMUCH_STATUS_SUCCESS: No error occurred.
51  *
52  * XXX: We don't really want to expose this lame XAPIAN_EXCEPTION
53  * value. Instead we should map to things like DATABASE_LOCKED or
54  * whatever.
55  *
56  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
57  *
58  * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to read or
59  *      write to a file (this could be file not found, permission
60  *      denied, etc.)
61  *
62  * NOTMUCH_STATUS_FILE_NOT_EMAIL: A file was presented that doesn't
63  *      appear to be an email message.
64  *
65  * NOTMUCH_STATUS_NULL_POINTER: The user erroneously passed a NULL
66  *      pointer to a notmuch function.
67  *
68  * NOTMUCH_STATUS_TAG_TOO_LONG: A tag value is too long.
69  *
70  * NOTMUCH_STATUS_LAST_STATUS: Not an actual status value. Just a way
71  *      to find out how many valid status values there are.
72  */
73 typedef enum _notmuch_status {
74     NOTMUCH_STATUS_SUCCESS = 0,
75     NOTMUCH_STATUS_XAPIAN_EXCEPTION,
76     NOTMUCH_STATUS_FILE_ERROR,
77     NOTMUCH_STATUS_FILE_NOT_EMAIL,
78     NOTMUCH_STATUS_NULL_POINTER,
79     NOTMUCH_STATUS_TAG_TOO_LONG,
80
81     NOTMUCH_STATUS_LAST_STATUS
82 } notmuch_status_t;
83
84 /* Get a string representation of a notmuch_status_t value.
85  *
86  * The result is readonly.
87  */
88 const char *
89 notmuch_status_to_string (notmuch_status_t status);
90
91 /* Various opaque data types. For each notmuch_<foo>_t see the various
92  * notmuch_<foo> functions below. */
93 typedef struct _notmuch_database notmuch_database_t;
94 typedef struct _notmuch_query notmuch_query_t;
95 typedef struct _notmuch_results notmuch_results_t;
96 typedef struct _notmuch_message notmuch_message_t;
97 typedef struct _notmuch_tags notmuch_tags_t;
98 typedef struct _notmuch_thread_ids notmuch_thread_ids_t;
99
100 /* Create a new, empty notmuch database located at 'path'.
101  *
102  * The path should be a top-level directory to a collection of
103  * plain-text email messages (one message per file). This call will
104  * create a new ".notmuch" directory within 'path' where notmuch will
105  * store its data.
106  *
107  * Passing a value of NULL for 'path' will cause notmuch to open the
108  * default database. The default database path can be specified by the
109  * NOTMUCH_BASE environment variable, and is equivalent to
110  * ${HOME}/mail if NOTMUCH_BASE is not set.
111  *
112  * After a successful call to notmuch_database_create, the returned
113  * database will be open so the caller should call
114  * notmuch_database_close when finished with it.
115  *
116  * The database will not yet have any data in it
117  * (notmuch_database_create itself is a very cheap function). Messages
118  * contained within 'path' can be added to the database by calling
119  * notmuch_database_add_message.
120  *
121  * In case of any failure, this function returns NULL, (after printing
122  * an error message on stderr).
123  */
124 notmuch_database_t *
125 notmuch_database_create (const char *path);
126
127 /* Open a an existing notmuch database located at 'path'.
128  *
129  * The database should have been created at some time in the past,
130  * (not necessarily by this process), by calling
131  * notmuch_database_create with 'path'.
132  *
133  * An existing notmuch database can be identified by the presence of a
134  * directory named ".notmuch" below 'path'.
135  *
136  * Passing a value of NULL for 'path' will cause notmuch to open the
137  * default database. The default database path can be specified by the
138  * NOTMUCH_BASE environment variable, and is equivalent to
139  * ${HOME}/mail if NOTMUCH_BASE is not set.
140  *
141  * The caller should call notmuch_database_close when finished with
142  * this database.
143  *
144  * In case of any failure, this function returns NULL, (after printing
145  * an error message on stderr).
146  */
147 notmuch_database_t *
148 notmuch_database_open (const char *path);
149
150 /* Close the given notmuch database, freeing all associated
151  * resources. See notmuch_database_open. */
152 void
153 notmuch_database_close (notmuch_database_t *database);
154
155 /* Lookup the default database path.
156  *
157  * This is the path that will be used by notmuch_database_create and
158  * notmuch_database_open if given a NULL path. Specifically it will be
159  * the value of the NOTMUCH_BASE environment variable if set,
160  * otherwise ${HOME}/mail
161  *
162  * Returns a newly allocated string which the caller should free()
163  * when finished with it.
164  */
165 char *
166 notmuch_database_default_path (void);
167
168 /* Return the database path of the given database.
169  *
170  * The return value is a string owned by notmuch so should not be
171  * modified nor freed by the caller. */
172 const char *
173 notmuch_database_get_path (notmuch_database_t *database);
174
175 /* Add a new message to the given notmuch database.
176  *
177  * Here,'filename' should be a path relative to the the path of
178  * 'database' (see notmuch_database_get_path). The file should be a
179  * single mail message (not a multi-message mbox) that is expected to
180  * remain at its current location, (since the notmuch database will
181  * reference the filename, and will not copy the entire contents of
182  * the file.
183  *
184  * Return value:
185  *
186  * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
187  *
188  * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
189  *      file, (such as permission denied, or file not found,
190  *      etc.). Nothing added to the database.
191  *
192  * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
193  *      like an email message. Nothing added to the database.
194  */
195 notmuch_status_t
196 notmuch_database_add_message (notmuch_database_t *database,
197                               const char *filename);
198
199 /* Find a message with the given messsage_id.
200  *
201  * If the database contains a message with the given message_id, then
202  * a new notmuch_message_t object is returned. The caller should call
203  * notmuch_message_destroy when done with the message.
204  *
205  * If no message is found with the given message_id, this function
206  * returns NULL.
207  */
208 notmuch_message_t *
209 notmuch_database_find_message (notmuch_database_t *database,
210                                const char *message_id);
211
212 /* Create a new query for 'database'.
213  *
214  * Here, 'database' should be an open database, (see
215  * notmuch_database_open and notmuch_database_create).
216  *
217  * For the query string, we'll document the syntax here more
218  * completely in the future, but it's likely to be a specialized
219  * version of the general Xapian query syntax:
220  *
221  * http://xapian.org/docs/queryparser.html
222  *
223  * As a special case, passing a length-zero string, (that is ""), will
224  * result in a query that returns all messages in the database.
225  *
226  * See notmuch_query_set_sort for controlling the order of results and
227  * notmuch_query_search to actually execute the query.
228  *
229  * User should call notmuch_query_destroy when finished with this
230  * query.
231  *
232  * Will return NULL if insufficient memory is available.
233  */
234 notmuch_query_t *
235 notmuch_query_create (notmuch_database_t *database,
236                       const char *query_string);
237
238 /* Sort values for notmuch_query_set_sort */
239 typedef enum {
240     NOTMUCH_SORT_DATE_OLDEST_FIRST,
241     NOTMUCH_SORT_DATE_NEWEST_FIRST,
242     NOTMUCH_SORT_MESSAGE_ID
243 } notmuch_sort_t;
244
245 /* Specify the sorting desired for this query. */
246 void
247 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
248
249 /* Execute a query, returning a notmuch_results_t object which can be
250  * used to iterate over the results. The results object is owned by
251  * the query and as such, will only be valid until notmuch_query_destroy.
252  *
253  * Typical usage might be:
254  *
255  *     notmuch_query_t *query;
256  *     notmuch_results_t *results;
257  *
258  *     query = notmuch_query_create (database, query_string);
259  *
260  *     for (results = notmuch_query_search (query);
261  *          notmuch_results_has_more (results);
262  *          notmuch_result_advance (results))
263  *     {
264  *         message = notmuch_results_get (results);
265  *         ....
266  *         notmuch_message_destroy (message);
267  *     }
268  *
269  *     notmuch_query_destroy (query);
270  *
271  * Note: If you are finished with a message before its containing
272  * query, you can call notmuch_message_destroy to clean up some memory
273  * sooner (as in the above example). Otherwise, if your message
274  * objects are long-lived, then you don't need to call
275  * notmuch_message_destroy and all the memory will still be reclaimed
276  * when the query is destroyed.
277  *
278  * Note that there's no explicit destructor needed for the
279  * notmuch_results_t object. (For consistency, we do provide a
280  * notmuch_results_destroy function, but there's no good reason to
281  * call it if the query is about to be destroyed).
282  */
283 notmuch_results_t *
284 notmuch_query_search (notmuch_query_t *query);
285
286 /* Destroy a notmuch_query_t along with any associated resources.
287  *
288  * This will in turn destroy any notmuch_results_t objects generated
289  * by this query, (and in turn any notmuch_message_t objects generated
290  * from those results, etc.).
291  */
292 void
293 notmuch_query_destroy (notmuch_query_t *query);
294
295 /* Does the given notmuch_results_t object contain any more results.
296  *
297  * When this function returns TRUE, notmuch_results_get will return a
298  * valid object. Whereas when this function returns FALSE,
299  * notmuch_results_get will return NULL.
300  *
301  * See the documentation of notmuch_query_search for example code
302  * showing how to iterate over a notmuch_results_t object.
303  */
304 notmuch_bool_t
305 notmuch_results_has_more (notmuch_results_t *results);
306
307 /* Get the current result from 'results' as a notmuch_message_t.
308  *
309  * Note: The returned message belongs to 'results' and has a lifetime
310  * identical to it (and the query to which it belongs).
311  *
312  * See the documentation of notmuch_query_search for example code
313  * showing how to iterate over a notmuch_results_t object.
314  */
315 notmuch_message_t *
316 notmuch_results_get (notmuch_results_t *results);
317
318 /* Advance the 'results' iterator to the next result.
319  *
320  * See the documentation of notmuch_query_search for example code
321  * showing how to iterate over a notmuch_results_t object.
322  */
323 void
324 notmuch_results_advance (notmuch_results_t *results);
325
326 /* Destroy a notmuch_results_t object.
327  *
328  * It's not strictly necessary to call this function. All memory from
329  * the notmuch_results_t object will be reclaimed when the containg
330  * query object is destroyed.
331  */
332 void
333 notmuch_results_destroy (notmuch_results_t *results);
334
335 /* Get the message ID of 'message'.
336  *
337  * The returned string belongs to 'message' and as such, should not be
338  * modified by the caller and will only be valid for as long as the
339  * message is valid, (which is until the query from which it derived
340  * is destroyed).
341  *
342  * This function will not return NULL since Notmuch ensures that every
343  * message has a unique message ID, (Notmuch will generate an ID for a
344  * message if the original file does not contain one).
345  */
346 const char *
347 notmuch_message_get_message_id (notmuch_message_t *message);
348
349 /* Get this filename for the email corresponding to 'message'.
350  *
351  * The returned filename is relative to the base of the database from
352  * which 'message' was obtained. See notmuch_database_get_path() .*/
353 const char *
354 notmuch_message_get_filename (notmuch_message_t *message);
355
356 /* Get the tags for 'message', returning a notmuch_tags_t object which
357  * can be used to iterate over all tags.
358  *
359  * The tags object is owned by the message and as such, will only be
360  * valid for as long as the message is valid, (which is until the
361  * query from which it derived is destroyed).
362  *
363  * Typical usage might be:
364  *
365  *     notmuch_message_t *message;
366  *     notmuch_tags_t *tags;
367  *     const char *tag;
368  *
369  *     message = notmuch_database_find_message (database, message_id);
370  *
371  *     for (tags = notmuch_message_get_tags (message);
372  *          notmuch_tags_has_more (tags);
373  *          notmuch_result_advance (tags))
374  *     {
375  *         tag = notmuch_tags_get (tags);
376  *         ....
377  *     }
378  *
379  *     notmuch_message_destroy (message);
380  *
381  * Note that there's no explicit destructor needed for the
382  * notmuch_tags_t object. (For consistency, we do provide a
383  * notmuch_tags_destroy function, but there's no good reason to call
384  * it if the message is about to be destroyed).
385  */
386 notmuch_tags_t *
387 notmuch_message_get_tags (notmuch_message_t *message);
388
389 /* Get the thread IDs for 'message', returning a notmuch_thread_ids_t
390  * object which can be used to iterate over all thread IDs.
391  *
392  * The thread_ids object is owned by the message and as such, will
393  * only be valid for as long as the message is valid, (which is until
394  * the query from which it derived is destroyed).
395  *
396  * Typical usage might be:
397  *
398  *     notmuch_message_t *message;
399  *     notmuch_thread_ids_t *thread_ids;
400  *     const char *thread_id;
401  *
402  *     message = notmuch_database_find_message (database, message_id);
403  *
404  *     for (thread_ids = notmuch_message_get_thread_ids (message);
405  *          notmuch_thread_ids_has_more (thread_ids);
406  *          notmuch_thread_ids_advance (thread_ids))
407  *     {
408  *         thread_id = notmuch_thread_ids_get (thread_ids);
409  *         ....
410  *     }
411  *
412  *     notmuch_message_destroy (message);
413  *
414  * Note that there's no explicit destructor needed for the
415  * notmuch_thread_ids_t object. (For consistency, we do provide a
416  * notmuch_thread_ids_destroy function, but there's no good reason to
417  * call it if the message is about to be destroyed).
418  */
419 notmuch_thread_ids_t *
420 notmuch_message_get_thread_ids (notmuch_message_t *message);
421
422 /* The longest possible tag value. */
423 #define NOTMUCH_TAG_MAX 200
424
425 /* Add a tag to the given message.
426  *
427  * Return value:
428  *
429  * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
430  *
431  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
432  *
433  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is longer than
434  *      too long (exceeds NOTMUCH_TAG_MAX)
435  */
436 notmuch_status_t
437 notmuch_message_add_tag (notmuch_message_t *message, const char *tag);
438
439 /* Remove a tag from the given message.
440  *
441  * Return value:
442  *
443  * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
444  *
445  * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
446  *
447  * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is longer than
448  *      too long (exceeds NOTMUCH_TAG_MAX)
449  */
450 notmuch_status_t
451 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag);
452
453 /* Destroy a notmuch_message_t object.
454  *
455  * It can be useful to call this function in the case of a single
456  * query object with many messages in the result, (such as iterating
457  * over the entire database). Otherwise, it's fine to never call this
458  * function and there will still be no memory leaks. (The memory from
459  * the messages get reclaimed when the containing query is destroyed.)
460  */
461 void
462 notmuch_message_destroy (notmuch_message_t *message);
463
464 /* Does the given notmuch_tags_t object contain any more tags.
465  *
466  * When this function returns TRUE, notmuch_tags_get will return a
467  * valid string. Whereas when this function returns FALSE,
468  * notmuch_tags_get will return NULL.
469  *
470  * See the documentation of notmuch_message_get_tags for example code
471  * showing how to iterate over a notmuch_tags_t object.
472  */
473 notmuch_bool_t
474 notmuch_tags_has_more (notmuch_tags_t *tags);
475
476 /* Get the current tag from 'tags' as a string.
477  *
478  * Note: The returned string belongs to 'tags' and has a lifetime
479  * identical to it (and the query to which it utlimately belongs).
480  *
481  * See the documentation of notmuch_message_get_tags for example code
482  * showing how to iterate over a notmuch_tags_t object.
483  */
484 const char *
485 notmuch_tags_get (notmuch_tags_t *tags);
486
487 /* Advance the 'tags' iterator to the next tag.
488  *
489  * See the documentation of notmuch_message_get_tags for example code
490  * showing how to iterate over a notmuch_tags_t object.
491  */
492 void
493 notmuch_tags_advance (notmuch_tags_t *tags);
494
495 /* Destroy a notmuch_tags_t object.
496  *
497  * It's not strictly necessary to call this function. All memory from
498  * the notmuch_tags_t object will be reclaimed when the containg
499  * message or query objects are destroyed.
500  */
501 void
502 notmuch_tags_destroy (notmuch_tags_t *tags);
503
504 /* Does the given notmuch_thread_ids_t object contain any more thread IDs.
505  *
506  * When this function returns TRUE, notmuch_thread_ids_get will return a
507  * valid string. Whereas when this function returns FALSE,
508  * notmuch_thread_ids_get will return NULL.
509  *
510  * See the documentation of notmuch_message_get_thread_ids for example code
511  * showing how to iterate over a notmuch_thread_ids_t object.
512  */
513 notmuch_bool_t
514 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids);
515
516 /* Get the current thread ID from 'thread_ids' as a string.
517  *
518  * Note: The returned string belongs to 'thread_ids' and has a lifetime
519  * identical to it (and the query to which it utlimately belongs).
520  *
521  * See the documentation of notmuch_message_get_thread_ids for example code
522  * showing how to iterate over a notmuch_thread_ids_t object.
523  */
524 const char *
525 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids);
526
527 /* Advance the 'thread_ids' iterator to the next tag.
528  *
529  * See the documentation of notmuch_message_get_thread_ids for example code
530  * showing how to iterate over a notmuch_thread_ids_t object.
531  */
532 void
533 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids);
534
535 /* Destroy a notmuch_thread_ids_t object.
536  *
537  * It's not strictly necessary to call this function. All memory from
538  * the notmuch_thread_ids_t object will be reclaimed when the containg
539  * message or query objects are destroyed.
540  */
541 void
542 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids);
543
544 NOTMUCH_END_DECLS
545
546 #endif