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