]> git.notmuchmail.org Git - notmuch/blob - notmuch-private.h
Fix _notmuch_message_create to catch Xapian DocNotFoundError.
[notmuch] / notmuch-private.h
1 /* notmuch-private.h - Internal interfaces for notmuch.
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_PRIVATE_H
22 #define NOTMUCH_PRIVATE_H
23
24 #include "notmuch.h"
25
26 NOTMUCH_BEGIN_DECLS
27
28 #ifndef _GNU_SOURCE
29 #define _GNU_SOURCE /* For getline */
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/mman.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <ctype.h>
43
44 #include <talloc.h>
45
46 #include "xutil.h"
47
48 /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of
49  * unlikely. The talloc source code comes to us via the GNU LGPL v. 3.
50  */
51 /* these macros gain us a few percent of speed on gcc */
52 #if (__GNUC__ >= 3)
53 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
54    as its first argument */
55 #ifndef likely
56 #define likely(x)   __builtin_expect(!!(x), 1)
57 #endif
58 #ifndef unlikely
59 #define unlikely(x) __builtin_expect(!!(x), 0)
60 #endif
61 #else
62 #ifndef likely
63 #define likely(x) (x)
64 #endif
65 #ifndef unlikely
66 #define unlikely(x) (x)
67 #endif
68 #endif
69
70 /* These value numbers are chosen to be sup compatible (for now at
71  * least). */
72
73 typedef enum {
74     NOTMUCH_VALUE_MESSAGE_ID = 0,
75     NOTMUCH_VALUE_THREAD = 1,
76     NOTMUCH_VALUE_DATE = 2
77 } notmuch_value_t;
78
79 /* Xapian (with flint backend) complains if we provide a term longer
80  * than this, but I haven't yet found a way to query the limit
81  * programmatically. */
82 #define NOTMUCH_TERM_MAX 245
83
84 typedef enum _notmuch_private_status {
85     /* First, copy all the public status values. */
86     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
87     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
88     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
89     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
90     NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
91
92     /* Then add our own private values. */
93     NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG,
94
95     NOTMUCH_PRIVATE_STATUS_LAST_STATUS
96 } notmuch_private_status_t;
97
98 /* message.cc */
99
100 notmuch_message_t *
101 _notmuch_message_create (const void *talloc_owner,
102                          notmuch_database_t *notmuch,
103                          unsigned int doc_id);
104
105 /* XXX: Here temporarily during code movement only. */
106 /* "128 bits of thread-id ought to be enough for anybody" */
107 #define NOTMUCH_THREAD_ID_BITS   128
108 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
109 typedef struct _thread_id {
110     char str[NOTMUCH_THREAD_ID_DIGITS + 1];
111 } thread_id_t;
112
113
114 void
115 thread_id_generate (thread_id_t *thread_id);
116
117 /* Lookup a prefix value by name.
118  *
119  * XXX: This should really be static inside of message.cc, and we can
120  * do that once we convert database.cc to use the
121  * _notmuch_message_add/remove_term functions. */
122 const char *
123 _find_prefix (const char *name);
124
125 notmuch_private_status_t
126 _notmuch_message_add_term (notmuch_message_t *message,
127                            const char *prefix_name,
128                            const char *value);
129
130 notmuch_private_status_t
131 _notmuch_message_remove_term (notmuch_message_t *message,
132                               const char *prefix_name,
133                               const char *value);
134
135 void
136 _notmuch_message_set_filename (notmuch_message_t *message,
137                                const char *filename);
138
139 void
140 _notmuch_message_add_thread_id (notmuch_message_t *message,
141                                 const char *thread_id);
142
143 void
144 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
145
146 void
147 _notmuch_message_set_date (notmuch_message_t *message,
148                            const char *date);
149
150 void
151 _notmuch_message_sync (notmuch_message_t *message);
152
153 /* message-file.c */
154
155 /* XXX: I haven't decided yet whether these will actually get exported
156  * into the public interface in notmuch.h
157  */
158
159 typedef struct _notmuch_message_file notmuch_message_file_t;
160
161 /* Open a file containing a single email message.
162  *
163  * The caller should call notmuch_message_close when done with this.
164  *
165  * Returns NULL if any error occurs.
166  */
167 notmuch_message_file_t *
168 notmuch_message_file_open (const char *filename);
169
170 /* Close a notmuch message preivously opened with notmuch_message_open. */
171 void
172 notmuch_message_file_close (notmuch_message_file_t *message);
173
174 /* Restrict 'message' to only save the named headers.
175  *
176  * When the caller is only interested in a short list of headers,
177  * known in advance, calling this function can avoid wasted time and
178  * memory parsing/saving header values that will never be needed.
179  *
180  * The variable arguments should be a list of const char * with a
181  * final '(const char *) NULL' to terminate the list.
182  *
183  * If this function is called, it must be called before any calls to
184  * notmuch_message_get_header for this message.
185  *
186  * After calling this function, if notmuch_message_get_header is
187  * called with a header name not in this list, then NULL will be
188  * returned even if that header exists in the actual message.
189  */
190 void
191 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
192
193 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
194 void
195 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
196                                         va_list va_headers);
197
198 /* Get the value of the specified header from the message.
199  *
200  * The header name is case insensitive.
201  *
202  * The returned value is owned by the notmuch message and is valid
203  * only until the message is closed. The caller should copy it if
204  * needing to modify the value or to hold onto it for longer.
205  *
206  * Returns NULL if the message does not contain a header line matching
207  * 'header'.
208  */
209 const char *
210 notmuch_message_file_get_header (notmuch_message_file_t *message,
211                                  const char *header);
212
213 /* date.c */
214
215 /* Parse an RFC 8222 date string to a time_t value.
216  *
217  * The tz_offset argument can be used to also obtain the time-zone
218  * offset, (but can be NULL if the call is not interested in that).
219  *
220  * Returns 0 on error.
221  */
222 time_t
223 notmuch_parse_date (const char *str, int *tz_offset);
224
225 /* sha1.c */
226
227 /* Create a hexadecimal string version of the SHA-1 digest of the
228  * named file.
229  *
230  * This function returns a newly allocated string which the caller
231  * should free() when finished.
232  *
233  * If any error occurs while reading the file, (permission denied,
234  * file not found, etc.), this function returns NULL.
235  */
236 char *
237 notmuch_sha1_of_file (const char *filename);
238
239 NOTMUCH_END_DECLS
240
241 #endif