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_DUPLICATE_MESSAGE_ID: A file contains a message ID
68 * that is identical to a message already in the database.
70 * NOTMUCH_STATUS_NULL_POINTER: The user erroneously passed a NULL
71 * pointer to a notmuch function.
73 * NOTMUCH_STATUS_TAG_TOO_LONG: A tag value is too long (exceeds
76 * NOTMUCH_STATUS_LAST_STATUS: Not an actual status value. Just a way
77 * to find out how many valid status values there are.
79 typedef enum _notmuch_status {
80 NOTMUCH_STATUS_SUCCESS = 0,
81 NOTMUCH_STATUS_XAPIAN_EXCEPTION,
82 NOTMUCH_STATUS_FILE_ERROR,
83 NOTMUCH_STATUS_FILE_NOT_EMAIL,
84 NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID,
85 NOTMUCH_STATUS_NULL_POINTER,
86 NOTMUCH_STATUS_TAG_TOO_LONG,
88 NOTMUCH_STATUS_LAST_STATUS
91 /* Get a string representation of a notmuch_status_t value.
93 * The result is readonly.
96 notmuch_status_to_string (notmuch_status_t status);
98 /* Various opaque data types. For each notmuch_<foo>_t see the various
99 * notmuch_<foo> functions below. */
100 typedef struct _notmuch_database notmuch_database_t;
101 typedef struct _notmuch_query notmuch_query_t;
102 typedef struct _notmuch_results notmuch_results_t;
103 typedef struct _notmuch_message notmuch_message_t;
104 typedef struct _notmuch_tags notmuch_tags_t;
105 typedef struct _notmuch_thread_ids notmuch_thread_ids_t;
107 /* Lookup the default database path.
109 * This is the path that will be used by notmuch_database_create and
110 * notmuch_database_open if given a NULL path. Specifically it will be
111 * the value of the NOTMUCH_BASE environment variable if set,
112 * otherwise ${HOME}/mail
114 * Returns a newly allocated string which the caller should free()
115 * when finished with it.
118 notmuch_database_default_path (void);
120 /* Create a new, empty notmuch database located at 'path'.
122 * The path should be a top-level directory to a collection of
123 * plain-text email messages (one message per file). This call will
124 * create a new ".notmuch" directory within 'path' where notmuch will
127 * Passing a value of NULL for 'path' will cause notmuch to open the
128 * default database. The default database path can be specified by the
129 * NOTMUCH_BASE environment variable, and is equivalent to
130 * ${HOME}/mail if NOTMUCH_BASE is not set.
132 * After a successful call to notmuch_database_create, the returned
133 * database will be open so the caller should call
134 * notmuch_database_close when finished with it.
136 * The database will not yet have any data in it
137 * (notmuch_database_create itself is a very cheap function). Messages
138 * contained within 'path' can be added to the database by calling
139 * notmuch_database_add_message.
141 * In case of any failure, this function returns NULL, (after printing
142 * an error message on stderr).
145 notmuch_database_create (const char *path);
147 /* Open a an existing notmuch database located at 'path'.
149 * The database should have been created at some time in the past,
150 * (not necessarily by this process), by calling
151 * notmuch_database_create with 'path'.
153 * An existing notmuch database can be identified by the presence of a
154 * directory named ".notmuch" below 'path'.
156 * Passing a value of NULL for 'path' will cause notmuch to open the
157 * default database. The default database path can be specified by the
158 * NOTMUCH_BASE environment variable, and is equivalent to
159 * ${HOME}/mail if NOTMUCH_BASE is not set.
161 * The caller should call notmuch_database_close when finished with
164 * In case of any failure, this function returns NULL, (after printing
165 * an error message on stderr).
168 notmuch_database_open (const char *path);
170 /* Close the given notmuch database, freeing all associated
171 * resources. See notmuch_database_open. */
173 notmuch_database_close (notmuch_database_t *database);
175 /* Return the database path of the given database.
177 * The return value is a string owned by notmuch so should not be
178 * modified nor freed by the caller. */
180 notmuch_database_get_path (notmuch_database_t *database);
182 /* Store a timestamp within the database.
184 * The Notmuch database will not interpret this key nor the timestamp
185 * values at all. It will merely store them together and return the
186 * timestamp when notmuch_database_get_timestamp is called with the
187 * same value for 'key'.
189 * The intention is for the caller to use the timestamp to allow
190 * efficient identification of new messages to be added to the
191 * database. The recommended usage is as follows:
193 * o Read the mtime of a directory from the filesystem
195 * o Call add_message for all mail files in the directory
197 * o Call notmuch_database_set_timestamp with the path of the
198 * directory as 'key' and the originally read mtime as 'value'.
200 * Then, when wanting to check for updates to the directory in the
201 * future, the client can call notmuch_database_get_timestamp and know
202 * that it only needs to add files if the mtime of the directory and
203 * files are newer than the stored timestamp.
205 * Note: The notmuch_database_get_timestamp function does not allow
206 * the caller to distinguish a timestamp of 0 from a non-existent
207 * timestamp. So don't store a timestamp of 0 unless you are
208 * comfortable with that.
212 * NOTMUCH_STATUS_SUCCESS: Timestamp successfully stored in database.
214 * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception
215 * occurred. Timestamp not stored.
218 notmuch_database_set_timestamp (notmuch_database_t *database,
219 const char *key, time_t timestamp);
221 /* Retrieve a timestamp from the database.
223 * Returns the timestamp value previously stored by calling
224 * notmuch_database_set_timestamp with the same value for 'key'.
226 * Returns 0 if no timestamp is stored for 'key' or if any error
227 * occurred querying the database.
230 notmuch_database_get_timestamp (notmuch_database_t *database,
233 /* Add a new message to the given notmuch database.
235 * Here,'filename' should be a path relative to the the path of
236 * 'database' (see notmuch_database_get_path). The file should be a
237 * single mail message (not a multi-message mbox) that is expected to
238 * remain at its current location, (since the notmuch database will
239 * reference the filename, and will not copy the entire contents of
244 * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
246 * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message
247 * ID as another message already in the database. Nothing added
250 * NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the
251 * file, (such as permission denied, or file not found,
252 * etc.). Nothing added to the database.
254 * NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look
255 * like an email message. Nothing added to the database.
258 notmuch_database_add_message (notmuch_database_t *database,
259 const char *filename);
261 /* Find a message with the given messsage_id.
263 * If the database contains a message with the given message_id, then
264 * a new notmuch_message_t object is returned. The caller should call
265 * notmuch_message_destroy when done with the message.
267 * If no message is found with the given message_id, this function
271 notmuch_database_find_message (notmuch_database_t *database,
272 const char *message_id);
274 /* Create a new query for 'database'.
276 * Here, 'database' should be an open database, (see
277 * notmuch_database_open and notmuch_database_create).
279 * For the query string, we'll document the syntax here more
280 * completely in the future, but it's likely to be a specialized
281 * version of the general Xapian query syntax:
283 * http://xapian.org/docs/queryparser.html
285 * As a special case, passing a length-zero string, (that is ""), will
286 * result in a query that returns all messages in the database.
288 * See notmuch_query_set_sort for controlling the order of results and
289 * notmuch_query_search to actually execute the query.
291 * User should call notmuch_query_destroy when finished with this
294 * Will return NULL if insufficient memory is available.
297 notmuch_query_create (notmuch_database_t *database,
298 const char *query_string);
300 /* Sort values for notmuch_query_set_sort */
302 NOTMUCH_SORT_DATE_OLDEST_FIRST,
303 NOTMUCH_SORT_DATE_NEWEST_FIRST,
304 NOTMUCH_SORT_MESSAGE_ID
307 /* Specify the sorting desired for this query. */
309 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
311 /* Execute a query, returning a notmuch_results_t object which can be
312 * used to iterate over the results. The results object is owned by
313 * the query and as such, will only be valid until notmuch_query_destroy.
315 * Typical usage might be:
317 * notmuch_query_t *query;
318 * notmuch_results_t *results;
320 * query = notmuch_query_create (database, query_string);
322 * for (results = notmuch_query_search (query);
323 * notmuch_results_has_more (results);
324 * notmuch_result_advance (results))
326 * message = notmuch_results_get (results);
328 * notmuch_message_destroy (message);
331 * notmuch_query_destroy (query);
333 * Note: If you are finished with a message before its containing
334 * query, you can call notmuch_message_destroy to clean up some memory
335 * sooner (as in the above example). Otherwise, if your message
336 * objects are long-lived, then you don't need to call
337 * notmuch_message_destroy and all the memory will still be reclaimed
338 * when the query is destroyed.
340 * Note that there's no explicit destructor needed for the
341 * notmuch_results_t object. (For consistency, we do provide a
342 * notmuch_results_destroy function, but there's no good reason to
343 * call it if the query is about to be destroyed).
346 notmuch_query_search (notmuch_query_t *query);
348 /* Destroy a notmuch_query_t along with any associated resources.
350 * This will in turn destroy any notmuch_results_t objects generated
351 * by this query, (and in turn any notmuch_message_t objects generated
352 * from those results, etc.).
355 notmuch_query_destroy (notmuch_query_t *query);
357 /* Does the given notmuch_results_t object contain any more results.
359 * When this function returns TRUE, notmuch_results_get will return a
360 * valid object. Whereas when this function returns FALSE,
361 * notmuch_results_get will return NULL.
363 * See the documentation of notmuch_query_search for example code
364 * showing how to iterate over a notmuch_results_t object.
367 notmuch_results_has_more (notmuch_results_t *results);
369 /* Get the current result from 'results' as a notmuch_message_t.
371 * Note: The returned message belongs to 'results' and has a lifetime
372 * identical to it (and the query to which it belongs).
374 * See the documentation of notmuch_query_search for example code
375 * showing how to iterate over a notmuch_results_t object.
378 notmuch_results_get (notmuch_results_t *results);
380 /* Advance the 'results' iterator to the next result.
382 * See the documentation of notmuch_query_search for example code
383 * showing how to iterate over a notmuch_results_t object.
386 notmuch_results_advance (notmuch_results_t *results);
388 /* Destroy a notmuch_results_t object.
390 * It's not strictly necessary to call this function. All memory from
391 * the notmuch_results_t object will be reclaimed when the containg
392 * query object is destroyed.
395 notmuch_results_destroy (notmuch_results_t *results);
397 /* Get the message ID of 'message'.
399 * The returned string belongs to 'message' and as such, should not be
400 * modified by the caller and will only be valid for as long as the
401 * message is valid, (which is until the query from which it derived
404 * This function will not return NULL since Notmuch ensures that every
405 * message has a unique message ID, (Notmuch will generate an ID for a
406 * message if the original file does not contain one).
409 notmuch_message_get_message_id (notmuch_message_t *message);
411 /* Get the filename for the email corresponding to 'message'.
413 * The returned filename is relative to the base of the database from
414 * which 'message' was obtained. See notmuch_database_get_path() .
415 * The returned string belongs to the message so should not be
416 * modified or freed by the caller (nor should it be referenced after
417 * the message is destroyed). */
419 notmuch_message_get_filename (notmuch_message_t *message);
421 /* Get the tags for 'message', returning a notmuch_tags_t object which
422 * can be used to iterate over all tags.
424 * The tags object is owned by the message and as such, will only be
425 * valid for as long as the message is valid, (which is until the
426 * query from which it derived is destroyed).
428 * Typical usage might be:
430 * notmuch_message_t *message;
431 * notmuch_tags_t *tags;
434 * message = notmuch_database_find_message (database, message_id);
436 * for (tags = notmuch_message_get_tags (message);
437 * notmuch_tags_has_more (tags);
438 * notmuch_result_advance (tags))
440 * tag = notmuch_tags_get (tags);
444 * notmuch_message_destroy (message);
446 * Note that there's no explicit destructor needed for the
447 * notmuch_tags_t object. (For consistency, we do provide a
448 * notmuch_tags_destroy function, but there's no good reason to call
449 * it if the message is about to be destroyed).
452 notmuch_message_get_tags (notmuch_message_t *message);
454 /* Get the thread IDs for 'message', returning a notmuch_thread_ids_t
455 * object which can be used to iterate over all thread IDs.
457 * The thread_ids object is owned by the message and as such, will
458 * only be valid for as long as the message is valid, (which is until
459 * the query from which it derived is destroyed).
461 * Typical usage might be:
463 * notmuch_message_t *message;
464 * notmuch_thread_ids_t *thread_ids;
465 * const char *thread_id;
467 * message = notmuch_database_find_message (database, message_id);
469 * for (thread_ids = notmuch_message_get_thread_ids (message);
470 * notmuch_thread_ids_has_more (thread_ids);
471 * notmuch_thread_ids_advance (thread_ids))
473 * thread_id = notmuch_thread_ids_get (thread_ids);
477 * notmuch_message_destroy (message);
479 * Note that there's no explicit destructor needed for the
480 * notmuch_thread_ids_t object. (For consistency, we do provide a
481 * notmuch_thread_ids_destroy function, but there's no good reason to
482 * call it if the message is about to be destroyed).
484 notmuch_thread_ids_t *
485 notmuch_message_get_thread_ids (notmuch_message_t *message);
487 /* The longest possible tag value. */
488 #define NOTMUCH_TAG_MAX 200
490 /* Add a tag to the given message.
494 * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
496 * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
498 * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is longer than
499 * too long (exceeds NOTMUCH_TAG_MAX)
502 notmuch_message_add_tag (notmuch_message_t *message, const char *tag);
504 /* Remove a tag from the given message.
508 * NOTMUCH_STATUS_SUCCESS: Tag successfully added to message
510 * NOTMUCH_STATUS_NULL_POINTER: The 'tag' argument is NULL
512 * NOTMUCH_STATUS_TAG_TOO_LONG: The length of 'tag' is longer than
513 * too long (exceeds NOTMUCH_TAG_MAX)
516 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag);
518 /* Destroy a notmuch_message_t object.
520 * It can be useful to call this function in the case of a single
521 * query object with many messages in the result, (such as iterating
522 * over the entire database). Otherwise, it's fine to never call this
523 * function and there will still be no memory leaks. (The memory from
524 * the messages get reclaimed when the containing query is destroyed.)
527 notmuch_message_destroy (notmuch_message_t *message);
529 /* Does the given notmuch_tags_t object contain any more tags.
531 * When this function returns TRUE, notmuch_tags_get will return a
532 * valid string. Whereas when this function returns FALSE,
533 * notmuch_tags_get will return NULL.
535 * See the documentation of notmuch_message_get_tags for example code
536 * showing how to iterate over a notmuch_tags_t object.
539 notmuch_tags_has_more (notmuch_tags_t *tags);
541 /* Get the current tag from 'tags' as a string.
543 * Note: The returned string belongs to 'tags' and has a lifetime
544 * identical to it (and the query to which it utlimately belongs).
546 * See the documentation of notmuch_message_get_tags for example code
547 * showing how to iterate over a notmuch_tags_t object.
550 notmuch_tags_get (notmuch_tags_t *tags);
552 /* Advance the 'tags' iterator to the next tag.
554 * See the documentation of notmuch_message_get_tags for example code
555 * showing how to iterate over a notmuch_tags_t object.
558 notmuch_tags_advance (notmuch_tags_t *tags);
560 /* Destroy a notmuch_tags_t object.
562 * It's not strictly necessary to call this function. All memory from
563 * the notmuch_tags_t object will be reclaimed when the containg
564 * message or query objects are destroyed.
567 notmuch_tags_destroy (notmuch_tags_t *tags);
569 /* Does the given notmuch_thread_ids_t object contain any more thread IDs.
571 * When this function returns TRUE, notmuch_thread_ids_get will return a
572 * valid string. Whereas when this function returns FALSE,
573 * notmuch_thread_ids_get will return NULL.
575 * See the documentation of notmuch_message_get_thread_ids for example code
576 * showing how to iterate over a notmuch_thread_ids_t object.
579 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids);
581 /* Get the current thread ID from 'thread_ids' as a string.
583 * Note: The returned string belongs to 'thread_ids' and has a lifetime
584 * identical to it (and the query to which it utlimately belongs).
586 * See the documentation of notmuch_message_get_thread_ids for example code
587 * showing how to iterate over a notmuch_thread_ids_t object.
590 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids);
592 /* Advance the 'thread_ids' iterator to the next tag.
594 * See the documentation of notmuch_message_get_thread_ids for example code
595 * showing how to iterate over a notmuch_thread_ids_t object.
598 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids);
600 /* Destroy a notmuch_thread_ids_t object.
602 * It's not strictly necessary to call this function. All memory from
603 * the notmuch_thread_ids_t object will be reclaimed when the containg
604 * message or query objects are destroyed.
607 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids);