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.
|
|
|
|
//
|
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/header.hpp"
|
|
|
|
#include "vmime/parserHelpers.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-12-30 20:32:05 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
namespace vmime
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
header::header()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
header::~header()
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
removeAllFields();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
RFC #822:
|
|
|
|
3.2. HEADER FIELD DEFINITIONS
|
|
|
|
|
|
|
|
field = field-name ":" [ field-body ] CRLF
|
|
|
|
|
|
|
|
field-name = 1*<any CHAR, excluding CTLs, SPACE, and ":">
|
|
|
|
|
|
|
|
field-body = field-body-contents
|
|
|
|
[CRLF LWSP-char field-body]
|
|
|
|
|
|
|
|
field-body-contents =
|
|
|
|
<the ASCII characters making up the field-body, as
|
|
|
|
defined in the following sections, and consisting
|
|
|
|
of combinations of atom, quoted-string, and
|
|
|
|
specials tokens, or else consisting of texts>
|
|
|
|
*/
|
|
|
|
|
|
|
|
void header::parse(const string& buffer, const string::size_type position,
|
|
|
|
const string::size_type end, string::size_type* newPosition)
|
|
|
|
{
|
|
|
|
string::size_type pos = position;
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
removeAllFields();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
while (pos < end)
|
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <headerField> field = headerField::parseNext(buffer, pos, end, &pos);
|
2005-03-12 16:58:59 +00:00
|
|
|
if (field == NULL) break;
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-03-12 16:58:59 +00:00
|
|
|
m_fields.push_back(field);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
2004-12-15 20:28:09 +00:00
|
|
|
setParsedBounds(position, pos);
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
if (newPosition)
|
|
|
|
*newPosition = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void header::generate(utility::outputStream& os, const string::size_type maxLineLength,
|
|
|
|
const string::size_type /* curLinePos */, string::size_type* newLinePos) const
|
|
|
|
{
|
|
|
|
// Generate the fields
|
2005-07-12 22:28:02 +00:00
|
|
|
for (std::vector <ref <headerField> >::const_iterator it = m_fields.begin() ;
|
2004-10-21 15:05:47 +00:00
|
|
|
it != m_fields.end() ; ++it)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
(*it)->generate(os, maxLineLength);
|
|
|
|
os << CRLF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newLinePos)
|
|
|
|
*newLinePos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <component> header::clone() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <header> hdr = vmime::create <header>();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
hdr->m_fields.reserve(m_fields.size());
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
for (std::vector <ref <headerField> >::const_iterator it = m_fields.begin() ;
|
|
|
|
it != m_fields.end() ; ++it)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
hdr->m_fields.push_back((*it)->clone().dynamicCast <headerField>());
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
return (hdr);
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void header::copyFrom(const component& other)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
const header& h = dynamic_cast <const header&>(other);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
std::vector <ref <headerField> > fields;
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
fields.reserve(h.m_fields.size());
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
for (std::vector <ref <headerField> >::const_iterator it = h.m_fields.begin() ;
|
|
|
|
it != h.m_fields.end() ; ++it)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
fields.push_back((*it)->clone().dynamicCast <headerField>());
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
2005-07-12 22:28:02 +00:00
|
|
|
|
|
|
|
m_fields.clear();
|
|
|
|
m_fields.resize(fields.size());
|
|
|
|
|
|
|
|
std::copy(fields.begin(), fields.end(), m_fields.begin());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
header& header::operator=(const header& other)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
copyFrom(other);
|
|
|
|
return (*this);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const bool header::hasField(const string& fieldName) const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-12-30 20:32:05 +00:00
|
|
|
std::vector <ref <headerField> >::const_iterator pos =
|
|
|
|
std::find_if(m_fields.begin(), m_fields.end(),
|
|
|
|
fieldHasName(utility::stringUtils::toLower(fieldName)));
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-12-30 20:32:05 +00:00
|
|
|
return (pos != m_fields.end());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <headerField> header::findField(const string& fieldName) const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
// Find the first field that matches the specified name
|
2005-12-30 20:32:05 +00:00
|
|
|
std::vector <ref <headerField> >::const_iterator pos =
|
|
|
|
std::find_if(m_fields.begin(), m_fields.end(),
|
|
|
|
fieldHasName(utility::stringUtils::toLower(fieldName)));
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
// No field with this name can be found
|
2005-12-30 20:32:05 +00:00
|
|
|
if (pos == m_fields.end())
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
throw exceptions::no_such_field();
|
|
|
|
}
|
|
|
|
// Else, return a reference to the existing field
|
|
|
|
else
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
return (*pos);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
std::vector <ref <headerField> > header::findAllFields(const string& fieldName)
|
2004-10-06 11:04:00 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
std::vector <ref <headerField> > result;
|
2005-12-30 20:32:05 +00:00
|
|
|
std::back_insert_iterator <std::vector <ref <headerField> > > back(result);
|
2004-10-06 11:04:00 +00:00
|
|
|
|
2005-12-30 20:32:05 +00:00
|
|
|
std::remove_copy_if(m_fields.begin(), m_fields.end(), back,
|
|
|
|
fieldHasNotName(utility::stringUtils::toLower(fieldName)));
|
2004-10-06 11:04:00 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <headerField> header::getField(const string& fieldName)
|
2004-10-06 11:04:00 +00:00
|
|
|
{
|
2005-01-02 17:17:12 +00:00
|
|
|
const string name = utility::stringUtils::toLower(fieldName);
|
2004-10-06 11:04:00 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
// Find the first field that matches the specified name
|
2005-07-12 22:28:02 +00:00
|
|
|
std::vector <ref <headerField> >::const_iterator pos = m_fields.begin();
|
|
|
|
const std::vector <ref <headerField> >::const_iterator end = m_fields.end();
|
2004-10-06 11:04:00 +00:00
|
|
|
|
2005-01-02 17:17:12 +00:00
|
|
|
for ( ; pos != end && utility::stringUtils::toLower((*pos)->getName()) != name ; ++pos);
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
// If no field with this name can be found, create a new one
|
|
|
|
if (pos == end)
|
2004-10-06 11:04:00 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <headerField> field = headerFieldFactory::getInstance()->create(fieldName);
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
appendField(field);
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
// Return a reference to the new field
|
|
|
|
return (field);
|
2004-10-06 11:04:00 +00:00
|
|
|
}
|
2004-10-21 15:05:47 +00:00
|
|
|
// Else, return a reference to the existing field
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (*pos);
|
|
|
|
}
|
|
|
|
}
|
2004-10-06 11:04:00 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void header::appendField(ref <headerField> field)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
m_fields.push_back(field);
|
2004-10-06 11:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void header::insertFieldBefore(ref <headerField> beforeField, ref <headerField> field)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <headerField> >::iterator it = std::find
|
2004-10-21 15:05:47 +00:00
|
|
|
(m_fields.begin(), m_fields.end(), beforeField);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
if (it == m_fields.end())
|
|
|
|
throw exceptions::no_such_field();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
m_fields.insert(it, field);
|
|
|
|
}
|
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 header::insertFieldBefore(const int pos, ref <headerField> field)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
m_fields.insert(m_fields.begin() + pos, field);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void header::insertFieldAfter(ref <headerField> afterField, ref <headerField> field)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <headerField> >::iterator it = std::find
|
2004-10-21 15:05:47 +00:00
|
|
|
(m_fields.begin(), m_fields.end(), afterField);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
if (it == m_fields.end())
|
|
|
|
throw exceptions::no_such_field();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
m_fields.insert(it + 1, field);
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
void header::insertFieldAfter(const int pos, ref <headerField> field)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
m_fields.insert(m_fields.begin() + pos + 1, field);
|
|
|
|
}
|
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 header::removeField(ref <headerField> field)
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <headerField> >::iterator it = std::find
|
2004-10-21 15:05:47 +00:00
|
|
|
(m_fields.begin(), m_fields.end(), field);
|
|
|
|
|
|
|
|
if (it == m_fields.end())
|
|
|
|
throw exceptions::no_such_field();
|
|
|
|
|
|
|
|
m_fields.erase(it);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void header::removeField(const int pos)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <headerField> >::iterator it = m_fields.begin() + pos;
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
m_fields.erase(it);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void header::removeAllFields()
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
m_fields.clear();
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-13 20:57:15 +00:00
|
|
|
void header::removeAllFields(const string& fieldName)
|
|
|
|
{
|
|
|
|
std::vector <ref <headerField> > fields = findAllFields(fieldName);
|
|
|
|
|
|
|
|
for (unsigned int i = 0 ; i < fields.size() ; ++i)
|
|
|
|
removeField(fields[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const int header::getFieldCount() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
return (m_fields.size());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const bool header::isEmpty() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
return (m_fields.empty());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-05 09:47:08 +00:00
|
|
|
const ref <headerField> header::getFieldAt(const int pos)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
return (m_fields[pos]);
|
|
|
|
}
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const ref <const headerField> header::getFieldAt(const int pos) const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
return (m_fields[pos]);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <const headerField> > header::getFieldList() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
std::vector <ref <const headerField> > list;
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
list.reserve(m_fields.size());
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
for (std::vector <ref <headerField> >::const_iterator it = m_fields.begin() ;
|
2004-10-21 15:05:47 +00:00
|
|
|
it != m_fields.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
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <headerField> > header::getFieldList()
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
return (m_fields);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <const component> > header::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_fields, list);
|
|
|
|
|
|
|
|
return (list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-30 20:32:05 +00:00
|
|
|
|
|
|
|
// Field search
|
|
|
|
|
|
|
|
|
|
|
|
header::fieldHasName::fieldHasName(const string& name)
|
|
|
|
: m_name(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool header::fieldHasName::operator() (const ref <const headerField>& field)
|
|
|
|
{
|
|
|
|
return utility::stringUtils::toLower(field->getName()) == m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
header::fieldHasNotName::fieldHasNotName(const string& name)
|
|
|
|
: m_name(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool header::fieldHasNotName::operator() (const ref <const headerField>& field)
|
|
|
|
{
|
|
|
|
return utility::stringUtils::toLower(field->getName()) != m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
} // vmime
|