]> git.notmuchmail.org Git - notmuch/blob - notmuch-client.h
0365baae4b89a131925f41207f228f749a670ea7
[notmuch] / notmuch-client.h
1 /* notmuch - Not much of an email program, (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 https://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #ifndef NOTMUCH_CLIENT_H
22 #define NOTMUCH_CLIENT_H
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE /* for getline */
26 #endif
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <sysexits.h>
30
31 #include "compat.h"
32
33 #include "gmime-extra.h"
34
35 typedef GMimeCryptoContext notmuch_crypto_context_t;
36 /* This is automatically included only since gmime 2.6.10 */
37 #include <gmime/gmime-pkcs7-context.h>
38
39 #include "notmuch.h"
40
41 /* This is separate from notmuch-private.h because we're trying to
42  * keep notmuch.c from looking into any internals, (which helps us
43  * develop notmuch.h into a plausible library interface).
44  */
45 #include "xutil.h"
46
47 #include <stddef.h>
48 #include <string.h>
49 #include <sys/stat.h>
50 #include <sys/time.h>
51 #include <unistd.h>
52 #include <dirent.h>
53 #include <errno.h>
54 #include <signal.h>
55 #include <ctype.h>
56
57 #include "talloc-extra.h"
58
59 #define unused(x) x __attribute__ ((unused))
60
61 #define STRINGIFY(s) STRINGIFY_(s)
62 #define STRINGIFY_(s) #s
63
64 typedef struct mime_node mime_node_t;
65 struct sprinter;
66 struct notmuch_show_params;
67
68 typedef struct notmuch_show_format {
69     struct sprinter *(*new_sprinter) (const void *ctx, FILE *stream);
70     notmuch_status_t (*part) (const void *ctx, struct sprinter *sprinter,
71                               struct mime_node *node, int indent,
72                               const struct notmuch_show_params *params);
73 } notmuch_show_format_t;
74
75 typedef struct notmuch_crypto {
76     bool verify;
77     bool decrypt;
78 #if (GMIME_MAJOR_VERSION < 3)
79     notmuch_crypto_context_t* gpgctx;
80     notmuch_crypto_context_t* pkcs7ctx;
81     const char *gpgpath;
82 #endif
83 } notmuch_crypto_t;
84
85 typedef struct notmuch_show_params {
86     bool entire_thread;
87     bool omit_excluded;
88     bool output_body;
89     int part;
90     notmuch_crypto_t crypto;
91     bool include_html;
92     GMimeStream *out_stream;
93 } notmuch_show_params_t;
94
95 /* There's no point in continuing when we've detected that we've done
96  * something wrong internally (as opposed to the user passing in a
97  * bogus value).
98  *
99  * Note that __location__ comes from talloc.h.
100  */
101 #define INTERNAL_ERROR(format, ...)                     \
102     do {                                                \
103         fprintf(stderr,                                 \
104                 "Internal error: " format " (%s)\n",    \
105                 ##__VA_ARGS__, __location__);           \
106         exit (1);                                       \
107     } while (0)
108
109 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
110
111 #define STRNCMP_LITERAL(var, literal) \
112     strncmp ((var), (literal), sizeof (literal) - 1)
113
114 static inline void
115 chomp_newline (char *str)
116 {
117     if (str && str[strlen(str)-1] == '\n')
118         str[strlen(str)-1] = '\0';
119 }
120
121 /* Exit status code indicating temporary failure; user is invited to
122  * retry.
123  *
124  * For example, file(s) in the mail store were removed or renamed
125  * after notmuch new scanned the directories but before indexing the
126  * file(s). If the file was renamed, the indexing might not be
127  * complete, and the user is advised to re-run notmuch new.
128  */
129 #define NOTMUCH_EXIT_TEMPFAIL EX_TEMPFAIL
130
131 /* Exit status code indicating the requested format version is too old
132  * (support for that version has been dropped).  CLI code should use
133  * notmuch_exit_if_unsupported_format rather than directly exiting
134  * with this code.
135  */
136 #define NOTMUCH_EXIT_FORMAT_TOO_OLD 20
137 /* Exit status code indicating the requested format version is newer
138  * than the version supported by the CLI.  CLI code should use
139  * notmuch_exit_if_unsupported_format rather than directly exiting
140  * with this code.
141  */
142 #define NOTMUCH_EXIT_FORMAT_TOO_NEW 21
143
144 /* The current structured output format version.  Requests for format
145  * versions above this will return an error.  Backwards-incompatible
146  * changes such as removing map fields, changing the meaning of map
147  * fields, or changing the meanings of list elements should increase
148  * this.  New (required) map fields can be added without increasing
149  * this.
150  */
151 #define NOTMUCH_FORMAT_CUR 4
152 /* The minimum supported structured output format version.  Requests
153  * for format versions below this will return an error. */
154 #define NOTMUCH_FORMAT_MIN 1
155 /* The minimum non-deprecated structured output format version.
156  * Requests for format versions below this will print a stern warning.
157  * Must be between NOTMUCH_FORMAT_MIN and NOTMUCH_FORMAT_CUR,
158  * inclusive.
159  */
160 #define NOTMUCH_FORMAT_MIN_ACTIVE 1
161
162 /* The output format version requested by the caller on the command
163  * line.  If no format version is requested, this will be set to
164  * NOTMUCH_FORMAT_CUR.  Even though the command-line option is
165  * per-command, this is global because commands can share structured
166  * output code.
167  */
168 extern int notmuch_format_version;
169
170 typedef struct _notmuch_config notmuch_config_t;
171
172 /* Commands that support structured output should support the
173  * following argument
174  *  { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 }
175  * and should invoke notmuch_exit_if_unsupported_format to check the
176  * requested version.  If notmuch_format_version is outside the
177  * supported range, this will print a detailed diagnostic message for
178  * the user and exit with NOTMUCH_EXIT_FORMAT_TOO_{OLD,NEW} to inform
179  * the invoking program of the problem.
180  */
181 void
182 notmuch_exit_if_unsupported_format (void);
183
184 #if (GMIME_MAJOR_VERSION <3)
185 notmuch_crypto_context_t *
186 notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol);
187 #endif
188
189 int
190 notmuch_crypto_cleanup (notmuch_crypto_t *crypto);
191
192 int
193 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]);
194
195 int
196 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]);
197
198 int
199 notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]);
200
201 int
202 notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]);
203
204 int
205 notmuch_reindex_command (notmuch_config_t *config, int argc, char *argv[]);
206
207 int
208 notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]);
209
210 int
211 notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]);
212
213 int
214 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]);
215
216 int
217 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[]);
218
219 int
220 notmuch_setup_command (notmuch_config_t *config, int argc, char *argv[]);
221
222 int
223 notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]);
224
225 int
226 notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[]);
227
228 int
229 notmuch_config_command (notmuch_config_t *config, int argc, char *argv[]);
230
231 int
232 notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[]);
233
234 const char *
235 notmuch_time_relative_date (const void *ctx, time_t then);
236
237 void
238 notmuch_time_print_formatted_seconds (double seconds);
239
240 double
241 notmuch_time_elapsed (struct timeval start, struct timeval end);
242
243 char *
244 query_string_from_args (void *ctx, int argc, char *argv[]);
245
246 notmuch_status_t
247 show_one_part (const char *filename, int part);
248
249 void
250 format_part_sprinter (const void *ctx, struct sprinter *sp, mime_node_t *node,
251                       bool output_body,
252                       bool include_html);
253
254 void
255 format_headers_sprinter (struct sprinter *sp, GMimeMessage *message,
256                          bool reply);
257
258 typedef enum {
259     NOTMUCH_SHOW_TEXT_PART_REPLY = 1 << 0,
260 } notmuch_show_text_part_flags;
261
262 void
263 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
264                         notmuch_show_text_part_flags flags);
265
266 char *
267 json_quote_chararray (const void *ctx, const char *str, const size_t len);
268
269 char *
270 json_quote_str (const void *ctx, const char *str);
271
272 /* notmuch-config.c */
273
274 typedef enum {
275     NOTMUCH_CONFIG_OPEN = 1 << 0,
276     NOTMUCH_CONFIG_CREATE = 1 << 1,
277 } notmuch_config_mode_t;
278
279 notmuch_config_t *
280 notmuch_config_open (void *ctx,
281                      const char *filename,
282                      notmuch_config_mode_t config_mode);
283
284 void
285 notmuch_config_close (notmuch_config_t *config);
286
287 int
288 notmuch_config_save (notmuch_config_t *config);
289
290 bool
291 notmuch_config_is_new (notmuch_config_t *config);
292
293 const char *
294 notmuch_config_get_database_path (notmuch_config_t *config);
295
296 void
297 notmuch_config_set_database_path (notmuch_config_t *config,
298                                   const char *database_path);
299
300 #if (GMIME_MAJOR_VERSION < 3)
301 const char *
302 notmuch_config_get_crypto_gpg_path (notmuch_config_t *config);
303
304 void
305 notmuch_config_set_crypto_gpg_path (notmuch_config_t *config,
306                                   const char *gpg_path);
307 #endif
308
309 const char *
310 notmuch_config_get_user_name (notmuch_config_t *config);
311
312 void
313 notmuch_config_set_user_name (notmuch_config_t *config,
314                               const char *user_name);
315
316 const char *
317 notmuch_config_get_user_primary_email (notmuch_config_t *config);
318
319 void
320 notmuch_config_set_user_primary_email (notmuch_config_t *config,
321                                        const char *primary_email);
322
323 const char **
324 notmuch_config_get_user_other_email (notmuch_config_t *config,
325                                      size_t *length);
326
327 void
328 notmuch_config_set_user_other_email (notmuch_config_t *config,
329                                      const char *other_email[],
330                                      size_t length);
331
332 const char **
333 notmuch_config_get_new_tags (notmuch_config_t *config,
334                              size_t *length);
335 void
336 notmuch_config_set_new_tags (notmuch_config_t *config,
337                              const char *new_tags[],
338                              size_t length);
339
340 const char **
341 notmuch_config_get_new_ignore (notmuch_config_t *config,
342                                size_t *length);
343
344 void
345 notmuch_config_set_new_ignore (notmuch_config_t *config,
346                                const char *new_ignore[],
347                                size_t length);
348
349 bool
350 notmuch_config_get_maildir_synchronize_flags (notmuch_config_t *config);
351
352 void
353 notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
354                                               bool synchronize_flags);
355
356 const char **
357 notmuch_config_get_search_exclude_tags (notmuch_config_t *config, size_t *length);
358
359 void
360 notmuch_config_set_search_exclude_tags (notmuch_config_t *config,
361                                       const char *list[],
362                                       size_t length);
363
364 int
365 notmuch_run_hook (const char *db_path, const char *hook);
366
367 bool
368 debugger_is_active (void);
369
370 /* mime-node.c */
371
372 /* mime_node_t represents a single node in a MIME tree.  A MIME tree
373  * abstracts the different ways of traversing different types of MIME
374  * parts, allowing a MIME message to be viewed as a generic tree of
375  * parts.  Message-type parts have one child, multipart-type parts
376  * have multiple children, and leaf parts have zero children.
377  */
378 struct mime_node {
379     /* The MIME object of this part.  This will be a GMimeMessage,
380      * GMimePart, GMimeMultipart, or a subclass of one of these.
381      *
382      * This will never be a GMimeMessagePart because GMimeMessagePart
383      * is structurally redundant with GMimeMessage.  If this part is a
384      * message (that is, 'part' is a GMimeMessage), then either
385      * envelope_file will be set to a notmuch_message_t (for top-level
386      * messages) or envelope_part will be set to a GMimeMessagePart
387      * (for embedded message parts).
388      */
389     GMimeObject *part;
390
391     /* If part is a GMimeMessage, these record the envelope of the
392      * message: either a notmuch_message_t representing a top-level
393      * message, or a GMimeMessagePart representing a MIME part
394      * containing a message.
395      */
396     notmuch_message_t *envelope_file;
397     GMimeMessagePart *envelope_part;
398
399     /* The number of children of this part. */
400     int nchildren;
401
402     /* The parent of this node or NULL if this is the root node. */
403     struct mime_node *parent;
404
405     /* The depth-first part number of this child if the MIME tree is
406      * being traversed in depth-first order, or -1 otherwise. */
407     int part_num;
408
409     /* True if decryption of this part was attempted. */
410     bool decrypt_attempted;
411     /* True if decryption of this part's child succeeded.  In this
412      * case, the decrypted part is substituted for the second child of
413      * this part (which would usually be the encrypted data). */
414     bool decrypt_success;
415
416     /* True if signature verification on this part was attempted. */
417     bool verify_attempted;
418
419     /* The list of signatures for signed or encrypted containers. If
420      * there are no signatures, this will be NULL. */
421     GMimeSignatureList* sig_list;
422
423     /* Internal: Context inherited from the root iterator. */
424     struct mime_node_context *ctx;
425
426     /* Internal: For successfully decrypted multipart parts, the
427      * decrypted part to substitute for the second child. */
428     GMimeObject *decrypted_child;
429
430     /* Internal: The next child for depth-first traversal and the part
431      * number to assign it (or -1 if unknown). */
432     int next_child;
433     int next_part_num;
434 };
435
436 /* Construct a new MIME node pointing to the root message part of
437  * message. If crypto->verify is true, signed child parts will be
438  * verified. If crypto->decrypt is true, encrypted child parts will be
439  * decrypted.  If the crypto contexts (crypto->gpgctx or
440  * crypto->pkcs7) are NULL, they will be lazily initialized.
441  *
442  * Return value:
443  *
444  * NOTMUCH_STATUS_SUCCESS: Root node is returned in *node_out.
445  *
446  * NOTMUCH_STATUS_FILE_ERROR: Failed to open message file.
447  *
448  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.
449  */
450 notmuch_status_t
451 mime_node_open (const void *ctx, notmuch_message_t *message,
452                 notmuch_crypto_t *crypto, mime_node_t **node_out);
453
454 /* Return a new MIME node for the requested child part of parent.
455  * parent will be used as the talloc context for the returned child
456  * node.
457  *
458  * In case of any failure, this function returns NULL, (after printing
459  * an error message on stderr).
460  */
461 mime_node_t *
462 mime_node_child (mime_node_t *parent, int child);
463
464 /* Return the nth child of node in a depth-first traversal.  If n is
465  * 0, returns node itself.  Returns NULL if there is no such part. */
466 mime_node_t *
467 mime_node_seek_dfs (mime_node_t *node, int n);
468
469 typedef enum dump_formats {
470     DUMP_FORMAT_AUTO,
471     DUMP_FORMAT_BATCH_TAG,
472     DUMP_FORMAT_SUP
473 } dump_format_t;
474
475 typedef enum dump_includes {
476     DUMP_INCLUDE_TAGS = 1,
477     DUMP_INCLUDE_CONFIG = 2,
478     DUMP_INCLUDE_PROPERTIES = 4
479 } dump_include_t;
480
481 #define DUMP_INCLUDE_DEFAULT (DUMP_INCLUDE_TAGS | DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_PROPERTIES)
482
483 #define NOTMUCH_DUMP_VERSION 3
484
485 int
486 notmuch_database_dump (notmuch_database_t *notmuch,
487                        const char *output_file_name,
488                        const char *query_str,
489                        dump_format_t output_format,
490                        dump_include_t include,
491                        bool gzip_output);
492
493 /* If status is non-zero (i.e. error) print appropriate
494    messages to stderr.
495 */
496
497 notmuch_status_t
498 print_status_query (const char *loc,
499                     const notmuch_query_t *query,
500                     notmuch_status_t status);
501
502 notmuch_status_t
503 print_status_database (const char *loc,
504                        const notmuch_database_t *database,
505                        notmuch_status_t status);
506
507 int
508 status_to_exit (notmuch_status_t status);
509
510 #include "command-line-arguments.h"
511
512 extern const char *notmuch_requested_db_uuid;
513 extern const notmuch_opt_desc_t  notmuch_shared_options [];
514 void notmuch_exit_if_unmatched_db_uuid (notmuch_database_t *notmuch);
515
516 void notmuch_process_shared_options (const char* subcommand_name);
517 int notmuch_minimal_options (const char* subcommand_name,
518                              int argc, char **argv);
519 #endif