]> git.notmuchmail.org Git - notmuch/blob - notmuch-show.c
notmuch: Break notmuch.c up into several smaller files.
[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_message_part (GMimeObject *part, int *part_count)
71 {
72     GMimeStream *stream;
73     GMimeDataWrapper *wrapper;
74     GMimeContentDisposition *disposition;
75     GMimeContentType *content_type;
76
77     *part_count = *part_count + 1;
78
79     if (GMIME_IS_MULTIPART (part)) {
80         GMimeMultipart *multipart = GMIME_MULTIPART (part);
81         int i;
82
83         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
84             if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
85                 /* Don't index the signature. */
86                 if (i == 1)
87                     continue;
88                 if (i > 1)
89                     fprintf (stderr, "Warning: Unexpected extra parts of mutlipart/signed. Continuing.\n");
90             }
91             show_message_part (g_mime_multipart_get_part (multipart, i),
92                                part_count);
93         }
94         return;
95     }
96
97     if (GMIME_IS_MESSAGE_PART (part)) {
98         GMimeMessage *mime_message;
99
100         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
101
102         show_message_part (g_mime_message_get_mime_part (mime_message),
103                            part_count);
104
105         return;
106     }
107
108     if (! (GMIME_IS_PART (part))) {
109         fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
110                  g_type_name (G_OBJECT_TYPE (part)));
111         return;
112     }
113
114     disposition = g_mime_object_get_content_disposition (part);
115     if (disposition &&
116         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
117     {
118         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
119         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
120
121         printf ("\fattachment{ ID: %d, Content-type: %s\n",
122                 *part_count,
123                 g_mime_content_type_to_string (content_type));
124         printf ("Attachment: %s (%s)\n", filename,
125                 g_mime_content_type_to_string (content_type));
126         printf ("\fattachment}\n");
127
128         return;
129     }
130
131     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
132
133     printf ("\fpart{ ID: %d, Content-type: %s\n",
134             *part_count,
135             g_mime_content_type_to_string (content_type));
136
137     if (g_mime_content_type_is_type (content_type, "text", "*") &&
138         !g_mime_content_type_is_type (content_type, "text", "html"))
139     {
140         stream = g_mime_stream_file_new (stdout);
141         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
142
143         wrapper = g_mime_part_get_content_object (GMIME_PART (part));
144         if (wrapper)
145             g_mime_data_wrapper_write_to_stream (wrapper, stream);
146
147         g_object_unref (stream);
148     }
149     else
150     {
151         printf ("Non-text part: %s\n",
152                 g_mime_content_type_to_string (content_type));
153     }
154
155     printf ("\fpart}\n");
156 }
157
158 static notmuch_status_t
159 show_message_body (const char *filename)
160 {
161     GMimeStream *stream = NULL;
162     GMimeParser *parser = NULL;
163     GMimeMessage *mime_message = NULL;
164     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
165     static int initialized = 0;
166     FILE *file = NULL;
167     int part_count = 0;
168
169     if (! initialized) {
170         g_mime_init (0);
171         initialized = 1;
172     }
173
174     file = fopen (filename, "r");
175     if (! file) {
176         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
177         ret = NOTMUCH_STATUS_FILE_ERROR;
178         goto DONE;
179     }
180
181     stream = g_mime_stream_file_new (file);
182     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
183
184     parser = g_mime_parser_new_with_stream (stream);
185
186     mime_message = g_mime_parser_construct_message (parser);
187
188     show_message_part (g_mime_message_get_mime_part (mime_message),
189                        &part_count);
190
191   DONE:
192     if (mime_message)
193         g_object_unref (mime_message);
194
195     if (parser)
196         g_object_unref (parser);
197
198     if (stream)
199         g_object_unref (stream);
200
201     if (file)
202         fclose (file);
203
204     return ret;
205 }
206
207 int
208 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
209 {
210     void *local = talloc_new (ctx);
211     char *query_string;
212     notmuch_database_t *notmuch = NULL;
213     notmuch_query_t *query = NULL;
214     notmuch_messages_t *messages;
215     notmuch_message_t *message;
216     int ret = 0;
217
218     const char *headers[] = {
219         "From", "To", "Cc", "Bcc", "Date"
220     };
221     const char *name, *value;
222     unsigned int i;
223
224     notmuch = notmuch_database_open (NULL);
225     if (notmuch == NULL) {
226         ret = 1;
227         goto DONE;
228     }
229
230     query_string = query_string_from_args (local, argc, argv);
231     if (query_string == NULL) {
232         fprintf (stderr, "Out of memory\n");
233         ret = 1;
234         goto DONE;
235     }
236
237     query = notmuch_query_create (notmuch, query_string);
238     if (query == NULL) {
239         fprintf (stderr, "Out of memory\n");
240         ret = 1;
241         goto DONE;
242     }
243
244     for (messages = notmuch_query_search_messages (query);
245          notmuch_messages_has_more (messages);
246          notmuch_messages_advance (messages))
247     {
248         message = notmuch_messages_get (messages);
249
250         printf ("\fmessage{ id:%s filename:%s\n",
251                 notmuch_message_get_message_id (message),
252                 notmuch_message_get_filename (message));
253
254         printf ("\fheader{\n");
255
256         printf ("%s\n", _get_one_line_summary (local, message));
257
258         printf ("%s\n", notmuch_message_get_header (message, "subject"));
259
260         for (i = 0; i < ARRAY_SIZE (headers); i++) {
261             name = headers[i];
262             value = notmuch_message_get_header (message, name);
263             if (value)
264                 printf ("%s: %s\n", name, value);
265         }
266
267         printf ("\fheader}\n");
268         printf ("\fbody{\n");
269
270         show_message_body (notmuch_message_get_filename (message));
271
272         printf ("\fbody}\n");
273
274         printf ("\fmessage}\n");
275
276         notmuch_message_destroy (message);
277     }
278
279   DONE:
280     if (local)
281         talloc_free (local);
282
283     if (query)
284         notmuch_query_destroy (query);
285
286     if (notmuch)
287         notmuch_database_close (notmuch);
288
289     return ret;
290 }