]> git.cworth.org Git - notmuch/blob - notmuch.c
CLI/config: migrate notmuch_config_open to new config
[notmuch] / notmuch.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see https://www.gnu.org/licenses/ .
18  *
19  * Authors: Carl Worth <cworth@cworth.org>
20  *          Keith Packard <keithp@keithp.com>
21  */
22
23 #include "notmuch-client.h"
24
25 /*
26  * Notmuch subcommand hook.
27  *
28  * The return value will be used as notmuch exit status code,
29  * preferably EXIT_SUCCESS or EXIT_FAILURE.
30  *
31  * Each subcommand should be passed either a config object, or an open
32  * database
33  */
34 typedef int (*command_function_t) (notmuch_config_t *config, notmuch_database_t *notmuch,
35                                    int argc, char *argv[]);
36
37 typedef struct command {
38     const char *name;
39     command_function_t function;
40     notmuch_command_mode_t mode;
41     const char *summary;
42 } command_t;
43
44 static int
45 notmuch_help_command (notmuch_config_t *config, notmuch_database_t *notmuch, int argc, char *argv[]);
46
47 static int
48 notmuch_command (notmuch_config_t *config, notmuch_database_t *notmuch, int argc, char *argv[]);
49
50 static int
51 _help_for (const char *topic);
52
53 static bool print_version = false, print_help = false;
54 const char *notmuch_requested_db_uuid = NULL;
55
56 const notmuch_opt_desc_t notmuch_shared_options [] = {
57     { .opt_bool = &print_version, .name = "version" },
58     { .opt_bool = &print_help, .name = "help" },
59     { .opt_string = &notmuch_requested_db_uuid, .name = "uuid" },
60     { }
61 };
62
63 /* any subcommand wanting to support these options should call
64  * inherit notmuch_shared_options and call
65  * notmuch_process_shared_options (subcommand_name);
66  */
67 void
68 notmuch_process_shared_options (const char *subcommand_name)
69 {
70     if (print_version) {
71         printf ("notmuch " STRINGIFY (NOTMUCH_VERSION) "\n");
72         exit (EXIT_SUCCESS);
73     }
74
75     if (print_help) {
76         int ret = _help_for (subcommand_name);
77         exit (ret);
78     }
79 }
80
81 /* This is suitable for subcommands that do not actually open the
82  * database.
83  */
84 int
85 notmuch_minimal_options (const char *subcommand_name,
86                          int argc, char **argv)
87 {
88     int opt_index;
89
90     notmuch_opt_desc_t options[] = {
91         { .opt_inherit = notmuch_shared_options },
92         { }
93     };
94
95     opt_index = parse_arguments (argc, argv, options, 1);
96
97     if (opt_index < 0)
98         return -1;
99
100     /* We can't use argv here as it is sometimes NULL */
101     notmuch_process_shared_options (subcommand_name);
102     return opt_index;
103 }
104
105
106 struct _notmuch_client_indexing_cli_choices indexing_cli_choices = { };
107 const notmuch_opt_desc_t notmuch_shared_indexing_options [] = {
108     { .opt_keyword = &indexing_cli_choices.decrypt_policy,
109       .present = &indexing_cli_choices.decrypt_policy_set, .keywords =
110           (notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
111                                   { "true", NOTMUCH_DECRYPT_TRUE },
112                                   { "auto", NOTMUCH_DECRYPT_AUTO },
113                                   { "nostash", NOTMUCH_DECRYPT_NOSTASH },
114                                   { 0, 0 } },
115       .name = "decrypt" },
116     { }
117 };
118
119
120 notmuch_status_t
121 notmuch_process_shared_indexing_options (notmuch_database_t *notmuch)
122 {
123     if (indexing_cli_choices.opts == NULL)
124         indexing_cli_choices.opts = notmuch_database_get_default_indexopts (notmuch);
125     if (indexing_cli_choices.decrypt_policy_set) {
126         notmuch_status_t status;
127         if (indexing_cli_choices.opts == NULL)
128             return NOTMUCH_STATUS_OUT_OF_MEMORY;
129         status = notmuch_indexopts_set_decrypt_policy (indexing_cli_choices.opts,
130                                                        indexing_cli_choices.decrypt_policy);
131         if (status != NOTMUCH_STATUS_SUCCESS) {
132             fprintf (stderr, "Error: Failed to set index decryption policy to %d. (%s)\n",
133                      indexing_cli_choices.decrypt_policy, notmuch_status_to_string (status));
134             notmuch_indexopts_destroy (indexing_cli_choices.opts);
135             indexing_cli_choices.opts = NULL;
136             return status;
137         }
138     }
139     return NOTMUCH_STATUS_SUCCESS;
140 }
141
142
143 static command_t commands[] = {
144     { NULL, notmuch_command, NOTMUCH_COMMAND_CONFIG_OPEN | NOTMUCH_COMMAND_CONFIG_CREATE
145       | NOTMUCH_COMMAND_CONFIG_LOAD,
146       "Notmuch main command." },
147     { "setup", notmuch_setup_command, NOTMUCH_COMMAND_CONFIG_OPEN | NOTMUCH_COMMAND_CONFIG_CREATE
148       | NOTMUCH_COMMAND_CONFIG_LOAD,
149       "Interactively set up notmuch for first use." },
150     { "new", notmuch_new_command,
151       NOTMUCH_COMMAND_DATABASE_EARLY | NOTMUCH_COMMAND_DATABASE_WRITE |
152       NOTMUCH_COMMAND_DATABASE_CREATE,
153       "Find and import new messages to the notmuch database." },
154     { "insert", notmuch_insert_command, NOTMUCH_COMMAND_DATABASE_EARLY |
155       NOTMUCH_COMMAND_DATABASE_WRITE,
156       "Add a new message into the maildir and notmuch database." },
157     { "search", notmuch_search_command, NOTMUCH_COMMAND_DATABASE_EARLY,
158       "Search for messages matching the given search terms." },
159     { "address", notmuch_address_command, NOTMUCH_COMMAND_DATABASE_EARLY,
160       "Get addresses from messages matching the given search terms." },
161     { "show", notmuch_show_command, NOTMUCH_COMMAND_DATABASE_EARLY,
162       "Show all messages matching the search terms." },
163     { "count", notmuch_count_command, NOTMUCH_COMMAND_DATABASE_EARLY,
164       "Count messages matching the search terms." },
165     { "reply", notmuch_reply_command, NOTMUCH_COMMAND_DATABASE_EARLY,
166       "Construct a reply template for a set of messages." },
167     { "tag", notmuch_tag_command, NOTMUCH_COMMAND_DATABASE_EARLY | NOTMUCH_COMMAND_DATABASE_WRITE,
168       "Add/remove tags for all messages matching the search terms." },
169     { "dump", notmuch_dump_command, NOTMUCH_COMMAND_DATABASE_EARLY | NOTMUCH_COMMAND_DATABASE_WRITE,
170       "Create a plain-text dump of the tags for each message." },
171     { "restore", notmuch_restore_command, NOTMUCH_COMMAND_DATABASE_EARLY |
172       NOTMUCH_COMMAND_DATABASE_WRITE,
173       "Restore the tags from the given dump file (see 'dump')." },
174     { "compact", notmuch_compact_command, NOTMUCH_COMMAND_DATABASE_EARLY |
175       NOTMUCH_COMMAND_DATABASE_WRITE,
176       "Compact the notmuch database." },
177     { "reindex", notmuch_reindex_command, NOTMUCH_COMMAND_DATABASE_EARLY |
178       NOTMUCH_COMMAND_DATABASE_WRITE,
179       "Re-index all messages matching the search terms." },
180     { "config", notmuch_config_command, NOTMUCH_COMMAND_CONFIG_OPEN | NOTMUCH_COMMAND_CONFIG_LOAD,
181       "Get or set settings in the notmuch configuration file." },
182 #if WITH_EMACS
183     { "emacs-mua", NULL, 0,
184       "send mail with notmuch and emacs." },
185 #endif
186     { "help", notmuch_help_command, NOTMUCH_COMMAND_CONFIG_CREATE, /* create but don't save config */
187       "This message, or more detailed help for the named command." }
188 };
189
190 typedef struct help_topic {
191     const char *name;
192     const char *summary;
193 } help_topic_t;
194
195 static help_topic_t help_topics[] = {
196     { "search-terms",
197       "Common search term syntax." },
198     { "hooks",
199       "Hooks that will be run before or after certain commands." },
200     { "properties",
201       "Message property conventions and documentation." },
202 };
203
204 static command_t *
205 find_command (const char *name)
206 {
207     size_t i;
208
209     for (i = 0; i < ARRAY_SIZE (commands); i++)
210         if ((! name && ! commands[i].name) ||
211             (name && commands[i].name && strcmp (name, commands[i].name) == 0))
212             return &commands[i];
213
214     return NULL;
215 }
216
217 int notmuch_format_version;
218
219 static void
220 usage (FILE *out)
221 {
222     command_t *command;
223     help_topic_t *topic;
224     unsigned int i;
225
226     fprintf (out,
227              "Usage: notmuch --help\n"
228              "       notmuch --version\n"
229              "       notmuch <command> [args...]\n");
230     fprintf (out, "\n");
231     fprintf (out, "The available commands are as follows:\n");
232     fprintf (out, "\n");
233
234     for (i = 0; i < ARRAY_SIZE (commands); i++) {
235         command = &commands[i];
236
237         if (command->name)
238             fprintf (out, "  %-12s  %s\n", command->name, command->summary);
239     }
240
241     fprintf (out, "\n");
242     fprintf (out, "Additional help topics are as follows:\n");
243     fprintf (out, "\n");
244
245     for (i = 0; i < ARRAY_SIZE (help_topics); i++) {
246         topic = &help_topics[i];
247         fprintf (out, "  %-12s  %s\n", topic->name, topic->summary);
248     }
249
250     fprintf (out, "\n");
251     fprintf (out,
252              "Use \"notmuch help <command or topic>\" for more details "
253              "on each command or topic.\n\n");
254 }
255
256 void
257 notmuch_exit_if_unsupported_format (void)
258 {
259     if (notmuch_format_version > NOTMUCH_FORMAT_CUR) {
260         fprintf (stderr,
261                  "\
262 A caller requested output format version %d, but the installed notmuch\n\
263 CLI only supports up to format version %d.  You may need to upgrade your\n\
264 notmuch CLI.\n",
265                  notmuch_format_version, NOTMUCH_FORMAT_CUR);
266         exit (NOTMUCH_EXIT_FORMAT_TOO_NEW);
267     } else if (notmuch_format_version < NOTMUCH_FORMAT_MIN) {
268         fprintf (stderr,
269                  "\
270 A caller requested output format version %d, which is no longer supported\n\
271 by the notmuch CLI (it requires at least version %d).  You may need to\n\
272 upgrade your notmuch front-end.\n",
273                  notmuch_format_version, NOTMUCH_FORMAT_MIN);
274         exit (NOTMUCH_EXIT_FORMAT_TOO_OLD);
275     } else if (notmuch_format_version < NOTMUCH_FORMAT_MIN_ACTIVE) {
276         /* Warn about old version requests so compatibility issues are
277          * less likely when we drop support for a deprecated format
278          * versions. */
279         fprintf (stderr,
280                  "\
281 A caller requested deprecated output format version %d, which may not\n\
282 be supported in the future.\n", notmuch_format_version);
283     }
284 }
285
286 void
287 notmuch_exit_if_unmatched_db_uuid (notmuch_database_t *notmuch)
288 {
289     const char *uuid = NULL;
290
291     if (! notmuch_requested_db_uuid)
292         return;
293     IGNORE_RESULT (notmuch_database_get_revision (notmuch, &uuid));
294
295     if (strcmp (notmuch_requested_db_uuid, uuid) != 0) {
296         fprintf (stderr, "Error: requested database revision %s does not match %s\n",
297                  notmuch_requested_db_uuid, uuid);
298         exit (1);
299     }
300 }
301
302 static void
303 exec_man (const char *page)
304 {
305     if (execlp ("man", "man", page, (char *) NULL)) {
306         perror ("exec man");
307         exit (1);
308     }
309 }
310
311 static int
312 _help_for (const char *topic_name)
313 {
314     command_t *command;
315     help_topic_t *topic;
316     unsigned int i;
317
318     if (! topic_name) {
319         printf ("The notmuch mail system.\n\n");
320         usage (stdout);
321         return EXIT_SUCCESS;
322     }
323
324     if (strcmp (topic_name, "help") == 0) {
325         printf ("The notmuch help system.\n\n"
326                 "\tNotmuch uses the man command to display help. In case\n"
327                 "\tof difficulties check that MANPATH includes the pages\n"
328                 "\tinstalled by notmuch.\n\n"
329                 "\tTry \"notmuch help\" for a list of topics.\n");
330         return EXIT_SUCCESS;
331     }
332
333     command = find_command (topic_name);
334     if (command) {
335         char *page = talloc_asprintf (NULL, "notmuch-%s", command->name);
336         exec_man (page);
337     }
338
339     for (i = 0; i < ARRAY_SIZE (help_topics); i++) {
340         topic = &help_topics[i];
341         if (strcmp (topic_name, topic->name) == 0) {
342             char *page = talloc_asprintf (NULL, "notmuch-%s", topic->name);
343             exec_man (page);
344         }
345     }
346
347     fprintf (stderr,
348              "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
349              topic_name);
350     return EXIT_FAILURE;
351 }
352
353 static int
354 notmuch_help_command (unused (notmuch_config_t *config), unused(notmuch_database_t *notmuch), int
355                       argc, char *argv[])
356 {
357     int opt_index;
358
359     opt_index = notmuch_minimal_options ("help", argc, argv);
360     if (opt_index < 0)
361         return EXIT_FAILURE;
362
363     /* skip at least subcommand argument */
364     argc -= opt_index;
365     argv += opt_index;
366
367     if (argc == 0) {
368         return _help_for (NULL);
369     }
370
371     return _help_for (argv[0]);
372 }
373
374 /* Handle the case of "notmuch" being invoked with no command
375  * argument. For now we just call notmuch_setup_command, but we plan
376  * to be more clever about this in the future.
377  */
378 static int
379 notmuch_command (notmuch_config_t *config,
380                  notmuch_database_t *notmuch,
381                  unused(int argc), unused(char **argv))
382 {
383     char *db_path;
384     struct stat st;
385
386     /* If the user has never configured notmuch, then run
387      * notmuch_setup_command which will give a nice welcome message,
388      * and interactively guide the user through the configuration. */
389     if (notmuch_config_is_new (config))
390         return notmuch_setup_command (config, notmuch, 0, NULL);
391
392     /* Notmuch is already configured, but is there a database? */
393     db_path = talloc_asprintf (config, "%s/%s",
394                                notmuch_config_get_database_path (config),
395                                ".notmuch");
396     if (stat (db_path, &st)) {
397         if (errno != ENOENT) {
398             fprintf (stderr, "Error looking for notmuch database at %s: %s\n",
399                      db_path, strerror (errno));
400             return EXIT_FAILURE;
401         }
402         printf ("Notmuch is configured, but there's not yet a database at\n\n\t%s\n\n",
403                 db_path);
404         printf ("You probably want to run \"notmuch new\" now to create that database.\n\n"
405                 "Note that the first run of \"notmuch new\" can take a very long time\n"
406                 "and that the resulting database will use roughly the same amount of\n"
407                 "storage space as the email being indexed.\n\n");
408         return EXIT_SUCCESS;
409     }
410
411     printf ("Notmuch is configured and appears to have a database. Excellent!\n\n"
412             "At this point you can start exploring the functionality of notmuch by\n"
413             "using commands such as:\n\n"
414             "\tnotmuch search tag:inbox\n\n"
415             "\tnotmuch search to:\"%s\"\n\n"
416             "\tnotmuch search from:\"%s\"\n\n"
417             "\tnotmuch search subject:\"my favorite things\"\n\n"
418             "See \"notmuch help search\" for more details.\n\n"
419             "You can also use \"notmuch show\" with any of the thread IDs resulting\n"
420             "from a search. Finally, you may want to explore using a more sophisticated\n"
421             "interface to notmuch such as the emacs interface implemented in notmuch.el\n"
422             "or any other interface described at https://notmuchmail.org\n\n"
423             "And don't forget to run \"notmuch new\" whenever new mail arrives.\n\n"
424             "Have fun, and may your inbox never have much mail.\n\n",
425             notmuch_config_get_user_name (config),
426             notmuch_config_get_user_primary_email (config));
427
428     return EXIT_SUCCESS;
429 }
430
431 /*
432  * Try to run subcommand in argv[0] as notmuch- prefixed external
433  * command. argv must be NULL terminated (argv passed to main always
434  * is).
435  *
436  * Does not return if the external command is found and
437  * executed. Return true if external command is not found. Return
438  * false on errors.
439  */
440 static bool
441 try_external_command (char *argv[])
442 {
443     char *old_argv0 = argv[0];
444     bool ret = true;
445
446     argv[0] = talloc_asprintf (NULL, "notmuch-%s", old_argv0);
447
448     /*
449      * This will only return on errors. Not finding an external
450      * command (ENOENT) is not an error from our perspective.
451      */
452     execvp (argv[0], argv);
453     if (errno != ENOENT) {
454         fprintf (stderr, "Error: Running external command '%s' failed: %s\n",
455                  argv[0], strerror (errno));
456         ret = false;
457     }
458
459     talloc_free (argv[0]);
460     argv[0] = old_argv0;
461
462     return ret;
463 }
464
465 int
466 main (int argc, char *argv[])
467 {
468     void *local;
469     char *talloc_report;
470     const char *command_name = NULL;
471     command_t *command;
472     const char *config_file_name = NULL;
473     notmuch_config_t *config = NULL;
474     notmuch_database_t *notmuch = NULL;
475     int opt_index;
476     int ret = EXIT_SUCCESS;
477
478     notmuch_opt_desc_t options[] = {
479         { .opt_string = &config_file_name, .name = "config" },
480         { .opt_inherit = notmuch_shared_options },
481         { }
482     };
483
484     talloc_enable_null_tracking ();
485
486     local = talloc_new (NULL);
487
488     g_mime_init ();
489 #if ! GLIB_CHECK_VERSION (2, 35, 1)
490     g_type_init ();
491 #endif
492
493     /* Globally default to the current output format version. */
494     notmuch_format_version = NOTMUCH_FORMAT_CUR;
495
496     opt_index = parse_arguments (argc, argv, options, 1);
497     if (opt_index < 0) {
498         ret = EXIT_FAILURE;
499         goto DONE;
500     }
501
502     if (opt_index < argc)
503         command_name = argv[opt_index];
504
505     notmuch_process_shared_options (command_name);
506
507     command = find_command (command_name);
508     /* if command->function is NULL, try external command */
509     if (! command || ! command->function) {
510         /* This won't return if the external command is found. */
511         if (try_external_command (argv + opt_index))
512             fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
513                      command_name);
514         ret = EXIT_FAILURE;
515         goto DONE;
516     }
517
518     if (command->mode & NOTMUCH_COMMAND_DATABASE_EARLY) {
519         char *status_string = NULL;
520         notmuch_database_mode_t mode;
521         notmuch_status_t status;
522
523         if (command->mode & NOTMUCH_COMMAND_DATABASE_WRITE ||
524             command->mode & NOTMUCH_COMMAND_DATABASE_CREATE)
525             mode = NOTMUCH_DATABASE_MODE_READ_WRITE;
526         else
527             mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
528
529         if (command->mode & NOTMUCH_COMMAND_DATABASE_CREATE) {
530             status = notmuch_database_create_with_config (NULL,
531                                                           config_file_name,
532                                                           NULL,
533                                                           &notmuch,
534                                                           &status_string);
535             if (status && status != NOTMUCH_STATUS_DATABASE_EXISTS) {
536                 if (status_string) {
537                     fputs (status_string, stderr);
538                     free (status_string);
539                 }
540
541                 if (status == NOTMUCH_STATUS_NO_CONFIG)
542                     fputs ("Try running 'notmuch setup' to create a configuration.", stderr);
543
544                 return EXIT_FAILURE;
545             }
546         }
547
548         if (notmuch == NULL) {
549             status = notmuch_database_open_with_config (NULL,
550                                                         mode,
551                                                         config_file_name,
552                                                         NULL,
553                                                         &notmuch,
554                                                         &status_string);
555             if (status) {
556                 if (status_string) {
557                     fputs (status_string, stderr);
558                     free (status_string);
559                 }
560
561                 return EXIT_FAILURE;
562             }
563         }
564     }
565
566     if (command->mode & NOTMUCH_COMMAND_CONFIG_LOAD) {
567         char *status_string = NULL;
568         notmuch_status_t status;
569         status = notmuch_database_load_config (NULL,
570                                                config_file_name,
571                                                NULL,
572                                                &notmuch,
573                                                &status_string);
574
575         if (status == NOTMUCH_STATUS_NO_CONFIG && ! (command->mode & NOTMUCH_COMMAND_CONFIG_CREATE)) {
576             fputs ("Try running 'notmuch setup' to create a configuration.", stderr);
577             goto DONE;
578         }
579
580         if (status && (status != NOTMUCH_STATUS_NO_CONFIG)) {
581             if (status_string) {
582                 fputs (status_string, stderr);
583                 free (status_string);
584             }
585             goto DONE;
586         }
587
588     }
589
590     if (command->mode & NOTMUCH_COMMAND_CONFIG_OPEN) {
591         config = notmuch_config_open (notmuch, config_file_name, command->mode);
592         if (! config) {
593             ret = EXIT_FAILURE;
594             goto DONE;
595         }
596     }
597     ret = (command->function)(config, notmuch, argc - opt_index, argv + opt_index);
598
599   DONE:
600     if (config)
601         notmuch_config_close (config);
602
603     talloc_report = getenv ("NOTMUCH_TALLOC_REPORT");
604     if (talloc_report && strcmp (talloc_report, "") != 0) {
605         /* this relies on the previous call to
606          * talloc_enable_null_tracking
607          */
608
609         FILE *report = fopen (talloc_report, "w");
610         if (report) {
611             talloc_report_full (NULL, report);
612         } else {
613             ret = 1;
614             fprintf (stderr, "ERROR: unable to write talloc log. ");
615             perror (talloc_report);
616         }
617     }
618
619     talloc_free (local);
620
621     return ret;
622 }