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