diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp index 08001d94..ce379deb 100644 --- a/examples/imapresource/imapserverproxy.cpp +++ b/examples/imapresource/imapserverproxy.cpp @@ -1,695 +1,703 @@ /* * Copyright (C) 2015 Christian Mollekopf * * 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. */ #include "imapserverproxy.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include #include #include "log.h" #include "test.h" using namespace Imap; const char* Imap::Flags::Seen = "\\Seen"; const char* Imap::Flags::Deleted = "\\Deleted"; const char* Imap::Flags::Answered = "\\Answered"; const char* Imap::Flags::Flagged = "\\Flagged"; const char* Imap::FolderFlags::Noselect = "\\Noselect"; const char* Imap::FolderFlags::Noinferiors = "\\Noinferiors"; const char* Imap::FolderFlags::Marked = "\\Marked"; const char* Imap::FolderFlags::Unmarked = "\\Unmarked"; const char* Imap::FolderFlags::Subscribed = "\\Subscribed"; //Special use const char* Imap::FolderFlags::Sent = "\\Sent"; const char* Imap::FolderFlags::Trash = "\\Trash"; const char* Imap::FolderFlags::Archive = "\\Archive"; const char* Imap::FolderFlags::Junk = "\\Junk"; const char* Imap::FolderFlags::Flagged = "\\Flagged"; const char* Imap::FolderFlags::Drafts = "\\Drafts"; const char* Imap::Capabilities::Namespace = "NAMESPACE"; const char* Imap::Capabilities::Uidplus = "UIDPLUS"; const char* Imap::Capabilities::Condstore = "CONDSTORE"; static int translateImapError(KJob *job) { const int error = job->error(); const bool isLoginJob = dynamic_cast(job); const bool isSelectJob = dynamic_cast(job); switch (error) { case KIMAP2::LoginJob::ErrorCode::ERR_HOST_NOT_FOUND: return Imap::HostNotFoundError; case KIMAP2::LoginJob::ErrorCode::ERR_COULD_NOT_CONNECT: return Imap::CouldNotConnectError; case KIMAP2::LoginJob::ErrorCode::ERR_SSL_HANDSHAKE_FAILED: return Imap::SslHandshakeError; } //Hack to detect login failures if (isLoginJob) { return Imap::LoginFailed; } //Hack to detect selection errors if (isSelectJob) { return Imap::SelectFailed; } //Hack to detect connection lost if (error == KJob::UserDefinedError) { return Imap::ConnectionLost; } return Imap::UnknownError; } template static KAsync::Job runJob(KJob *job, const std::function &f) { return KAsync::start([job, f](KAsync::Future &future) { QObject::connect(job, &KJob::result, [&future, f](KJob *job) { SinkTrace() << "Job done: " << job->metaObject()->className(); if (job->error()) { SinkWarning() << "Job failed: " << job->errorString() << job->metaObject()->className() << job->error(); auto proxyError = translateImapError(job); future.setError(proxyError, job->errorString()); } else { future.setValue(f(job)); future.setFinished(); } }); SinkTrace() << "Starting job: " << job->metaObject()->className(); job->start(); }); } static KAsync::Job runJob(KJob *job) { return KAsync::start([job](KAsync::Future &future) { QObject::connect(job, &KJob::result, [&future](KJob *job) { SinkTrace() << "Job done: " << job->metaObject()->className(); if (job->error()) { SinkWarning() << "Job failed: " << job->errorString() << job->metaObject()->className() << job->error(); auto proxyError = translateImapError(job); future.setError(proxyError, job->errorString()); } else { future.setFinished(); } }); SinkTrace() << "Starting job: " << job->metaObject()->className(); job->start(); }); } KIMAP2::Session *createNewSession(const QString &serverUrl, int port) { auto newSession = new KIMAP2::Session(serverUrl, qint16(port)); if (Sink::Test::testModeEnabled()) { newSession->setTimeout(1); } else { newSession->setTimeout(40); } QObject::connect(newSession, &KIMAP2::Session::sslErrors, [=](const QList &errors) { SinkLog() << "Received ssl error: " << errors; newSession->ignoreErrors(errors); }); return newSession; } ImapServerProxy::ImapServerProxy(const QString &serverUrl, int port, EncryptionMode encryptionMode, SessionCache *sessionCache) : mSessionCache(sessionCache), mSession(nullptr), mEncryptionMode(encryptionMode) { if (!mSessionCache || mSessionCache->isEmpty()) { mSession = createNewSession(serverUrl, port); } } QDebug operator<<(QDebug debug, const KIMAP2::MailBoxDescriptor &c) { QDebugStateSaver saver(debug); debug.nospace() << c.name; return debug; } KAsync::Job ImapServerProxy::login(const QString &username, const QString &password) { if (password.isEmpty()) { return KAsync::error(Imap::MissingCredentialsError); } if (mSessionCache) { auto session = mSessionCache->getSession(); if (session.isValid()) { mSession = session.mSession; mCapabilities = session.mCapabilities; mNamespaces = session.mNamespaces; } } Q_ASSERT(mSession); if (mSession->state() == KIMAP2::Session::Authenticated || mSession->state() == KIMAP2::Session::Selected) { SinkLog() << "Reusing existing session."; return KAsync::null(); } auto loginJob = new KIMAP2::LoginJob(mSession); loginJob->setUserName(username); loginJob->setPassword(password); if (mEncryptionMode == Starttls) { loginJob->setEncryptionMode(QSsl::TlsV1_0OrLater, true); } else if (mEncryptionMode == Tls) { loginJob->setEncryptionMode(QSsl::AnyProtocol, false); } loginJob->setAuthenticationMode(KIMAP2::LoginJob::Plain); auto capabilitiesJob = new KIMAP2::CapabilitiesJob(mSession); QObject::connect(capabilitiesJob, &KIMAP2::CapabilitiesJob::capabilitiesReceived, &mGuard, [this](const QStringList &capabilities) { mCapabilities = capabilities; }); auto namespaceJob = new KIMAP2::NamespaceJob(mSession); return runJob(loginJob).then(runJob(capabilitiesJob)).then([this](){ SinkTrace() << "Supported capabilities: " << mCapabilities; QStringList requiredExtensions = QStringList() << Capabilities::Uidplus << Capabilities::Namespace; for (const auto &requiredExtension : requiredExtensions) { if (!mCapabilities.contains(requiredExtension)) { SinkWarning() << "Server doesn't support required capability: " << requiredExtension; //TODO fail the job } } }).then(runJob(namespaceJob)).then([this, namespaceJob] { mNamespaces.personal = namespaceJob->personalNamespaces(); mNamespaces.shared = namespaceJob->sharedNamespaces(); mNamespaces.user = namespaceJob->userNamespaces(); // SinkTrace() << "Found personal namespaces: " << mNamespaces.personal; // SinkTrace() << "Found shared namespaces: " << mNamespaces.shared; // SinkTrace() << "Found user namespaces: " << mNamespaces.user; }); } KAsync::Job ImapServerProxy::logout() { if (mSessionCache) { auto session = CachedSession{mSession, mCapabilities, mNamespaces}; if (session.isConnected()) { mSessionCache->recycleSession(session); return KAsync::null(); } } if (mSession->state() == KIMAP2::Session::State::Authenticated || mSession->state() == KIMAP2::Session::State::Selected) { return runJob(new KIMAP2::LogoutJob(mSession)); } else { return KAsync::null(); } } bool ImapServerProxy::isGmail() const { //Magic capability that only gmail has return mCapabilities.contains("X-GM-EXT-1"); } KAsync::Job ImapServerProxy::select(const QString &mailbox) { auto select = new KIMAP2::SelectJob(mSession); select->setMailBox(mailbox); select->setCondstoreEnabled(mCapabilities.contains(Capabilities::Condstore)); return runJob(select, [select](KJob* job) -> SelectResult { return {select->uidValidity(), select->nextUid(), select->highestModSequence()}; }).onError([=] (const KAsync::Error &error) { SinkWarning() << "Select failed: " << mailbox; }); } KAsync::Job ImapServerProxy::select(const Folder &folder) { return select(mailboxFromFolder(folder)); } KAsync::Job ImapServerProxy::examine(const QString &mailbox) { auto select = new KIMAP2::SelectJob(mSession); select->setOpenReadOnly(true); select->setMailBox(mailbox); select->setCondstoreEnabled(mCapabilities.contains(Capabilities::Condstore)); return runJob(select, [select](KJob* job) -> SelectResult { return {select->uidValidity(), select->nextUid(), select->highestModSequence()}; }).onError([=] (const KAsync::Error &error) { SinkWarning() << "Examine failed: " << mailbox; }); } KAsync::Job ImapServerProxy::examine(const Folder &folder) { return examine(mailboxFromFolder(folder)); } KAsync::Job ImapServerProxy::append(const QString &mailbox, const QByteArray &content, const QList &flags, const QDateTime &internalDate) { auto append = new KIMAP2::AppendJob(mSession); append->setMailBox(mailbox); append->setContent(content); append->setFlags(flags); append->setInternalDate(internalDate); return runJob(append, [](KJob *job) -> qint64{ return static_cast(job)->uid(); }); } KAsync::Job ImapServerProxy::store(const KIMAP2::ImapSet &set, const QList &flags) { return storeFlags(set, flags); } KAsync::Job ImapServerProxy::storeFlags(const KIMAP2::ImapSet &set, const QList &flags) { auto store = new KIMAP2::StoreJob(mSession); store->setUidBased(true); store->setMode(KIMAP2::StoreJob::SetFlags); store->setSequenceSet(set); store->setFlags(flags); return runJob(store); } KAsync::Job ImapServerProxy::addFlags(const KIMAP2::ImapSet &set, const QList &flags) { auto store = new KIMAP2::StoreJob(mSession); store->setUidBased(true); store->setMode(KIMAP2::StoreJob::AppendFlags); store->setSequenceSet(set); store->setFlags(flags); return runJob(store); } KAsync::Job ImapServerProxy::removeFlags(const KIMAP2::ImapSet &set, const QList &flags) { auto store = new KIMAP2::StoreJob(mSession); store->setUidBased(true); store->setMode(KIMAP2::StoreJob::RemoveFlags); store->setSequenceSet(set); store->setFlags(flags); return runJob(store); } KAsync::Job ImapServerProxy::create(const QString &mailbox) { auto create = new KIMAP2::CreateJob(mSession); create->setMailBox(mailbox); return runJob(create); } +KAsync::Job ImapServerProxy::subscribe(const QString &mailbox) +{ + auto job = new KIMAP2::SubscribeJob(mSession); + job->setMailBox(mailbox); + return runJob(job); +} + KAsync::Job ImapServerProxy::rename(const QString &mailbox, const QString &newMailbox) { auto rename = new KIMAP2::RenameJob(mSession); rename->setSourceMailBox(mailbox); rename->setDestinationMailBox(newMailbox); return runJob(rename); } KAsync::Job ImapServerProxy::remove(const QString &mailbox) { auto job = new KIMAP2::DeleteJob(mSession); job->setMailBox(mailbox); return runJob(job); } KAsync::Job ImapServerProxy::expunge() { auto job = new KIMAP2::ExpungeJob(mSession); return runJob(job); } KAsync::Job ImapServerProxy::expunge(const KIMAP2::ImapSet &set) { //FIXME implement UID EXPUNGE auto job = new KIMAP2::ExpungeJob(mSession); return runJob(job); } KAsync::Job ImapServerProxy::copy(const KIMAP2::ImapSet &set, const QString &newMailbox) { auto copy = new KIMAP2::CopyJob(mSession); copy->setSequenceSet(set); copy->setUidBased(true); copy->setMailBox(newMailbox); return runJob(copy); } KAsync::Job ImapServerProxy::fetch(const KIMAP2::ImapSet &set, KIMAP2::FetchJob::FetchScope scope, FetchCallback callback) { auto fetch = new KIMAP2::FetchJob(mSession); fetch->setSequenceSet(set); fetch->setUidBased(true); fetch->setScope(scope); fetch->setAvoidParsing(true); QObject::connect(fetch, &KIMAP2::FetchJob::resultReceived, callback); return runJob(fetch); } KAsync::Job> ImapServerProxy::search(const KIMAP2::ImapSet &set) { return search(KIMAP2::Term(KIMAP2::Term::Uid, set)); } KAsync::Job> ImapServerProxy::search(const KIMAP2::Term &term) { auto search = new KIMAP2::SearchJob(mSession); search->setTerm(term); search->setUidBased(true); return runJob>(search, [](KJob *job) -> QVector { return static_cast(job)->results(); }); } KAsync::Job ImapServerProxy::fetch(const KIMAP2::ImapSet &set, KIMAP2::FetchJob::FetchScope scope, const std::function &callback) { const bool fullPayload = (scope.mode == KIMAP2::FetchJob::FetchScope::Full); return fetch(set, scope, [callback, fullPayload](const KIMAP2::FetchJob::Result &result) { callback(Message{result.uid, result.size, result.attributes, result.flags, result.message, fullPayload}); }); } QStringList ImapServerProxy::getCapabilities() const { return mCapabilities; } KAsync::Job> ImapServerProxy::fetchHeaders(const QString &mailbox, const qint64 minUid) { auto list = QSharedPointer>::create(); KIMAP2::FetchJob::FetchScope scope; scope.mode = KIMAP2::FetchJob::FetchScope::Flags; //Fetch headers of all messages return fetch(KIMAP2::ImapSet(minUid, 0), scope, [list](const KIMAP2::FetchJob::Result &result) { // SinkTrace() << "Received " << uids.size() << " headers from " << mailbox; // SinkTrace() << uids.size() << sizes.size() << attrs.size() << flags.size() << messages.size(); //TODO based on the data available here, figure out which messages to actually fetch //(we only fetched headers and structure so far) //We could i.e. build chunks to fetch based on the size list->append(result.uid); }) .then([list](){ return *list; }); } KAsync::Job> ImapServerProxy::fetchUids(const QString &mailbox) { auto notDeleted = KIMAP2::Term(KIMAP2::Term::Deleted); notDeleted.setNegated(true); return select(mailbox).then>(search(notDeleted)); } KAsync::Job> ImapServerProxy::fetchUidsSince(const QString &mailbox, const QDate &since) { auto sinceTerm = KIMAP2::Term(KIMAP2::Term::Since, since); auto notDeleted = KIMAP2::Term(KIMAP2::Term::Deleted); notDeleted.setNegated(true); auto term = KIMAP2::Term(KIMAP2::Term::And, QVector() << sinceTerm << notDeleted); return select(mailbox).then>(search(term)); } KAsync::Job ImapServerProxy::list(KIMAP2::ListJob::Option option, const std::function &flags)> &callback) { auto listJob = new KIMAP2::ListJob(mSession); listJob->setOption(option); // listJob->setQueriedNamespaces(serverNamespaces()); QObject::connect(listJob, &KIMAP2::ListJob::resultReceived, listJob, callback); return runJob(listJob); } KAsync::Job ImapServerProxy::remove(const QString &mailbox, const KIMAP2::ImapSet &set) { return select(mailbox).then(store(set, QByteArrayList() << Flags::Deleted)).then(expunge(set)); } KAsync::Job ImapServerProxy::remove(const QString &mailbox, const QByteArray &imapSet) { const auto set = KIMAP2::ImapSet::fromImapSequenceSet(imapSet); return remove(mailbox, set); } KAsync::Job ImapServerProxy::move(const QString &mailbox, const KIMAP2::ImapSet &set, const QString &newMailbox) { return select(mailbox).then(copy(set, newMailbox)).then(store(set, QByteArrayList() << Flags::Deleted)).then(expunge(set)); } KAsync::Job ImapServerProxy::createSubfolder(const QString &parentMailbox, const QString &folderName) { return KAsync::start([this, parentMailbox, folderName]() { QString folder; if (parentMailbox.isEmpty()) { auto ns = mNamespaces.getDefaultNamespace(); folder = ns.name + folderName; } else { auto ns = mNamespaces.getNamespace(parentMailbox); folder = parentMailbox + ns.separator + folderName; } SinkTrace() << "Creating subfolder: " << folder; return create(folder) .then([=]() { return folder; }); }); } KAsync::Job ImapServerProxy::renameSubfolder(const QString &oldMailbox, const QString &newName) { return KAsync::start([this, oldMailbox, newName] { auto ns = mNamespaces.getNamespace(oldMailbox); auto parts = oldMailbox.split(ns.separator); parts.removeLast(); QString folder = parts.join(ns.separator) + ns.separator + newName; SinkTrace() << "Renaming subfolder: " << oldMailbox << folder; return rename(oldMailbox, folder) .then([=]() { return folder; }); }); } QString ImapServerProxy::getNamespace(const QString &name) { auto ns = mNamespaces.getNamespace(name); return ns.name; } static bool caseInsensitiveContains(const QByteArray &f, const QByteArrayList &list) { return list.contains(f) || list.contains(f.toLower()); } bool Imap::flagsContain(const QByteArray &f, const QByteArrayList &flags) { return caseInsensitiveContains(f, flags); } static void reportFolder(const Folder &f, QSharedPointer> reportedList, std::function callback) { if (!reportedList->contains(f.path())) { reportedList->insert(f.path()); auto c = f; c.noselect = true; callback(c); if (!f.parentPath().isEmpty()){ reportFolder(f.parentFolder(), reportedList, callback); } } } KAsync::Job ImapServerProxy::getMetaData(std::function > &metadata)> callback) { if (!mCapabilities.contains("METADATA")) { return KAsync::null(); } KIMAP2::GetMetaDataJob *meta = new KIMAP2::GetMetaDataJob(mSession); meta->setMailBox(QLatin1String("*")); meta->setServerCapability( KIMAP2::MetaDataJobBase::Metadata ); meta->setDepth(KIMAP2::GetMetaDataJob::AllLevels); meta->addRequestedEntry("/shared/vendor/kolab/folder-type"); meta->addRequestedEntry("/private/vendor/kolab/folder-type"); return runJob(meta).then([callback, meta] () { callback(meta->allMetaDataForMailboxes()); }); } KAsync::Job ImapServerProxy::fetchFolders(std::function callback) { SinkTrace() << "Fetching folders"; auto subscribedList = QSharedPointer>::create() ; auto reportedList = QSharedPointer>::create() ; auto metaData = QSharedPointer>>::create() ; return getMetaData([=] (const QHash> &m) { *metaData = m; }).then(list(KIMAP2::ListJob::NoOption, [=](const KIMAP2::MailBoxDescriptor &mailbox, const QList &){ *subscribedList << mailbox.name; })).then(list(KIMAP2::ListJob::IncludeUnsubscribed, [=](const KIMAP2::MailBoxDescriptor &mailbox, const QList &flags) { bool noselect = caseInsensitiveContains(FolderFlags::Noselect, flags); bool subscribed = subscribedList->contains(mailbox.name); if (isGmail()) { bool inbox = mailbox.name.toLower() == "inbox"; bool sent = caseInsensitiveContains(FolderFlags::Sent, flags); bool drafts = caseInsensitiveContains(FolderFlags::Drafts, flags); bool trash = caseInsensitiveContains(FolderFlags::Trash, flags); /** * Because gmail duplicates messages all over the place we only support a few selected folders for now that should be mostly exclusive. */ if (!(inbox || sent || drafts || trash)) { return; } } SinkLog() << "Found mailbox: " << mailbox.name << flags << FolderFlags::Noselect << noselect << " sub: " << subscribed; //Ignore all non-mail folders if (metaData->contains(mailbox.name)) { auto m = metaData->value(mailbox.name); auto sharedType = m.value("/shared/vendor/kolab/folder-type"); auto privateType = m.value("/private/vendor/kolab/folder-type"); auto type = !privateType.isEmpty() ? privateType : sharedType; if (!type.isEmpty() && !type.contains("mail")) { SinkLog() << "Skipping due to folder type: " << type; return; } } auto ns = getNamespace(mailbox.name); auto folder = Folder{mailbox.name, ns, mailbox.separator, noselect, subscribed, flags}; //call callback for parents if that didn't already happen. //This is necessary because we can have missing bits in the hierarchy in IMAP, but this will not work in sink because we'd end up with an incomplete tree. if (!folder.parentPath().isEmpty() && !reportedList->contains(folder.parentPath())) { reportFolder(folder.parentFolder(), reportedList, callback); } reportedList->insert(folder.path()); callback(folder); })); } QString ImapServerProxy::mailboxFromFolder(const Folder &folder) const { Q_ASSERT(!folder.path().isEmpty()); return folder.path(); } KAsync::Job ImapServerProxy::fetchFlags(const Folder &folder, const KIMAP2::ImapSet &set, qint64 changedsince, std::function callback) { SinkTrace() << "Fetching flags " << folder.path(); return select(folder).then([=](const SelectResult &selectResult) -> KAsync::Job { SinkTrace() << "Modeseq " << folder.path() << selectResult.highestModSequence << changedsince; if (selectResult.highestModSequence == static_cast(changedsince)) { SinkTrace()<< folder.path() << "Changedsince didn't change, nothing to do."; return KAsync::value(selectResult); } SinkTrace() << "Fetching flags " << folder.path() << set << selectResult.highestModSequence << changedsince; KIMAP2::FetchJob::FetchScope scope; scope.mode = KIMAP2::FetchJob::FetchScope::Flags; scope.changedSince = changedsince; return fetch(set, scope, callback).then([selectResult] { return selectResult; }); }); } KAsync::Job ImapServerProxy::fetchMessages(const Folder &folder, qint64 uidNext, std::function callback, std::function progress) { auto time = QSharedPointer::create(); time->start(); return select(folder).then([this, callback, folder, time, progress, uidNext](const SelectResult &selectResult) -> KAsync::Job { SinkTrace() << "UIDNEXT " << folder.path() << selectResult.uidNext << uidNext; if (selectResult.uidNext == (uidNext + 1)) { SinkTrace()<< folder.path() << "Uidnext didn't change, nothing to do."; return KAsync::null(); } SinkTrace() << "Fetching messages from " << folder.path() << selectResult.uidNext << uidNext; return fetchHeaders(mailboxFromFolder(folder), (uidNext + 1)).then>([this, callback, time, progress, folder](const QVector &uidsToFetch){ SinkTrace() << "Fetched headers" << folder.path(); SinkTrace() << " Total: " << uidsToFetch.size(); SinkTrace() << " Uids to fetch: " << uidsToFetch; SinkTrace() << " Took: " << Sink::Log::TraceTime(time->elapsed()); return fetchMessages(folder, uidsToFetch, false, callback, progress); }); }); } KAsync::Job ImapServerProxy::fetchMessages(const Folder &folder, const QVector &uidsToFetch, bool headersOnly, std::function callback, std::function progress) { auto time = QSharedPointer::create(); time->start(); return select(folder).then([this, callback, folder, time, progress, uidsToFetch, headersOnly](const SelectResult &selectResult) -> KAsync::Job { SinkTrace() << "Fetching messages" << folder.path(); SinkTrace() << " Total: " << uidsToFetch.size(); SinkTrace() << " Uids to fetch: " << uidsToFetch; auto totalCount = uidsToFetch.size(); if (progress) { progress(0, totalCount); } if (uidsToFetch.isEmpty()) { SinkTrace() << "Nothing to fetch"; return KAsync::null(); } KIMAP2::FetchJob::FetchScope scope; scope.parts.clear(); if (headersOnly) { scope.mode = KIMAP2::FetchJob::FetchScope::Headers; } else { scope.mode = KIMAP2::FetchJob::FetchScope::Full; } KIMAP2::ImapSet set; set.add(uidsToFetch); auto count = QSharedPointer::create(); return fetch(set, scope, [=](const Message &message) { *count += 1; if (progress) { progress(*count, totalCount); } callback(message); }); }) .then([time]() { SinkTrace() << "The fetch took: " << Sink::Log::TraceTime(time->elapsed()); }); } KAsync::Job ImapServerProxy::fetchMessages(const Folder &folder, std::function callback, std::function progress) { return fetchMessages(folder, 0, callback, progress); } KAsync::Job> ImapServerProxy::fetchUids(const Folder &folder) { return fetchUids(mailboxFromFolder(folder)); } diff --git a/examples/imapresource/imapserverproxy.h b/examples/imapresource/imapserverproxy.h index 013c18f5..cb39b29d 100644 --- a/examples/imapresource/imapserverproxy.h +++ b/examples/imapresource/imapserverproxy.h @@ -1,321 +1,322 @@ /* * Copyright (C) 2015 Christian Mollekopf * * 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. */ #pragma once #include #include #include #include #include #include namespace Imap { enum ErrorCode { NoError, LoginFailed, HostNotFoundError, CouldNotConnectError, SslHandshakeError, ConnectionLost, MissingCredentialsError, SelectFailed, UnknownError }; namespace Flags { /// The flag for a message being seen (i.e. opened by user). extern const char* Seen; /// The flag for a message being deleted by the user. extern const char* Deleted; /// The flag for a message being replied to by the user. extern const char* Answered; /// The flag for a message being marked as flagged. extern const char* Flagged; } namespace FolderFlags { extern const char* Noinferiors; extern const char* Noselect; extern const char* Marked; extern const char* Unmarked; extern const char* Subscribed; extern const char* Sent; extern const char* Trash; extern const char* Archive; extern const char* Junk; extern const char* Flagged; extern const char* All; extern const char* Drafts; } namespace Capabilities { extern const char* Condstore; extern const char* Uidplus; extern const char* Namespace; } struct Message { qint64 uid; qint64 size; KIMAP2::MessageAttributes attributes; KIMAP2::MessageFlags flags; KMime::Message::Ptr msg; bool fullPayload; }; bool flagsContain(const QByteArray &f, const QByteArrayList &flags); struct Folder { Folder() = default; Folder(const QString &path, const QString &ns, const QChar &separator, bool noselect_, bool subscribed_, const QByteArrayList &flags_) : noselect(noselect_), subscribed(subscribed_), flags(flags_), mPath(path), mNamespace(ns), mSeparator(separator) { } Folder(const QString &path_) : mPath(path_) { } QString path() const { Q_ASSERT(!mPath.isEmpty()); return mPath; } QString parentPath() const { Q_ASSERT(!mSeparator.isNull()); auto parts = mPath.split(mSeparator); parts.removeLast(); auto parentPath = parts.join(mSeparator); //Don't return the namespace for root folders as parent folder if (mNamespace.startsWith(parentPath)) { return QString{}; } return parentPath; } Folder parentFolder() const { Folder parent; parent.mPath = parentPath(); parent.mNamespace = mNamespace; parent.mSeparator = mSeparator; return parent; } QString name() const { auto pathParts = mPath.split(mSeparator); Q_ASSERT(!pathParts.isEmpty()); return pathParts.last(); } bool noselect = false; bool subscribed = false; QByteArrayList flags; private: QString mPath; QString mNamespace; QChar mSeparator; }; struct SelectResult { qint64 uidValidity; qint64 uidNext; quint64 highestModSequence; }; class Namespaces { public: QList personal; QList shared; QList user; KIMAP2::MailBoxDescriptor getDefaultNamespace() { return personal.isEmpty() ? KIMAP2::MailBoxDescriptor{} : personal.first(); } KIMAP2::MailBoxDescriptor getNamespace(const QString &mailbox) { for (const auto &ns : personal) { if (mailbox.startsWith(ns.name)) { return ns; } } for (const auto &ns : shared) { if (mailbox.startsWith(ns.name)) { return ns; } } for (const auto &ns : user) { if (mailbox.startsWith(ns.name)) { return ns; } } return KIMAP2::MailBoxDescriptor{}; } }; class CachedSession { public: CachedSession() = default; CachedSession(KIMAP2::Session *session, const QStringList &cap, const Namespaces &ns) : mSession(session), mCapabilities(cap), mNamespaces(ns) { } bool operator==(const CachedSession &other) const { return mSession && (mSession == other.mSession); } bool isConnected() { return (mSession->state() == KIMAP2::Session::State::Authenticated || mSession->state() == KIMAP2::Session::State::Selected) ; } bool isValid() { return mSession; } KIMAP2::Session *mSession = nullptr; QStringList mCapabilities; Namespaces mNamespaces; }; class SessionCache : public QObject { Q_OBJECT public: void recycleSession(const CachedSession &session) { QObject::connect(session.mSession, &KIMAP2::Session::stateChanged, this, [this, session](KIMAP2::Session::State newState, KIMAP2::Session::State oldState) { if (newState == KIMAP2::Session::Disconnected) { mSessions.removeOne(session); } }); mSessions << session; } CachedSession getSession() { while (!mSessions.isEmpty()) { auto session = mSessions.takeLast(); if (session.isConnected()) { return session; } } return {}; } bool isEmpty() const { return mSessions.isEmpty(); } private: QList mSessions; }; enum EncryptionMode { NoEncryption, Tls, Starttls }; class ImapServerProxy { public: ImapServerProxy(const QString &serverUrl, int port, EncryptionMode encryption, SessionCache *sessionCache = nullptr); //Standard IMAP calls KAsync::Job login(const QString &username, const QString &password); KAsync::Job logout(); KAsync::Job select(const QString &mailbox); KAsync::Job select(const Folder &mailbox); KAsync::Job examine(const QString &mailbox); KAsync::Job examine(const Folder &mailbox); KAsync::Job append(const QString &mailbox, const QByteArray &content, const QList &flags = QList(), const QDateTime &internalDate = QDateTime()); KAsync::Job store(const KIMAP2::ImapSet &set, const QList &flags); KAsync::Job storeFlags(const KIMAP2::ImapSet &set, const QList &flags); KAsync::Job addFlags(const KIMAP2::ImapSet &set, const QList &flags); KAsync::Job removeFlags(const KIMAP2::ImapSet &set, const QList &flags); KAsync::Job create(const QString &mailbox); KAsync::Job rename(const QString &mailbox, const QString &newMailbox); KAsync::Job remove(const QString &mailbox); + KAsync::Job subscribe(const QString &mailbox); KAsync::Job expunge(); KAsync::Job expunge(const KIMAP2::ImapSet &set); KAsync::Job copy(const KIMAP2::ImapSet &set, const QString &newMailbox); KAsync::Job> search(const KIMAP2::ImapSet &set); KAsync::Job> search(const KIMAP2::Term &term); typedef std::function FetchCallback; KAsync::Job fetch(const KIMAP2::ImapSet &set, KIMAP2::FetchJob::FetchScope scope, FetchCallback callback); KAsync::Job fetch(const KIMAP2::ImapSet &set, KIMAP2::FetchJob::FetchScope scope, const std::function &callback); KAsync::Job list(KIMAP2::ListJob::Option option, const std::function &flags)> &callback); QStringList getCapabilities() const; //Composed calls that do login etc. KAsync::Job> fetchHeaders(const QString &mailbox, qint64 minUid = 1); KAsync::Job remove(const QString &mailbox, const KIMAP2::ImapSet &set); KAsync::Job remove(const QString &mailbox, const QByteArray &imapSet); KAsync::Job move(const QString &mailbox, const KIMAP2::ImapSet &set, const QString &newMailbox); KAsync::Job createSubfolder(const QString &parentMailbox, const QString &folderName); KAsync::Job renameSubfolder(const QString &mailbox, const QString &newName); KAsync::Job> fetchUids(const QString &mailbox); KAsync::Job> fetchUidsSince(const QString &mailbox, const QDate &since); QString mailboxFromFolder(const Folder &) const; KAsync::Job fetchFolders(std::function callback); KAsync::Job fetchMessages(const Folder &folder, std::function callback, std::function progress = std::function()); KAsync::Job fetchMessages(const Folder &folder, qint64 uidNext, std::function callback, std::function progress = std::function()); KAsync::Job fetchMessages(const Folder &folder, const QVector &uidsToFetch, bool headersOnly, std::function callback, std::function progress); KAsync::Job fetchFlags(const Folder &folder, const KIMAP2::ImapSet &set, qint64 changedsince, std::function callback); KAsync::Job> fetchUids(const Folder &folder); private: KAsync::Job getMetaData(std::function > &metadata)> callback); bool isGmail() const; QString getNamespace(const QString &name); QObject mGuard; SessionCache *mSessionCache; KIMAP2::Session *mSession; QStringList mCapabilities; Namespaces mNamespaces; EncryptionMode mEncryptionMode; }; } diff --git a/examples/imapresource/tests/imapmailsynctest.cpp b/examples/imapresource/tests/imapmailsynctest.cpp index e40aec8b..2d937cef 100644 --- a/examples/imapresource/tests/imapmailsynctest.cpp +++ b/examples/imapresource/tests/imapmailsynctest.cpp @@ -1,183 +1,184 @@ /* * Copyright (C) 2016 Christian Mollekopf * * 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. */ #include #include #include #include "../imapresource.h" #include "../imapserverproxy.h" #include "common/test.h" #include "common/domain/applicationdomaintype.h" #include "common/secretstore.h" #include "common/store.h" #include "common/resourcecontrol.h" #include "common/notifier.h" using namespace Sink; using namespace Sink::ApplicationDomain; /** * Test of complete system using the imap resource. * * This test requires the imap resource installed. */ class ImapMailSyncTest : public Sink::MailSyncTest { Q_OBJECT protected: bool isBackendAvailable() Q_DECL_OVERRIDE { QTcpSocket socket; socket.connectToHost("localhost", 143); return socket.waitForConnected(200); } void resetTestEnvironment() Q_DECL_OVERRIDE { system("resetmailbox.sh"); } Sink::ApplicationDomain::SinkResource createResource() Q_DECL_OVERRIDE { auto resource = ApplicationDomain::ImapResource::create("account1"); resource.setProperty("server", "localhost"); resource.setProperty("port", 143); resource.setProperty("username", "doe"); resource.setProperty("daysToSync", 0); Sink::SecretStore::instance().insert(resource.identifier(), "doe"); return resource; } Sink::ApplicationDomain::SinkResource createFaultyResource() Q_DECL_OVERRIDE { auto resource = ApplicationDomain::ImapResource::create("account1"); //Using a bogus ip instead of a bogus hostname avoids getting stuck in the hostname lookup resource.setProperty("server", "111.111.1.1"); resource.setProperty("port", 143); resource.setProperty("username", "doe"); Sink::SecretStore::instance().insert(resource.identifier(), "doe"); return resource; } void removeResourceFromDisk(const QByteArray &identifier) Q_DECL_OVERRIDE { ::ImapResource::removeFromDisk(identifier); } void createFolder(const QStringList &folderPath) Q_DECL_OVERRIDE { Imap::ImapServerProxy imap("localhost", 143, Imap::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); VERIFYEXEC(imap.create("INBOX." + folderPath.join('.'))); + VERIFYEXEC(imap.subscribe("INBOX." + folderPath.join('.'))); } void removeFolder(const QStringList &folderPath) Q_DECL_OVERRIDE { Imap::ImapServerProxy imap("localhost", 143, Imap::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); VERIFYEXEC(imap.remove("INBOX." + folderPath.join('.'))); } QByteArray createMessage(const QStringList &folderPath, const QByteArray &message) Q_DECL_OVERRIDE { Imap::ImapServerProxy imap("localhost", 143, Imap::NoEncryption); VERIFYEXEC_RET(imap.login("doe", "doe"), {}); VERIFYEXEC_RET(imap.append("INBOX." + folderPath.join('.'), message), {}); return "2:*"; } void removeMessage(const QStringList &folderPath, const QByteArray &messages) Q_DECL_OVERRIDE { Imap::ImapServerProxy imap("localhost", 143, Imap::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); VERIFYEXEC(imap.remove("INBOX." + folderPath.join('.'), messages)); } void markAsImportant(const QStringList &folderPath, const QByteArray &messageIdentifier) Q_DECL_OVERRIDE { Imap::ImapServerProxy imap("localhost", 143, Imap::NoEncryption); VERIFYEXEC(imap.login("doe", "doe")); VERIFYEXEC(imap.select("INBOX." + folderPath.join('.'))); VERIFYEXEC(imap.addFlags(KIMAP2::ImapSet::fromImapSequenceSet(messageIdentifier), QByteArrayList() << Imap::Flags::Flagged)); } static QByteArray newMessage(const QString &subject) { auto msg = KMime::Message::Ptr::create(); msg->subject(true)->fromUnicodeString(subject, "utf8"); msg->date(true)->setDateTime(QDateTime::currentDateTimeUtc()); msg->assemble(); return msg->encodedContent(true); } private slots: void testNewMailNotification() { const auto syncFolders = Sink::SyncScope{ApplicationDomain::getTypeName()}.resourceFilter(mResourceInstanceIdentifier); //Fetch folders initially VERIFYEXEC(Store::synchronize(syncFolders)); VERIFYEXEC(ResourceControl::flushMessageQueue(mResourceInstanceIdentifier)); auto folder = Store::readOne(Sink::Query{}.resourceFilter(mResourceInstanceIdentifier).filter("test")); Q_ASSERT(!folder.identifier().isEmpty()); const auto syncTestMails = Sink::SyncScope{ApplicationDomain::getTypeName()}.resourceFilter(mResourceInstanceIdentifier).filter(QVariant::fromValue(folder.identifier())); bool notificationReceived = false; auto notifier = QSharedPointer::create(mResourceInstanceIdentifier); notifier->registerHandler([&](const Notification ¬ification) { if (notification.type == Sink::Notification::Info && notification.code == ApplicationDomain::NewContentAvailable && notification.entities.contains(folder.identifier())) { notificationReceived = true; } }); //Should result in a change notification for test VERIFYEXEC(Store::synchronize(syncFolders)); VERIFYEXEC(ResourceControl::flushMessageQueue(mResourceInstanceIdentifier)); QTRY_VERIFY(notificationReceived); notificationReceived = false; //Fetch test mails to skip change notification VERIFYEXEC(Store::synchronize(syncTestMails)); VERIFYEXEC(ResourceControl::flushMessageQueue(mResourceInstanceIdentifier)); //Should no longer result in change notifications for test VERIFYEXEC(Store::synchronize(syncFolders)); VERIFYEXEC(ResourceControl::flushMessageQueue(mResourceInstanceIdentifier)); QVERIFY(!notificationReceived); //Create message and retry createMessage(QStringList() << "test", newMessage("This is a Subject.")); //Should result in change notification VERIFYEXEC(Store::synchronize(syncFolders)); VERIFYEXEC(ResourceControl::flushMessageQueue(mResourceInstanceIdentifier)); QTRY_VERIFY(notificationReceived); } }; QTEST_MAIN(ImapMailSyncTest) #include "imapmailsynctest.moc"