1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
4 * Copyright © 2009 Keith Packard
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.
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.
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/ .
19 * Authors: Carl Worth <cworth@cworth.org>
20 * Keith Packard <keithp@keithp.com>
23 #include "notmuch-client.h"
26 show_message_part (GMimeObject *part,
28 const notmuch_show_format_t *format,
32 fputs (format->part_sep, stdout);
34 if (GMIME_IS_MULTIPART (part)) {
35 GMimeMultipart *multipart = GMIME_MULTIPART (part);
38 *part_count = *part_count + 1;
39 format->part (part, part_count);
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);
47 format->part_end (part);
52 if (GMIME_IS_MESSAGE_PART (part)) {
53 GMimeMessage *mime_message;
55 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
57 show_message_part (g_mime_message_get_mime_part (mime_message),
58 part_count, format, first);
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)));
69 *part_count = *part_count + 1;
71 format->part (part, part_count);
73 format->part_end (part);
77 show_message_body (const char *filename,
78 const notmuch_show_format_t *format)
80 GMimeStream *stream = NULL;
81 GMimeParser *parser = NULL;
82 GMimeMessage *mime_message = NULL;
83 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
87 file = fopen (filename, "r");
89 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
90 ret = NOTMUCH_STATUS_FILE_ERROR;
94 stream = g_mime_stream_file_new (file);
95 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
97 parser = g_mime_parser_new_with_stream (stream);
99 mime_message = g_mime_parser_construct_message (parser);
101 show_message_part (g_mime_message_get_mime_part (mime_message),
108 g_object_unref (mime_message);
111 g_object_unref (parser);
114 g_object_unref (stream);
123 show_one_part_output (GMimeObject *part)
125 GMimeStream *stream_filter = NULL;
126 GMimeDataWrapper *wrapper;
127 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
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);
134 g_object_unref(stream_filter);
138 show_one_part_worker (GMimeObject *part, int *part_count, int desired_part)
140 if (GMIME_IS_MULTIPART (part)) {
141 GMimeMultipart *multipart = GMIME_MULTIPART (part);
144 *part_count = *part_count + 1;
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);
153 if (GMIME_IS_MESSAGE_PART (part)) {
154 GMimeMessage *mime_message;
156 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
158 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
159 part_count, desired_part);
164 if (! (GMIME_IS_PART (part)))
167 *part_count = *part_count + 1;
169 if (*part_count == desired_part)
170 show_one_part_output (part);
174 show_one_part (const char *filename, int part)
176 GMimeStream *stream = NULL;
177 GMimeParser *parser = NULL;
178 GMimeMessage *mime_message = NULL;
179 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
183 file = fopen (filename, "r");
185 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
186 ret = NOTMUCH_STATUS_FILE_ERROR;
190 stream = g_mime_stream_file_new (file);
191 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
193 parser = g_mime_parser_new_with_stream (stream);
195 mime_message = g_mime_parser_construct_message (parser);
197 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
202 g_object_unref (mime_message);
205 g_object_unref (parser);
208 g_object_unref (stream);