2004-10-05 10:28:21 +00:00
|
|
|
//
|
2005-03-18 21:33:11 +00:00
|
|
|
// VMime library (http://www.vmime.org)
|
2005-01-03 12:26:48 +00:00
|
|
|
// Copyright (C) 2002-2005 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.
|
|
|
|
//
|
|
|
|
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/messageParser.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/defaultAttachment.hpp"
|
|
|
|
#include "vmime/textPartFactory.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace vmime
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
messageParser::messageParser(const string& buffer)
|
|
|
|
{
|
|
|
|
vmime::message msg;
|
|
|
|
msg.parse(buffer);
|
|
|
|
|
|
|
|
parse(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messageParser::messageParser(const message& msg)
|
|
|
|
{
|
|
|
|
parse(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messageParser::~messageParser()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void messageParser::parse(const message& msg)
|
|
|
|
{
|
|
|
|
// 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) \
|
|
|
|
try { var = dynamic_cast<type&>(*msg.getHeader()->findField(name)).getValue(); } \
|
|
|
|
catch (exceptions::no_such_field) { }
|
|
|
|
|
|
|
|
TRY_FIELD(m_from, mailboxField, fields::FROM);
|
|
|
|
|
|
|
|
TRY_FIELD(m_to, addressListField, fields::TO);
|
|
|
|
TRY_FIELD(m_cc, addressListField, fields::CC);
|
|
|
|
TRY_FIELD(m_bcc, addressListField, fields::BCC);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
TRY_FIELD(m_subject, textField, 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
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
vmime::relayField& recv = dynamic_cast <vmime::relayField&>
|
|
|
|
(*msg.getHeader()->findField(fields::RECEIVED));
|
|
|
|
|
|
|
|
m_date = recv.getValue().getDate();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (vmime::exceptions::no_such_field&)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
vmime::dateField& date = dynamic_cast <vmime::dateField&>
|
|
|
|
(*msg.getHeader()->findField(fields::DATE));
|
|
|
|
|
|
|
|
m_date = date.getValue();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (vmime::exceptions::no_such_field&)
|
|
|
|
{
|
|
|
|
m_date = datetime::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attachments
|
|
|
|
findAttachments(msg);
|
|
|
|
|
|
|
|
// Text parts
|
|
|
|
findTextParts(msg, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void messageParser::findAttachments(const bodyPart& part)
|
|
|
|
{
|
|
|
|
// We simply search for parts that are not "Content-disposition: inline".
|
2004-10-21 15:05:47 +00:00
|
|
|
for (int i = 0 ; i < part.getBody()->getPartCount() ; ++i)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
const bodyPart& p = *part.getBody()->getPartAt(i);
|
|
|
|
const header& hdr = *p.getHeader();
|
|
|
|
const body& bdy = *p.getBody();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
// Is this part an attachment?
|
|
|
|
bool isAttachment = false;
|
|
|
|
const contentDispositionField* contentDispField = NULL;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const contentDispositionField& cdf = dynamic_cast<contentDispositionField&>
|
2004-10-21 15:05:47 +00:00
|
|
|
(*hdr.findField(fields::CONTENT_DISPOSITION));
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-12-24 12:37:52 +00:00
|
|
|
if (cdf.getValue().getName() != contentDispositionTypes::INLINE)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
contentDispField = &cdf;
|
|
|
|
isAttachment = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field)
|
|
|
|
{
|
|
|
|
// No "Content-disposition" field: assume "attachment" if
|
|
|
|
// type is not "text/..." or "multipart/...".
|
|
|
|
mediaType type;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const contentTypeField& ctf = dynamic_cast<contentTypeField&>
|
2004-10-21 15:05:47 +00:00
|
|
|
(*hdr.findField(fields::CONTENT_TYPE));
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
type = ctf.getValue();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field)
|
|
|
|
{
|
|
|
|
// No "Content-type" field: assume "application/octet-stream".
|
|
|
|
type = mediaType(mediaTypes::APPLICATION,
|
|
|
|
mediaTypes::APPLICATION_OCTET_STREAM);
|
|
|
|
}
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
if (type.getType() != mediaTypes::TEXT &&
|
|
|
|
type.getType() != mediaTypes::MULTIPART)
|
|
|
|
{
|
2004-10-05 10:28:21 +00:00
|
|
|
isAttachment = true;
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isAttachment)
|
|
|
|
{
|
|
|
|
// Determine the media type of this attachment
|
|
|
|
mediaType type;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const contentTypeField& ctf = dynamic_cast<contentTypeField&>
|
2004-10-21 15:05:47 +00:00
|
|
|
(*hdr.findField(fields::CONTENT_TYPE));
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
type = ctf.getValue();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field)
|
|
|
|
{
|
|
|
|
// No "Content-type" field: assume "application/octet-stream".
|
|
|
|
type = mediaType(mediaTypes::APPLICATION,
|
|
|
|
mediaTypes::APPLICATION_OCTET_STREAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the description (if available)
|
|
|
|
text description;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const textField& cd = dynamic_cast<textField&>
|
2004-10-21 15:05:47 +00:00
|
|
|
(*hdr.findField(fields::CONTENT_DESCRIPTION));
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
description = cd.getValue();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field)
|
|
|
|
{
|
|
|
|
// No description available.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the attachment object
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <attachment> attach = vmime::create <defaultAttachment>
|
|
|
|
(bdy.getContents()->clone().dynamicCast <contentHandler>(),
|
|
|
|
bdy.getEncoding(), type, description);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
if (contentDispField != NULL)
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
m_attachInfo.insert(std::map <attachment*, ref <contentDispositionField> >::
|
|
|
|
value_type(attach.get(), contentDispField->clone().
|
|
|
|
dynamicCast <contentDispositionField>()));
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the attachment to the list
|
|
|
|
m_attach.push_back(attach);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find attachments in sub-parts
|
2004-10-21 15:05:47 +00:00
|
|
|
if (bdy.getPartCount())
|
|
|
|
findAttachments(p);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void messageParser::findTextParts(const bodyPart& msg, const bodyPart& part)
|
|
|
|
{
|
|
|
|
// Handle the case in which the message is not multipart: if the body part is
|
|
|
|
// "text/*", take this part.
|
2004-10-21 15:05:47 +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&>
|
2004-10-21 15:05:47 +00:00
|
|
|
(*msg.getHeader()->findField(fields::CONTENT_TYPE));
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
if (ctf.getValue().getType() == mediaTypes::TEXT)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
type = ctf.getValue();
|
2004-10-05 10:28:21 +00:00
|
|
|
accept = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field)
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool messageParser::findSubTextParts(const bodyPart& msg, const bodyPart& part)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
for (int i = 0 ; i < part.getBody()->getPartCount() ; ++i)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +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
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
if (ctf.getValue().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
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (exceptions::no_such_field)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <textPart> txtPart = textPartFactory::getInstance()->create(ctf.getValue());
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//else
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
for (int i = 0 ; !found && (i < part.getBody()->getPartCount()) ; ++i)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
found = findSubTextParts(msg, *part.getBody()->getPartAt(i));
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const ref <const contentDispositionField> messageParser::getAttachmentInfo(const ref <const attachment> a) const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
std::map <attachment*, ref <contentDispositionField> >::const_iterator
|
|
|
|
it = m_attachInfo.find(ref <attachment>(a.constCast <attachment>()).get());
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
return (it != m_attachInfo.end() ? (*it).second : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-07-12 22:28:02 +00:00
|
|
|
std::vector <ref <const attachment> > res;
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
res.reserve(m_attach.size());
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
for (std::vector <ref <attachment> >::const_iterator it = m_attach.begin() ;
|
2004-10-21 15:05:47 +00:00
|
|
|
it != m_attach.end() ; ++it)
|
|
|
|
{
|
|
|
|
res.push_back(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|