void extract_addresses(InternetAddressList *address_list, GMimeStream *out_stream) { int i; for (i=0; i<internet_address_list_length(address_list); i++) { if (i > 0) { g_mime_stream_printf (out_stream, ", "); } InternetAddress *address; char *address_string; address = internet_address_list_get_address(address_list, i); address_string = internet_address_to_string(address, TRUE); address_string = str_replace(address_string, "\"", "\\\""); g_mime_stream_printf (out_stream, "\"%s\"", address_string); } }
int main (int argc, char *argv[]) { gboolean rv; GMimeStream *out_stream; out_stream = g_mime_stream_file_new (stdout); if (argc < 2) { g_printerr ("usage: %s <msg-files>\n", argv[0]); return 1; } setlocale (LC_ALL, ""); g_mime_init(GMIME_ENABLE_RFC2047_WORKAROUNDS); if (argc == 2) { g_mime_stream_printf (out_stream, "%%{"); g_mime_stream_printf (out_stream, "path: \"%s\", ", argv[1]); rv = test_file (argv[1], out_stream); g_mime_stream_printf (out_stream, "}"); } else { g_mime_stream_printf (out_stream, "["); int x; for (x = 1; x < argc; x++ ) { /* printf("File: %s\n", argv[x]); */ if (x > 1) { g_mime_stream_printf (out_stream, ", "); } g_mime_stream_printf (out_stream, "%%{"); g_mime_stream_printf (out_stream, "path: \"%s\", ", argv[x]); rv = test_file (argv[x], out_stream); g_mime_stream_printf (out_stream, "}"); } g_mime_stream_printf (out_stream, "]"); } g_mime_shutdown (); /* flush stdout */ g_mime_stream_flush (out_stream); /* free/close the stream */ g_object_unref (out_stream); return rv ? 0 : 1; }
static void print_mime_struct (GMimeStream *stream, GMimeObject *part, int depth) { GMimeMultipart *multipart; GMimeMessagePart *mpart; GMimeContentType *type; GMimeObject *subpart; GMimeObject *body; GMimeMessage *msg; int i, n; print_depth (stream, depth); type = g_mime_object_get_content_type (part); g_mime_stream_printf (stream, "Content-Type: %s/%s\n", g_mime_content_type_get_media_type (type), g_mime_content_type_get_media_subtype (type)); if (GMIME_IS_MULTIPART (part)) { multipart = (GMimeMultipart *) part; n = g_mime_multipart_get_count (multipart); for (i = 0; i < n; i++) { subpart = g_mime_multipart_get_part (multipart, i); print_mime_struct (stream, subpart, depth + 1); } } else if (GMIME_IS_MESSAGE_PART (part)) { mpart = (GMimeMessagePart *) part; msg = g_mime_message_part_get_message (mpart); if (msg != NULL) { body = g_mime_message_get_mime_part (msg); print_mime_struct (stream, body, depth + 1); } } }
static ssize_t multipart_write_to_stream (GMimeObject *object, GMimeStream *stream) { GMimeMultipart *multipart = (GMimeMultipart *) object; ssize_t nwritten, total = 0; const char *boundary; GMimeObject *part; guint i; /* make sure a boundary is set unless we are writing out a raw * header (in which case it should already be set... or if * not, then it's a broken multipart and so we don't want to * alter it or we'll completely break the output) */ boundary = g_mime_object_get_content_type_parameter (object, "boundary"); if (!boundary && !g_mime_header_list_get_stream (object->headers)) { g_mime_multipart_set_boundary (multipart, NULL); boundary = g_mime_object_get_content_type_parameter (object, "boundary"); } /* write the content headers */ if ((nwritten = g_mime_header_list_write_to_stream (object->headers, stream)) == -1) return -1; total += nwritten; /* write the preface */ if (multipart->preface) { /* terminate the headers */ if (g_mime_stream_write (stream, "\n", 1) == -1) return -1; total++; if ((nwritten = g_mime_stream_write_string (stream, multipart->preface)) == -1) return -1; total += nwritten; } for (i = 0; i < multipart->children->len; i++) { part = multipart->children->pdata[i]; /* write the boundary */ if ((nwritten = g_mime_stream_printf (stream, "\n--%s\n", boundary)) == -1) return -1; total += nwritten; /* write this part out */ if ((nwritten = g_mime_object_write_to_stream (part, stream)) == -1) return -1; total += nwritten; } /* write the end-boundary (but only if a boundary is set) */ if (boundary) { if ((nwritten = g_mime_stream_printf (stream, "\n--%s--\n", boundary)) == -1) return -1; total += nwritten; } /* write the postface */ if (multipart->postface) { if ((nwritten = g_mime_stream_write_string (stream, multipart->postface)) == -1) return -1; total += nwritten; } return total; }
static ssize_t write_content (GMimePart *part, GMimeStream *stream) { ssize_t nwritten, total = 0; if (!part->content) return 0; /* Evil Genius's "slight" optimization: Since GMimeDataWrapper::write_to_stream() * decodes its content stream to the raw format, we can cheat by requesting its * content stream and not doing any encoding on the data if the source and * destination encodings are identical. */ if (part->encoding != g_mime_data_wrapper_get_encoding (part->content)) { GMimeStream *filtered_stream; const char *filename; GMimeFilter *filter; switch (part->encoding) { case GMIME_CONTENT_ENCODING_UUENCODE: filename = g_mime_part_get_filename (part); nwritten = g_mime_stream_printf (stream, "begin 0644 %s\n", filename ? filename : "unknown"); if (nwritten == -1) return -1; total += nwritten; /* fall thru... */ case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE: case GMIME_CONTENT_ENCODING_BASE64: filtered_stream = g_mime_stream_filter_new (stream); filter = g_mime_filter_basic_new (part->encoding, TRUE); g_mime_stream_filter_add (GMIME_STREAM_FILTER (filtered_stream), filter); g_object_unref (filter); break; default: filtered_stream = stream; g_object_ref (stream); break; } nwritten = g_mime_data_wrapper_write_to_stream (part->content, filtered_stream); g_mime_stream_flush (filtered_stream); g_object_unref (filtered_stream); if (nwritten == -1) return -1; total += nwritten; if (part->encoding == GMIME_CONTENT_ENCODING_UUENCODE) { /* FIXME: get rid of this special-case x-uuencode crap */ nwritten = g_mime_stream_write (stream, "end\n", 4); if (nwritten == -1) return -1; total += nwritten; } } else { GMimeStream *content_stream; content_stream = g_mime_data_wrapper_get_stream (part->content); g_mime_stream_reset (content_stream); nwritten = g_mime_stream_write_to_stream (content_stream, stream); g_mime_stream_reset (content_stream); if (nwritten == -1) return -1; total += nwritten; } return total; }
static void test_parser (GMimeParser *parser, GMimeStream *mbox, GMimeStream *summary) { gint64 message_begin, message_end, headers_begin, headers_end; GMimeFormatOptions *format = g_mime_format_options_get_default (); InternetAddressList *list; GMimeMessage *message; char *marker, *buf; const char *subject; GMimeObject *body; GDateTime *date; int nmsg = 0; while (!g_mime_parser_eos (parser)) { message_begin = g_mime_parser_tell (parser); if (!(message = g_mime_parser_construct_message (parser, NULL))) throw (exception_new ("failed to parse message #%d", nmsg)); message_end = g_mime_parser_tell (parser); headers_begin = g_mime_parser_get_headers_begin (parser); headers_end = g_mime_parser_get_headers_end (parser); g_mime_stream_printf (summary, "message offsets: %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT "\n", message_begin, message_end); g_mime_stream_printf (summary, "header offsets: %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT "\n", headers_begin, headers_end); marker = g_mime_parser_get_mbox_marker (parser); g_mime_stream_printf (summary, "%s\n", marker); if ((list = g_mime_message_get_from (message)) != NULL && internet_address_list_length (list) > 0) { buf = internet_address_list_to_string (list, format, FALSE); g_mime_stream_printf (summary, "From: %s\n", buf); g_free (buf); } if ((list = g_mime_message_get_addresses (message, GMIME_ADDRESS_TYPE_TO)) != NULL && internet_address_list_length (list) > 0) { buf = internet_address_list_to_string (list, format, FALSE); g_mime_stream_printf (summary, "To: %s\n", buf); g_free (buf); } if (!(subject = g_mime_message_get_subject (message))) subject = ""; g_mime_stream_printf (summary, "Subject: %s\n", subject); if (!(date = g_mime_message_get_date (message))) { date = g_date_time_new_from_unix_utc (0); } else { g_date_time_ref (date); } buf = g_mime_utils_header_format_date (date); g_mime_stream_printf (summary, "Date: %s\n", buf); g_date_time_unref (date); g_free (buf); body = g_mime_message_get_mime_part (message); print_mime_struct (summary, body, 0); g_mime_stream_write (summary, "\n", 1); if (mbox) { if (nmsg > 0) g_mime_stream_write (mbox, "\n", 1); g_mime_stream_printf (mbox, "%s\n", marker); g_mime_object_write_to_stream ((GMimeObject *) message, format, mbox); } g_object_unref (message); g_free (marker); nmsg++; } }
void extract_headers (GMimeMessage *message, GMimeStream *out_stream) { const char *subject; char *escaped_subject; const char *sender; char *escaped_sender; const char *reply_to; char *escaped_reply_to; time_t t; int tz; char buf[64]; struct tm *t_m; subject = g_mime_message_get_subject(message); escaped_subject = str_replace((char *) subject, "\"", ""); g_mime_stream_printf (out_stream, "subject: \"%s\", ", escaped_subject); g_mime_stream_printf (out_stream, "message_id: \"%s\", ", g_mime_message_get_message_id(message)); sender = g_mime_message_get_sender(message); escaped_sender = str_replace((char *) sender, "\"", ""); g_mime_stream_printf (out_stream, "sender: \"%s\", ", escaped_sender); reply_to = g_mime_message_get_reply_to(message); escaped_reply_to = str_replace((char *) reply_to, "\"", ""); g_mime_stream_printf (out_stream, "reply_to: \"%s\", ", escaped_reply_to); g_mime_message_get_date (message, &t, &tz); t_m = localtime (&t); strftime(buf, sizeof(buf) - 1, "%c", t_m); g_mime_stream_printf (out_stream, "date: \"%s\", ", buf); strftime(buf, sizeof(buf) - 1, "%Y%m%d%H%M%S", t_m); g_mime_stream_printf (out_stream, "sort_date: \"%s\", ", buf); InternetAddressList *recipients; recipients = g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_TO); g_mime_stream_printf (out_stream, "recipients_to: ["); extract_addresses(recipients, out_stream); g_mime_stream_printf (out_stream, "], "); recipients = g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_CC); g_mime_stream_printf (out_stream, "recipients_cc: ["); extract_addresses(recipients, out_stream); g_mime_stream_printf (out_stream, "], "); recipients = g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_BCC); g_mime_stream_printf (out_stream, "recipients_bcc: ["); extract_addresses(recipients, out_stream); g_mime_stream_printf (out_stream, "] "); }