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