1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
22 #include "dump-restore-private.h"
25 notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
27 notmuch_config_t *config;
28 notmuch_database_t *notmuch;
29 notmuch_query_t *query;
30 FILE *output = stdout;
31 notmuch_messages_t *messages;
32 notmuch_message_t *message;
34 const char *query_str = "";
36 config = notmuch_config_open (ctx, NULL, NULL);
40 if (notmuch_database_open (notmuch_config_get_database_path (config),
41 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
44 char *output_file_name = NULL;
47 int output_format = DUMP_FORMAT_SUP;
49 notmuch_opt_desc_t options[] = {
50 { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
51 (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
52 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
54 { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0 },
58 opt_index = parse_arguments (argc, argv, options, 1);
61 /* diagnostics already printed */
65 if (output_file_name) {
66 output = fopen (output_file_name, "w");
68 fprintf (stderr, "Error opening %s for writing: %s\n",
69 output_file_name, strerror (errno));
75 if (opt_index < argc) {
76 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
77 if (query_str == NULL) {
78 fprintf (stderr, "Out of memory.\n");
83 query = notmuch_query_create (notmuch, query_str);
85 fprintf (stderr, "Out of memory\n");
88 /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
89 * first results quickly at the expense of total time.
91 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
94 size_t buffer_size = 0;
96 for (messages = notmuch_query_search_messages (query);
97 notmuch_messages_valid (messages);
98 notmuch_messages_move_to_next (messages)) {
100 const char *message_id;
102 message = notmuch_messages_get (messages);
103 message_id = notmuch_message_get_message_id (message);
105 if (output_format == DUMP_FORMAT_BATCH_TAG &&
106 strchr (message_id, '\n')) {
107 /* This will produce a line break in the output, which
108 * would be difficult to handle in tools. However, it's
109 * also impossible to produce an email containing a line
110 * break in a message ID because of unfolding, so we can
111 * safely disallow it. */
112 fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
113 notmuch_message_destroy (message);
117 if (output_format == DUMP_FORMAT_SUP) {
118 fprintf (output, "%s (", message_id);
121 for (tags = notmuch_message_get_tags (message);
122 notmuch_tags_valid (tags);
123 notmuch_tags_move_to_next (tags)) {
124 const char *tag_str = notmuch_tags_get (tags);
131 if (output_format == DUMP_FORMAT_SUP) {
132 fputs (tag_str, output);
134 if (hex_encode (notmuch, tag_str,
135 &buffer, &buffer_size) != HEX_SUCCESS) {
136 fprintf (stderr, "Error: failed to hex-encode tag %s\n",
140 fprintf (output, "+%s", buffer);
144 if (output_format == DUMP_FORMAT_SUP) {
145 fputs (")\n", output);
147 if (hex_encode (notmuch, message_id,
148 &buffer, &buffer_size) != HEX_SUCCESS) {
149 fprintf (stderr, "Error: failed to hex-encode msg-id %s\n",
153 fprintf (output, " -- id:%s\n", buffer);
156 notmuch_message_destroy (message);
159 if (output != stdout)
162 notmuch_query_destroy (query);
163 notmuch_database_destroy (notmuch);