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/messageId.hpp"
|
|
|
|
#include "vmime/utility/random.hpp"
|
|
|
|
#include "vmime/platformDependant.hpp"
|
|
|
|
#include "vmime/parserHelpers.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace vmime
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
messageId::messageId()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messageId::messageId(const string& id)
|
|
|
|
{
|
|
|
|
parse(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messageId::messageId(const messageId& mid)
|
|
|
|
: component(), m_left(mid.m_left), m_right(mid.m_right)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messageId::messageId(const string& left, const string& right)
|
|
|
|
: m_left(left), m_right(right)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
RFC-2822:
|
|
|
|
3.6.4. Identification fields
|
|
|
|
|
|
|
|
msg-id = [CFWS] "<" id-left "@" id-right ">" [CFWS]
|
|
|
|
*/
|
|
|
|
|
|
|
|
void messageId::parse(const string& buffer, const string::size_type position,
|
|
|
|
const string::size_type end, string::size_type* newPosition)
|
|
|
|
{
|
|
|
|
const string::value_type* const pend = buffer.data() + end;
|
|
|
|
const string::value_type* const pstart = buffer.data() + position;
|
|
|
|
const string::value_type* p = pstart;
|
|
|
|
|
|
|
|
m_left.clear();
|
|
|
|
m_right.clear();
|
|
|
|
|
|
|
|
unsigned int commentLevel = 0;
|
|
|
|
bool escape = false;
|
|
|
|
bool stop = false;
|
|
|
|
|
|
|
|
for ( ; !stop && p < pend ; ++p)
|
|
|
|
{
|
|
|
|
if (escape)
|
|
|
|
{
|
|
|
|
// Ignore this character
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case '(': ++commentLevel; break;
|
|
|
|
case ')': --commentLevel; break;
|
|
|
|
case '\\': escape = true; break;
|
|
|
|
case '<':
|
|
|
|
{
|
|
|
|
if (commentLevel == 0)
|
|
|
|
{
|
|
|
|
stop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p < pend)
|
|
|
|
{
|
|
|
|
// Extract left part
|
|
|
|
const string::size_type leftStart = position + (p - pstart);
|
|
|
|
|
|
|
|
while (p < pend && *p != '@') ++p;
|
|
|
|
|
|
|
|
m_left = string(buffer.begin() + leftStart,
|
|
|
|
buffer.begin() + position + (p - pstart));
|
|
|
|
|
|
|
|
if (p < pend)
|
|
|
|
{
|
|
|
|
// Skip '@'
|
|
|
|
++p;
|
|
|
|
|
|
|
|
// Extract right part
|
|
|
|
const string::size_type rightStart = position + (p - pstart);
|
|
|
|
|
|
|
|
while (p < pend && *p != '>') ++p;
|
|
|
|
|
|
|
|
m_right = string(buffer.begin() + rightStart,
|
|
|
|
buffer.begin() + position + (p - pstart));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-15 20:28:09 +00:00
|
|
|
setParsedBounds(position, end);
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
if (newPosition)
|
|
|
|
*newPosition = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <messageId> messageId::parseNext(const string& buffer, const string::size_type position,
|
2005-03-27 13:06:45 +00:00
|
|
|
const string::size_type end, string::size_type* newPosition)
|
|
|
|
{
|
|
|
|
string::size_type pos = position;
|
|
|
|
|
|
|
|
while (pos < end && parserHelpers::isSpace(buffer[pos]))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (pos != end)
|
|
|
|
{
|
|
|
|
const string::size_type begin = pos;
|
|
|
|
|
|
|
|
while (pos < end && !parserHelpers::isSpace(buffer[pos]))
|
|
|
|
++pos;
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <messageId> mid = vmime::create <messageId>();
|
2005-03-27 13:06:45 +00:00
|
|
|
mid->parse(buffer, begin, pos, NULL);
|
|
|
|
|
|
|
|
if (newPosition != NULL)
|
|
|
|
*newPosition = pos;
|
|
|
|
|
|
|
|
return (mid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newPosition != NULL)
|
|
|
|
*newPosition = end;
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const string messageId::getId() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
return (m_left + '@' + m_right);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-27 13:06:45 +00:00
|
|
|
void messageId::generate(utility::outputStream& os, const string::size_type maxLineLength,
|
2004-10-05 10:28:21 +00:00
|
|
|
const string::size_type curLinePos, string::size_type* newLinePos) const
|
|
|
|
{
|
2005-03-27 13:06:45 +00:00
|
|
|
string::size_type pos = curLinePos;
|
|
|
|
|
|
|
|
if (curLinePos + m_left.length() + m_right.length() + 3 > maxLineLength)
|
|
|
|
{
|
|
|
|
os << NEW_LINE_SEQUENCE;
|
|
|
|
pos = NEW_LINE_SEQUENCE_LENGTH;
|
|
|
|
}
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
os << '<' << m_left << '@' << m_right << '>';
|
|
|
|
|
|
|
|
if (newLinePos)
|
2005-03-27 13:06:45 +00:00
|
|
|
*newLinePos = pos + m_left.length() + m_right.length() + 3;
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messageId& messageId::operator=(const string& id)
|
|
|
|
{
|
|
|
|
parse(id);
|
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messageId messageId::generateId()
|
|
|
|
{
|
|
|
|
std::ostringstream left;
|
|
|
|
|
|
|
|
left << "vmime";
|
|
|
|
left << '.';
|
2004-10-21 15:05:47 +00:00
|
|
|
left << std::hex << utility::random::getTime();
|
2004-10-05 10:28:21 +00:00
|
|
|
left << '.';
|
2004-10-21 15:05:47 +00:00
|
|
|
left << std::hex << utility::random::getProcess();
|
2004-10-05 10:28:21 +00:00
|
|
|
left << '.';
|
2004-10-21 15:05:47 +00:00
|
|
|
left << std::hex << utility::random::getNext();
|
|
|
|
left << std::hex << utility::random::getNext();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
return (messageId(left.str(), platformDependant::getHandler()->getHostName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const bool messageId::operator==(const messageId& mid) const
|
|
|
|
{
|
|
|
|
return (m_left == mid.m_left && m_right == mid.m_right);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const bool messageId::operator!=(const messageId& mid) const
|
|
|
|
{
|
|
|
|
return !(*this == mid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
ref <component> messageId::clone() const
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
return vmime::create <messageId>(*this);
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void messageId::copyFrom(const component& other)
|
|
|
|
{
|
|
|
|
const messageId& mid = dynamic_cast <const messageId&>(other);
|
|
|
|
|
|
|
|
m_left = mid.m_left;
|
|
|
|
m_right = mid.m_right;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messageId& messageId::operator=(const messageId& other)
|
|
|
|
{
|
|
|
|
copyFrom(other);
|
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const string& messageId::getLeft() const
|
|
|
|
{
|
|
|
|
return (m_left);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void messageId::setLeft(const string& left)
|
|
|
|
{
|
|
|
|
m_left = left;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const string& messageId::getRight() const
|
|
|
|
{
|
|
|
|
return (m_right);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void messageId::setRight(const string& right)
|
|
|
|
{
|
|
|
|
m_right = right;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-12 22:28:02 +00:00
|
|
|
const std::vector <ref <const component> > messageId::getChildComponents() const
|
2004-12-20 12:33:55 +00:00
|
|
|
{
|
2005-07-12 22:28:02 +00:00
|
|
|
return std::vector <ref <const component> >();
|
2004-12-20 12:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
} // vmime
|