2004-10-05 10:28:21 +00:00
|
|
|
//
|
2005-03-18 21:33:11 +00:00
|
|
|
// VMime library (http://www.vmime.org)
|
2009-09-06 12:02:10 +00:00
|
|
|
// Copyright (C) 2002-2009 Vincent Richard <vincent@vincent-richard.net>
|
2004-10-05 10:28:21 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
2009-09-06 12:02:10 +00:00
|
|
|
// published by the Free Software Foundation; either version 3 of
|
2004-10-05 10:28:21 +00:00
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
2005-09-17 10:10:29 +00:00
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// Linking this library statically or dynamically with other modules is making
|
|
|
|
// a combined work based on this library. Thus, the terms and conditions of
|
|
|
|
// the GNU General Public License cover the whole combination.
|
2004-10-05 10:28:21 +00:00
|
|
|
//
|
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/bodyPart.hpp"
|
|
|
|
#include "vmime/body.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/options.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/contentTypeField.hpp"
|
2005-11-04 23:21:22 +00:00
|
|
|
#include "vmime/text.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/utility/random.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/parserHelpers.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-01-28 17:50:53 +00:00
|
|
|
#include "vmime/emptyContentHandler.hpp"
|
|
|
|
#include "vmime/stringContentHandler.hpp"
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
namespace vmime
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
body::body()
|
2005-07-12 22:28:02 +00:00
|
|
|
: m_contents(create <emptyContentHandler>()), m_part(NULL), m_header(NULL)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
body::~body()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
void body::parse(const string& buffer, const string::size_type position,
|
|
|
|
const string::size_type end, string::size_type* newPosition)
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
removeAllParts();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
// Check whether the body is a MIME-multipart
|
|
|
|
bool isMultipart = false;
|
|
|
|
string boundary;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
const ref <const contentTypeField> ctf =
|
2006-03-29 20:06:39 +00:00
|
|
|
m_header.acquire()->findField(fields::CONTENT_TYPE).dynamicCast <contentTypeField>();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-11-04 23:21:22 +00:00
|
|
|
const mediaType type = *ctf->getValue().dynamicCast <const mediaType>();
|
|
|
|
|
|
|
|
if (type.getType() == mediaTypes::MULTIPART)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
isMultipart = true;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
boundary = ctf->getBoundary();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::no_such_parameter&)
|
|
|
|
{
|
|
|
|
// No "boundary" parameter specified: we can try to
|
|
|
|
// guess it by scanning the body contents...
|
|
|
|
string::size_type pos = buffer.find("\n--", position);
|
|
|
|
|
|
|
|
if ((pos != string::npos) && (pos < end))
|
|
|
|
{
|
|
|
|
pos += 3;
|
|
|
|
|
|
|
|
const string::size_type start = pos;
|
|
|
|
|
|
|
|
char_t c = buffer[pos];
|
|
|
|
string::size_type length = 0;
|
|
|
|
|
|
|
|
// We have to stop after a reasonnably long boundary length (100)
|
|
|
|
// not to take the whole body contents for a boundary...
|
|
|
|
while (pos < end && length < 100 && !(c == '\r' || c == '\n'))
|
|
|
|
{
|
|
|
|
++length;
|
|
|
|
c = buffer[pos++];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos < end && length < 100)
|
|
|
|
{
|
|
|
|
// RFC #1521, Page 31:
|
|
|
|
// "...the boundary parameter, which consists of 1 to 70
|
|
|
|
// characters from a set of characters known to be very
|
|
|
|
// robust through email gateways, and NOT ending with
|
|
|
|
// white space..."
|
2005-03-16 17:13:08 +00:00
|
|
|
while (pos != start && parserHelpers::isSpace(buffer[pos - 1]))
|
2004-10-05 10:28:21 +00:00
|
|
|
--pos;
|
|
|
|
|
|
|
|
boundary = string(buffer.begin() + start,
|
|
|
|
buffer.begin() + pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field&)
|
|
|
|
{
|
|
|
|
// No "Content-Type" field...
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a multi-part body
|
|
|
|
if (isMultipart && !boundary.empty())
|
|
|
|
{
|
|
|
|
const string boundarySep("--" + boundary);
|
|
|
|
|
|
|
|
string::size_type partStart = position;
|
|
|
|
string::size_type pos = buffer.find(boundarySep, position);
|
|
|
|
|
|
|
|
bool lastPart = false;
|
|
|
|
|
|
|
|
if (pos != string::npos && pos < end)
|
|
|
|
{
|
|
|
|
m_prologText = string(buffer.begin() + position, buffer.begin() + pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0 ; !lastPart && (pos != string::npos) && (pos < end) ; ++index)
|
|
|
|
{
|
|
|
|
string::size_type partEnd = pos;
|
|
|
|
|
|
|
|
// Get rid of the [CR]LF just before the boundary string
|
2008-07-11 21:46:32 +00:00
|
|
|
if (pos >= (position + 1) && buffer[pos - 1] == '\n') --partEnd;
|
|
|
|
if (pos >= (position + 2) && buffer[pos - 2] == '\r') --partEnd;
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
// Check whether it is the last part (boundary terminated by "--")
|
|
|
|
pos += boundarySep.length();
|
|
|
|
|
|
|
|
if (pos + 1 < end && buffer[pos] == '-' && buffer[pos + 1] == '-')
|
|
|
|
{
|
|
|
|
lastPart = true;
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC #1521, Page 31:
|
|
|
|
// "...(If a boundary appears to end with white space, the
|
|
|
|
// white space must be presumed to have been added by a
|
|
|
|
// gateway, and must be deleted.)..."
|
|
|
|
while (pos < end && (buffer[pos] == ' ' || buffer[pos] == '\t'))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
// End of boundary line
|
|
|
|
if (pos + 1 < end && buffer[pos] == '\r' && buffer[pos + 1] =='\n')
|
|
|
|
{
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
else if (pos < end && buffer[pos] == '\n')
|
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index > 0)
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <bodyPart> part = vmime::create <bodyPart>();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
part->parse(buffer, partStart, partEnd, NULL);
|
2004-10-21 15:05:47 +00:00
|
|
|
part->m_parent = m_part;
|
|
|
|
|
|
|
|
m_parts.push_back(part);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
partStart = pos;
|
|
|
|
pos = buffer.find(boundarySep, partStart);
|
|
|
|
}
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
m_contents = vmime::create <emptyContentHandler>();
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2008-07-11 20:45:17 +00:00
|
|
|
// Last part was not found: recover from missing boundary
|
|
|
|
if (!lastPart && pos == string::npos)
|
|
|
|
{
|
|
|
|
ref <bodyPart> part = vmime::create <bodyPart>();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
part->parse(buffer, partStart, end);
|
|
|
|
}
|
|
|
|
catch (std::exception&)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
part->m_parent = m_part;
|
|
|
|
|
|
|
|
m_parts.push_back(part);
|
|
|
|
}
|
|
|
|
// Treat remaining text as epilog
|
|
|
|
else if (partStart < end)
|
|
|
|
{
|
2004-10-05 10:28:21 +00:00
|
|
|
m_epilogText = string(buffer.begin() + partStart, buffer.begin() + end);
|
2008-07-11 20:45:17 +00:00
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
// Treat the contents as 'simple' data
|
|
|
|
else
|
|
|
|
{
|
2010-05-23 16:18:00 +00:00
|
|
|
encoding enc;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const ref <const headerField> cef =
|
|
|
|
m_header.acquire()->findField(fields::CONTENT_TRANSFER_ENCODING);
|
|
|
|
|
|
|
|
enc = *cef->getValue().dynamicCast <const encoding>();
|
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field&)
|
|
|
|
{
|
|
|
|
// Defaults to "7bit" (RFC-1521)
|
|
|
|
enc = vmime::encoding(encodingTypes::SEVEN_BIT);
|
|
|
|
|
|
|
|
// Set header field
|
|
|
|
m_header.acquire()->ContentTransferEncoding()->setValue(enc);
|
|
|
|
}
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
// Extract the (encoded) contents
|
2010-05-23 16:18:00 +00:00
|
|
|
m_contents = vmime::create <stringContentHandler>(buffer, position, end, enc);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
2004-12-15 20:28:09 +00:00
|
|
|
setParsedBounds(position, end);
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
if (newPosition)
|
|
|
|
*newPosition = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void body::generate(utility::outputStream& os, const string::size_type maxLineLength,
|
|
|
|
const string::size_type /* curLinePos */, string::size_type* newLinePos) const
|
|
|
|
{
|
|
|
|
// MIME-Multipart
|
2004-10-21 15:05:47 +00:00
|
|
|
if (getPartCount() != 0)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
string boundary;
|
|
|
|
|
2006-03-29 20:06:39 +00:00
|
|
|
if (m_header.acquire() == NULL)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
boundary = generateRandomBoundaryString();
|
|
|
|
}
|
2004-10-21 15:05:47 +00:00
|
|
|
else
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
try
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <const contentTypeField> ctf =
|
2006-03-29 20:06:39 +00:00
|
|
|
m_header.acquire()->findField(fields::CONTENT_TYPE)
|
|
|
|
.dynamicCast <const contentTypeField>();
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
boundary = ctf->getBoundary();
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field&)
|
|
|
|
{
|
|
|
|
// Warning: no content-type and no boundary string specified!
|
|
|
|
boundary = generateRandomBoundaryString();
|
|
|
|
}
|
|
|
|
catch (exceptions::no_such_parameter&)
|
|
|
|
{
|
|
|
|
// Warning: no boundary string specified!
|
|
|
|
boundary = generateRandomBoundaryString();
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const string& prologText =
|
|
|
|
m_prologText.empty()
|
|
|
|
? (isRootPart()
|
2004-10-21 15:05:47 +00:00
|
|
|
? options::getInstance()->multipart.getPrologText()
|
2004-10-05 10:28:21 +00:00
|
|
|
: NULL_STRING
|
|
|
|
)
|
|
|
|
: m_prologText;
|
|
|
|
|
|
|
|
const string& epilogText =
|
|
|
|
m_epilogText.empty()
|
|
|
|
? (isRootPart()
|
2004-10-21 15:05:47 +00:00
|
|
|
? options::getInstance()->multipart.getEpilogText()
|
2004-10-05 10:28:21 +00:00
|
|
|
: NULL_STRING
|
|
|
|
)
|
|
|
|
: m_epilogText;
|
|
|
|
|
|
|
|
if (!prologText.empty())
|
|
|
|
{
|
2004-11-07 10:33:01 +00:00
|
|
|
text prolog(word(prologText, getCharset()));
|
|
|
|
|
|
|
|
prolog.encodeAndFold(os, maxLineLength, 0,
|
|
|
|
NULL, text::FORCE_NO_ENCODING | text::NO_NEW_LINE_SEQUENCE);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
os << CRLF;
|
|
|
|
}
|
|
|
|
|
|
|
|
os << "--" << boundary;
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
for (int p = 0 ; p < getPartCount() ; ++p)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
os << CRLF;
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
getPartAt(p)->generate(os, maxLineLength, 0);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
os << CRLF << "--" << boundary;
|
|
|
|
}
|
|
|
|
|
|
|
|
os << "--" << CRLF;
|
|
|
|
|
|
|
|
if (!epilogText.empty())
|
|
|
|
{
|
2004-11-07 10:33:01 +00:00
|
|
|
text epilog(word(epilogText, getCharset()));
|
|
|
|
|
|
|
|
epilog.encodeAndFold(os, maxLineLength, 0,
|
|
|
|
NULL, text::FORCE_NO_ENCODING | text::NO_NEW_LINE_SEQUENCE);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
os << CRLF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newLinePos)
|
|
|
|
*newLinePos = 0;
|
|
|
|
}
|
|
|
|
// Simple body
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Generate the contents
|
2005-01-28 17:50:53 +00:00
|
|
|
m_contents->generate(os, getEncoding(), maxLineLength);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
RFC #1521, Page 32:
|
|
|
|
7.2.1. Multipart: The common syntax
|
|
|
|
|
|
|
|
"...Encapsulation boundaries must not appear within the
|
|
|
|
encapsulations, and must be no longer than 70 characters..."
|
|
|
|
|
|
|
|
|
|
|
|
boundary := 0*69<bchars> bcharsnospace
|
|
|
|
|
|
|
|
bchars := bcharsnospace / " "
|
|
|
|
|
|
|
|
bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" / "+" /"_"
|
|
|
|
/ "," / "-" / "." / "/" / ":" / "=" / "?"
|
|
|
|
*/
|
|
|
|
|
|
|
|
const string body::generateRandomBoundaryString()
|
|
|
|
{
|
|
|
|
// 64 characters that can be _safely_ used in a boundary string
|
|
|
|
static const char bchars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-+";
|
|
|
|
|
|
|
|
/*
|
|
|
|
RFC #1521, Page 19:
|
|
|
|
|
|
|
|
Since the hyphen character ("-") is represented as itself in the
|
|
|
|
Quoted-Printable encoding, care must be taken, when encapsulating a
|
|
|
|
quoted-printable encoded body in a multipart entity, to ensure that
|
|
|
|
the encapsulation boundary does not appear anywhere in the encoded
|
|
|
|
body. (A good strategy is to choose a boundary that includes a
|
|
|
|
character sequence such as "=_" which can never appear in a quoted-
|
|
|
|
printable body. See the definition of multipart messages later in
|
|
|
|
this document.)
|
|
|
|
*/
|
|
|
|
|
|
|
|
string::value_type boundary[2 + 48 + 1] = { 0 };
|
|
|
|
|
|
|
|
boundary[0] = '=';
|
|
|
|
boundary[1] = '_';
|
|
|
|
|
|
|
|
// Generate a string of random characters
|
2004-10-21 15:05:47 +00:00
|
|
|
unsigned int r = utility::random::getTime();
|
2004-10-05 10:28:21 +00:00
|
|
|
unsigned int m = sizeof(unsigned int);
|
|
|
|
|
|
|
|
for (size_t i = 2 ; i < (sizeof(boundary) / sizeof(boundary[0]) - 1) ; ++i)
|
|
|
|
{
|
|
|
|
boundary[i] = bchars[r & 63];
|
|
|
|
r >>= 6;
|
|
|
|
|
|
|
|
if (--m == 0)
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
r = utility::random::getNext();
|
2004-10-05 10:28:21 +00:00
|
|
|
m = sizeof(unsigned int);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (string(boundary));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool body::isValidBoundary(const string& boundary)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
static const string validChars("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'()+_,-./:=?");
|
|
|
|
|
|
|
|
const string::const_iterator end = boundary.end();
|
|
|
|
bool valid = false;
|
|
|
|
|
|
|
|
if (boundary.length() > 0 && boundary.length() < 70)
|
|
|
|
{
|
|
|
|
const string::value_type last = *(end - 1);
|
|
|
|
|
|
|
|
if (!(last == ' ' || last == '\t' || last == '\n'))
|
|
|
|
{
|
|
|
|
valid = true;
|
|
|
|
|
|
|
|
for (string::const_iterator i = boundary.begin() ; valid && i != end ; ++i)
|
|
|
|
valid = (validChars.find_first_of(*i) != string::npos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (valid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Quick-access functions
|
|
|
|
//
|
|
|
|
|
2010-05-23 16:18:00 +00:00
|
|
|
|
|
|
|
void body::setContentType(const mediaType& type, const charset& chset)
|
|
|
|
{
|
|
|
|
ref <contentTypeField> ctf = m_header.acquire()->ContentType().dynamicCast <contentTypeField>();
|
|
|
|
|
|
|
|
ctf->setValue(type);
|
|
|
|
ctf->setCharset(chset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void body::setContentType(const mediaType& type)
|
|
|
|
{
|
|
|
|
m_header.acquire()->ContentType()->setValue(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const mediaType body::getContentType() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <const contentTypeField> ctf =
|
2006-03-29 20:06:39 +00:00
|
|
|
m_header.acquire()->findField(fields::CONTENT_TYPE).dynamicCast <const contentTypeField>();
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-11-04 23:21:22 +00:00
|
|
|
return (*ctf->getValue().dynamicCast <const mediaType>());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field&)
|
|
|
|
{
|
|
|
|
// Defaults to "text/plain" (RFC-1521)
|
|
|
|
return (mediaType(mediaTypes::TEXT, mediaTypes::TEXT_PLAIN));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-23 16:18:00 +00:00
|
|
|
void body::setCharset(const charset& chset)
|
|
|
|
{
|
|
|
|
// If a Content-Type field exists, set charset
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ref <contentTypeField> ctf =
|
|
|
|
m_header.acquire()->findField(fields::CONTENT_TYPE).dynamicCast <contentTypeField>();
|
|
|
|
|
|
|
|
ctf->setCharset(chset);
|
|
|
|
}
|
|
|
|
// Else, create a new Content-Type field of default type "text/plain"
|
|
|
|
// and set charset on it
|
|
|
|
catch (exceptions::no_such_field&)
|
|
|
|
{
|
|
|
|
setContentType(mediaType(mediaTypes::TEXT, mediaTypes::TEXT_PLAIN), chset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const charset body::getCharset() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
const ref <const contentTypeField> ctf =
|
2006-03-29 20:06:39 +00:00
|
|
|
m_header.acquire()->findField(fields::CONTENT_TYPE).dynamicCast <contentTypeField>();
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
return (ctf->getCharset());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::no_such_parameter&)
|
|
|
|
{
|
|
|
|
// Defaults to "us-ascii" (RFC-1521)
|
|
|
|
return (vmime::charset(charsets::US_ASCII));
|
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field&)
|
|
|
|
{
|
|
|
|
// Defaults to "us-ascii" (RFC-1521)
|
|
|
|
return (vmime::charset(charsets::US_ASCII));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-23 16:18:00 +00:00
|
|
|
void body::setEncoding(const encoding& enc)
|
|
|
|
{
|
|
|
|
m_header.acquire()->ContentTransferEncoding()->setValue(enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const encoding body::getEncoding() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2005-11-04 23:21:22 +00:00
|
|
|
const ref <const headerField> cef =
|
2006-03-29 20:06:39 +00:00
|
|
|
m_header.acquire()->findField(fields::CONTENT_TRANSFER_ENCODING);
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-11-04 23:21:22 +00:00
|
|
|
return (*cef->getValue().dynamicCast <const encoding>());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field&)
|
|
|
|
{
|
2010-05-23 16:18:00 +00:00
|
|
|
if (m_contents->isEncoded())
|
|
|
|
{
|
|
|
|
return m_contents->getEncoding();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Defaults to "7bit" (RFC-1521)
|
|
|
|
return vmime::encoding(encodingTypes::SEVEN_BIT);
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-29 20:06:39 +00:00
|
|
|
void body::setParentPart(ref <bodyPart> parent)
|
2005-07-12 22:28:02 +00:00
|
|
|
{
|
|
|
|
m_part = parent;
|
|
|
|
m_header = (parent != NULL ? parent->getHeader() : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool body::isRootPart() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2006-03-29 20:06:39 +00:00
|
|
|
ref <const bodyPart> part = m_part.acquire();
|
|
|
|
return (part == NULL || part->getParentPart() == NULL);
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <component> body::clone() const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <body> bdy = vmime::create <body>();
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
bdy->copyFrom(*this);
|
|
|
|
|
|
|
|
return (bdy);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void body::copyFrom(const component& other)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
const body& bdy = dynamic_cast <const body&>(other);
|
|
|
|
|
|
|
|
m_prologText = bdy.m_prologText;
|
|
|
|
m_epilogText = bdy.m_epilogText;
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
m_contents = bdy.m_contents;
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
removeAllParts();
|
|
|
|
|
|
|
|
for (int p = 0 ; p < bdy.getPartCount() ; ++p)
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <bodyPart> part = bdy.getPartAt(p)->clone().dynamicCast <bodyPart>();
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
part->m_parent = m_part;
|
|
|
|
|
|
|
|
m_parts.push_back(part);
|
|
|
|
}
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
body& body::operator=(const body& other)
|
|
|
|
{
|
|
|
|
copyFrom(other);
|
2004-10-05 10:28:21 +00:00
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const string& body::getPrologText() const
|
|
|
|
{
|
|
|
|
return (m_prologText);
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void body::setPrologText(const string& prologText)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
m_prologText = prologText;
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const string& body::getEpilogText() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
return (m_epilogText);
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void body::setEpilogText(const string& epilogText)
|
|
|
|
{
|
|
|
|
m_epilogText = epilogText;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const ref <const contentHandler> body::getContents() const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
return (m_contents);
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-08-17 14:17:18 +00:00
|
|
|
void body::setContents(ref <const contentHandler> contents)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
m_contents = contents;
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-23 16:18:00 +00:00
|
|
|
void body::setContents(ref <const contentHandler> contents, const mediaType& type)
|
|
|
|
{
|
|
|
|
m_contents = contents;
|
|
|
|
|
|
|
|
setContentType(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void body::setContents(ref <const contentHandler> contents, const mediaType& type, const charset& chset)
|
|
|
|
{
|
|
|
|
m_contents = contents;
|
|
|
|
|
|
|
|
setContentType(type, chset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void body::setContents(ref <const contentHandler> contents, const mediaType& type,
|
|
|
|
const charset& chset, const encoding& enc)
|
|
|
|
{
|
|
|
|
m_contents = contents;
|
|
|
|
|
|
|
|
setContentType(type, chset);
|
|
|
|
setEncoding(enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void body::initNewPart(ref <bodyPart> part)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
part->m_parent = m_part;
|
|
|
|
|
2006-03-29 20:06:39 +00:00
|
|
|
ref <header> hdr = m_header.acquire();
|
|
|
|
|
|
|
|
if (hdr != NULL)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
// Check whether we have a boundary string
|
2004-10-05 10:28:21 +00:00
|
|
|
try
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <contentTypeField> ctf =
|
2006-03-29 20:06:39 +00:00
|
|
|
hdr->findField(fields::CONTENT_TYPE).dynamicCast <contentTypeField>();
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
const string boundary = ctf->getBoundary();
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
if (boundary.empty() || !isValidBoundary(boundary))
|
2005-07-12 22:28:02 +00:00
|
|
|
ctf->setBoundary(generateRandomBoundaryString());
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::no_such_parameter&)
|
|
|
|
{
|
|
|
|
// No "boundary" parameter: generate a random one.
|
2005-07-12 22:28:02 +00:00
|
|
|
ctf->setBoundary(generateRandomBoundaryString());
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-11-04 23:21:22 +00:00
|
|
|
if (ctf->getValue().dynamicCast <const mediaType>()->getType() != mediaTypes::MULTIPART)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
// Warning: multi-part body but the Content-Type is
|
|
|
|
// not specified as "multipart/..."
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
2004-10-21 15:05:47 +00:00
|
|
|
catch (exceptions::no_such_field&)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
// No "Content-Type" field: create a new one and generate
|
|
|
|
// a random boundary string.
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <contentTypeField> ctf =
|
2006-03-29 20:06:39 +00:00
|
|
|
hdr->getField(fields::CONTENT_TYPE).dynamicCast <contentTypeField>();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
ctf->setValue(mediaType(mediaTypes::MULTIPART, mediaTypes::MULTIPART_MIXED));
|
|
|
|
ctf->setBoundary(generateRandomBoundaryString());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void body::appendPart(ref <bodyPart> part)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
initNewPart(part);
|
|
|
|
|
|
|
|
m_parts.push_back(part);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void body::insertPartBefore(ref <bodyPart> beforePart, ref <bodyPart> part)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
initNewPart(part);
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <bodyPart> >::iterator it = std::find
|
2004-10-21 15:05:47 +00:00
|
|
|
(m_parts.begin(), m_parts.end(), beforePart);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
if (it == m_parts.end())
|
|
|
|
throw exceptions::no_such_part();
|
|
|
|
|
|
|
|
m_parts.insert(it, part);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void body::insertPartBefore(const int pos, ref <bodyPart> part)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
initNewPart(part);
|
|
|
|
|
|
|
|
m_parts.insert(m_parts.begin() + pos, part);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void body::insertPartAfter(ref <bodyPart> afterPart, ref <bodyPart> part)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
initNewPart(part);
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <bodyPart> >::iterator it = std::find
|
2004-10-21 15:05:47 +00:00
|
|
|
(m_parts.begin(), m_parts.end(), afterPart);
|
|
|
|
|
|
|
|
if (it == m_parts.end())
|
|
|
|
throw exceptions::no_such_part();
|
|
|
|
|
|
|
|
m_parts.insert(it + 1, part);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void body::insertPartAfter(const int pos, ref <bodyPart> part)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
initNewPart(part);
|
|
|
|
|
|
|
|
m_parts.insert(m_parts.begin() + pos + 1, part);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void body::removePart(ref <bodyPart> part)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <bodyPart> >::iterator it = std::find
|
2004-10-21 15:05:47 +00:00
|
|
|
(m_parts.begin(), m_parts.end(), part);
|
|
|
|
|
|
|
|
if (it == m_parts.end())
|
|
|
|
throw exceptions::no_such_part();
|
|
|
|
|
|
|
|
m_parts.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void body::removePart(const int pos)
|
|
|
|
{
|
|
|
|
m_parts.erase(m_parts.begin() + pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void body::removeAllParts()
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
m_parts.clear();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
int body::getPartCount() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
return (m_parts.size());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool body::isEmpty() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
return (m_parts.size() == 0);
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <bodyPart> body::getPartAt(const int pos)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
return (m_parts[pos]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const ref <const bodyPart> body::getPartAt(const int pos) const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
return (m_parts[pos]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <const bodyPart> > body::getPartList() const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
std::vector <ref <const bodyPart> > list;
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
list.reserve(m_parts.size());
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
for (std::vector <ref <bodyPart> >::const_iterator it = m_parts.begin() ;
|
2004-10-21 15:05:47 +00:00
|
|
|
it != m_parts.end() ; ++it)
|
|
|
|
{
|
|
|
|
list.push_back(*it);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
return (list);
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <bodyPart> > body::getPartList()
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
return (m_parts);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <const component> > body::getChildComponents() const
|
2004-12-20 12:33:55 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
std::vector <ref <const component> > list;
|
2004-12-20 12:33:55 +00:00
|
|
|
|
|
|
|
copy_vector(m_parts, list);
|
|
|
|
|
|
|
|
return (list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
} // vmime
|