static void format_part_content_raw (GMimeObject *part) { if (! GMIME_IS_PART (part)) return; GMimeStream *stream_stdout; GMimeStream *stream_filter = NULL; GMimeDataWrapper *wrapper; stream_stdout = g_mime_stream_file_new (stdout); g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); stream_filter = g_mime_stream_filter_new (stream_stdout); wrapper = g_mime_part_get_content_object (GMIME_PART (part)); if (wrapper && stream_filter) g_mime_data_wrapper_write_to_stream (wrapper, stream_filter); if (stream_filter) g_object_unref (stream_filter); if (stream_stdout) g_object_unref(stream_stdout); }
static void reply_part_content (GMimeObject *part) { GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part)); GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part); if (g_mime_content_type_is_type (content_type, "multipart", "*") || g_mime_content_type_is_type (content_type, "message", "rfc822")) { /* Output nothing, since multipart subparts will be handled individually. */ } else if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") || g_mime_content_type_is_type (content_type, "application", "pgp-signature")) { /* Ignore PGP/MIME cruft parts */ } else if (g_mime_content_type_is_type (content_type, "text", "*") && !g_mime_content_type_is_type (content_type, "text", "html")) { GMimeStream *stream_stdout = NULL, *stream_filter = NULL; GMimeDataWrapper *wrapper; const char *charset; charset = g_mime_object_get_content_type_parameter (part, "charset"); stream_stdout = g_mime_stream_file_new (stdout); if (stream_stdout) { g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); stream_filter = g_mime_stream_filter_new(stream_stdout); if (charset) { g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter), g_mime_filter_charset_new(charset, "UTF-8")); } } g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter), g_mime_filter_reply_new(TRUE)); wrapper = g_mime_part_get_content_object (GMIME_PART (part)); if (wrapper && stream_filter) g_mime_data_wrapper_write_to_stream (wrapper, stream_filter); if (stream_filter) g_object_unref(stream_filter); if (stream_stdout) g_object_unref(stream_stdout); } else { if (disposition && strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0) { const char *filename = g_mime_part_get_filename (GMIME_PART (part)); printf ("Attachment: %s (%s)\n", filename, g_mime_content_type_to_string (content_type)); } else { printf ("Non-text part: %s\n", g_mime_content_type_to_string (content_type)); } } }
static void format_part_reply (mime_node_t *node) { int i; if (node->envelope_file) { printf ("On %s, %s wrote:\n", notmuch_message_get_header (node->envelope_file, "date"), notmuch_message_get_header (node->envelope_file, "from")); } else if (GMIME_IS_MESSAGE (node->part)) { GMimeMessage *message = GMIME_MESSAGE (node->part); InternetAddressList *recipients; const char *recipients_string; printf ("> From: %s\n", g_mime_message_get_sender (message)); recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO); recipients_string = internet_address_list_to_string (recipients, 0); if (recipients_string) printf ("> To: %s\n", recipients_string); recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC); recipients_string = internet_address_list_to_string (recipients, 0); if (recipients_string) printf ("> Cc: %s\n", recipients_string); printf ("> Subject: %s\n", g_mime_message_get_subject (message)); printf ("> Date: %s\n", g_mime_message_get_date_as_string (message)); printf (">\n"); } else if (GMIME_IS_PART (node->part)) { GMimeContentType *content_type = g_mime_object_get_content_type (node->part); GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (node->part); if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") || g_mime_content_type_is_type (content_type, "application", "pgp-signature")) { /* Ignore PGP/MIME cruft parts */ } else if (g_mime_content_type_is_type (content_type, "text", "*") && !g_mime_content_type_is_type (content_type, "text", "html")) { GMimeStream *stream_stdout = g_mime_stream_file_new (stdout); g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); show_text_part_content (node->part, stream_stdout, NOTMUCH_SHOW_TEXT_PART_REPLY); g_object_unref(stream_stdout); } else if (disposition && strcasecmp (g_mime_content_disposition_get_disposition (disposition), GMIME_DISPOSITION_ATTACHMENT) == 0) { const char *filename = g_mime_part_get_filename (GMIME_PART (node->part)); printf ("Attachment: %s (%s)\n", filename, g_mime_content_type_to_string (content_type)); } else { printf ("Non-text part: %s\n", g_mime_content_type_to_string (content_type)); } } for (i = 0; i < node->nchildren; i++) format_part_reply (mime_node_child (node, i)); }
static GMimeStream * stream_substream (GMimeStream *stream, gint64 start, gint64 end) { GMimeStreamFile *fstream; fstream = g_object_newv (GMIME_TYPE_STREAM_FILE, 0, NULL); g_mime_stream_construct (GMIME_STREAM (fstream), start, end); fstream->owner = FALSE; fstream->fp = GMIME_STREAM_FILE (stream)->fp; return GMIME_STREAM (fstream); }
static void show_reply_headers (GMimeMessage *message) { GMimeStream *stream_stdout = NULL; stream_stdout = g_mime_stream_file_new (stdout); if (stream_stdout) { g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); /* Output RFC 2822 formatted (and RFC 2047 encoded) headers. */ g_mime_object_write_to_stream (GMIME_OBJECT(message), stream_stdout); g_object_unref(stream_stdout); } }
static void reply_part_content (GMimeObject *part) { GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part)); GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part); if (g_mime_content_type_is_type (content_type, "text", "*") && !g_mime_content_type_is_type (content_type, "text", "html")) { GMimeStream *stream_stdout = NULL, *stream_filter = NULL; GMimeDataWrapper *wrapper; const char *charset; charset = g_mime_object_get_content_type_parameter (part, "charset"); stream_stdout = g_mime_stream_file_new (stdout); if (stream_stdout) { g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); stream_filter = g_mime_stream_filter_new(stream_stdout); if (charset) { g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter), g_mime_filter_charset_new(charset, "UTF-8")); } } g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter), g_mime_filter_reply_new(TRUE)); wrapper = g_mime_part_get_content_object (GMIME_PART (part)); if (wrapper && stream_filter) g_mime_data_wrapper_write_to_stream (wrapper, stream_filter); if (stream_filter) g_object_unref(stream_filter); if (stream_stdout) g_object_unref(stream_stdout); } else { if (disposition && strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0) { const char *filename = g_mime_part_get_filename (GMIME_PART (part)); printf ("Attachment: %s (%s)\n", filename, g_mime_content_type_to_string (content_type)); } else { printf ("Non-text part: %s\n", g_mime_content_type_to_string (content_type)); } } }
static void show_reply_headers (GMimeMessage *message) { GMimeStream *stream_stdout = NULL, *stream_filter = NULL; stream_stdout = g_mime_stream_file_new (stdout); if (stream_stdout) { g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); stream_filter = g_mime_stream_filter_new(stream_stdout); if (stream_filter) { g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter), g_mime_filter_headers_new()); g_mime_object_write_to_stream(GMIME_OBJECT(message), stream_filter); g_object_unref(stream_filter); } g_object_unref(stream_stdout); } }
static void format_part_content_text (GMimeObject *part) { const char *cid = g_mime_object_get_content_id (part); GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part)); if (GMIME_IS_PART (part)) { const char *filename = g_mime_part_get_filename (GMIME_PART (part)); if (filename) printf (", Filename: %s", filename); } if (cid) printf (", Content-id: %s", cid); printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type)); if (g_mime_content_type_is_type (content_type, "text", "*") && !g_mime_content_type_is_type (content_type, "text", "html")) { GMimeStream *stream_stdout = g_mime_stream_file_new (stdout); g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); show_text_part_content (part, stream_stdout); g_object_unref(stream_stdout); } else if (g_mime_content_type_is_type (content_type, "multipart", "*") || g_mime_content_type_is_type (content_type, "message", "rfc822")) { /* Do nothing for multipart since its content will be printed * when recursing. */ } else { printf ("Non-text part: %s\n", g_mime_content_type_to_string (content_type)); } }
static GMimeMessage *gmime_message_from_file(FILE *file) { g_return_val_if_fail(file != NULL, NULL); GMimeStream *stream = g_mime_stream_file_new(file); if (!stream) { g_printerr("file stream could not be opened\r\n"); fclose(file); return NULL; } // Being owner of the stream will automatically close the file when released g_mime_stream_file_set_owner(GMIME_STREAM_FILE(stream), TRUE); GMimeMessage *message = gmime_message_from_stream(stream); g_object_unref (stream); if (!message) { g_printerr("message could not be constructed from stream\r\n"); return NULL; } return message; }
static notmuch_status_t format_part_raw (unused (const void *ctx), mime_node_t *node, unused (int indent), unused (const notmuch_show_params_t *params)) { if (node->envelope_file) { /* Special case the entire message to avoid MIME parsing. */ const char *filename; FILE *file; size_t size; char buf[4096]; filename = notmuch_message_get_filename (node->envelope_file); if (filename == NULL) { fprintf (stderr, "Error: Cannot get message filename.\n"); return NOTMUCH_STATUS_FILE_ERROR; } file = fopen (filename, "r"); if (file == NULL) { fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno)); return NOTMUCH_STATUS_FILE_ERROR; } while (!feof (file)) { size = fread (buf, 1, sizeof (buf), file); if (ferror (file)) { fprintf (stderr, "Error: Read failed from %s\n", filename); fclose (file); return NOTMUCH_STATUS_FILE_ERROR; } if (fwrite (buf, size, 1, stdout) != 1) { fprintf (stderr, "Error: Write failed\n"); fclose (file); return NOTMUCH_STATUS_FILE_ERROR; } } fclose (file); return NOTMUCH_STATUS_SUCCESS; } GMimeStream *stream_stdout; GMimeStream *stream_filter = NULL; stream_stdout = g_mime_stream_file_new (stdout); g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); stream_filter = g_mime_stream_filter_new (stream_stdout); if (GMIME_IS_PART (node->part)) { /* For leaf parts, we emit only the transfer-decoded * body. */ GMimeDataWrapper *wrapper; wrapper = g_mime_part_get_content_object (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, stream_filter); } if (stream_filter) g_object_unref (stream_filter); if (stream_stdout) g_object_unref(stream_stdout); return NOTMUCH_STATUS_SUCCESS; }
static notmuch_status_t format_part_text (const void *ctx, mime_node_t *node, int indent, const notmuch_show_params_t *params) { /* The disposition and content-type metadata are associated with * the envelope for message parts */ GMimeObject *meta = node->envelope_part ? GMIME_OBJECT (node->envelope_part) : node->part; GMimeContentType *content_type = g_mime_object_get_content_type (meta); const notmuch_bool_t leaf = GMIME_IS_PART (node->part); const char *part_type; int i; if (node->envelope_file) { notmuch_message_t *message = node->envelope_file; part_type = "message"; printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n", part_type, notmuch_message_get_message_id (message), indent, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0, notmuch_message_get_filename (message)); } else { GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta); const char *cid = g_mime_object_get_content_id (meta); const char *filename = leaf ? g_mime_part_get_filename (GMIME_PART (node->part)) : NULL; if (disposition && strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0) part_type = "attachment"; else part_type = "part"; printf ("\f%s{ ID: %d", part_type, node->part_num); if (filename) printf (", Filename: %s", filename); if (cid) printf (", Content-id: %s", cid); printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type)); } if (GMIME_IS_MESSAGE (node->part)) { GMimeMessage *message = GMIME_MESSAGE (node->part); InternetAddressList *recipients; const char *recipients_string; printf ("\fheader{\n"); if (node->envelope_file) printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file)); printf ("Subject: %s\n", g_mime_message_get_subject (message)); printf ("From: %s\n", g_mime_message_get_sender (message)); recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO); recipients_string = internet_address_list_to_string (recipients, 0); if (recipients_string) printf ("To: %s\n", recipients_string); recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC); recipients_string = internet_address_list_to_string (recipients, 0); if (recipients_string) printf ("Cc: %s\n", recipients_string); printf ("Date: %s\n", g_mime_message_get_date_as_string (message)); printf ("\fheader}\n"); printf ("\fbody{\n"); } if (leaf) { if (g_mime_content_type_is_type (content_type, "text", "*") && !g_mime_content_type_is_type (content_type, "text", "html")) { GMimeStream *stream_stdout = g_mime_stream_file_new (stdout); g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); show_text_part_content (node->part, stream_stdout, 0); g_object_unref(stream_stdout); } else { printf ("Non-text part: %s\n", g_mime_content_type_to_string (content_type)); } } for (i = 0; i < node->nchildren; i++) format_part_text (ctx, mime_node_child (node, i), indent, params); if (GMIME_IS_MESSAGE (node->part)) printf ("\fbody}\n"); printf ("\f%s}\n", part_type); return NOTMUCH_STATUS_SUCCESS; }