]> git.notmuchmail.org Git - notmuch/blob - show-message.c
version: bump to 0.11.1
[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 typedef struct show_message_state {
26     int part_count;
27     int in_zone;
28 } show_message_state_t;
29
30 static void
31 show_message_part (GMimeObject *part,
32                    show_message_state_t *state,
33                    const notmuch_show_format_t *format,
34                    notmuch_show_params_t *params,
35                    int first)
36 {
37     GMimeObject *decryptedpart = NULL;
38     int selected;
39     state->part_count += 1;
40
41     if (! (GMIME_IS_PART (part) || GMIME_IS_MULTIPART (part) || GMIME_IS_MESSAGE_PART (part))) {
42         fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
43                  g_type_name (G_OBJECT_TYPE (part)));
44         return;
45     }
46
47     selected = (params->part <= 0 || state->part_count == params->part);
48
49     if (selected || state->in_zone) {
50         if (!first && (params->part <= 0 || state->in_zone))
51             fputs (format->part_sep, stdout);
52
53         if (format->part_start)
54             format->part_start (part, &(state->part_count));
55     }
56
57     /* handle PGP/MIME parts */
58     if (GMIME_IS_MULTIPART (part) && params->cryptoctx) {
59         GMimeMultipart *multipart = GMIME_MULTIPART (part);
60         GError* err = NULL;
61
62         if (GMIME_IS_MULTIPART_ENCRYPTED (part) && params->decrypt)
63         {
64             if ( g_mime_multipart_get_count (multipart) != 2 ) {
65                 /* this violates RFC 3156 section 4, so we won't bother with it. */
66                 fprintf (stderr,
67                          "Error: %d part(s) for a multipart/encrypted message (should be exactly 2)\n",
68                          g_mime_multipart_get_count (multipart));
69             } else {
70                 GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
71                 decryptedpart = g_mime_multipart_encrypted_decrypt (encrypteddata, params->cryptoctx, &err);
72                 if (decryptedpart) {
73                     if ((selected || state->in_zone) && format->part_encstatus)
74                         format->part_encstatus (1);
75                     const GMimeSignatureValidity *sigvalidity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
76                     if (!sigvalidity)
77                         fprintf (stderr, "Failed to verify signed part: %s\n", (err ? err->message : "no error explanation given"));
78                     if ((selected || state->in_zone) && format->part_sigstatus)
79                         format->part_sigstatus (sigvalidity);
80                 } else {
81                     fprintf (stderr, "Failed to decrypt part: %s\n", (err ? err->message : "no error explanation given"));
82                     if ((selected || state->in_zone) && format->part_encstatus)
83                         format->part_encstatus (0);
84                 }
85             }
86         }
87         else if (GMIME_IS_MULTIPART_SIGNED (part))
88         {
89             if ( g_mime_multipart_get_count (multipart) != 2 ) {
90                 /* this violates RFC 3156 section 5, so we won't bother with it. */
91                 fprintf (stderr,
92                          "Error: %d part(s) for a multipart/signed message (should be exactly 2)\n",
93                          g_mime_multipart_get_count (multipart));
94             } else {
95                 /* For some reason the GMimeSignatureValidity returned
96                  * here is not a const (inconsistent with that
97                  * returned by
98                  * g_mime_multipart_encrypted_get_signature_validity,
99                  * and therefore needs to be properly disposed of.
100                  * Hopefully the API will become more consistent. */
101                 GMimeSignatureValidity *sigvalidity = g_mime_multipart_signed_verify (GMIME_MULTIPART_SIGNED (part), params->cryptoctx, &err);
102                 if (!sigvalidity) {
103                     fprintf (stderr, "Failed to verify signed part: %s\n", (err ? err->message : "no error explanation given"));
104                 }
105                 if ((selected || state->in_zone) && format->part_sigstatus)
106                     format->part_sigstatus (sigvalidity);
107                 if (sigvalidity)
108                     g_mime_signature_validity_free (sigvalidity);
109             }
110         }
111
112         if (err)
113             g_error_free (err);
114     }
115     /* end handle PGP/MIME parts */
116
117     if (selected || state->in_zone)
118         format->part_content (part);
119
120     if (GMIME_IS_MULTIPART (part)) {
121         GMimeMultipart *multipart = GMIME_MULTIPART (part);
122         int i;
123
124         if (selected)
125             state->in_zone = 1;
126
127         if (decryptedpart) {
128             /* We emit the useless application/pgp-encrypted version
129              * part here only to keep the emitted output as consistent
130              * as possible between decrypted output and the
131              * unprocessed multipart/mime. For some strange reason,
132              * the actual encrypted data is the second part of the
133              * multipart. */
134             show_message_part (g_mime_multipart_get_part (multipart, 0), state, format, params, TRUE);
135             show_message_part (decryptedpart, state, format, params, FALSE);
136         } else {
137             for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
138                 show_message_part (g_mime_multipart_get_part (multipart, i),
139                                    state, format, params, i == 0);
140             }
141         }
142
143         if (selected)
144             state->in_zone = 0;
145
146     } else if (GMIME_IS_MESSAGE_PART (part)) {
147         GMimeMessage *mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
148
149         if (selected)
150             state->in_zone = 1;
151
152         if (selected || (!selected && state->in_zone)) {
153             fputs (format->header_start, stdout);
154             if (format->header_message_part)
155                 format->header_message_part (mime_message);
156             fputs (format->header_end, stdout);
157
158             fputs (format->body_start, stdout);
159         }
160
161         show_message_part (g_mime_message_get_mime_part (mime_message),
162                            state, format, params, TRUE);
163
164         if (selected || (!selected && state->in_zone))
165             fputs (format->body_end, stdout);
166
167         if (selected)
168             state->in_zone = 0;
169     }
170
171     if (selected || state->in_zone) {
172         if (format->part_end)
173             format->part_end (part);
174     }
175 }
176
177 notmuch_status_t
178 show_message_body (const char *filename,
179                    const notmuch_show_format_t *format,
180                    notmuch_show_params_t *params)
181 {
182     GMimeStream *stream = NULL;
183     GMimeParser *parser = NULL;
184     GMimeMessage *mime_message = NULL;
185     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
186     FILE *file = NULL;
187     show_message_state_t state;
188
189     state.part_count = 0;
190     state.in_zone = 0;
191
192     file = fopen (filename, "r");
193     if (! file) {
194         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
195         ret = NOTMUCH_STATUS_FILE_ERROR;
196         goto DONE;
197     }
198
199     stream = g_mime_stream_file_new (file);
200     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
201
202     parser = g_mime_parser_new_with_stream (stream);
203
204     mime_message = g_mime_parser_construct_message (parser);
205
206     show_message_part (g_mime_message_get_mime_part (mime_message),
207                        &state,
208                        format,
209                        params,
210                        TRUE);
211
212   DONE:
213     if (mime_message)
214         g_object_unref (mime_message);
215
216     if (parser)
217         g_object_unref (parser);
218
219     if (stream)
220         g_object_unref (stream);
221
222     if (file)
223         fclose (file);
224
225     return ret;
226 }