]> git.notmuchmail.org Git - notmuch/blob - notmuch-private.h
Shuffle the value numbers around in the database.
[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
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 typedef enum {
71     NOTMUCH_VALUE_TIMESTAMP = 0,
72     NOTMUCH_VALUE_MESSAGE_ID,
73     NOTMUCH_VALUE_THREAD
74 } notmuch_value_t;
75
76 /* Xapian (with flint backend) complains if we provide a term longer
77  * than this, but I haven't yet found a way to query the limit
78  * programmatically. */
79 #define NOTMUCH_TERM_MAX 245
80
81 typedef enum _notmuch_private_status {
82     /* First, copy all the public status values. */
83     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
84     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
85     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
86     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
87     NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
88
89     /* Then add our own private values. */
90     NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG,
91     NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
92
93     NOTMUCH_PRIVATE_STATUS_LAST_STATUS
94 } notmuch_private_status_t;
95
96 /* message.cc */
97
98 notmuch_message_t *
99 _notmuch_message_create (const void *talloc_owner,
100                          notmuch_database_t *notmuch,
101                          unsigned int doc_id);
102
103 notmuch_message_t *
104 _notmuch_message_create_for_message_id (const void *talloc_owner,
105                                         notmuch_database_t *notmuch,
106                                         const char *message_id);
107
108 /* Lookup a prefix value by name.
109  *
110  * XXX: This should really be static inside of message.cc, and we can
111  * do that once we convert database.cc to use the
112  * _notmuch_message_add/remove_term functions. */
113 const char *
114 _find_prefix (const char *name);
115
116 notmuch_private_status_t
117 _notmuch_message_add_term (notmuch_message_t *message,
118                            const char *prefix_name,
119                            const char *value);
120
121 notmuch_private_status_t
122 _notmuch_message_remove_term (notmuch_message_t *message,
123                               const char *prefix_name,
124                               const char *value);
125
126 void
127 _notmuch_message_set_filename (notmuch_message_t *message,
128                                const char *filename);
129
130 void
131 _notmuch_message_add_thread_id (notmuch_message_t *message,
132                                 const char *thread_id);
133
134 void
135 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
136
137 void
138 _notmuch_message_set_date (notmuch_message_t *message,
139                            const char *date);
140
141 void
142 _notmuch_message_sync (notmuch_message_t *message);
143
144 /* message-file.c */
145
146 /* XXX: I haven't decided yet whether these will actually get exported
147  * into the public interface in notmuch.h
148  */
149
150 typedef struct _notmuch_message_file notmuch_message_file_t;
151
152 /* Open a file containing a single email message.
153  *
154  * The caller should call notmuch_message_close when done with this.
155  *
156  * Returns NULL if any error occurs.
157  */
158 notmuch_message_file_t *
159 notmuch_message_file_open (const char *filename);
160
161 /* Close a notmuch message preivously opened with notmuch_message_open. */
162 void
163 notmuch_message_file_close (notmuch_message_file_t *message);
164
165 /* Restrict 'message' to only save the named headers.
166  *
167  * When the caller is only interested in a short list of headers,
168  * known in advance, calling this function can avoid wasted time and
169  * memory parsing/saving header values that will never be needed.
170  *
171  * The variable arguments should be a list of const char * with a
172  * final '(const char *) NULL' to terminate the list.
173  *
174  * If this function is called, it must be called before any calls to
175  * notmuch_message_get_header for this message.
176  *
177  * After calling this function, if notmuch_message_get_header is
178  * called with a header name not in this list, then NULL will be
179  * returned even if that header exists in the actual message.
180  */
181 void
182 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
183
184 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
185 void
186 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
187                                         va_list va_headers);
188
189 /* Get the value of the specified header from the message.
190  *
191  * The header name is case insensitive.
192  *
193  * The returned value is owned by the notmuch message and is valid
194  * only until the message is closed. The caller should copy it if
195  * needing to modify the value or to hold onto it for longer.
196  *
197  * Returns NULL if the message does not contain a header line matching
198  * 'header'.
199  */
200 const char *
201 notmuch_message_file_get_header (notmuch_message_file_t *message,
202                                  const char *header);
203
204 /* date.c */
205
206 /* Parse an RFC 8222 date string to a time_t value.
207  *
208  * The tz_offset argument can be used to also obtain the time-zone
209  * offset, (but can be NULL if the call is not interested in that).
210  *
211  * Returns 0 on error.
212  */
213 time_t
214 notmuch_parse_date (const char *str, int *tz_offset);
215
216 /* sha1.c */
217
218 char *
219 notmuch_sha1_of_string (const char *str);
220
221 char *
222 notmuch_sha1_of_file (const char *filename);
223
224 NOTMUCH_END_DECLS
225
226 #endif