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