2004-10-05 10:28:21 +00:00
|
|
|
//
|
2005-03-18 21:33:11 +00:00
|
|
|
// VMime library (http://www.vmime.org)
|
2006-02-05 10:22:59 +00:00
|
|
|
// Copyright (C) 2002-2006 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
|
|
|
|
// published by the Free Software Foundation; either version 2 of
|
|
|
|
// 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/messageParser.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-10-13 21:27:46 +00:00
|
|
|
#include "vmime/attachmentHelper.hpp"
|
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/defaultAttachment.hpp"
|
|
|
|
#include "vmime/textPartFactory.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-11-04 23:21:22 +00:00
|
|
|
#include "vmime/relay.hpp"
|
|
|
|
#include "vmime/contentTypeField.hpp"
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
namespace vmime
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
messageParser::messageParser(const string& buffer)
|
|
|
|
{
|
2005-10-13 21:27:46 +00:00
|
|
|
ref <message> msg = vmime::create <message>();
|
|
|
|
msg->parse(buffer);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
parse(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-13 21:27:46 +00:00
|
|
|
messageParser::messageParser(ref <const message> msg)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
parse(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messageParser::~messageParser()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-13 21:27:46 +00:00
|
|
|
void messageParser::parse(ref <const message> msg)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
// Header fields (if field is present, copy its value, else do nothing)
|
2004-12-22 14:55:43 +00:00
|
|
|
#ifndef VMIME_BUILDING_DOC
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
#define TRY_FIELD(var, type, name) \
|
2005-11-04 23:21:22 +00:00
|
|
|
try { var = *msg->getHeader()->findField(name)->getValue().dynamicCast <type>(); } \
|
2004-10-21 15:05:47 +00:00
|
|
|
catch (exceptions::no_such_field) { }
|
|
|
|
|
2005-11-04 23:21:22 +00:00
|
|
|
TRY_FIELD(m_from, mailbox, fields::FROM);
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-11-04 23:21:22 +00:00
|
|
|
TRY_FIELD(m_to, addressList, fields::TO);
|
|
|
|
TRY_FIELD(m_cc, addressList, fields::CC);
|
|
|
|
TRY_FIELD(m_bcc, addressList, fields::BCC);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-11-04 23:21:22 +00:00
|
|
|
TRY_FIELD(m_subject, text, fields::SUBJECT);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
#undef TRY_FIELD
|
|
|
|
|
2004-12-22 14:55:43 +00:00
|
|
|
#endif // VMIME_BUILDING_DOC
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
// Date
|
|
|
|
try
|
|
|
|
{
|
2005-11-04 23:21:22 +00:00
|
|
|
const headerField& recv = *msg->getHeader()->findField(fields::RECEIVED);
|
|
|
|
m_date = recv.getValue().dynamicCast <const relay>()->getDate();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (vmime::exceptions::no_such_field&)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2005-11-04 23:21:22 +00:00
|
|
|
const headerField& date = *msg->getHeader()->findField(fields::DATE);
|
|
|
|
m_date = *date.getValue().dynamicCast <const datetime>();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (vmime::exceptions::no_such_field&)
|
|
|
|
{
|
|
|
|
m_date = datetime::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attachments
|
|
|
|
findAttachments(msg);
|
|
|
|
|
|
|
|
// Text parts
|
2006-07-13 16:32:39 +00:00
|
|
|
findTextParts(msg, msg);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-13 21:27:46 +00:00
|
|
|
void messageParser::findAttachments(ref <const message> msg)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-10-13 21:27:46 +00:00
|
|
|
m_attach = attachmentHelper::findAttachmentsInMessage(msg);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-13 16:32:39 +00:00
|
|
|
void messageParser::findTextParts(ref <const bodyPart> msg, ref <const bodyPart> part)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
// Handle the case in which the message is not multipart: if the body part is
|
|
|
|
// "text/*", take this part.
|
2006-07-13 16:32:39 +00:00
|
|
|
if (part->getBody()->getPartCount() == 0)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
mediaType type(mediaTypes::TEXT, mediaTypes::TEXT_PLAIN);
|
|
|
|
bool accept = false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const contentTypeField& ctf = dynamic_cast<contentTypeField&>
|
2006-07-13 16:32:39 +00:00
|
|
|
(*msg->getHeader()->findField(fields::CONTENT_TYPE));
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-11-04 23:21:22 +00:00
|
|
|
const mediaType ctfType =
|
|
|
|
*ctf.getValue().dynamicCast <const mediaType>();
|
|
|
|
|
|
|
|
if (ctfType.getType() == mediaTypes::TEXT)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-11-04 23:21:22 +00:00
|
|
|
type = ctfType;
|
2004-10-05 10:28:21 +00:00
|
|
|
accept = true;
|
|
|
|
}
|
|
|
|
}
|
2005-11-04 23:21:22 +00:00
|
|
|
catch (exceptions::no_such_field&)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
// No "Content-type" field: assume "text/plain".
|
|
|
|
accept = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accept)
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <textPart> txtPart = textPartFactory::getInstance()->create(type);
|
|
|
|
txtPart->parse(msg, msg, msg);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
m_textParts.push_back(txtPart);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Multipart message
|
|
|
|
else
|
|
|
|
{
|
|
|
|
findSubTextParts(msg, part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-13 16:32:39 +00:00
|
|
|
bool messageParser::findSubTextParts(ref <const bodyPart> msg, ref <const bodyPart> part)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
// In general, all the text parts are contained in parallel in the same
|
|
|
|
// parent part (or message).
|
|
|
|
// So, wherever the text parts are, all we have to do is to find the first
|
|
|
|
// MIME part which is a text part.
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
std::vector <ref <const bodyPart> > textParts;
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2006-07-13 16:32:39 +00:00
|
|
|
for (int i = 0 ; i < part->getBody()->getPartCount() ; ++i)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2006-07-13 16:32:39 +00:00
|
|
|
const ref <const bodyPart> p = part->getBody()->getPartAt(i);
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
try
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
const contentTypeField& ctf = dynamic_cast <const contentTypeField&>
|
|
|
|
(*(p->getHeader()->findField(fields::CONTENT_TYPE)));
|
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::TEXT)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
textParts.push_back(p);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
2005-11-04 23:21:22 +00:00
|
|
|
catch (exceptions::no_such_field&)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
// No "Content-type" field.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textParts.size())
|
|
|
|
{
|
|
|
|
// Okay. So we have found at least one text part
|
2005-07-12 22:28:02 +00:00
|
|
|
for (std::vector <ref <const bodyPart> >::const_iterator p = textParts.begin() ;
|
2004-10-21 15:05:47 +00:00
|
|
|
p != textParts.end() ; ++p)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
const contentTypeField& ctf = dynamic_cast <const contentTypeField&>
|
|
|
|
(*((*p)->getHeader()->findField(fields::CONTENT_TYPE)));
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-11-04 23:21:22 +00:00
|
|
|
const mediaType type = *ctf.getValue().dynamicCast <const mediaType>();
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
try
|
|
|
|
{
|
2005-11-04 23:21:22 +00:00
|
|
|
ref <textPart> txtPart = textPartFactory::getInstance()->create(type);
|
2006-07-13 16:32:39 +00:00
|
|
|
txtPart->parse(msg, part, *p);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
m_textParts.push_back(txtPart);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::no_factory_available& e)
|
|
|
|
{
|
|
|
|
// Content-type not recognized.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-13 16:32:39 +00:00
|
|
|
bool found = false;
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2006-07-13 16:32:39 +00:00
|
|
|
for (int i = 0 ; !found && (i < part->getBody()->getPartCount()) ; ++i)
|
|
|
|
{
|
|
|
|
found = findSubTextParts(msg, part->getBody()->getPartAt(i));
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
2006-07-13 16:32:39 +00:00
|
|
|
|
|
|
|
return found;
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const mailbox& messageParser::getExpeditor() const
|
|
|
|
{
|
|
|
|
return (m_from);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const addressList& messageParser::getRecipients() const
|
|
|
|
{
|
|
|
|
return (m_to);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const addressList& messageParser::getCopyRecipients() const
|
|
|
|
{
|
|
|
|
return (m_cc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const addressList& messageParser::getBlindCopyRecipients() const
|
|
|
|
{
|
|
|
|
return (m_bcc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const text& messageParser::getSubject() const
|
|
|
|
{
|
|
|
|
return (m_subject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const datetime& messageParser::getDate() const
|
|
|
|
{
|
|
|
|
return (m_date);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <const attachment> > messageParser::getAttachmentList() const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-10-13 21:27:46 +00:00
|
|
|
return m_attach;
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const int messageParser::getAttachmentCount() const
|
|
|
|
{
|
|
|
|
return (m_attach.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const ref <const attachment> messageParser::getAttachmentAt(const int pos) const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
return (m_attach[pos]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <const textPart> > messageParser::getTextPartList() const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
std::vector <ref <const textPart> > res;
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
res.reserve(m_textParts.size());
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
for (std::vector <ref <textPart> >::const_iterator it = m_textParts.begin() ;
|
2004-10-21 15:05:47 +00:00
|
|
|
it != m_textParts.end() ; ++it)
|
|
|
|
{
|
|
|
|
res.push_back(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const int messageParser::getTextPartCount() const
|
|
|
|
{
|
|
|
|
return (m_textParts.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const ref <const textPart> messageParser::getTextPartAt(const int pos) const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
return (m_textParts[pos]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
} // vmime
|