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
44 typedef int notmuch_bool_t;
46 /* Status codes used for the return values of most functions.
48 * A zero value (NOTMUCH_STATUS_SUCCESS) indicates that the function
49 * completed without error. Any other value indicates an error as
52 * NOTMUCH_STATUS_SUCCESS: No error occurred.
54 * XXX: We don't really want to expose this lame XAPIAN_EXCEPTION
55 * value. Instead we should map to things like DATABASE_LOCKED or
58 * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
60 * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to read or
61 * write to a file (this could be file not found, permission
64 * NOTMUCH_STATUS_FILE_NOT_EMAIL: A file was presented that doesn't
65 * appear to be an email message.
67 * NOTMUCH_STATUS_NULL_POINTER: The user erroneously passed a NULL
68 * pointer to a notmuch function.
70 * NOTMUCH_STATUS_TAG_TOO_LONG: A tag value is too long.
72 * NOTMUCH_STATUS_LAST_STATUS: Not an actual status value. Just a way
73 * to find out how many valid status values there are.
75 typedef enum _notmuch_status {
76 NOTMUCH_STATUS_SUCCESS = 0,
77 NOTMUCH_STATUS_XAPIAN_EXCEPTION,
78 NOTMUCH_STATUS_FILE_ERROR,
79 NOTMUCH_STATUS_FILE_NOT_EMAIL,
80 NOTMUCH_STATUS_NULL_POINTER,
81 NOTMUCH_STATUS_TAG_TOO_LONG,
83 NOTMUCH_STATUS_LAST_STATUS
86 /* Get a string representation of a notmuch_status_t value.
88 * The result is readonly.
91 notmuch_status_to_string (notmuch_status_t status);
93 /* Various opaque data types. For each notmuch_<foo>_t see the various
94 * notmuch_<foo> functions below. */
95 typedef struct _notmuch_database notmuch_database_t;
96 typedef struct _notmuch_query notmuch_query_t;
97 typedef struct _notmuch_results notmuch_results_t;
98 typedef struct _notmuch_message notmuch_message_t;
99 typedef struct _notmuch_tags notmuch_tags_t;
100 typedef struct _notmuch_thread_ids notmuch_thread_ids_t;
102 /* Create a new, empty notmuch database located at 'path'.
104 * The path should be a top-level directory to a collection of
105 * plain-text email messages (one message per file). This call will
106 * create a new ".notmuch" directory within 'path' where notmuch will
109 * Passing a value of NULL for 'path' will cause notmuch to open the
110 * default database. The default database path can be specified by the
111 * NOTMUCH_BASE environment variable, and is equivalent to
112 * ${HOME}/mail if NOTMUCH_BASE is not set.
114 * After a successful call to notmuch_database_create, the returned
115 * database will be open so the caller should call
116 * notmuch_database_close when finished with it.
118 * The database will not yet have any data in it
119 * (notmuch_database_create itself is a very cheap function). Messages
120 * contained within 'path' can be added to the database by calling
121 * notmuch_database_add_message.
123 * In case of any failure, this function returns NULL, (after printing
124 * an error message on stderr).
127 notmuch_database_create (const char *path);
129 /* Open a an existing notmuch database located at 'path'.
131 * The database should have been created at some time in the past,
132 * (not necessarily by this process), by calling
133 * notmuch_database_create with 'path'.
135 * An existing notmuch database can be identified by the presence of a
136 * directory named ".notmuch" below 'path'.
138 * Passing a value of NULL for 'path' will cause notmuch to open the
139 * default database. The default database path can be specified by the
140 * NOTMUCH_BASE environment variable, and is equivalent to
141 * ${HOME}/mail if NOTMUCH_BASE is not set.
143 * The caller should call notmuch_database_close when finished with
146 * In case of any failure, this function returns NULL, (after printing
147 * an error message on stderr).
150 notmuch_database_open (const char *path);
152 /* Close the given notmuch database, freeing all associated
153 * resources. See notmuch_database_open. */
155 notmuch_database_close (notmuch_database_t *database);
157 /* Lookup the default database path.
159 * This is the path that will be used by notmuch_database_create and
160 * notmuch_database_open if given a NULL path. Specifically it will be
161 * the value of the NOTMUCH_BASE environment variable if set,
162 * otherwise ${HOME}/mail
164 * Returns a newly allocated string which the caller should free()
165 * when finished with it.
168 notmuch_database_default_path (void);
170 /* Return the database path of the given database.
172 * The return value is a string owned by notmuch so should not be
173 * modified nor freed by the caller. */
175 notmuch_database_get_path (notmuch_database_t *database);
177 /* Store a timestamp within the database.
179 * The Notmuch database will not interpret this key nor the timestamp
180 * values at all. It will merely store them together and return the
181 * timestamp when notmuch_database_get_timestamp is called with the
182 * same value for 'key'.
184 * The intention is for the caller to use the timestamp to allow
185 * efficient identification of new messages to be added to the
186 * database. The recommended usage is as follows:
188 * o Read the mtime of a directory from the filesystem
190 * o Call add_message for all mail files in the directory
192 * o Call notmuch_database_set_timestamp with the path of the
193 * directory as 'key' and the originally read mtime as 'value'.
195 * Then, when wanting to check for updates to the directory in the
196 * future, the client can call notmuch_database_get_timestamp and know
197 * that it only needs to add files if the mtime of the directory and
198 * files are newer than the stored timestamp.
200 * Note: The notmuch_database_get_timestamp function does not allow
201 * the caller to distinguish a timestamp of 0 from a non-existent
202 * timestamp. So don't store a timestamp of 0 unless you are
203 * comfortable with that.
207 * NOTMUCH_STATUS_SUCCESS: Timestamp successfully stored in database.
209 * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception
210 * occurred. Timestamp not stored.
213 notmuch_database_set_timestamp (notmuch_database_t *database,
214 const char *key, time_t timestamp);
216 /* Retrieve a timestamp from the database.
218 * Returns the timestamp value previously stored by calling
219 * notmuch_database_set_timestamp with the same value for 'key'.
221 * Returns 0 if no timestamp is stored for 'key' or if any error
222 * occurred querying the database.
225 notmuch_database_get_timestamp (notmuch_database_t *database,
228 /* Add a new message to the given notmuch database.
230 * Here,'filename' should be a path relative to the the path of
231 * 'database' (see notmuch_database_get_path). The file should be a
232 * single mail message (not a multi-message mbox) that is expected to
233 * remain at its current location, (since the notmuch database will
234 * reference the filename, and will not copy the entire contents of
239 * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
241 * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
242 * file, (such as permission denied, or file not found,
243 * etc.). Nothing added to the database.
245 * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
246 * like an email message. Nothing added to the database.
249 notmuch_database_add_message (notmuch_database_t *database,
250 const char *filename);
252 /* Find a message with the given messsage_id.
254 * If the database contains a message with the given message_id, then
255 * a new notmuch_message_t object is returned. The caller should call
256 * notmuch_message_destroy when done with the message.
258 * If no message is found with the given message_id, this function
262 notmuch_database_find_message (notmuch_database_t *database,
263 const char *message_id);
265 /* Create a new query for 'database'.
267 * Here, 'database' should be an open database, (see
268 * notmuch_database_open and notmuch_database_create).
270 * For the query string, we'll document the syntax here more
271 * completely in the future, but it's likely to be a specialized
272 * version of the general Xapian query syntax:
274 * http://xapian.org/docs/queryparser.html
276 * As a special case, passing a length-zero string, (that is ""), will
277 * result in a query that returns all messages in the database.
279 * See notmuch_query_set_sort for controlling the order of results and
280 * notmuch_query_search to actually execute the query.
282 * User should call notmuch_query_destroy when finished with this
285 * Will return NULL if insufficient memory is available.
288 notmuch_query_create (notmuch_database_t *database,
289 const char *query_string);
291 /* Sort values for notmuch_query_set_sort */
293 NOTMUCH_SORT_DATE_OLDEST_FIRST,
294 NOTMUCH_SORT_DATE_NEWEST_FIRST,
295 NOTMUCH_SORT_MESSAGE_ID
298 /* Specify the sorting desired for this query. */
300 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
302 /* Execute a query, returning a notmuch_results_t object which can be
303 * used to iterate over the results. The results object is owned by
304 * the query and as such, will only be valid until notmuch_query_destroy.
306 * Typical usage might be:
308 * notmuch_query_t *query;
309 * notmuch_results_t *results;
311 * query = notmuch_query_create (database, query_string);
313 * for (results = notmuch_query_search (query);
314 * notmuch_results_has_more (results);
315 * notmuch_result_advance (results))
317 * message = notmuch_results_get (results);
319 * notmuch_message_destroy (message);
322 * notmuch_query_destroy (query);
324 * Note: If you are finished with a message before its containing
325 * query, you can call notmuch_message_destroy to clean up some memory
326 * sooner (as in the above example). Otherwise, if your message
327 * objects are long-lived, then you don't need to call
328 * notmuch_message_destroy and all the memory will still be reclaimed
329 * when the query is destroyed.
331 * Note that there's no explicit destructor needed for the
332 * notmuch_results_t object. (For consistency, we do provide a
333 * notmuch_results_destroy function, but there's no good reason to
334 * call it if the query is about to be destroyed).
337 notmuch_query_search (notmuch_query_t *query);
339 /* Destroy a notmuch_query_t along with any associated resources.
341 * This will in turn destroy any notmuch_results_t objects generated
342 * by this query, (and in turn any notmuch_message_t objects generated
343 * from those results, etc.).
346 notmuch_query_destroy (notmuch_query_t *query);
348 /* Does the given notmuch_results_t object contain any more results.
350 * When this function returns TRUE, notmuch_results_get will return a
351 * valid object. Whereas when this function returns FALSE,
352 * notmuch_results_get will return NULL.
354 * See the documentation of notmuch_query_search for example code
355 * showing how to iterate over a notmuch_results_t object.
358 notmuch_results_has_more (notmuch_results_t *results);
360 /* Get the current result from 'results' as a notmuch_message_t.
362 * Note: The returned message belongs to 'results' and has a lifetime
363 * identical to it (and the query to which it belongs).
365 * See the documentation of notmuch_query_search for example code
366 * showing how to iterate over a notmuch_results_t object.
369 notmuch_results_get (notmuch_results_t *results);
371 /* Advance the 'results' iterator to the next result.
373 * See the documentation of notmuch_query_search for example code
374 * showing how to iterate over a notmuch_results_t object.
377 notmuch_results_advance (notmuch_results_t *results);
379 /* Destroy a notmuch_results_t object.
381 * It's not strictly necessary to call this function. All memory from
382 * the notmuch_results_t object will be reclaimed when the containg
383 * query object is destroyed.
386 notmuch_results_destroy (notmuch_results_t *results);
388 /* Get the message ID of 'message'.
390 * The returned string belongs to 'message' and as such, should not be
391 * modified by the caller and will only be valid for as long as the
392 * message is valid, (which is until the query from which it derived
395 * This function will not return NULL since Notmuch ensures that every
396 * message has a unique message ID, (Notmuch will generate an ID for a
397 * message if the original file does not contain one).
400 notmuch_message_get_message_id (notmuch_message_t *message);
402 /* Get this filename for the email corresponding to 'message'.
404 * The returned filename is relative to the base of the database from
405 * which 'message' was obtained. See notmuch_database_get_path() .*/
407 notmuch_message_get_filename (notmuch_message_t *message);
409 /* Get the tags for 'message', returning a notmuch_tags_t object which
410 * can be used to iterate over all tags.
412 * The tags object is owned by the message and as such, will only be
413 * valid for as long as the message is valid, (which is until the
414 * query from which it derived is destroyed).
416 * Typical usage might be:
418 * notmuch_message_t *message;
419 * notmuch_tags_t *tags;
422 * message = notmuch_database_find_message (database, message_id);
424 * for (tags = notmuch_message_get_tags (message);
425 * notmuch_tags_has_more (tags);
426 * notmuch_result_advance (tags))
428 * tag = notmuch_tags_get (tags);
432 * notmuch_message_destroy (message);
434 * Note that there's no explicit destructor needed for the
435 * notmuch_tags_t object. (For consistency, we do provide a
436 * notmuch_tags_destroy function, but there's no good reason to call
437 * it if the message is about to be destroyed).
440 notmuch_message_get_tags (notmuch_message_t *message);
442 /* Get the thread IDs for 'message', returning a notmuch_thread_ids_t
443 * object which can be used to iterate over all thread IDs.
445 * The thread_ids object is owned by the message and as such, will
446 * only be valid for as long as the message is valid, (which is until
447 * the query from which it derived is destroyed).
449 * Typical usage might be:
451 * notmuch_message_t *message;
452 * notmuch_thread_ids_t *thread_ids;
453 * const char *thread_id;
455 * message = notmuch_database_find_message (database, message_id);
457 * for (thread_ids = notmuch_message_get_thread_ids (message);
458 * notmuch_thread_ids_has_more (thread_ids);
459 * notmuch_thread_ids_advance (thread_ids))
461 * thread_id = notmuch_thread_ids_get (thread_ids);
465 * notmuch_message_destroy (message);
467 * Note that there's no explicit destructor needed for the
468 * notmuch_thread_ids_t object. (For consistency, we do provide a
469 * notmuch_thread_ids_destroy function, but there's no good reason to
470 * call it if the message is about to be destroyed).
472 notmuch_thread_ids_t *
473 notmuch_message_get_thread_ids (notmuch_message_t *message);
475 /* The longest possible tag value. */
476 #define NOTMUCH_TAG_MAX 200
478 /* Add a tag to the given message.
482 * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
484 * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
486 * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is longer than
487 * too long (exceeds NOTMUCH_TAG_MAX)
490 notmuch_message_add_tag (notmuch_message_t *message, const char *tag);
492 /* Remove a tag from the given message.
496 * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
498 * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
500 * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is longer than
501 * too long (exceeds NOTMUCH_TAG_MAX)
504 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag);
506 /* Destroy a notmuch_message_t object.
508 * It can be useful to call this function in the case of a single
509 * query object with many messages in the result, (such as iterating
510 * over the entire database). Otherwise, it's fine to never call this
511 * function and there will still be no memory leaks. (The memory from
512 * the messages get reclaimed when the containing query is destroyed.)
515 notmuch_message_destroy (notmuch_message_t *message);
517 /* Does the given notmuch_tags_t object contain any more tags.
519 * When this function returns TRUE, notmuch_tags_get will return a
520 * valid string. Whereas when this function returns FALSE,
521 * notmuch_tags_get will return NULL.
523 * See the documentation of notmuch_message_get_tags for example code
524 * showing how to iterate over a notmuch_tags_t object.
527 notmuch_tags_has_more (notmuch_tags_t *tags);
529 /* Get the current tag from 'tags' as a string.
531 * Note: The returned string belongs to 'tags' and has a lifetime
532 * identical to it (and the query to which it utlimately belongs).
534 * See the documentation of notmuch_message_get_tags for example code
535 * showing how to iterate over a notmuch_tags_t object.
538 notmuch_tags_get (notmuch_tags_t *tags);
540 /* Advance the 'tags' iterator to the next tag.
542 * See the documentation of notmuch_message_get_tags for example code
543 * showing how to iterate over a notmuch_tags_t object.
546 notmuch_tags_advance (notmuch_tags_t *tags);
548 /* Destroy a notmuch_tags_t object.
550 * It's not strictly necessary to call this function. All memory from
551 * the notmuch_tags_t object will be reclaimed when the containg
552 * message or query objects are destroyed.
555 notmuch_tags_destroy (notmuch_tags_t *tags);
557 /* Does the given notmuch_thread_ids_t object contain any more thread IDs.
559 * When this function returns TRUE, notmuch_thread_ids_get will return a
560 * valid string. Whereas when this function returns FALSE,
561 * notmuch_thread_ids_get will return NULL.
563 * See the documentation of notmuch_message_get_thread_ids for example code
564 * showing how to iterate over a notmuch_thread_ids_t object.
567 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids);
569 /* Get the current thread ID from 'thread_ids' as a string.
571 * Note: The returned string belongs to 'thread_ids' and has a lifetime
572 * identical to it (and the query to which it utlimately belongs).
574 * See the documentation of notmuch_message_get_thread_ids for example code
575 * showing how to iterate over a notmuch_thread_ids_t object.
578 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids);
580 /* Advance the 'thread_ids' iterator to the next tag.
582 * See the documentation of notmuch_message_get_thread_ids for example code
583 * showing how to iterate over a notmuch_thread_ids_t object.
586 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids);
588 /* Destroy a notmuch_thread_ids_t object.
590 * It's not strictly necessary to call this function. All memory from
591 * the notmuch_thread_ids_t object will be reclaimed when the containg
592 * message or query objects are destroyed.
595 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids);