diff options
Diffstat (limited to 'src/attachmentHelper.cpp')
-rw-r--r-- | src/attachmentHelper.cpp | 118 |
1 files changed, 95 insertions, 23 deletions
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 <const attachment> if (!isBodyPartAnAttachment(part)) return NULL; - return vmime::create <bodyPartAttachment>(part); + mediaType type; + + try + { + const contentTypeField& ctf = dynamic_cast<contentTypeField&> + (*part->getHeader()->findField(fields::CONTENT_TYPE)); + + type = *ctf.getValue().dynamicCast <const mediaType>(); + } + 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 <generatedMessageAttachment>(part); + } + else + { + return vmime::create <bodyPartAttachment>(part); + } } @@ -144,36 +172,72 @@ void attachmentHelper::addAttachment(ref <message> msg, ref <attachment> att) if (part == NULL) // create it { - // Create a new container part for the parts that were in - // the root part of the message - ref <bodyPart> container = vmime::create <bodyPart>(); - - 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 <bodyPart> container = vmime::create <bodyPart>(); + + 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 <ref <bodyPart> > 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 <bodyPart> child = vmime::create <bodyPart>(); + + 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 <emptyContentHandler>()); + + 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 <ref <bodyPart> > 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 <bodyPart> attachmentHelper::findBodyPart } +// static +void attachmentHelper::addAttachment(ref <message> msg, ref <message> amsg) +{ + ref <attachment> att = vmime::create <parsedMessageAttachment>(amsg); + addAttachment(msg, att); +} + + } // vmime |