static GMimeStream* get_mime_stream (GMimeObject *obj, GError **err) { GMimeStream *mstream; GMimeDataWrapper *wrapper; if (!GMIME_IS_PART(obj)) { char *str; GByteArray *ar; str = g_mime_object_to_string (obj); ar = g_byte_array_new_take ((guchar*)str, strlen(str)); mstream = g_mime_stream_mem_new_with_byte_array (ar); g_mime_stream_mem_set_owner ( GMIME_STREAM_MEM(mstream), TRUE); return mstream; } wrapper = get_data_wrapper (obj, err); if (!wrapper) return NULL; mstream = g_mime_data_wrapper_get_stream (wrapper); if (!mstream || g_mime_stream_reset (mstream) != 0) { g_set_error (err, G_IO_ERROR, G_IO_ERROR_FAILED, "invalid mime-stream"); return NULL; } return mstream; }
int main(int argc, char **argv) { GByteArray *garray; GMimeStream *stream; GMimeParser *parse; GMimeMessage *message; char buf[1]; int len; g_mime_init(0); garray = g_byte_array_new(); while (!feof(stdin)) { len = fread(buf, 1, 1, stdin); if (len == 1) g_byte_array_append(garray, buf, len); } stream = g_mime_stream_mem_new_with_byte_array(garray); parse = g_mime_parser_new_with_stream(stream); message = g_mime_parser_construct_message(parse); remove_message(message); printf(g_mime_object_to_string((GMimeObject*)message)); g_object_unref(message); g_object_unref(parse); g_object_unref(stream); return 0; }
/* This format is currently tuned for a git send-email --notmuch hook */ static int notmuch_reply_format_headers_only(void *ctx, notmuch_config_t *config, notmuch_query_t *query, unused (notmuch_show_params_t *params), notmuch_bool_t reply_all, unused (sprinter_t *sp)) { GMimeMessage *reply; notmuch_messages_t *messages; notmuch_message_t *message; const char *in_reply_to, *orig_references, *references; char *reply_headers; for (messages = notmuch_query_search_messages (query); notmuch_messages_valid (messages); notmuch_messages_move_to_next (messages)) { message = notmuch_messages_get (messages); /* The 0 means we do not want headers in a "pretty" order. */ reply = g_mime_message_new (0); if (reply == NULL) { fprintf (stderr, "Out of memory\n"); return 1; } in_reply_to = talloc_asprintf (ctx, "<%s>", notmuch_message_get_message_id (message)); g_mime_object_set_header (GMIME_OBJECT (reply), "In-Reply-To", in_reply_to); orig_references = notmuch_message_get_header (message, "references"); /* We print In-Reply-To followed by References because git format-patch treats them * specially. Git does not interpret the other headers specially */ references = talloc_asprintf (ctx, "%s%s%s", orig_references ? orig_references : "", orig_references ? " " : "", in_reply_to); g_mime_object_set_header (GMIME_OBJECT (reply), "References", references); (void)add_recipients_from_message (reply, config, message, reply_all); reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply)); printf ("%s", reply_headers); free (reply_headers); g_object_unref (G_OBJECT (reply)); reply = NULL; notmuch_message_destroy (message); } return 0; }
static gboolean write_object_to_fd (GMimeObject *obj, int fd, GError **err) { gchar *str; str = g_mime_object_to_string (obj); if (!str) { g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_GMIME, "could not get string from object"); return FALSE; } if (write (fd, str, strlen(str)) == -1) { g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_GMIME, "failed to write object: %s", strerror(errno)); return FALSE; } return TRUE; }
END_TEST START_TEST(test_imap_get_partspec) { DbmailMessage *message; GMimeObject *object; char *result, *expect; /* text/plain */ message = dbmail_message_new(NULL); message = dbmail_message_init_with_string(message, rfc822); object = imap_get_partspec(GMIME_OBJECT(message->content),"HEADER"); result = g_mime_object_to_string(object); fail_unless(MATCH(rfc822,result), "imap_get_partsec failed \n[%s] !=\n[%s]\n", rfc822, result); g_free(result); result = imap_get_logical_part(object,"HEADER"); expect = g_strdup("From nobody Wed Sep 14 16:47:48 2005\r\n" "Content-Type: text/plain; charset=\"us-ascii\"\r\n" "MIME-Version: 1.0\r\n" "Content-Transfer-Encoding: 7bit\r\n" "Message-Id: <1199706209l.3020l.1l@(none)>\r\n" "To: [email protected]\r\n" "From: [email protected]\r\n" "Subject: dbmail test message\r\n" "\r\n"); fail_unless(MATCH(expect,result),"imap_get_partsec failed \n[%s] !=\n[%s]\n", expect, result); g_free(expect); g_free(result); object = imap_get_partspec(GMIME_OBJECT(message->content),"TEXT"); result = imap_get_logical_part(object,"TEXT"); expect = g_strdup("\r\n" " this is a test message\r\n" "\r\n"); fail_unless(MATCH(expect,result),"imap_get_partsec failed \n[%s] !=\n[%s]\n", expect, result); g_free(expect); g_free(result); dbmail_message_free(message); /* multipart */ message = dbmail_message_new(NULL); message = dbmail_message_init_with_string(message, multipart_message); /* test a simple mime part */ object = imap_get_partspec(GMIME_OBJECT(message->content),"1"); result = imap_get_logical_part(object,"MIME"); expect = g_strdup("Content-type: text/html\r\n" "Content-disposition: inline\r\n\r\n"); fail_unless(MATCH(expect,result), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); result = imap_get_logical_part(object,NULL); expect = g_strdup("Test message one\r\n and more.\r\n"); fail_unless(MATCH(expect,result),"imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); /* object isn't a message/rfc822 so these are * acually invalid. Let's try anyway */ result = imap_get_logical_part(object,"HEADER"); expect = g_strdup("Content-type: text/html\r\n" "Content-disposition: inline\r\n\r\n"); fail_unless(MATCH(expect,result), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); result = imap_get_logical_part(object,"TEXT"); expect = g_strdup("Test message one\r\n and more.\r\n"); fail_unless(MATCH(expect,result), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); /* moving on */ object = imap_get_partspec(GMIME_OBJECT(message->content),"2"); result = imap_get_logical_part(object,"MIME"); expect = g_strdup( "Content-type: text/plain; charset=us-ascii; name=testfile\r\n" "Content-transfer-encoding: base64\r\n" "\r\n" ); fail_unless(MATCH(expect,result), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); dbmail_message_free(message); /* multipart mixed */ message = dbmail_message_new(NULL); message = dbmail_message_init_with_string(message, multipart_mixed); object = imap_get_partspec(GMIME_OBJECT(message->content),"2"); result = imap_get_logical_part(object,"HEADER"); expect = g_strdup("From: \"try\" <*****@*****.**>"); fail_unless((strncmp(expect,result,29)==0), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(expect); g_free(result); object = imap_get_partspec(GMIME_OBJECT(message->content),"2.1.1"); result = imap_get_logical_part(object,NULL); expect = g_strdup("Body of doc2\r\n\r\n"); fail_unless(MATCH(expect,result), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); result = imap_get_logical_part(object,"MIME"); expect = g_strdup("Content-Type: text/plain;\r\n" " charset=\"us-ascii\"\r\n" "Content-Transfer-Encoding: 7bit\r\n\r\n"); fail_unless(MATCH(expect,result), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); dbmail_message_free(message); /* multipart signed */ message = dbmail_message_new(NULL); message = dbmail_message_init_with_string(message, multipart_signed); object = imap_get_partspec(GMIME_OBJECT(message->content),"1.1"); result = imap_get_logical_part(object,NULL); expect = g_strdup("quo-pri text"); fail_unless(MATCH(expect,result),"imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); object = imap_get_partspec(GMIME_OBJECT(message->content),"1.3"); result = g_mime_object_to_string(object); expect = g_strdup("Content-Type: message/rfc822;\n" "Content-Transfer-Encoding: 7bit\n" "Content-Disposition: attachment;\n" " filename=\"msg1.eml\"\n" "\n" "Date: Mon, 19 Aug 2013 14:54:05 +0200\n" "To: a@b\n" "From: d@b\n" "Reply-To: e@b\n" "Subject: msg1\n" "MIME-Version: 1.0\n" "Content-Type: multipart/alternative;\n" " boundary=b1_7ad0d7cccab59d27194f9ad69c14606001f05f531376916845\n" "\n" "\n" "--b1_7ad0d7cccab59d27194f9ad69c14606001f05f531376916845\n" "Content-Type: text/plain; charset=\"ISO-8859-1\"\n" "Content-Transfer-Encoding: quoted-printable\n" "\n" "quo-pri text\n" "--b1_7ad0d7cccab59d27194f9ad69c14606001f05f531376916845\n" "Content-Type: text/html; charset=\"ISO-8859-1\"\n" "Content-Transfer-Encoding: quoted-printable\n" "\n" "html text\n" "\n" "--b1_7ad0d7cccab59d27194f9ad69c14606001f05f531376916845--\n" "\n" "\n" "\n"); fail_unless(MATCH(expect,result), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); result = imap_get_logical_part(object,"MIME"); expect = g_strdup("Content-Type: message/rfc822;\r\n" "Content-Transfer-Encoding: 7bit\r\n" "Content-Disposition: attachment;\r\n" " filename=\"msg1.eml\"\r\n" "\r\n"); fail_unless(MATCH(expect,result), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); result = imap_get_logical_part(object,NULL); expect = g_strdup("Date: Mon, 19 Aug 2013 14:54:05 +0200\r\n" "To: a@b\r\n" "From: d@b\r\n" "Reply-To: e@b\r\n" "Subject: msg1\r\n" "MIME-Version: 1.0\r\n" "Content-Type: multipart/alternative;\r\n" " boundary=b1_7ad0d7cccab59d27194f9ad69c14606001f05f531376916845\r\n" "\r\n" "\r\n" "--b1_7ad0d7cccab59d27194f9ad69c14606001f05f531376916845\r\n" "Content-Type: text/plain; charset=\"ISO-8859-1\"\r\n" "Content-Transfer-Encoding: quoted-printable\r\n" "\r\n" "quo-pri text\r\n" "--b1_7ad0d7cccab59d27194f9ad69c14606001f05f531376916845\r\n" "Content-Type: text/html; charset=\"ISO-8859-1\"\r\n" "Content-Transfer-Encoding: quoted-printable\r\n" "\r\n" "html text\r\n" "\r\n" "--b1_7ad0d7cccab59d27194f9ad69c14606001f05f531376916845--\r\n" "\r\n" "\r\n" "\r\n"); fail_unless(MATCH(expect,result), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); result = imap_get_logical_part(object,"MIME"); expect = g_strdup("Content-Type: message/rfc822;\r\n" "Content-Transfer-Encoding: 7bit\r\n" "Content-Disposition: attachment;\r\n" " filename=\"msg1.eml\"\r\n" "\r\n"); fail_unless(MATCH(expect,result), "imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); result = imap_get_logical_part(object,"HEADER"); expect = g_strdup("Date: Mon, 19 Aug 2013 14:54:05 +0200\r\n" "To: a@b\r\n" "From: d@b\r\n" "Reply-To: e@b\r\n" "Subject: msg1\r\n" "MIME-Version: 1.0\r\n" "Content-Type: multipart/alternative;\r\n" "\tboundary=b1_7ad0d7cccab59d27194f9ad69c14606001f05f531376916845\r\n" "\r\n"); fail_unless(MATCH(expect,result),"imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); dbmail_message_free(message); }
void GetMessageBody(GMimeMessage* pMessage) { GMimeObject *ps =g_mime_message_get_body(pMessage); printf("\n\n\nbody:%s\n", g_mime_object_to_string(ps)); }