Fixed special value 'last' in message sets.

This commit is contained in:
Vincent Richard 2016-03-24 20:34:27 +01:00
parent 14011ca29a
commit 12781598bd
13 changed files with 209 additions and 99 deletions

View File

@ -542,79 +542,75 @@ std::vector <shared_ptr <message> > IMAPFolder::getMessages(const messageSet& ms
std::vector <shared_ptr <message> > messages;
if (msgs.isNumberSet())
// Sequence number message set:
// C: . FETCH uuuu1,uuuu2,uuuu3 UID
// S: * nnnn1 FETCH (UID uuuu1)
// S: * nnnn2 FETCH (UID uuuu2)
// S: * nnnn3 FETCH (UID uuuu3)
// S: . OK FETCH completed
// UID message set:
// C: . UID FETCH uuuu1,uuuu2,uuuu3 UID
// S: * nnnn1 FETCH (UID uuuu1)
// S: * nnnn2 FETCH (UID uuuu2)
// S: * nnnn3 FETCH (UID uuuu3)
// S: . OK UID FETCH completed
std::vector <string> params;
params.push_back("UID");
IMAPCommand::FETCH(msgs, params)->send(m_connection);
// Get the response
scoped_ptr <IMAPParser::response> resp(m_connection->readResponse());
if (resp->isBad() || resp->response_done()->response_tagged()->
resp_cond_state()->status() != IMAPParser::resp_cond_state::OK)
{
const std::vector <size_t> numbers = IMAPUtils::messageSetToNumberList(msgs);
shared_ptr <IMAPFolder> thisFolder = dynamicCast <IMAPFolder>(shared_from_this());
for (std::vector <size_t>::const_iterator it = numbers.begin() ; it != numbers.end() ; ++it)
messages.push_back(make_shared <IMAPMessage>(thisFolder, *it));
throw exceptions::command_error("UID FETCH ... UID", resp->getErrorLog(), "bad response");
}
else if (msgs.isUIDSet())
// Process the response
const std::vector <IMAPParser::continue_req_or_response_data*>& respDataList =
resp->continue_req_or_response_data();
for (std::vector <IMAPParser::continue_req_or_response_data*>::const_iterator
it = respDataList.begin() ; it != respDataList.end() ; ++it)
{
// C: . UID FETCH uuuu1,uuuu2,uuuu3 UID
// S: * nnnn1 FETCH (UID uuuu1)
// S: * nnnn2 FETCH (UID uuuu2)
// S: * nnnn3 FETCH (UID uuuu3)
// S: . OK UID FETCH completed
std::vector <string> params;
params.push_back("UID");
IMAPCommand::FETCH(msgs, params)->send(m_connection);
// Get the response
scoped_ptr <IMAPParser::response> resp(m_connection->readResponse());
if (resp->isBad() || resp->response_done()->response_tagged()->
resp_cond_state()->status() != IMAPParser::resp_cond_state::OK)
if ((*it)->response_data() == NULL)
{
throw exceptions::command_error("UID FETCH ... UID", resp->getErrorLog(), "bad response");
throw exceptions::command_error("UID FETCH ... UID",
resp->getErrorLog(), "invalid response");
}
// Process the response
const std::vector <IMAPParser::continue_req_or_response_data*>& respDataList =
resp->continue_req_or_response_data();
const IMAPParser::message_data* messageData =
(*it)->response_data()->message_data();
for (std::vector <IMAPParser::continue_req_or_response_data*>::const_iterator
it = respDataList.begin() ; it != respDataList.end() ; ++it)
// We are only interested in responses of type "FETCH"
if (messageData == NULL || messageData->type() != IMAPParser::message_data::FETCH)
continue;
// Get Process fetch response for this message
const size_t msgNum = messageData->number();
message::uid msgUID;
// Find UID in message attributes
const std::vector <IMAPParser::msg_att_item*> atts = messageData->msg_att()->items();
for (std::vector <IMAPParser::msg_att_item*>::const_iterator
it = atts.begin() ; it != atts.end() ; ++it)
{
if ((*it)->response_data() == NULL)
if ((*it)->type() == IMAPParser::msg_att_item::UID)
{
throw exceptions::command_error("UID FETCH ... UID",
resp->getErrorLog(), "invalid response");
msgUID = (*it)->unique_id()->value();
break;
}
}
const IMAPParser::message_data* messageData =
(*it)->response_data()->message_data();
// We are only interested in responses of type "FETCH"
if (messageData == NULL || messageData->type() != IMAPParser::message_data::FETCH)
continue;
// Get Process fetch response for this message
const size_t msgNum = messageData->number();
message::uid msgUID;
// Find UID in message attributes
const std::vector <IMAPParser::msg_att_item*> atts = messageData->msg_att()->items();
for (std::vector <IMAPParser::msg_att_item*>::const_iterator
it = atts.begin() ; it != atts.end() ; ++it)
{
if ((*it)->type() == IMAPParser::msg_att_item::UID)
{
msgUID = (*it)->unique_id()->value();
break;
}
}
if (!msgUID.empty())
{
shared_ptr <IMAPFolder> thisFolder = dynamicCast <IMAPFolder>(shared_from_this());
messages.push_back(make_shared <IMAPMessage>(thisFolder, msgNum, msgUID));
}
if (!msgUID.empty())
{
shared_ptr <IMAPFolder> thisFolder = dynamicCast <IMAPFolder>(shared_from_this());
messages.push_back(make_shared <IMAPMessage>(thisFolder, msgNum, msgUID));
}
}

View File

@ -703,6 +703,8 @@ public:
if (range.getFirst() == range.getLast())
m_oss << range.getFirst();
else if (range.getLast() == size_t(-1))
m_oss << range.getFirst() << ":*";
else
m_oss << range.getFirst() << ":" << range.getLast();
@ -716,6 +718,8 @@ public:
if (range.getFirst() == range.getLast())
m_oss << range.getFirst();
else if (range.getLast() == size_t(-1))
m_oss << range.getFirst() << ":*";
else
m_oss << range.getFirst() << ":" << range.getLast();
@ -771,16 +775,6 @@ const string IMAPUtils::messageSetToSequenceSet(const messageSet& msgs)
}
// static
const std::vector <size_t> IMAPUtils::messageSetToNumberList(const messageSet& msgs)
{
IMAPMessageSetEnumerator en;
msgs.enumerate(en);
return en.list();
}
// static
messageSet IMAPUtils::buildMessageSet(const IMAPParser::uid_set* uidSet)
{

View File

@ -114,13 +114,6 @@ public:
*/
static const string messageSetToSequenceSet(const messageSet& msgs);
/** Returns a list of message sequence numbers given a message set.
*
* @param msgs message set
* @return list of message numbers
*/
static const std::vector <size_t> messageSetToNumberList(const messageSet& msgs);
/** Constructs a message set from a parser 'uid_set' structure.
*
* @param uidSet UID set, as returned by the parser

View File

@ -427,7 +427,7 @@ std::vector <shared_ptr <message> > maildirFolder::getMessages(const messageSet&
if (msgs.isNumberSet())
{
const std::vector <size_t> numbers = maildirUtils::messageSetToNumberList(msgs);
const std::vector <size_t> numbers = maildirUtils::messageSetToNumberList(msgs, m_messageCount);
std::vector <shared_ptr <message> > messages;
shared_ptr <maildirFolder> thisFolder = dynamicCast <maildirFolder>(shared_from_this());
@ -595,7 +595,7 @@ void maildirFolder::setMessageFlags
if (msgs.isNumberSet())
{
const std::vector <size_t> nums = maildirUtils::messageSetToNumberList(msgs);
const std::vector <size_t> nums = maildirUtils::messageSetToNumberList(msgs, m_messageCount);
// Change message flags
shared_ptr <utility::fileSystemFactory> fsf = platform::getHandler()->getFileSystemFactory();
@ -950,7 +950,7 @@ messageSet maildirFolder::copyMessages(const folder::path& dest, const messageSe
}
// Copy messages
const std::vector <size_t> nums = maildirUtils::messageSetToNumberList(msgs);
const std::vector <size_t> nums = maildirUtils::messageSetToNumberList(msgs, m_messageCount);
try
{
@ -1013,6 +1013,8 @@ void maildirFolder::status(size_t& count, size_t& unseen)
count = status->getMessageCount();
unseen = status->getUnseenCount();
m_messageCount = count;
}

View File

@ -218,9 +218,18 @@ class maildirMessageSetEnumerator : public messageSetEnumerator
{
public:
maildirMessageSetEnumerator(const size_t msgCount)
: m_msgCount(msgCount)
{
}
void enumerateNumberMessageRange(const vmime::net::numberMessageRange& range)
{
for (size_t i = range.getFirst(), last = range.getLast() ; i <= last ; ++i)
size_t last = range.getLast();
if (last == size_t(-1)) last = m_msgCount;
for (size_t i = range.getFirst() ; i <= last ; ++i)
list.push_back(i);
}
@ -232,13 +241,17 @@ public:
public:
std::vector <size_t> list;
private:
size_t m_msgCount;
};
// static
const std::vector <size_t> maildirUtils::messageSetToNumberList(const messageSet& msgs)
const std::vector <size_t> maildirUtils::messageSetToNumberList(const messageSet& msgs, const size_t msgCount)
{
maildirMessageSetEnumerator en;
maildirMessageSetEnumerator en(msgCount);
msgs.enumerate(en);
return en.list;

View File

@ -135,9 +135,10 @@ public:
/** Returns a list of message numbers given a message set.
*
* @param msgs message set
* @param msgCount number of messages in folder
* @return list of message numbers
*/
static const std::vector <size_t> messageSetToNumberList(const messageSet& msgs);
static const std::vector <size_t> messageSetToNumberList(const messageSet& msgs, const size_t msgCount);
};

View File

@ -63,9 +63,9 @@ numberMessageRange::numberMessageRange(const size_t number)
numberMessageRange::numberMessageRange(const size_t first, const size_t last)
: m_first(first), m_last(last)
{
if (first < 1)
if (first < 1 || first == static_cast <size_t>(-1))
throw std::invalid_argument("first");
else if (last != -1U && last < first)
else if (last != static_cast <size_t>(-1) && last < first)
throw std::invalid_argument("last");
}
@ -208,7 +208,7 @@ messageSet messageSet::byNumber(const std::vector <size_t>& numbers)
std::sort(sortedNumbers.begin(), sortedNumbers.end());
// Build the set by detecting ranges of continuous numbers
size_t previous = -1U, rangeStart = -1U;
size_t previous = static_cast <size_t>(-1), rangeStart = static_cast <size_t>(-1);
messageSet set;
for (std::vector <size_t>::const_iterator it = sortedNumbers.begin() ;
@ -219,7 +219,10 @@ messageSet messageSet::byNumber(const std::vector <size_t>& numbers)
if (current == previous)
continue; // skip duplicates
if (previous == -1U)
if (current == static_cast <size_t>(-1))
throw std::invalid_argument("numbers");
if (previous == static_cast <size_t>(-1))
{
previous = current;
rangeStart = current;
@ -302,7 +305,7 @@ messageSet messageSet::byUID(const std::vector <message::uid>& uids)
std::sort(sortedUIDs.begin(), sortedUIDs.end());
// Build the set by detecting ranges of continuous numbers
vmime_uint32 previous = -1U, rangeStart = -1U;
vmime_uint32 previous = static_cast <vmime_uint32>(-1), rangeStart = static_cast <vmime_uint32>(-1);
messageSet set;
for (std::vector <vmime_uint32>::const_iterator it = sortedUIDs.begin() ;
@ -313,7 +316,7 @@ messageSet messageSet::byUID(const std::vector <message::uid>& uids)
if (current == previous)
continue; // skip duplicates
if (previous == -1U)
if (previous == static_cast <vmime_uint32>(-1))
{
previous = current;
rangeStart = current;

View File

@ -245,7 +245,7 @@ std::vector <shared_ptr <message> > POP3Folder::getMessages(const messageSet& ms
if (msgs.isNumberSet())
{
const std::vector <size_t> numbers = POP3Utils::messageSetToNumberList(msgs);
const std::vector <size_t> numbers = POP3Utils::messageSetToNumberList(msgs, m_messageCount);
std::vector <shared_ptr <message> > messages;
shared_ptr <POP3Folder> thisFolder(dynamicCast <POP3Folder>(shared_from_this()));
@ -558,7 +558,7 @@ void POP3Folder::deleteMessages(const messageSet& msgs)
{
shared_ptr <POP3Store> store = m_store.lock();
const std::vector <size_t> nums = POP3Utils::messageSetToNumberList(msgs);
const std::vector <size_t> nums = POP3Utils::messageSetToNumberList(msgs, m_messageCount);
if (nums.empty())
throw exceptions::invalid_argument();
@ -653,6 +653,8 @@ void POP3Folder::status(size_t& count, size_t& unseen)
count = status->getMessageCount();
unseen = status->getUnseenCount();
m_messageCount = count;
}

View File

@ -78,9 +78,18 @@ class POP3MessageSetEnumerator : public messageSetEnumerator
{
public:
POP3MessageSetEnumerator(const size_t msgCount)
: m_msgCount(msgCount)
{
}
void enumerateNumberMessageRange(const vmime::net::numberMessageRange& range)
{
for (size_t i = range.getFirst(), last = range.getLast() ; i <= last ; ++i)
size_t last = range.getLast();
if (last == size_t(-1)) last = m_msgCount;
for (size_t i = range.getFirst() ; i <= last ; ++i)
list.push_back(i);
}
@ -92,13 +101,17 @@ public:
public:
std::vector <size_t> list;
private:
size_t m_msgCount;
};
// static
const std::vector <size_t> POP3Utils::messageSetToNumberList(const messageSet& msgs)
const std::vector <size_t> POP3Utils::messageSetToNumberList(const messageSet& msgs, const size_t msgCount)
{
POP3MessageSetEnumerator en;
POP3MessageSetEnumerator en(msgCount);
msgs.enumerate(en);
return en.list;

View File

@ -69,9 +69,10 @@ public:
/** Returns a list of message numbers given a message set.
*
* @param msgs message set
* @param msgCount number of messages in folder
* @return list of message numbers
*/
static const std::vector <size_t> messageSetToNumberList(const messageSet& msgs);
static const std::vector <size_t> messageSetToNumberList(const messageSet& msgs, const size_t msgCount);
};

View File

@ -0,0 +1,53 @@
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
//
// 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 3 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.
//
#include "tests/testUtils.hpp"
#include "vmime/net/maildir/maildirUtils.hpp"
using namespace vmime::net::maildir;
VMIME_TEST_SUITE_BEGIN(maildirUtilsTest)
VMIME_TEST_LIST_BEGIN
VMIME_TEST(testMessageSetToNumberList)
VMIME_TEST_LIST_END
void testMessageSetToNumberList()
{
const std::vector <size_t> msgNums =
maildirUtils::messageSetToNumberList
(vmime::net::messageSet::byNumber(5, -1), /* msgCount */ 8);
VASSERT_EQ("Count", 4, msgNums.size());
VASSERT_EQ("1", 5, msgNums[0]);
VASSERT_EQ("2", 6, msgNums[1]);
VASSERT_EQ("3", 7, msgNums[2]);
VASSERT_EQ("4", 8, msgNums[3]);
}
VMIME_TEST_SUITE_END

View File

@ -31,6 +31,8 @@ VMIME_TEST_SUITE_BEGIN(messageSetTest)
VMIME_TEST_LIST_BEGIN
VMIME_TEST(testNumberSet_Single)
VMIME_TEST(testNumberSet_Range)
VMIME_TEST(testNumberSet_InvalidRange)
VMIME_TEST(testNumberSet_InvalidFirst)
VMIME_TEST(testNumberSet_InfiniteRange)
VMIME_TEST(testNumberSet_Multiple)
VMIME_TEST(testUIDSet_Single)
@ -40,6 +42,7 @@ VMIME_TEST_SUITE_BEGIN(messageSetTest)
VMIME_TEST(testUIDSet_MultipleNonNumeric)
VMIME_TEST(testIsNumberSet)
VMIME_TEST(testIsUIDSet)
VMIME_TEST(testMixedRanges)
VMIME_TEST_LIST_END
@ -59,6 +62,8 @@ VMIME_TEST_SUITE_BEGIN(messageSetTest)
if (range.getFirst() == range.getLast())
m_oss << range.getFirst();
else if (range.getLast() == size_t(-1))
m_oss << range.getFirst() << ":(LAST)";
else
m_oss << range.getFirst() << ":" << range.getLast();
@ -72,6 +77,8 @@ VMIME_TEST_SUITE_BEGIN(messageSetTest)
if (range.getFirst() == range.getLast())
m_oss << range.getFirst();
else if (range.getLast() == size_t(-1))
m_oss << range.getFirst() << ":(LAST)";
else
m_oss << range.getFirst() << ":" << range.getLast();
@ -109,9 +116,19 @@ VMIME_TEST_SUITE_BEGIN(messageSetTest)
VASSERT_EQ("str", "42:100", enumerateAsString(vmime::net::messageSet::byNumber(42, 100)));
}
void testNumberSet_InvalidRange()
{
VASSERT_THROW("first > last", vmime::net::messageSet::byNumber(100, 42), std::invalid_argument);
}
void testNumberSet_InvalidFirst()
{
VASSERT_THROW("first == -1", vmime::net::messageSet::byNumber(-1, 42), std::invalid_argument);
}
void testNumberSet_InfiniteRange()
{
VASSERT_EQ("str", "42:-1", enumerateAsString(vmime::net::messageSet::byNumber(42, -1)));
VASSERT_EQ("str", "42:(LAST)", enumerateAsString(vmime::net::messageSet::byNumber(42, -1)));
}
void testNumberSet_Multiple()
@ -197,4 +214,12 @@ VMIME_TEST_SUITE_BEGIN(messageSetTest)
VASSERT_TRUE("uid2", vmime::net::messageSet::byUID("42", "*").isUIDSet());
}
void testMixedRanges()
{
vmime::net::messageSet set = vmime::net::messageSet::byNumber(1, 5);
set.addRange(vmime::net::numberMessageRange(6, 8));
VASSERT_THROW("mixed ranges", set.addRange(vmime::net::UIDMessageRange("123")), std::invalid_argument);
}
VMIME_TEST_SUITE_END

View File

@ -36,6 +36,7 @@ VMIME_TEST_SUITE_BEGIN(POP3UtilsTest)
VMIME_TEST_LIST_BEGIN
VMIME_TEST(testParseMultiListOrUidlResponse)
VMIME_TEST(testMessageSetToNumberList)
VMIME_TEST_LIST_END
@ -69,5 +70,18 @@ VMIME_TEST_SUITE_BEGIN(POP3UtilsTest)
VASSERT_EQ("5 (with extra space)", "yz", result[8]);
}
void testMessageSetToNumberList()
{
const std::vector <size_t> msgNums =
POP3Utils::messageSetToNumberList
(vmime::net::messageSet::byNumber(5, -1), /* msgCount */ 8);
VASSERT_EQ("Count", 4, msgNums.size());
VASSERT_EQ("1", 5, msgNums[0]);
VASSERT_EQ("2", 6, msgNums[1]);
VASSERT_EQ("3", 7, msgNums[2]);
VASSERT_EQ("4", 8, msgNums[3]);
}
VMIME_TEST_SUITE_END