]> git.notmuchmail.org Git - notmuch/blob - show-message.c
notmuch part: Fix part numbering to match what's reported by "notmuch show"
[notmuch] / show-message.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see http://www.gnu.org/licenses/ .
18  *
19  * Authors: Carl Worth <cworth@cworth.org>
20  *          Keith Packard <keithp@keithp.com>
21  */
22
23 #include "notmuch-client.h"
24
25 static void
26 show_message_part (GMimeObject *part,
27                    int *part_count,
28                    const notmuch_show_format_t *format,
29                    int first)
30 {
31     if (GMIME_IS_MULTIPART (part)) {
32         GMimeMultipart *multipart = GMIME_MULTIPART (part);
33         int i;
34
35         *part_count = *part_count + 1;
36         format->part (part, part_count, first);
37
38         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
39             show_message_part (g_mime_multipart_get_part (multipart, i),
40                                part_count, format, i == 0);
41         }
42
43         if (format->part_end)
44             format->part_end (part);
45
46         return;
47     }
48
49     if (GMIME_IS_MESSAGE_PART (part)) {
50         GMimeMessage *mime_message;
51
52         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
53
54         show_message_part (g_mime_message_get_mime_part (mime_message),
55                            part_count, format, first);
56
57         return;
58     }
59
60     if (! (GMIME_IS_PART (part))) {
61         fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
62                  g_type_name (G_OBJECT_TYPE (part)));
63         return;
64     }
65
66     *part_count = *part_count + 1;
67
68     format->part (part, part_count, first);
69     if (format->part_end)
70         format->part_end (part);
71 }
72
73 notmuch_status_t
74 show_message_body (const char *filename,
75                    const notmuch_show_format_t *format)
76 {
77     GMimeStream *stream = NULL;
78     GMimeParser *parser = NULL;
79     GMimeMessage *mime_message = NULL;
80     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
81     FILE *file = NULL;
82     int part_count = 0;
83
84     file = fopen (filename, "r");
85     if (! file) {
86         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
87         ret = NOTMUCH_STATUS_FILE_ERROR;
88         goto DONE;
89     }
90
91     stream = g_mime_stream_file_new (file);
92     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
93
94     parser = g_mime_parser_new_with_stream (stream);
95
96     mime_message = g_mime_parser_construct_message (parser);
97
98     show_message_part (g_mime_message_get_mime_part (mime_message),
99                        &part_count,
100                        format,
101                        TRUE);
102
103   DONE:
104     if (mime_message)
105         g_object_unref (mime_message);
106
107     if (parser)
108         g_object_unref (parser);
109
110     if (stream)
111         g_object_unref (stream);
112
113     if (file)
114         fclose (file);
115
116     return ret;
117 }
118
119 static void
120 show_one_part_output (GMimeObject *part)
121 {
122     GMimeStream *stream_filter = NULL;
123     GMimeDataWrapper *wrapper;
124     GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
125
126     stream_filter = g_mime_stream_filter_new(stream_stdout);
127     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
128     if (wrapper && stream_filter)
129         g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
130     if (stream_filter)
131         g_object_unref(stream_filter);
132 }
133
134 static void
135 show_one_part_worker (GMimeObject *part, int *part_count, int desired_part)
136 {
137     if (GMIME_IS_MULTIPART (part)) {
138         GMimeMultipart *multipart = GMIME_MULTIPART (part);
139         int i;
140
141         *part_count = *part_count + 1;
142
143         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
144                 show_one_part_worker (g_mime_multipart_get_part (multipart, i),
145                                       part_count, desired_part);
146         }
147         return;
148     }
149
150     if (GMIME_IS_MESSAGE_PART (part)) {
151         GMimeMessage *mime_message;
152
153         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
154
155         show_one_part_worker (g_mime_message_get_mime_part (mime_message),
156                               part_count, desired_part);
157
158         return;
159     }
160
161     if (! (GMIME_IS_PART (part)))
162         return;
163
164     *part_count = *part_count + 1;
165
166     if (*part_count == desired_part)
167             show_one_part_output (part);
168 }
169
170 notmuch_status_t
171 show_one_part (const char *filename, int part)
172 {
173         GMimeStream *stream = NULL;
174         GMimeParser *parser = NULL;
175         GMimeMessage *mime_message = NULL;
176         notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
177         FILE *file = NULL;
178         int part_count = 0;
179
180         file = fopen (filename, "r");
181         if (! file) {
182                 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
183                 ret = NOTMUCH_STATUS_FILE_ERROR;
184                 goto DONE;
185         }
186
187         stream = g_mime_stream_file_new (file);
188         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
189
190         parser = g_mime_parser_new_with_stream (stream);
191
192         mime_message = g_mime_parser_construct_message (parser);
193
194         show_one_part_worker (g_mime_message_get_mime_part (mime_message),
195                               &part_count, part);
196
197  DONE:
198         if (mime_message)
199                 g_object_unref (mime_message);
200
201         if (parser)
202                 g_object_unref (parser);
203
204         if (stream)
205                 g_object_unref (stream);
206
207         if (file)
208                 fclose (file);
209
210         return ret;
211 }