1 /* notmuch - Not much of an email library, (just index and search)
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
25 # define NOTMUCH_BEGIN_DECLS extern "C" {
26 # define NOTMUCH_END_DECLS }
28 # define NOTMUCH_BEGIN_DECLS
29 # define NOTMUCH_END_DECLS
42 typedef int notmuch_bool_t;
44 /* Status codes used for the return values of most functions.
46 * A zero value (NOTMUCH_STATUS_SUCCESS) indicates that the function
47 * completed without error. Any other value indicates an error as
50 * NOTMUCH_STATUS_SUCCESS: No error occurred.
52 * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
54 * NOTMUCH_STATUS_FILE_NOT_EMAIL: A file was presented that doesn't
55 * appear to be an email message.
57 typedef enum _notmuch_status {
58 NOTMUCH_STATUS_SUCCESS = 0,
59 NOTMUCH_STATUS_XAPIAN_EXCEPTION,
60 NOTMUCH_STATUS_FILE_NOT_EMAIL
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;
72 /* Create a new, empty notmuch database located at 'path'.
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
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.
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.
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.
93 * In case of any failure, this function returns NULL, (after printing
94 * an error message on stderr).
97 notmuch_database_create (const char *path);
99 /* Open a an existing notmuch database located at 'path'.
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'.
105 * An existing notmuch database can be identified by the presence of a
106 * directory named ".notmuch" below 'path'.
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.
113 * The caller should call notmuch_database_close when finished with
116 * In case of any failure, this function returns NULL, (after printing
117 * an error message on stderr).
120 notmuch_database_open (const char *path);
122 /* Close the given notmuch database, freeing all associated
123 * resources. See notmuch_database_open. */
125 notmuch_database_close (notmuch_database_t *database);
127 /* Lookup the default database path.
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
134 * Returns a newly allocated string which the caller should free()
135 * when finished with it.
138 notmuch_database_default_path (void);
140 /* Return the database path of the given database.
142 * The return value is a string owned by notmuch so should not be
143 * modified nor freed by the caller. */
145 notmuch_database_get_path (notmuch_database_t *database);
147 /* Add a new message to the given notmuch database.
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
158 * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
160 * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
161 * like an email message. Nothing added to the database.
164 notmuch_database_add_message (notmuch_database_t *database,
165 const char *filename);
167 /* Create a new query for 'database'.
169 * Here, 'database' should be an open database, (see
170 * notmuch_database_open and notmuch_database_create).
172 * For the query string, we'll document the syntax here more
173 * completely in the future, but it's likely to be a specialized
174 * version of the general Xapian query syntax:
176 * http://xapian.org/docs/queryparser.html
178 * As a special case, passing a length-zero string, (that is ""), will
179 * result in a query that returns all messages in the database.
181 * See notmuch_query_set_sort for controlling the order of results and
182 * notmuch_query_search to actually execute the query.
184 * User should call notmuch_query_destroy when finished with this
187 * Will return NULL if insufficient memory is available.
190 notmuch_query_create (notmuch_database_t *database,
191 const char *query_string);
193 /* Sort values for notmuch_query_set_sort */
195 NOTMUCH_SORT_DATE_OLDEST_FIRST,
196 NOTMUCH_SORT_DATE_NEWEST_FIRST,
197 NOTMUCH_SORT_MESSAGE_ID
200 /* Specify the sorting desired for this query. */
202 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
204 /* Execute a query, returning a notmuch_results_t object which can be
205 * used to iterate over the results. The results object is owned by
206 * the query and as such, will only be valid until notmuch_query_destroy.
208 * Typical usage might be:
210 * notmuch_query_t *query;
211 * notmuch_results_t *results;
213 * query = notmuch_query_create (database, query_string);
215 * for (results = notmuch_query_search (query);
216 * notmuch_results_has_more (results);
217 * notmuch_result_advance (results))
219 * message = notmuch_results_get (results);
223 * notmuch_query_destroy (query);
225 * Note that there's no explicit destructor needed for the
226 * notmuch_results_t object.
228 * (For consistency, we do provide a notmuch_results_destroy function,
229 * but there's no point in calling it if you're about to destroy the
230 * query object as well too---either call will free all the memory of
234 notmuch_query_search (notmuch_query_t *query);
236 /* Destroy a notmuch_query_t along with any associated resources.
238 * This will in turn destroy any notmuch_results_t objects generated
239 * by this query, (and in turn any notmuch_message_t objects generated
240 * from those results, etc.).
243 notmuch_query_destroy (notmuch_query_t *query);
245 /* Does the given notmuch_results_t object contain any more results.
247 * When this function returns TRUE, notmuch_results_get will return a
248 * valid object. Whereas when this function returns FALSE,
249 * notmuch_results_get will return NULL.
251 * See the documentation of notmuch_query_search for example code
252 * showing how to iterate over a notmuch_results_t object.
255 notmuch_results_has_more (notmuch_results_t *results);
257 /* Get the current result from 'results' as a notmuch_message_t.
259 * Note: The returned message belongs to 'results' and has a lifetime
260 * identical to it (and the query to which it belongs).
262 * See the documentation of notmuch_query_search for example code
263 * showing how to iterate over a notmuch_results_t object.
266 notmuch_results_get (notmuch_results_t *results);
268 /* Advance the 'results' iterator to the next result.
270 * See the documentation of notmuch_query_search for example code
271 * showing how to iterate over a notmuch_results_t object.
274 notmuch_results_advance (notmuch_results_t *results);
276 /* Destroy a notmuch_results_t object.
278 * It's not strictly necessary to call this function. All memory from
279 * the notmuch_results_t object will be reclaimed when the containg
280 * query object is destroyed.
283 notmuch_results_destroy (notmuch_results_t *results);
285 /* Get the message ID of 'message'.
287 * The returned string belongs to 'message' and as such, should not be
288 * modified by the caller and will only be valid for as long as the
289 * message is valid, (which is until the query from which it derived
293 notmuch_message_get_message_id (notmuch_message_t *message);
295 /* Get the tags for 'message', returning a notmuch_tags_t object which
296 * can be used to iterate over all tags.
298 * The tags object is owned by the message and as such, will only be
299 * valid for as long as the message is valid, (which is until the
300 * query from which it derived is destroyed).
302 * Typical usage might be:
304 * notmuch_message_t *message;
305 * notmuch_tags_t *tags;
308 * message = notmuch_database_find_message (database, message_id);
310 * for (tags = notmuch_message_get_tags (message);
311 * notmuch_tags_has_more (tags);
312 * notmuch_result_advance (tags))
314 * tag = notmuch_tags_get (tags);
318 * notmuch_message_destroy (message);
320 * Note that there's no explicit destructor needed for the
321 * notmuch_tags_t object. (For consistency, we do provide a
322 * notmuch_tags_destroy function, but there's no good reason to call
323 * it if the message is about to be destroyed).
326 notmuch_message_get_tags (notmuch_message_t *message);
328 /* Get the thread IDs for 'message', returning a notmuch_thread_ids_t
329 * object which can be used to iterate over all thread IDs.
331 * The thread_ids object is owned by the message and as such, will
332 * only be valid for as long as the message is valid, (which is until
333 * the query from which it derived is destroyed).
335 * Typical usage might be:
337 * notmuch_message_t *message;
338 * notmuch_thread_ids_t *thread_ids;
339 * const char *thread_id;
341 * message = notmuch_database_find_message (database, message_id);
343 * for (thread_ids = notmuch_message_get_thread_ids (message);
344 * notmuch_thread_ids_has_more (thread_ids);
345 * notmuch_thread_ids_advance (thread_ids))
347 * thread_id = notmuch_thread_ids_get (thread_ids);
351 * notmuch_message_destroy (message);
353 * Note that there's no explicit destructor needed for the
354 * notmuch_thread_ids_t object. (For consistency, we do provide a
355 * notmuch_thread_ids_destroy function, but there's no good reason to
356 * call it if the message is about to be destroyed).
358 notmuch_thread_ids_t *
359 notmuch_message_get_thread_ids (notmuch_message_t *message);
361 /* Destroy a notmuch_message_t object.
363 * It can be useful to call this function in the case of a single
364 * query object with many messages in the result, (such as iterating
365 * over the entire database). Otherwise, it's fine to never call this
366 * function and there will still be no memory leaks. (The memory from
367 * the messages get reclaimed when the containing query is destroyed.)
370 notmuch_message_destroy (notmuch_message_t *message);
372 /* Does the given notmuch_tags_t object contain any more results.
374 * When this function returns TRUE, notmuch_tags_get will return a
375 * valid string. Whereas when this function returns FALSE,
376 * notmuch_tags_get will return NULL.
378 * See the documentation of notmuch_message_get_tags for example code
379 * showing how to iterate over a notmuch_tags_t object.
382 notmuch_tags_has_more (notmuch_tags_t *tags);
384 /* Get the current result from 'tags' as a string.
386 * Note: The returned string belongs to 'tags' and has a lifetime
387 * identical to it (and the query to which it utlimately belongs).
389 * See the documentation of notmuch_message_get_tags for example code
390 * showing how to iterate over a notmuch_tags_t object.
393 notmuch_tags_get (notmuch_tags_t *tags);
395 /* Advance the 'tags' iterator to the next tag.
397 * See the documentation of notmuch_message_get_tags for example code
398 * showing how to iterate over a notmuch_tags_t object.
401 notmuch_tags_advance (notmuch_tags_t *results);
403 /* Destroy a notmuch_tags_t object.
405 * It's not strictly necessary to call this function. All memory from
406 * the notmuch_tags_t object will be reclaimed when the containg
407 * message or query objects are destroyed.
410 notmuch_tags_destroy (notmuch_tags_t *tags);
412 /* Does the given notmuch_thread_ids_t object contain any more thread IDs.
414 * When this function returns TRUE, notmuch_thread_ids_get will return a
415 * valid string. Whereas when this function returns FALSE,
416 * notmuch_thread_ids_get will return NULL.
418 * See the documentation of notmuch_message_get_thread_ids for example code
419 * showing how to iterate over a notmuch_thread_ids_t object.
422 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids);
424 /* Get the current thread ID from 'thread_ids' as a string.
426 * Note: The returned string belongs to 'thread_ids' and has a lifetime
427 * identical to it (and the query to which it utlimately belongs).
429 * See the documentation of notmuch_message_get_thread_ids for example code
430 * showing how to iterate over a notmuch_thread_ids_t object.
433 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids);
435 /* Advance the 'thread_ids' iterator to the next tag.
437 * See the documentation of notmuch_message_get_thread_ids for example code
438 * showing how to iterate over a notmuch_thread_ids_t object.
441 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids);
443 /* Destroy a notmuch_thread_ids_t object.
445 * It's not strictly necessary to call this function. All memory from
446 * the notmuch_thread_ids_t object will be reclaimed when the containg
447 * message or query objects are destroyed.
450 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids);