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"
25 typedef struct show_message_state {
28 } show_message_state_t;
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,
37 GMimeObject *decryptedpart = NULL;
39 state->part_count += 1;
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)));
47 selected = (params->part <= 0 || state->part_count == params->part);
49 if (selected || state->in_zone) {
50 if (!first && (params->part <= 0 || state->in_zone))
51 fputs (format->part_sep, stdout);
53 if (format->part_start)
54 format->part_start (part, &(state->part_count));
57 /* handle PGP/MIME parts */
58 if (GMIME_IS_MULTIPART (part) && params->cryptoctx) {
59 GMimeMultipart *multipart = GMIME_MULTIPART (part);
62 if (GMIME_IS_MULTIPART_ENCRYPTED (part) && params->decrypt)
64 if ( g_mime_multipart_get_count (multipart) != 2 ) {
65 /* this violates RFC 3156 section 4, so we won't bother with it. */
67 "Error: %d part(s) for a multipart/encrypted message (should be exactly 2)\n",
68 g_mime_multipart_get_count (multipart));
70 GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
71 decryptedpart = g_mime_multipart_encrypted_decrypt (encrypteddata, params->cryptoctx, &err);
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);
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);
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);
87 else if (GMIME_IS_MULTIPART_SIGNED (part))
89 if ( g_mime_multipart_get_count (multipart) != 2 ) {
90 /* this violates RFC 3156 section 5, so we won't bother with it. */
92 "Error: %d part(s) for a multipart/signed message (should be exactly 2)\n",
93 g_mime_multipart_get_count (multipart));
95 /* For some reason the GMimeSignatureValidity returned
96 * here is not a const (inconsistent with that
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);
103 fprintf (stderr, "Failed to verify signed part: %s\n", (err ? err->message : "no error explanation given"));
105 if ((selected || state->in_zone) && format->part_sigstatus)
106 format->part_sigstatus (sigvalidity);
108 g_mime_signature_validity_free (sigvalidity);
115 /* end handle PGP/MIME parts */
117 if (selected || state->in_zone)
118 format->part_content (part);
120 if (GMIME_IS_MULTIPART (part)) {
121 GMimeMultipart *multipart = GMIME_MULTIPART (part);
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
134 show_message_part (g_mime_multipart_get_part (multipart, 0), state, format, params, TRUE);
135 show_message_part (decryptedpart, state, format, params, FALSE);
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);
146 } else if (GMIME_IS_MESSAGE_PART (part)) {
147 GMimeMessage *mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
152 show_message_part (g_mime_message_get_mime_part (mime_message),
153 state, format, params, TRUE);
159 if (selected || state->in_zone) {
160 if (format->part_end)
161 format->part_end (part);
166 show_message_body (const char *filename,
167 const notmuch_show_format_t *format,
168 notmuch_show_params_t *params)
170 GMimeStream *stream = NULL;
171 GMimeParser *parser = NULL;
172 GMimeMessage *mime_message = NULL;
173 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
175 show_message_state_t state;
177 state.part_count = 0;
180 file = fopen (filename, "r");
182 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
183 ret = NOTMUCH_STATUS_FILE_ERROR;
187 stream = g_mime_stream_file_new (file);
188 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
190 parser = g_mime_parser_new_with_stream (stream);
192 mime_message = g_mime_parser_construct_message (parser);
194 show_message_part (g_mime_message_get_mime_part (mime_message),
202 g_object_unref (mime_message);
205 g_object_unref (parser);
208 g_object_unref (stream);