vmime/src/mdn/MDNHelper.cpp

317 lines
9.0 KiB
C++
Raw Normal View History

2005-03-25 20:49:54 +00:00
//
// VMime library (http://www.vmime.org)
2008-01-04 18:07:40 +00:00
// Copyright (C) 2002-2008 Vincent Richard <vincent@vincent-richard.net>
2005-03-25 20:49:54 +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.,
// 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.
2005-03-25 20:49:54 +00:00
//
#include "vmime/mdn/MDNHelper.hpp"
#include "vmime/exception.hpp"
#include "vmime/stringContentHandler.hpp"
#include "vmime/contentTypeField.hpp"
#include "vmime/path.hpp"
#include "vmime/dateTime.hpp"
2005-03-25 20:49:54 +00:00
namespace vmime {
namespace mdn {
2005-07-12 22:28:02 +00:00
void MDNHelper::attachMDNRequest(ref <message> msg, const mailboxList& mailboxes)
2005-04-09 19:46:40 +00:00
{
2005-07-12 22:28:02 +00:00
ref <header> hdr = msg->getHeader();
2005-04-09 19:46:40 +00:00
2005-07-12 22:28:02 +00:00
hdr->DispositionNotificationTo()->setValue(mailboxes);
2005-04-09 19:46:40 +00:00
}
2005-07-12 22:28:02 +00:00
void MDNHelper::attachMDNRequest(ref <message> msg, const mailbox& mbox)
2005-04-09 19:46:40 +00:00
{
mailboxList mboxList;
2005-07-12 22:28:02 +00:00
mboxList.appendMailbox(mbox.clone().dynamicCast <mailbox>());
2005-04-09 19:46:40 +00:00
attachMDNRequest(msg, mboxList);
}
2005-07-12 22:28:02 +00:00
const std::vector <sendableMDNInfos> MDNHelper::getPossibleMDNs(const ref <const message> msg)
2005-03-25 20:49:54 +00:00
{
std::vector <sendableMDNInfos> result;
2005-07-12 22:28:02 +00:00
const ref <const header> hdr = msg->getHeader();
2005-03-25 20:49:54 +00:00
if (hdr->hasField(fields::DISPOSITION_NOTIFICATION_TO))
{
const mailboxList& dnto = *hdr->DispositionNotificationTo()->getValue()
.dynamicCast <const mailboxList>();
2005-03-25 20:49:54 +00:00
for (int i = 0 ; i < dnto.getMailboxCount() ; ++i)
result.push_back(sendableMDNInfos(msg, *dnto.getMailboxAt(i)));
}
return (result);
}
2005-07-12 22:28:02 +00:00
const bool MDNHelper::isMDN(const ref <const message> msg)
2005-03-25 20:49:54 +00:00
{
2005-07-12 22:28:02 +00:00
const ref <const header> hdr = msg->getHeader();
2005-03-25 20:49:54 +00:00
// A MDN message implies the following:
// - a Content-Type field is present and its value is "multipart/report"
// - a "report-type" parameter is present in the Content-Type field,
// and its value is "disposition-notification"
if (hdr->hasField(fields::CONTENT_TYPE))
{
const contentTypeField& ctf = *(hdr->ContentType()
.dynamicCast <const contentTypeField>());
const mediaType type = *ctf.getValue().dynamicCast <const mediaType>();
2005-03-25 20:49:54 +00:00
if (type.getType() == vmime::mediaTypes::MULTIPART &&
type.getSubType() == vmime::mediaTypes::MULTIPART_REPORT)
2005-03-25 20:49:54 +00:00
{
if (ctf.hasParameter("report-type") &&
ctf.getReportType() == "disposition-notification")
{
return (true);
}
}
}
return (false);
}
2005-07-12 22:28:02 +00:00
receivedMDNInfos MDNHelper::getReceivedMDN(const ref <const message> msg)
2005-03-25 20:49:54 +00:00
{
if (!isMDN(msg))
throw exceptions::invalid_argument();
return receivedMDNInfos(msg);
}
2005-07-12 22:28:02 +00:00
const bool MDNHelper::needConfirmation(const ref <const message> msg)
2005-03-25 20:49:54 +00:00
{
ref <const header> hdr = msg->getHeader();
2005-03-25 20:49:54 +00:00
// No "Return-Path" field
if (!hdr->hasField(fields::RETURN_PATH))
return true;
2005-03-25 20:49:54 +00:00
// More than one address in Disposition-Notification-To
if (hdr->hasField(fields::DISPOSITION_NOTIFICATION_TO))
{
const mailboxList& dnto = *hdr->DispositionNotificationTo()->getValue()
.dynamicCast <const mailboxList>();
2005-03-25 20:49:54 +00:00
if (dnto.getMailboxCount() > 1)
return true;
else if (dnto.getMailboxCount() == 0)
return false;
2005-03-25 20:49:54 +00:00
// Return-Path != Disposition-Notification-To
const mailbox& mbox = *dnto.getMailboxAt(0);
const path& rp = *hdr->ReturnPath()->getValue().dynamicCast <const path>();
2005-03-25 20:49:54 +00:00
if (mbox.getEmail() != rp.getLocalPart() + "@" + rp.getDomain())
return true;
2005-03-25 20:49:54 +00:00
}
// User confirmation not needed
return false;
2005-03-25 20:49:54 +00:00
}
2005-07-12 22:28:02 +00:00
ref <message> MDNHelper::buildMDN(const sendableMDNInfos& mdnInfos,
const string& text,
const charset& ch,
const mailbox& expeditor,
const disposition& dispo,
const string& reportingUA,
const std::vector <string>& reportingUAProducts)
2005-03-25 20:49:54 +00:00
{
// Create a new message
2005-07-12 22:28:02 +00:00
ref <message> msg = vmime::create <message>();
2005-03-25 20:49:54 +00:00
// Fill-in header fields
2005-07-12 22:28:02 +00:00
ref <header> hdr = msg->getHeader();
2005-03-25 20:49:54 +00:00
2005-07-12 22:28:02 +00:00
hdr->ContentType()->setValue(mediaType(vmime::mediaTypes::MULTIPART,
vmime::mediaTypes::MULTIPART_REPORT));
hdr->ContentType().dynamicCast <contentTypeField>()->setReportType("disposition-notification");
2005-03-25 20:49:54 +00:00
2005-07-12 22:28:02 +00:00
hdr->Disposition()->setValue(dispo);
2005-03-25 20:49:54 +00:00
addressList to;
to.appendAddress(vmime::create <mailbox>(mdnInfos.getRecipient()));
hdr->To()->setValue(to);
hdr->From()->setValue(expeditor);
hdr->Subject()->setValue(vmime::text(word("Disposition notification")));
2005-03-25 20:49:54 +00:00
2005-07-12 22:28:02 +00:00
hdr->Date()->setValue(datetime::now());
hdr->MimeVersion()->setValue(string(SUPPORTED_MIME_VERSION));
2005-03-25 20:49:54 +00:00
msg->getBody()->appendPart(createFirstMDNPart(mdnInfos, text, ch));
msg->getBody()->appendPart(createSecondMDNPart(mdnInfos,
dispo, reportingUA, reportingUAProducts));
msg->getBody()->appendPart(createThirdMDNPart(mdnInfos));
return (msg);
}
2005-07-12 22:28:02 +00:00
ref <bodyPart> MDNHelper::createFirstMDNPart(const sendableMDNInfos& /* mdnInfos */,
const string& text, const charset& ch)
2005-03-25 20:49:54 +00:00
{
2005-07-12 22:28:02 +00:00
ref <bodyPart> part = vmime::create <bodyPart>();
2005-03-25 20:49:54 +00:00
// Header
2005-07-12 22:28:02 +00:00
ref <header> hdr = part->getHeader();
2005-03-25 20:49:54 +00:00
2005-07-12 22:28:02 +00:00
hdr->ContentType()->setValue(mediaType(vmime::mediaTypes::TEXT,
vmime::mediaTypes::TEXT_PLAIN));
2005-03-25 20:49:54 +00:00
hdr->ContentType().dynamicCast <contentTypeField>()->setCharset(ch);
2005-03-25 20:49:54 +00:00
// Body
2005-07-12 22:28:02 +00:00
part->getBody()->setContents(vmime::create <stringContentHandler>(text));
2005-03-25 20:49:54 +00:00
return (part);
}
2005-07-12 22:28:02 +00:00
ref <bodyPart> MDNHelper::createSecondMDNPart(const sendableMDNInfos& mdnInfos,
const disposition& dispo,
const string& reportingUA,
const std::vector <string>& reportingUAProducts)
2005-03-25 20:49:54 +00:00
{
2005-07-12 22:28:02 +00:00
ref <bodyPart> part = vmime::create <bodyPart>();
2005-03-25 20:49:54 +00:00
// Header
2005-07-12 22:28:02 +00:00
ref <header> hdr = part->getHeader();
2005-03-25 20:49:54 +00:00
2005-07-12 22:28:02 +00:00
hdr->ContentDisposition()->setValue(vmime::contentDispositionTypes::INLINE);
hdr->ContentType()->setValue(mediaType(vmime::mediaTypes::MESSAGE,
vmime::mediaTypes::MESSAGE_DISPOSITION_NOTIFICATION));
2005-03-25 20:49:54 +00:00
// Body
//
// The body of a message/disposition-notification consists of one or
// more "fields" formatted according to the ABNF of [RFC-MSGFMT] header
// "fields". The syntax of the message/disposition-notification content
// is as follows:
//
// disposition-notification-content = [ reporting-ua-field CRLF ]
// [ mdn-gateway-field CRLF ]
// [ original-recipient-field CRLF ]
// final-recipient-field CRLF
// [ original-message-id-field CRLF ]
// disposition-field CRLF
// *( failure-field CRLF )
// *( error-field CRLF )
// *( warning-field CRLF )
// *( extension-field CRLF )
//
header fields;
// -- Reporting-UA (optional)
if (!reportingUA.empty())
{
string ruaText;
ruaText = reportingUA;
for (unsigned int i = 0 ; i < reportingUAProducts.size() ; ++i)
{
if (i == 0)
ruaText += "; ";
else
ruaText += ", ";
ruaText += reportingUAProducts[i];
}
ref <headerField> rua = headerFieldFactory::getInstance()->
create(vmime::fields::REPORTING_UA);
2005-03-25 20:49:54 +00:00
rua->setValue(ruaText);
fields.appendField(rua);
}
// -- Final-Recipient
ref <headerField> fr = headerFieldFactory::getInstance()->
create(vmime::fields::FINAL_RECIPIENT);
2005-03-25 20:49:54 +00:00
fr->setValue("rfc822; " + mdnInfos.getRecipient().getEmail());
// -- Original-Message-ID
if (mdnInfos.getMessage()->getHeader()->hasField(vmime::fields::MESSAGE_ID))
{
fields.OriginalMessageId()->setValueConst
2005-07-12 22:28:02 +00:00
(mdnInfos.getMessage()->getHeader()->MessageId()->getValue());
2005-03-25 20:49:54 +00:00
}
// -- Disposition
2005-07-12 22:28:02 +00:00
fields.Disposition()->setValue(dispo);
2005-03-25 20:49:54 +00:00
std::ostringstream oss;
utility::outputStreamAdapter vos(oss);
fields.generate(vos);
2005-07-12 22:28:02 +00:00
part->getBody()->setContents(vmime::create <stringContentHandler>(oss.str()));
2005-03-25 20:49:54 +00:00
return (part);
}
2005-07-12 22:28:02 +00:00
ref <bodyPart> MDNHelper::createThirdMDNPart(const sendableMDNInfos& mdnInfos)
2005-03-25 20:49:54 +00:00
{
2005-07-12 22:28:02 +00:00
ref <bodyPart> part = vmime::create <bodyPart>();
2005-03-25 20:49:54 +00:00
// Header
2005-07-12 22:28:02 +00:00
ref <header> hdr = part->getHeader();
2005-03-25 20:49:54 +00:00
2005-07-12 22:28:02 +00:00
hdr->ContentDisposition()->setValue(vmime::contentDispositionTypes::INLINE);
hdr->ContentType()->setValue(mediaType(vmime::mediaTypes::TEXT,
vmime::mediaTypes::TEXT_RFC822_HEADERS));
2005-03-25 20:49:54 +00:00
// Body: original message headers
std::ostringstream oss;
utility::outputStreamAdapter vos(oss);
mdnInfos.getMessage()->getHeader()->generate(vos);
2005-07-12 22:28:02 +00:00
part->getBody()->setContents(vmime::create <stringContentHandler>(oss.str()));
2005-03-25 20:49:54 +00:00
return (part);
}
} // mdn
} // vmime