]> git.notmuchmail.org Git - notmuch/blob - notmuch-private.h
add_message: Propagate error status from notmuch_message_create_for_message_id
[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 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE /* For getline */
26 #endif
27 #include <stdio.h>
28
29 #include "notmuch.h"
30
31 NOTMUCH_BEGIN_DECLS
32
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 #include <assert.h>
44
45 #include <talloc.h>
46
47 #include "xutil.h"
48
49 #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
50
51 /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of
52  * unlikely. The talloc source code comes to us via the GNU LGPL v. 3.
53  */
54 /* these macros gain us a few percent of speed on gcc */
55 #if (__GNUC__ >= 3)
56 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
57    as its first argument */
58 #ifndef likely
59 #define likely(x)   __builtin_expect(!!(x), 1)
60 #endif
61 #ifndef unlikely
62 #define unlikely(x) __builtin_expect(!!(x), 0)
63 #endif
64 #else
65 #ifndef likely
66 #define likely(x) (x)
67 #endif
68 #ifndef unlikely
69 #define unlikely(x) (x)
70 #endif
71 #endif
72
73 typedef enum {
74     NOTMUCH_VALUE_TIMESTAMP = 0,
75     NOTMUCH_VALUE_MESSAGE_ID
76 } notmuch_value_t;
77
78 /* Xapian (with flint backend) complains if we provide a term longer
79  * than this, but I haven't yet found a way to query the limit
80  * programmatically. */
81 #define NOTMUCH_TERM_MAX 245
82
83 typedef enum _notmuch_private_status {
84     /* First, copy all the public status values. */
85     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
86     NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
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     NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
95
96     NOTMUCH_PRIVATE_STATUS_LAST_STATUS
97 } notmuch_private_status_t;
98
99 /* message.cc */
100
101 notmuch_message_t *
102 _notmuch_message_create (const void *talloc_owner,
103                          notmuch_database_t *notmuch,
104                          unsigned int doc_id,
105                          notmuch_private_status_t *status);
106
107 notmuch_message_t *
108 _notmuch_message_create_for_message_id (const void *talloc_owner,
109                                         notmuch_database_t *notmuch,
110                                         const char *message_id,
111                                         notmuch_status_t *status);
112
113 /* Lookup a prefix value by name.
114  *
115  * XXX: This should really be static inside of message.cc, and we can
116  * do that once we convert database.cc to use the
117  * _notmuch_message_add/remove_term functions. */
118 const char *
119 _find_prefix (const char *name);
120
121 notmuch_private_status_t
122 _notmuch_message_add_term (notmuch_message_t *message,
123                            const char *prefix_name,
124                            const char *value);
125
126 notmuch_private_status_t
127 _notmuch_message_remove_term (notmuch_message_t *message,
128                               const char *prefix_name,
129                               const char *value);
130
131 void
132 _notmuch_message_set_filename (notmuch_message_t *message,
133                                const char *filename);
134
135 void
136 _notmuch_message_add_thread_id (notmuch_message_t *message,
137                                 const char *thread_id);
138
139 void
140 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
141
142 void
143 _notmuch_message_set_date (notmuch_message_t *message,
144                            const char *date);
145
146 void
147 _notmuch_message_sync (notmuch_message_t *message);
148
149 /* message-file.c */
150
151 /* XXX: I haven't decided yet whether these will actually get exported
152  * into the public interface in notmuch.h
153  */
154
155 typedef struct _notmuch_message_file notmuch_message_file_t;
156
157 /* Open a file containing a single email message.
158  *
159  * The caller should call notmuch_message_close when done with this.
160  *
161  * Returns NULL if any error occurs.
162  */
163 notmuch_message_file_t *
164 notmuch_message_file_open (const char *filename);
165
166 /* Close a notmuch message preivously opened with notmuch_message_open. */
167 void
168 notmuch_message_file_close (notmuch_message_file_t *message);
169
170 /* Restrict 'message' to only save the named headers.
171  *
172  * When the caller is only interested in a short list of headers,
173  * known in advance, calling this function can avoid wasted time and
174  * memory parsing/saving header values that will never be needed.
175  *
176  * The variable arguments should be a list of const char * with a
177  * final '(const char *) NULL' to terminate the list.
178  *
179  * If this function is called, it must be called before any calls to
180  * notmuch_message_get_header for this message.
181  *
182  * After calling this function, if notmuch_message_get_header is
183  * called with a header name not in this list, then NULL will be
184  * returned even if that header exists in the actual message.
185  */
186 void
187 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
188
189 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
190 void
191 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
192                                         va_list va_headers);
193
194 /* Get the value of the specified header from the message.
195  *
196  * The header name is case insensitive.
197  *
198  * The returned value is owned by the notmuch message and is valid
199  * only until the message is closed. The caller should copy it if
200  * needing to modify the value or to hold onto it for longer.
201  *
202  * Returns NULL if the message does not contain a header line matching
203  * 'header'.
204  */
205 const char *
206 notmuch_message_file_get_header (notmuch_message_file_t *message,
207                                  const char *header);
208
209 /* date.c */
210
211 /* Parse an RFC 8222 date string to a time_t value.
212  *
213  * The tz_offset argument can be used to also obtain the time-zone
214  * offset, (but can be NULL if the call is not interested in that).
215  *
216  * Returns 0 on error.
217  */
218 time_t
219 notmuch_parse_date (const char *str, int *tz_offset);
220
221 /* sha1.c */
222
223 char *
224 notmuch_sha1_of_string (const char *str);
225
226 char *
227 notmuch_sha1_of_file (const char *filename);
228
229 NOTMUCH_END_DECLS
230
231 #endif