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