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"
23 #include "string-util.h"
26 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
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 if (notmuch_database_open (notmuch_config_get_database_path (config),
37 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
40 char *output_file_name = NULL;
43 int output_format = DUMP_FORMAT_SUP;
45 notmuch_opt_desc_t options[] = {
46 { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
47 (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
48 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
50 { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0 },
54 opt_index = parse_arguments (argc, argv, options, 1);
57 /* diagnostics already printed */
61 if (output_file_name) {
62 output = fopen (output_file_name, "w");
64 fprintf (stderr, "Error opening %s for writing: %s\n",
65 output_file_name, strerror (errno));
71 if (opt_index < argc) {
72 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
73 if (query_str == NULL) {
74 fprintf (stderr, "Out of memory.\n");
79 query = notmuch_query_create (notmuch, query_str);
81 fprintf (stderr, "Out of memory\n");
84 /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
85 * first results quickly at the expense of total time.
87 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
90 size_t buffer_size = 0;
92 for (messages = notmuch_query_search_messages (query);
93 notmuch_messages_valid (messages);
94 notmuch_messages_move_to_next (messages)) {
96 const char *message_id;
98 message = notmuch_messages_get (messages);
99 message_id = notmuch_message_get_message_id (message);
101 if (output_format == DUMP_FORMAT_BATCH_TAG &&
102 strchr (message_id, '\n')) {
103 /* This will produce a line break in the output, which
104 * would be difficult to handle in tools. However, it's
105 * also impossible to produce an email containing a line
106 * break in a message ID because of unfolding, so we can
107 * safely disallow it. */
108 fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
109 notmuch_message_destroy (message);
113 if (output_format == DUMP_FORMAT_SUP) {
114 fprintf (output, "%s (", message_id);
117 for (tags = notmuch_message_get_tags (message);
118 notmuch_tags_valid (tags);
119 notmuch_tags_move_to_next (tags)) {
120 const char *tag_str = notmuch_tags_get (tags);
127 if (output_format == DUMP_FORMAT_SUP) {
128 fputs (tag_str, output);
130 if (hex_encode (notmuch, tag_str,
131 &buffer, &buffer_size) != HEX_SUCCESS) {
132 fprintf (stderr, "Error: failed to hex-encode tag %s\n",
136 fprintf (output, "+%s", buffer);
140 if (output_format == DUMP_FORMAT_SUP) {
141 fputs (")\n", output);
143 if (make_boolean_term (notmuch, "id", message_id,
144 &buffer, &buffer_size)) {
145 fprintf (stderr, "Error quoting message id %s: %s\n",
146 message_id, strerror (errno));
149 fprintf (output, " -- %s\n", buffer);
152 notmuch_message_destroy (message);
155 if (output != stdout)
158 notmuch_query_destroy (query);
159 notmuch_database_destroy (notmuch);