]> git.notmuchmail.org Git - notmuch/blob - show-message.c
rename do_show_raw to do_show_single, and create params.raw for raw message output
[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 (!first)
32         fputs (format->part_sep, stdout);
33
34     if (GMIME_IS_MULTIPART (part)) {
35         GMimeMultipart *multipart = GMIME_MULTIPART (part);
36         int i;
37
38         *part_count = *part_count + 1;
39         format->part (part, part_count);
40
41         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
42             show_message_part (g_mime_multipart_get_part (multipart, i),
43                                part_count, format, i == 0);
44         }
45
46         if (format->part_end)
47             format->part_end (part);
48
49         return;
50     }
51
52     if (GMIME_IS_MESSAGE_PART (part)) {
53         GMimeMessage *mime_message;
54
55         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
56
57         show_message_part (g_mime_message_get_mime_part (mime_message),
58                            part_count, format, first);
59
60         return;
61     }
62
63     if (! (GMIME_IS_PART (part))) {
64         fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
65                  g_type_name (G_OBJECT_TYPE (part)));
66         return;
67     }
68
69     *part_count = *part_count + 1;
70
71     format->part (part, part_count);
72     if (format->part_end)
73         format->part_end (part);
74 }
75
76 notmuch_status_t
77 show_message_body (const char *filename,
78                    const notmuch_show_format_t *format)
79 {
80     GMimeStream *stream = NULL;
81     GMimeParser *parser = NULL;
82     GMimeMessage *mime_message = NULL;
83     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
84     FILE *file = NULL;
85     int part_count = 0;
86
87     file = fopen (filename, "r");
88     if (! file) {
89         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
90         ret = NOTMUCH_STATUS_FILE_ERROR;
91         goto DONE;
92     }
93
94     stream = g_mime_stream_file_new (file);
95     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
96
97     parser = g_mime_parser_new_with_stream (stream);
98
99     mime_message = g_mime_parser_construct_message (parser);
100
101     show_message_part (g_mime_message_get_mime_part (mime_message),
102                        &part_count,
103                        format,
104                        TRUE);
105
106   DONE:
107     if (mime_message)
108         g_object_unref (mime_message);
109
110     if (parser)
111         g_object_unref (parser);
112
113     if (stream)
114         g_object_unref (stream);
115
116     if (file)
117         fclose (file);
118
119     return ret;
120 }
121
122 static void
123 show_one_part_output (GMimeObject *part)
124 {
125     GMimeStream *stream_filter = NULL;
126     GMimeDataWrapper *wrapper;
127     GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
128
129     stream_filter = g_mime_stream_filter_new(stream_stdout);
130     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
131     if (wrapper && stream_filter)
132         g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
133     if (stream_filter)
134         g_object_unref(stream_filter);
135 }
136
137 static void
138 show_one_part_worker (GMimeObject *part, int *part_count, int desired_part)
139 {
140     if (GMIME_IS_MULTIPART (part)) {
141         GMimeMultipart *multipart = GMIME_MULTIPART (part);
142         int i;
143
144         *part_count = *part_count + 1;
145
146         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
147                 show_one_part_worker (g_mime_multipart_get_part (multipart, i),
148                                       part_count, desired_part);
149         }
150         return;
151     }
152
153     if (GMIME_IS_MESSAGE_PART (part)) {
154         GMimeMessage *mime_message;
155
156         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
157
158         show_one_part_worker (g_mime_message_get_mime_part (mime_message),
159                               part_count, desired_part);
160
161         return;
162     }
163
164     if (! (GMIME_IS_PART (part)))
165         return;
166
167     *part_count = *part_count + 1;
168
169     if (*part_count == desired_part)
170             show_one_part_output (part);
171 }
172
173 notmuch_status_t
174 show_one_part (const char *filename, int part)
175 {
176         GMimeStream *stream = NULL;
177         GMimeParser *parser = NULL;
178         GMimeMessage *mime_message = NULL;
179         notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
180         FILE *file = NULL;
181         int part_count = 0;
182
183         file = fopen (filename, "r");
184         if (! file) {
185                 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
186                 ret = NOTMUCH_STATUS_FILE_ERROR;
187                 goto DONE;
188         }
189
190         stream = g_mime_stream_file_new (file);
191         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
192
193         parser = g_mime_parser_new_with_stream (stream);
194
195         mime_message = g_mime_parser_construct_message (parser);
196
197         show_one_part_worker (g_mime_message_get_mime_part (mime_message),
198                               &part_count, part);
199
200  DONE:
201         if (mime_message)
202                 g_object_unref (mime_message);
203
204         if (parser)
205                 g_object_unref (parser);
206
207         if (stream)
208                 g_object_unref (stream);
209
210         if (file)
211                 fclose (file);
212
213         return ret;
214 }