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