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_SUP) {
106 fprintf (output, "%s (", message_id);
109 for (tags = notmuch_message_get_tags (message);
110 notmuch_tags_valid (tags);
111 notmuch_tags_move_to_next (tags)) {
112 const char *tag_str = notmuch_tags_get (tags);
119 if (output_format == DUMP_FORMAT_SUP) {
120 fputs (tag_str, output);
122 if (hex_encode (notmuch, tag_str,
123 &buffer, &buffer_size) != HEX_SUCCESS) {
124 fprintf (stderr, "Error: failed to hex-encode tag %s\n",
128 fprintf (output, "+%s", buffer);
132 if (output_format == DUMP_FORMAT_SUP) {
133 fputs (")\n", output);
135 if (hex_encode (notmuch, message_id,
136 &buffer, &buffer_size) != HEX_SUCCESS) {
137 fprintf (stderr, "Error: failed to hex-encode msg-id %s\n",
141 fprintf (output, " -- id:%s\n", buffer);
144 notmuch_message_destroy (message);
147 if (output != stdout)
150 notmuch_query_destroy (query);
151 notmuch_database_destroy (notmuch);