]> git.notmuchmail.org Git - notmuch/blob - notmuch-dump.c
NEWS: discuss changes for dump and restore syntax.
[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
23 int
24 notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
25 {
26     notmuch_config_t *config;
27     notmuch_database_t *notmuch;
28     notmuch_query_t *query;
29     FILE *output = stdout;
30     notmuch_messages_t *messages;
31     notmuch_message_t *message;
32     notmuch_tags_t *tags;
33     const char* query_str = "";
34
35     config = notmuch_config_open (ctx, NULL, NULL);
36     if (config == NULL)
37         return 1;
38
39     if (notmuch_database_open (notmuch_config_get_database_path (config),
40                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
41         return 1;
42
43     char *output_file_name = NULL;
44     int opt_index;
45
46     notmuch_opt_desc_t options[] = {
47         { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
48         { 0, 0, 0, 0, 0 }
49     };
50
51     opt_index = parse_arguments (argc, argv, options, 1);
52
53     if (opt_index < 0) {
54         /* diagnostics already printed */
55         return 1;
56     }
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 1;
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 1;
73         }
74     }
75
76     query = notmuch_query_create (notmuch, query_str);
77     if (query == NULL) {
78         fprintf (stderr, "Out of memory\n");
79         return 1;
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     for (messages = notmuch_query_search_messages (query);
87          notmuch_messages_valid (messages);
88          notmuch_messages_move_to_next (messages))
89     {
90         int first = 1;
91         message = notmuch_messages_get (messages);
92
93         fprintf (output,
94                  "%s (", notmuch_message_get_message_id (message));
95
96         for (tags = notmuch_message_get_tags (message);
97              notmuch_tags_valid (tags);
98              notmuch_tags_move_to_next (tags))
99         {
100             if (! first)
101                 fprintf (output, " ");
102
103             fprintf (output, "%s", notmuch_tags_get (tags));
104
105             first = 0;
106         }
107
108         fprintf (output, ")\n");
109
110         notmuch_message_destroy (message);
111     }
112
113     if (output != stdout)
114         fclose (output);
115
116     notmuch_query_destroy (query);
117     notmuch_database_destroy (notmuch);
118
119     return 0;
120 }