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