]> rtime.felk.cvut.cz Git - notmuch.git/blob - notmuch-dump.c
Merge commit 'refs/top-bases/t/introduce-mailstore-abstraction' into t/introduce...
[notmuch.git] / 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;
30     notmuch_messages_t *messages;
31     notmuch_message_t *message;
32     notmuch_tags_t *tags;
33
34     config = notmuch_config_open (ctx, NULL, NULL);
35     if (config == NULL)
36         return 1;
37
38     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
39                                      NOTMUCH_DATABASE_MODE_READ_ONLY,
40                                      notmuch_config_get_mailstore (config));
41     if (notmuch == NULL)
42         return 1;
43
44     query = notmuch_query_create (notmuch, "");
45     if (query == NULL) {
46         fprintf (stderr, "Out of memory\n");
47         return 1;
48     }
49     notmuch_query_set_sort (query, NOTMUCH_SORT_MESSAGE_ID);
50
51     if (argc) {
52         output = fopen (argv[0], "w");
53         if (output == NULL) {
54             fprintf (stderr, "Error opening %s for writing: %s\n",
55                      argv[0], strerror (errno));
56             return 1;
57         }
58     } else {
59         output = stdout;
60     }
61
62     for (messages = notmuch_query_search_messages (query);
63          notmuch_messages_valid (messages);
64          notmuch_messages_move_to_next (messages))
65     {
66         int first = 1;
67         message = notmuch_messages_get (messages);
68
69         fprintf (output,
70                  "%s (", notmuch_message_get_message_id (message));
71
72         for (tags = notmuch_message_get_tags (message);
73              notmuch_tags_valid (tags);
74              notmuch_tags_move_to_next (tags))
75         {
76             if (! first)
77                 fprintf (output, " ");
78
79             fprintf (output, "%s", notmuch_tags_get (tags));
80
81             first = 0;
82         }
83
84         fprintf (output, ")\n");
85
86         notmuch_message_destroy (message);
87     }
88
89     if (output != stdout)
90         fclose (output);
91
92     notmuch_query_destroy (query);
93     notmuch_database_close (notmuch);
94
95     return 0;
96 }