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