2005-08-23 19:11:19 +00:00
|
|
|
//
|
|
|
|
// VMime library (http://www.vmime.org)
|
2013-01-10 16:30:31 +00:00
|
|
|
// Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
|
2005-08-23 19:11:19 +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
|
2009-09-06 12:02:10 +00:00
|
|
|
// published by the Free Software Foundation; either version 3 of
|
2005-08-23 19:11:19 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2007-03-27 12:42:53 +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.
|
2005-08-23 19:11:19 +00:00
|
|
|
//
|
|
|
|
|
2012-11-01 17:20:06 +00:00
|
|
|
#include "vmime/config.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#if VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_IMAP
|
|
|
|
|
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
#include "vmime/net/imap/IMAPStore.hpp"
|
|
|
|
#include "vmime/net/imap/IMAPFolder.hpp"
|
|
|
|
#include "vmime/net/imap/IMAPConnection.hpp"
|
|
|
|
|
|
|
|
#include "vmime/exception.hpp"
|
2006-10-11 14:52:41 +00:00
|
|
|
#include "vmime/platform.hpp"
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
|
|
namespace vmime {
|
|
|
|
namespace net {
|
|
|
|
namespace imap {
|
|
|
|
|
|
|
|
|
2005-10-03 21:29:04 +00:00
|
|
|
IMAPStore::IMAPStore(ref <session> sess, ref <security::authenticator> auth, const bool secured)
|
2006-01-29 17:36:34 +00:00
|
|
|
: store(sess, getInfosInstance(), auth), m_connection(NULL), m_isIMAPS(secured)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
2005-09-17 09:08:45 +00:00
|
|
|
}
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
|
2005-09-17 09:08:45 +00:00
|
|
|
IMAPStore::~IMAPStore()
|
|
|
|
{
|
|
|
|
try
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
2005-09-17 09:08:45 +00:00
|
|
|
if (isConnected())
|
|
|
|
disconnect();
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
2005-09-17 09:08:45 +00:00
|
|
|
catch (vmime::exception&)
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
2005-09-17 09:08:45 +00:00
|
|
|
// Ignore
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const string IMAPStore::getProtocolName() const
|
|
|
|
{
|
|
|
|
return "imap";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ref <folder> IMAPStore::getRootFolder()
|
|
|
|
{
|
|
|
|
if (!isConnected())
|
|
|
|
throw exceptions::illegal_state("Not connected");
|
|
|
|
|
2006-03-29 20:06:39 +00:00
|
|
|
return vmime::create <IMAPFolder>(folder::path(),
|
|
|
|
thisRef().dynamicCast <IMAPStore>());
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ref <folder> IMAPStore::getDefaultFolder()
|
|
|
|
{
|
|
|
|
if (!isConnected())
|
|
|
|
throw exceptions::illegal_state("Not connected");
|
|
|
|
|
2006-03-29 20:06:39 +00:00
|
|
|
return vmime::create <IMAPFolder>(folder::path::component("INBOX"),
|
|
|
|
thisRef().dynamicCast <IMAPStore>());
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ref <folder> IMAPStore::getFolder(const folder::path& path)
|
|
|
|
{
|
|
|
|
if (!isConnected())
|
|
|
|
throw exceptions::illegal_state("Not connected");
|
|
|
|
|
2006-03-29 20:06:39 +00:00
|
|
|
return vmime::create <IMAPFolder>(path, thisRef().dynamicCast <IMAPStore>());
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool IMAPStore::isValidFolderName(const folder::path::component& /* name */) const
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IMAPStore::connect()
|
|
|
|
{
|
|
|
|
if (isConnected())
|
|
|
|
throw exceptions::already_connected();
|
|
|
|
|
|
|
|
m_connection = vmime::create <IMAPConnection>
|
2006-03-29 20:06:39 +00:00
|
|
|
(thisRef().dynamicCast <IMAPStore>(), getAuthenticator());
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_connection->connect();
|
|
|
|
}
|
|
|
|
catch (std::exception&)
|
|
|
|
{
|
|
|
|
m_connection = NULL;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool IMAPStore::isConnected() const
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
|
|
|
return (m_connection && m_connection->isConnected());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool IMAPStore::isIMAPS() const
|
2006-01-29 17:36:34 +00:00
|
|
|
{
|
|
|
|
return m_isIMAPS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
bool IMAPStore::isSecuredConnection() const
|
2006-01-29 17:36:34 +00:00
|
|
|
{
|
|
|
|
if (m_connection == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return m_connection->isSecuredConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ref <connectionInfos> IMAPStore::getConnectionInfos() const
|
|
|
|
{
|
|
|
|
if (m_connection == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return m_connection->getConnectionInfos();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-16 09:07:16 +00:00
|
|
|
ref <IMAPConnection> IMAPStore::getConnection()
|
|
|
|
{
|
|
|
|
return m_connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-23 19:11:19 +00:00
|
|
|
void IMAPStore::disconnect()
|
|
|
|
{
|
|
|
|
if (!isConnected())
|
|
|
|
throw exceptions::not_connected();
|
|
|
|
|
|
|
|
for (std::list <IMAPFolder*>::iterator it = m_folders.begin() ;
|
|
|
|
it != m_folders.end() ; ++it)
|
|
|
|
{
|
|
|
|
(*it)->onStoreDisconnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_folders.clear();
|
|
|
|
|
|
|
|
|
|
|
|
m_connection->disconnect();
|
|
|
|
|
|
|
|
m_connection = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IMAPStore::noop()
|
|
|
|
{
|
|
|
|
if (!isConnected())
|
|
|
|
throw exceptions::not_connected();
|
|
|
|
|
|
|
|
m_connection->send(true, "NOOP", true);
|
|
|
|
|
|
|
|
utility::auto_ptr <IMAPParser::response> resp(m_connection->readResponse());
|
|
|
|
|
|
|
|
if (resp->isBad() || resp->response_done()->response_tagged()->
|
|
|
|
resp_cond_state()->status() != IMAPParser::resp_cond_state::OK)
|
|
|
|
{
|
2013-07-17 10:46:50 +00:00
|
|
|
throw exceptions::command_error("NOOP", resp->getErrorLog());
|
2013-07-16 09:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (std::list <IMAPFolder*>::iterator it = m_folders.begin() ;
|
|
|
|
it != m_folders.end() ; ++it)
|
|
|
|
{
|
2013-07-16 20:04:56 +00:00
|
|
|
if ((*it)->isOpen())
|
|
|
|
(*it)->noop();
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ref <IMAPConnection> IMAPStore::connection()
|
|
|
|
{
|
|
|
|
return (m_connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IMAPStore::registerFolder(IMAPFolder* folder)
|
|
|
|
{
|
|
|
|
m_folders.push_back(folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IMAPStore::unregisterFolder(IMAPFolder* folder)
|
|
|
|
{
|
|
|
|
std::list <IMAPFolder*>::iterator it = std::find(m_folders.begin(), m_folders.end(), folder);
|
|
|
|
if (it != m_folders.end()) m_folders.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-12 10:05:28 +00:00
|
|
|
int IMAPStore::getCapabilities() const
|
2005-08-23 19:11:19 +00:00
|
|
|
{
|
|
|
|
return (CAPABILITY_CREATE_FOLDER |
|
|
|
|
CAPABILITY_RENAME_FOLDER |
|
|
|
|
CAPABILITY_ADD_MESSAGE |
|
|
|
|
CAPABILITY_COPY_MESSAGE |
|
|
|
|
CAPABILITY_DELETE_MESSAGE |
|
|
|
|
CAPABILITY_PARTIAL_FETCH |
|
|
|
|
CAPABILITY_MESSAGE_FLAGS |
|
|
|
|
CAPABILITY_EXTRACT_PART);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Service infos
|
|
|
|
|
2005-10-03 21:29:04 +00:00
|
|
|
IMAPServiceInfos IMAPStore::sm_infos(false);
|
2005-08-23 19:11:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
const serviceInfos& IMAPStore::getInfosInstance()
|
|
|
|
{
|
2005-10-03 21:29:04 +00:00
|
|
|
return sm_infos;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const serviceInfos& IMAPStore::getInfos() const
|
|
|
|
{
|
2005-10-03 21:29:04 +00:00
|
|
|
return sm_infos;
|
2005-08-23 19:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // imap
|
|
|
|
} // net
|
|
|
|
} // vmime
|
2012-11-01 17:20:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endif // VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_IMAP
|
|
|
|
|