]> git.notmuchmail.org Git - notmuch/blob - notmuch-show.c
notmuch_database_add_message: Add missing error-value propagation.
[notmuch] / notmuch-show.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 static const char *
24 _get_tags_as_string (void *ctx, notmuch_message_t *message)
25 {
26     notmuch_tags_t *tags;
27     int first = 1;
28     const char *tag;
29     char *result;
30
31     result = talloc_strdup (ctx, "");
32     if (result == NULL)
33         return NULL;
34
35     for (tags = notmuch_message_get_tags (message);
36          notmuch_tags_has_more (tags);
37          notmuch_tags_advance (tags))
38     {
39         tag = notmuch_tags_get (tags);
40
41         result = talloc_asprintf_append (result, "%s%s",
42                                          first ? "" : " ", tag);
43         first = 0;
44     }
45
46     return result;
47 }
48
49 /* Get a nice, single-line summary of message. */
50 static const char *
51 _get_one_line_summary (void *ctx, notmuch_message_t *message)
52 {
53     const char *from;
54     time_t date;
55     const char *relative_date;
56     const char *tags;
57
58     from = notmuch_message_get_header (message, "from");
59
60     date = notmuch_message_get_date (message);
61     relative_date = notmuch_time_relative_date (ctx, date);
62
63     tags = _get_tags_as_string (ctx, message);
64
65     return talloc_asprintf (ctx, "%s (%s) (%s)",
66                             from, relative_date, tags);
67 }
68
69 static void
70 show_part (GMimeObject *part, int *part_count)
71 {
72     GMimeContentDisposition *disposition;
73     GMimeContentType *content_type;
74     GMimeDataWrapper *wrapper;
75
76     disposition = g_mime_object_get_content_disposition (part);
77     if (disposition &&
78         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
79     {
80         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
81         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
82
83         printf ("\fattachment{ ID: %d, Content-type: %s\n",
84                 *part_count,
85                 g_mime_content_type_to_string (content_type));
86         printf ("Attachment: %s (%s)\n", filename,
87                 g_mime_content_type_to_string (content_type));
88         printf ("\fattachment}\n");
89
90         return;
91     }
92
93     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
94
95     printf ("\fpart{ ID: %d, Content-type: %s\n",
96             *part_count,
97             g_mime_content_type_to_string (content_type));
98
99     if (g_mime_content_type_is_type (content_type, "text", "*") &&
100         !g_mime_content_type_is_type (content_type, "text", "html"))
101     {
102         GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
103         GMimeStream *stream_filter = NULL;
104
105         if (stream_stdout) {
106             g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
107             stream_filter = g_mime_stream_filter_new(stream_stdout);
108             g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
109                                      g_mime_filter_crlf_new(FALSE, FALSE));
110         }
111
112         wrapper = g_mime_part_get_content_object (GMIME_PART (part));
113         if (wrapper && stream_filter)
114             g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
115         if (stream_filter)
116             g_object_unref(stream_filter);
117         if (stream_stdout)
118             g_object_unref(stream_stdout);
119     }
120     else
121     {
122         printf ("Non-text part: %s\n",
123                 g_mime_content_type_to_string (content_type));
124     }
125
126     printf ("\fpart}\n");
127 }
128
129 static void
130 show_message (void *ctx, notmuch_message_t *message, int indent)
131 {
132     const char *headers[] = {
133         "Subject", "From", "To", "Cc", "Bcc", "Date"
134     };
135     const char *name, *value;
136     unsigned int i;
137
138     printf ("\fmessage{ id:%s depth:%d filename:%s\n",
139             notmuch_message_get_message_id (message),
140             indent,
141             notmuch_message_get_filename (message));
142
143     printf ("\fheader{\n");
144
145     printf ("%s\n", _get_one_line_summary (ctx, message));
146
147     for (i = 0; i < ARRAY_SIZE (headers); i++) {
148         name = headers[i];
149         value = notmuch_message_get_header (message, name);
150         if (value)
151             printf ("%s: %s\n", name, value);
152     }
153
154     printf ("\fheader}\n");
155     printf ("\fbody{\n");
156
157     show_message_body (notmuch_message_get_filename (message), show_part);
158
159     printf ("\fbody}\n");
160
161     printf ("\fmessage}\n");
162 }
163
164
165 static void
166 show_messages (void *ctx, notmuch_messages_t *messages, int indent)
167 {
168     notmuch_message_t *message;
169
170     for (;
171          notmuch_messages_has_more (messages);
172          notmuch_messages_advance (messages))
173     {
174         message = notmuch_messages_get (messages);
175
176         show_message (ctx, message, indent);
177
178         show_messages (ctx, notmuch_message_get_replies (message), indent + 1);
179
180         notmuch_message_destroy (message);
181     }
182 }
183
184 int
185 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
186 {
187     notmuch_config_t *config;
188     notmuch_database_t *notmuch;
189     notmuch_query_t *query;
190     notmuch_threads_t *threads;
191     notmuch_thread_t *thread;
192     notmuch_messages_t *messages;
193     char *query_string;
194
195     config = notmuch_config_open (ctx, NULL, NULL);
196     if (config == NULL)
197         return 1;
198
199     query_string = query_string_from_args (ctx, argc, argv);
200     if (query_string == NULL) {
201         fprintf (stderr, "Out of memory\n");
202         return 1;
203     }
204
205     if (*query_string == '\0') {
206         fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
207         return 1;
208     }
209
210     notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
211     if (notmuch == NULL)
212         return 1;
213
214     query = notmuch_query_create (notmuch, query_string);
215     if (query == NULL) {
216         fprintf (stderr, "Out of memory\n");
217         return 1;
218     }
219
220     for (threads = notmuch_query_search_threads (query, 0, -1);
221          notmuch_threads_has_more (threads);
222          notmuch_threads_advance (threads))
223     {
224         thread = notmuch_threads_get (threads);
225
226         messages = notmuch_thread_get_toplevel_messages (thread);
227
228         if (messages == NULL)
229             INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
230                             notmuch_thread_get_thread_id (thread));
231
232         show_messages (ctx, messages, 0);
233
234         notmuch_thread_destroy (thread);
235     }
236
237     notmuch_query_destroy (query);
238     notmuch_database_close (notmuch);
239
240     return 0;
241 }