From 8e0080b0ed1ca804cdb8a6e2846567231022f18d Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Mon, 16 Jan 2006 21:55:37 +0000 Subject: Added support for attachments of type 'message/rfc822'. --- src/attachmentHelper.cpp | 118 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 95 insertions(+), 23 deletions(-) (limited to 'src/attachmentHelper.cpp') diff --git a/src/attachmentHelper.cpp b/src/attachmentHelper.cpp index a4970dd0..c1a1f057 100644 --- a/src/attachmentHelper.cpp +++ b/src/attachmentHelper.cpp @@ -24,7 +24,11 @@ #include "vmime/attachmentHelper.hpp" #include "vmime/bodyPartAttachment.hpp" +#include "vmime/parsedMessageAttachment.hpp" +#include "vmime/generatedMessageAttachment.hpp" + #include "vmime/disposition.hpp" +#include "vmime/emptyContentHandler.hpp" namespace vmime @@ -88,7 +92,31 @@ ref if (!isBodyPartAnAttachment(part)) return NULL; - return vmime::create (part); + mediaType type; + + try + { + const contentTypeField& ctf = dynamic_cast + (*part->getHeader()->findField(fields::CONTENT_TYPE)); + + type = *ctf.getValue().dynamicCast (); + } + catch (exceptions::no_such_field&) + { + // No "Content-type" field: assume "application/octet-stream". + type = mediaType(mediaTypes::APPLICATION, + mediaTypes::APPLICATION_OCTET_STREAM); + } + + if (type.getType() == mediaTypes::MESSAGE && + type.getSubType() == mediaTypes::MESSAGE_RFC822) + { + return vmime::create (part); + } + else + { + return vmime::create (part); + } } @@ -144,36 +172,72 @@ void attachmentHelper::addAttachment(ref msg, ref att) if (part == NULL) // create it { - // Create a new container part for the parts that were in - // the root part of the message - ref container = vmime::create (); - - try + if (msg->getBody()->getPartCount() != 0) { - container->getHeader()->ContentType()-> - setValue(msg->getHeader()->ContentType()->getValue()); + // Create a new container part for the parts that were in + // the root part of the message + ref container = vmime::create (); + + try + { + if (msg->getHeader()->hasField(fields::CONTENT_TYPE)) + { + container->getHeader()->ContentType()->setValue + (msg->getHeader()->ContentType()->getValue()); + } + + if (msg->getHeader()->hasField(fields::CONTENT_TRANSFER_ENCODING)) + { + container->getHeader()->ContentTransferEncoding()->setValue + (msg->getHeader()->ContentTransferEncoding()->getValue()); + } + } + catch (exceptions::no_such_field&) + { + // Ignore + } + + // Move parts from the root part to this new part + const std::vector > partList = + msg->getBody()->getPartList(); + + msg->getBody()->removeAllParts(); + + for (unsigned int i = 0 ; i < partList.size() ; ++i) + container->getBody()->appendPart(partList[i]); + + msg->getBody()->appendPart(container); } - catch (exceptions::no_such_field&) + else { - // Ignore + // The message is a simple (RFC-822) message, and do not + // contains any MIME part. Move the contents from the + // root to a new child part. + ref child = vmime::create (); + + if (msg->getHeader()->hasField(fields::CONTENT_TYPE)) + { + child->getHeader()->ContentType()->setValue + (msg->getHeader()->ContentType()->getValue()); + } + + if (msg->getHeader()->hasField(fields::CONTENT_TRANSFER_ENCODING)) + { + child->getHeader()->ContentTransferEncoding()->setValue + (msg->getHeader()->ContentTransferEncoding()->getValue()); + } + + child->getBody()->setContents(msg->getBody()->getContents()); + msg->getBody()->setContents(vmime::create ()); + + msg->getBody()->appendPart(child); } - msg->getHeader()->removeAllFields(vmime::fields::CONTENT_DISPOSITION); - msg->getHeader()->removeAllFields(vmime::fields::CONTENT_TRANSFER_ENCODING); - - // Move parts from the root part to this new part - const std::vector > partList = - msg->getBody()->getPartList(); - - msg->getBody()->removeAllParts(); - - for (unsigned int i = 0 ; i < partList.size() ; ++i) - container->getBody()->appendPart(partList[i]); - // Set the root part to 'multipart/mixed' msg->getHeader()->ContentType()->setValue(mpMixed); - msg->getBody()->appendPart(container); + msg->getHeader()->removeAllFields(vmime::fields::CONTENT_DISPOSITION); + msg->getHeader()->removeAllFields(vmime::fields::CONTENT_TRANSFER_ENCODING); part = msg; } @@ -206,5 +270,13 @@ ref attachmentHelper::findBodyPart } +// static +void attachmentHelper::addAttachment(ref msg, ref amsg) +{ + ref att = vmime::create (amsg); + addAttachment(msg, att); +} + + } // vmime -- cgit v1.2.3