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