]> git.notmuchmail.org Git - notmuch/blob - notmuch-dump.c
cli: config: make notmuch_config_open() "is new" parameter input only
[notmuch] / notmuch-dump.c
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 http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22 #include "dump-restore-private.h"
23 #include "string-util.h"
24
25 int
26 notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
27 {
28     notmuch_config_t *config;
29     notmuch_database_t *notmuch;
30     notmuch_query_t *query;
31     FILE *output = stdout;
32     notmuch_messages_t *messages;
33     notmuch_message_t *message;
34     notmuch_tags_t *tags;
35     const char *query_str = "";
36
37     config = notmuch_config_open (ctx, NULL, FALSE);
38     if (config == NULL)
39         return 1;
40
41     if (notmuch_database_open (notmuch_config_get_database_path (config),
42                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
43         return 1;
44
45     char *output_file_name = NULL;
46     int opt_index;
47
48     int output_format = DUMP_FORMAT_SUP;
49
50     notmuch_opt_desc_t options[] = {
51         { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
52           (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
53                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },
54                                   { 0, 0 } } },
55         { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
56         { 0, 0, 0, 0, 0 }
57     };
58
59     opt_index = parse_arguments (argc, argv, options, 1);
60
61     if (opt_index < 0) {
62         /* diagnostics already printed */
63         return 1;
64     }
65
66     if (output_file_name) {
67         output = fopen (output_file_name, "w");
68         if (output == NULL) {
69             fprintf (stderr, "Error opening %s for writing: %s\n",
70                      output_file_name, strerror (errno));
71             return 1;
72         }
73     }
74
75
76     if (opt_index < argc) {
77         query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
78         if (query_str == NULL) {
79             fprintf (stderr, "Out of memory.\n");
80             return 1;
81         }
82     }
83
84     query = notmuch_query_create (notmuch, query_str);
85     if (query == NULL) {
86         fprintf (stderr, "Out of memory\n");
87         return 1;
88     }
89     /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
90      * first results quickly at the expense of total time.
91      */
92     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
93
94     char *buffer = NULL;
95     size_t buffer_size = 0;
96
97     for (messages = notmuch_query_search_messages (query);
98          notmuch_messages_valid (messages);
99          notmuch_messages_move_to_next (messages)) {
100         int first = 1;
101         const char *message_id;
102
103         message = notmuch_messages_get (messages);
104         message_id = notmuch_message_get_message_id (message);
105
106         if (output_format == DUMP_FORMAT_BATCH_TAG &&
107             strchr (message_id, '\n')) {
108             /* This will produce a line break in the output, which
109              * would be difficult to handle in tools.  However, it's
110              * also impossible to produce an email containing a line
111              * break in a message ID because of unfolding, so we can
112              * safely disallow it. */
113             fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
114             notmuch_message_destroy (message);
115             continue;
116         }
117
118         if (output_format == DUMP_FORMAT_SUP) {
119             fprintf (output, "%s (", message_id);
120         }
121
122         for (tags = notmuch_message_get_tags (message);
123              notmuch_tags_valid (tags);
124              notmuch_tags_move_to_next (tags)) {
125             const char *tag_str = notmuch_tags_get (tags);
126
127             if (! first)
128                 fputs (" ", output);
129
130             first = 0;
131
132             if (output_format == DUMP_FORMAT_SUP) {
133                 fputs (tag_str, output);
134             } else {
135                 if (hex_encode (notmuch, tag_str,
136                                 &buffer, &buffer_size) != HEX_SUCCESS) {
137                     fprintf (stderr, "Error: failed to hex-encode tag %s\n",
138                              tag_str);
139                     return 1;
140                 }
141                 fprintf (output, "+%s", buffer);
142             }
143         }
144
145         if (output_format == DUMP_FORMAT_SUP) {
146             fputs (")\n", output);
147         } else {
148             if (make_boolean_term (notmuch, "id", message_id,
149                                    &buffer, &buffer_size)) {
150                     fprintf (stderr, "Error quoting message id %s: %s\n",
151                              message_id, strerror (errno));
152                     return 1;
153             }
154             fprintf (output, " -- %s\n", buffer);
155         }
156
157         notmuch_message_destroy (message);
158     }
159
160     if (output != stdout)
161         fclose (output);
162
163     notmuch_query_destroy (query);
164     notmuch_database_destroy (notmuch);
165
166     return 0;
167 }