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