]> git.cworth.org Git - notmuch/blob - notmuch-dump.c
notmuch: Break notmuch.c up into several smaller files.
[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     FILE *output = NULL;
27     notmuch_database_t *notmuch = NULL;
28     notmuch_query_t *query;
29     notmuch_messages_t *messages;
30     notmuch_message_t *message;
31     notmuch_tags_t *tags;
32     int ret = 0;
33
34     if (argc) {
35         output = fopen (argv[0], "w");
36         if (output == NULL) {
37             fprintf (stderr, "Error opening %s for writing: %s\n",
38                      argv[0], strerror (errno));
39             ret = 1;
40             goto DONE;
41         }
42     } else {
43         output = stdout;
44     }
45
46     notmuch = notmuch_database_open (NULL);
47     if (notmuch == NULL) {
48         ret = 1;
49         goto DONE;
50     }
51
52     query = notmuch_query_create (notmuch, "");
53     if (query == NULL) {
54         fprintf (stderr, "Out of memory\n");
55         ret = 1;
56         goto DONE;
57     }
58
59     notmuch_query_set_sort (query, NOTMUCH_SORT_MESSAGE_ID);
60
61     for (messages = notmuch_query_search_messages (query);
62          notmuch_messages_has_more (messages);
63          notmuch_messages_advance (messages))
64     {
65         int first = 1;
66         message = notmuch_messages_get (messages);
67
68         fprintf (output,
69                  "%s (", notmuch_message_get_message_id (message));
70
71         for (tags = notmuch_message_get_tags (message);
72              notmuch_tags_has_more (tags);
73              notmuch_tags_advance (tags))
74         {
75             if (! first)
76                 fprintf (output, " ");
77
78             fprintf (output, "%s", notmuch_tags_get (tags));
79
80             first = 0;
81         }
82
83         fprintf (output, ")\n");
84
85         notmuch_message_destroy (message);
86     }
87
88     notmuch_query_destroy (query);
89
90   DONE:
91     if (notmuch)
92         notmuch_database_close (notmuch);
93     if (output && output != stdout)
94         fclose (output);
95
96     return ret;
97 }