1 /* notmuch-private.h - Internal interfaces for notmuch.
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #ifndef NOTMUCH_PRIVATE_H
22 #define NOTMUCH_PRIVATE_H
25 #define _GNU_SOURCE /* For getline */
35 #include <sys/types.h>
50 # define DEBUG_DATABASE_SANITY 1
51 # define DEBUG_QUERY 1
54 #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
56 /* There's no point in continuing when we've detected that we've done
57 * something wrong internally (as opposed to the user passing in a
60 * Note that PRINTF_ATTRIBUTE comes from talloc.h
63 _internal_error (const char *format, ...) PRINTF_ATTRIBUTE (1, 2);
65 /* There's no point in continuing when we've detected that we've done
66 * something wrong internally (as opposed to the user passing in a
69 * Note that __location__ comes from talloc.h.
71 #define INTERNAL_ERROR(format, ...) \
72 _internal_error (format " (%s).\n", \
73 ##__VA_ARGS__, __location__)
75 /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of
76 * unlikely. The talloc source code comes to us via the GNU LGPL v. 3.
78 /* these macros gain us a few percent of speed on gcc */
80 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
81 as its first argument */
83 #define likely(x) __builtin_expect(!!(x), 1)
86 #define unlikely(x) __builtin_expect(!!(x), 0)
93 #define unlikely(x) (x)
98 NOTMUCH_VALUE_TIMESTAMP = 0,
99 NOTMUCH_VALUE_MESSAGE_ID
102 /* Xapian (with flint backend) complains if we provide a term longer
103 * than this, but I haven't yet found a way to query the limit
104 * programmatically. */
105 #define NOTMUCH_TERM_MAX 245
107 typedef enum _notmuch_private_status {
108 /* First, copy all the public status values. */
109 NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
110 NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
111 NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
112 NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
113 NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
114 NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
116 /* Then add our own private values. */
117 NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG,
118 NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
120 NOTMUCH_PRIVATE_STATUS_LAST_STATUS
121 } notmuch_private_status_t;
123 /* Coerce a notmuch_private_status_t value to a notmuch_status_t
124 * value, generating an internal error if the private value is equal
125 * to or greater than NOTMUCH_STATUS_LAST_STATUS. (The idea here is
126 * that the caller has previously handled any expected
127 * notmuch_private_status_t values.)
129 #define COERCE_STATUS(private_status, format, ...) \
130 ((private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)\
132 (notmuch_status_t) _internal_error (format " (%s).\n", \
136 (notmuch_status_t) private_status)
141 _notmuch_message_create (const void *talloc_owner,
142 notmuch_database_t *notmuch,
144 notmuch_private_status_t *status);
147 _notmuch_message_create_for_message_id (const void *talloc_owner,
148 notmuch_database_t *notmuch,
149 const char *message_id,
150 notmuch_status_t *status);
152 /* Lookup a prefix value by name.
154 * XXX: This should really be static inside of message.cc, and we can
155 * do that once we convert database.cc to use the
156 * _notmuch_message_add/remove_term functions. */
158 _find_prefix (const char *name);
160 notmuch_private_status_t
161 _notmuch_message_add_term (notmuch_message_t *message,
162 const char *prefix_name,
165 notmuch_private_status_t
166 _notmuch_message_remove_term (notmuch_message_t *message,
167 const char *prefix_name,
171 _notmuch_message_set_filename (notmuch_message_t *message,
172 const char *filename);
175 _notmuch_message_add_thread_id (notmuch_message_t *message,
176 const char *thread_id);
179 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
182 _notmuch_message_set_date (notmuch_message_t *message,
186 _notmuch_message_sync (notmuch_message_t *message);
190 /* XXX: I haven't decided yet whether these will actually get exported
191 * into the public interface in notmuch.h
194 typedef struct _notmuch_message_file notmuch_message_file_t;
196 /* Open a file containing a single email message.
198 * The caller should call notmuch_message_close when done with this.
200 * Returns NULL if any error occurs.
202 notmuch_message_file_t *
203 notmuch_message_file_open (const char *filename);
205 /* Close a notmuch message preivously opened with notmuch_message_open. */
207 notmuch_message_file_close (notmuch_message_file_t *message);
209 /* Restrict 'message' to only save the named headers.
211 * When the caller is only interested in a short list of headers,
212 * known in advance, calling this function can avoid wasted time and
213 * memory parsing/saving header values that will never be needed.
215 * The variable arguments should be a list of const char * with a
216 * final '(const char *) NULL' to terminate the list.
218 * If this function is called, it must be called before any calls to
219 * notmuch_message_get_header for this message.
221 * After calling this function, if notmuch_message_get_header is
222 * called with a header name not in this list, then NULL will be
223 * returned even if that header exists in the actual message.
226 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
228 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
230 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
233 /* Get the value of the specified header from the message.
235 * The header name is case insensitive.
237 * The returned value is owned by the notmuch message and is valid
238 * only until the message is closed. The caller should copy it if
239 * needing to modify the value or to hold onto it for longer.
241 * Returns NULL if the message does not contain a header line matching
245 notmuch_message_file_get_header (notmuch_message_file_t *message,
250 /* Parse an RFC 8222 date string to a time_t value.
252 * The tz_offset argument can be used to also obtain the time-zone
253 * offset, (but can be NULL if the call is not interested in that).
255 * Returns 0 on error.
258 notmuch_parse_date (const char *str, int *tz_offset);
263 notmuch_sha1_of_string (const char *str);
266 notmuch_sha1_of_file (const char *filename);