+static notmuch_status_t
+format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
+ mime_node_t *node, unused (int indent),
+ const notmuch_show_params_t *params)
+{
+ if (node->envelope_file) {
+ /* Special case the entire message to avoid MIME parsing. */
+ const char *filename;
+ GMimeStream *stream = NULL;
+ ssize_t ssize;
+ char buf[4096];
+ notmuch_status_t ret = NOTMUCH_STATUS_FILE_ERROR;
+
+ filename = _get_filename (node->envelope_file, params->duplicate);
+ if (filename == NULL) {
+ fprintf (stderr, "Error: Cannot get message filename.\n");
+ goto DONE;
+ }
+
+ stream = g_mime_stream_gzfile_open (filename);
+ if (stream == NULL) {
+ fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
+ goto DONE;
+ }
+
+ while (! g_mime_stream_eos (stream)) {
+ ssize = g_mime_stream_read (stream, buf, sizeof (buf));
+ if (ssize < 0) {
+ fprintf (stderr, "Error: Read failed from %s\n", filename);
+ goto DONE;
+ }
+
+ if (ssize > 0 && fwrite (buf, ssize, 1, stdout) != 1) {
+ fprintf (stderr, "Error: Write %zd chars to stdout failed\n", ssize);
+ goto DONE;
+ }
+ }
+
+ ret = NOTMUCH_STATUS_SUCCESS;
+
+ /* XXX This DONE is just for the special case of a node in a single file */
+ DONE:
+ if (stream)
+ g_object_unref (stream);
+
+ return ret;
+ }
+
+ GMimeStream *stream_filter = g_mime_stream_filter_new (params->out_stream);
+
+ if (GMIME_IS_PART (node->part)) {
+ /* For leaf parts, we emit only the transfer-decoded
+ * body. */
+ GMimeDataWrapper *wrapper;
+ wrapper = g_mime_part_get_content (GMIME_PART (node->part));
+
+ if (wrapper && stream_filter)
+ g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
+ } else {
+ /* Write out the whole part. For message parts (the root
+ * part and embedded message parts), this will be the
+ * message including its headers (but not the
+ * encapsulating part's headers). For multipart parts,
+ * this will include the headers. */
+ if (stream_filter)
+ g_mime_object_write_to_stream (node->part, NULL, stream_filter);
+ }
+
+ if (stream_filter)
+ g_object_unref (stream_filter);
+
+ return NOTMUCH_STATUS_SUCCESS;
+}
+
+static notmuch_status_t
+show_message (void *ctx,
+ const notmuch_show_format_t *format,
+ sprinter_t *sp,
+ notmuch_message_t *message,
+ int indent,
+ notmuch_show_params_t *params)
+{
+ void *local = talloc_new (ctx);
+ mime_node_t *root, *part;
+ notmuch_status_t status;
+ unsigned int session_keys = 0;
+ notmuch_status_t session_key_count_error = NOTMUCH_STATUS_SUCCESS;
+
+ if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
+ session_key_count_error = notmuch_message_count_properties (message, "session-key",
+ &session_keys);
+
+ status = mime_node_open (local, message, params->duplicate, &(params->crypto), &root);
+ if (status)
+ goto DONE;
+ part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
+ if (part)
+ status = format->part (local, sp, part, indent, params);
+ if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE && session_key_count_error ==
+ NOTMUCH_STATUS_SUCCESS) {
+ unsigned int new_session_keys = 0;
+ if (notmuch_message_count_properties (message, "session-key", &new_session_keys) ==
+ NOTMUCH_STATUS_SUCCESS &&
+ new_session_keys > session_keys) {
+ /* try a quiet re-indexing */
+ notmuch_indexopts_t *indexopts = notmuch_database_get_default_indexopts (
+ notmuch_message_get_database (message));
+ if (indexopts) {
+ notmuch_indexopts_set_decrypt_policy (indexopts, NOTMUCH_DECRYPT_AUTO);
+ print_status_message ("Error re-indexing message with --decrypt=stash",
+ message, notmuch_message_reindex (message, indexopts));
+ }
+ }