]> git.notmuchmail.org Git - notmuch/blob - notmuch-show.c
notmuch new: Fix deletion support to recurse on removed directories.
[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_content (GMimeObject *part)
71 {
72     GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
73     GMimeStream *stream_filter = NULL;
74     GMimeDataWrapper *wrapper;
75     const char *charset;
76
77     charset = g_mime_object_get_content_type_parameter (part, "charset");
78
79     if (stream_stdout) {
80         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
81         stream_filter = g_mime_stream_filter_new(stream_stdout);
82         g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
83                                  g_mime_filter_crlf_new(FALSE, FALSE));
84         if (charset) {
85           g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
86                                    g_mime_filter_charset_new(charset, "UTF-8"));
87         }
88     }
89
90     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
91     if (wrapper && stream_filter)
92         g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
93     if (stream_filter)
94         g_object_unref(stream_filter);
95     if (stream_stdout)
96         g_object_unref(stream_stdout);
97 }
98
99 static void
100 show_part (GMimeObject *part, int *part_count)
101 {
102     GMimeContentDisposition *disposition;
103     GMimeContentType *content_type;
104
105     disposition = g_mime_object_get_content_disposition (part);
106     if (disposition &&
107         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
108     {
109         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
110         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
111
112         printf ("\fattachment{ ID: %d, Content-type: %s\n",
113                 *part_count,
114                 g_mime_content_type_to_string (content_type));
115         printf ("Attachment: %s (%s)\n", filename,
116                 g_mime_content_type_to_string (content_type));
117
118         if (g_mime_content_type_is_type (content_type, "text", "*") &&
119             !g_mime_content_type_is_type (content_type, "text", "html"))
120         {
121             show_part_content (part);
122         }
123
124         printf ("\fattachment}\n");
125
126         return;
127     }
128
129     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
130
131     printf ("\fpart{ ID: %d, Content-type: %s\n",
132             *part_count,
133             g_mime_content_type_to_string (content_type));
134
135     if (g_mime_content_type_is_type (content_type, "text", "*") &&
136         !g_mime_content_type_is_type (content_type, "text", "html"))
137     {
138         show_part_content (part);
139     }
140     else
141     {
142         printf ("Non-text part: %s\n",
143                 g_mime_content_type_to_string (content_type));
144     }
145
146     printf ("\fpart}\n");
147 }
148
149 static void
150 show_message (void *ctx, notmuch_message_t *message, int indent)
151 {
152     const char *headers[] = {
153         "Subject", "From", "To", "Cc", "Bcc", "Date"
154     };
155     const char *name, *value;
156     unsigned int i;
157
158     printf ("\fmessage{ id:%s depth:%d match:%d filename:%s\n",
159             notmuch_message_get_message_id (message),
160             indent,
161             notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
162             notmuch_message_get_filename (message));
163
164     printf ("\fheader{\n");
165
166     printf ("%s\n", _get_one_line_summary (ctx, message));
167
168     for (i = 0; i < ARRAY_SIZE (headers); i++) {
169         name = headers[i];
170         value = notmuch_message_get_header (message, name);
171         if (value)
172             printf ("%s: %s\n", name, value);
173     }
174
175     printf ("\fheader}\n");
176     printf ("\fbody{\n");
177
178     show_message_body (notmuch_message_get_filename (message), show_part);
179
180     printf ("\fbody}\n");
181
182     printf ("\fmessage}\n");
183 }
184
185
186 static void
187 show_messages (void *ctx, notmuch_messages_t *messages, int indent,
188                notmuch_bool_t entire_thread)
189 {
190     notmuch_message_t *message;
191     notmuch_bool_t match;
192     int next_indent;
193
194     for (;
195          notmuch_messages_has_more (messages);
196          notmuch_messages_advance (messages))
197     {
198         message = notmuch_messages_get (messages);
199
200         match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
201
202         next_indent = indent;
203
204         if (match || entire_thread) {
205             show_message (ctx, message, indent);
206             next_indent = indent + 1;
207         }
208
209         show_messages (ctx, notmuch_message_get_replies (message),
210                        next_indent, entire_thread);
211
212         notmuch_message_destroy (message);
213     }
214 }
215
216 int
217 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
218 {
219     notmuch_config_t *config;
220     notmuch_database_t *notmuch;
221     notmuch_query_t *query;
222     notmuch_threads_t *threads;
223     notmuch_thread_t *thread;
224     notmuch_messages_t *messages;
225     char *query_string;
226     int entire_thread = 0;
227     int i;
228
229     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
230         if (strcmp (argv[i], "--") == 0) {
231             i++;
232             break;
233         }
234         if (strcmp(argv[i], "--entire-thread") == 0) {
235             entire_thread = 1;
236         } else {
237             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
238             return 1;
239         }
240     }
241
242     argc -= i;
243     argv += i;
244
245     config = notmuch_config_open (ctx, NULL, NULL);
246     if (config == NULL)
247         return 1;
248
249     query_string = query_string_from_args (ctx, argc, argv);
250     if (query_string == NULL) {
251         fprintf (stderr, "Out of memory\n");
252         return 1;
253     }
254
255     if (*query_string == '\0') {
256         fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
257         return 1;
258     }
259
260     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
261                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
262     if (notmuch == NULL)
263         return 1;
264
265     query = notmuch_query_create (notmuch, query_string);
266     if (query == NULL) {
267         fprintf (stderr, "Out of memory\n");
268         return 1;
269     }
270
271     for (threads = notmuch_query_search_threads (query);
272          notmuch_threads_has_more (threads);
273          notmuch_threads_advance (threads))
274     {
275         thread = notmuch_threads_get (threads);
276
277         messages = notmuch_thread_get_toplevel_messages (thread);
278
279         if (messages == NULL)
280             INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
281                             notmuch_thread_get_thread_id (thread));
282
283         show_messages (ctx, messages, 0, entire_thread);
284
285         notmuch_thread_destroy (thread);
286     }
287
288     notmuch_query_destroy (query);
289     notmuch_database_close (notmuch);
290
291     return 0;
292 }