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