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