]> git.notmuchmail.org Git - notmuch/blob - notmuch-dump.c
ac5d0742e0f9c61d74eca06633e199ee13491f47
[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     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
40                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
41     if (notmuch == NULL)
42         return 1;
43
44     if (argc && strcmp (argv[0], "--") != 0) {
45         output = fopen (argv[0], "w");
46         if (output == NULL) {
47             fprintf (stderr, "Error opening %s for writing: %s\n",
48                      argv[0], strerror (errno));
49             return 1;
50         }
51         argc--;
52         argv++;
53     }
54
55     if (argc && strcmp (argv[0], "--") == 0){
56         argc--;
57         argv++;
58     }
59
60     if (argc) {
61         query_str = query_string_from_args (notmuch, argc, argv);
62         if (query_str == NULL) {
63             fprintf (stderr, "Out of memory.\n");
64             return 1;
65         }
66     }
67  
68     query = notmuch_query_create (notmuch, query_str);
69     if (query == NULL) {
70         fprintf (stderr, "Out of memory\n");
71         return 1;
72     }
73     notmuch_query_set_sort (query, NOTMUCH_SORT_MESSAGE_ID);
74
75     for (messages = notmuch_query_search_messages (query);
76          notmuch_messages_valid (messages);
77          notmuch_messages_move_to_next (messages))
78     {
79         int first = 1;
80         message = notmuch_messages_get (messages);
81
82         fprintf (output,
83                  "%s (", notmuch_message_get_message_id (message));
84
85         for (tags = notmuch_message_get_tags (message);
86              notmuch_tags_valid (tags);
87              notmuch_tags_move_to_next (tags))
88         {
89             if (! first)
90                 fprintf (output, " ");
91
92             fprintf (output, "%s", notmuch_tags_get (tags));
93
94             first = 0;
95         }
96
97         fprintf (output, ")\n");
98
99         notmuch_message_destroy (message);
100     }
101
102     if (output != stdout)
103         fclose (output);
104
105     notmuch_query_destroy (query);
106     notmuch_database_close (notmuch);
107
108     return 0;
109 }