File indexing completed on 2024-06-23 05:21:15

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #ifndef IMAP_OPENCONNECTIONTASK_H
0024 #define IMAP_OPENCONNECTIONTASK_H
0025 
0026 #include "ImapTask.h"
0027 #include <QSslError>
0028 #include "../Model/Model.h"
0029 
0030 namespace Imap
0031 {
0032 namespace Mailbox
0033 {
0034 
0035 /** @short Create new connection and make sure it reaches authenticated state
0036 
0037 Use this task if you want to obtain a new connection which ends up in the authenticated
0038 state. It will establish a new connection and baby-sit it until it reaches the request
0039 authenticated state.
0040 
0041 Obtaining capabilities, issuing STARTTLS and logging in are all handled here.
0042 */
0043 class OpenConnectionTask : public ImapTask
0044 {
0045     Q_OBJECT
0046 public:
0047     explicit OpenConnectionTask(Model *model);
0048     void perform() override;
0049 
0050     bool handleStateHelper(const Imap::Responses::State *const resp) override;
0051     // FIXME: reimplement handleCapability(), add some guards against "unexpected changes" to Model's implementation
0052     bool handleSocketEncryptedResponse(const Responses::SocketEncryptedResponse *const resp) override;
0053 
0054     /** @short Inform the task that the auth credentials are now available and can be used */
0055     void authCredentialsNowAvailable();
0056 
0057     /** @short A decision about the future whereabouts of the conneciton has been made */
0058     void sslConnectionPolicyDecided(bool ok);
0059 
0060     /** @short Return the peer's chain of digital certificates, or an empty list of certificates */
0061     QList<QSslCertificate> sslCertificateChain() const;
0062 
0063     /** @short Return a list of SSL errors which the underlying socket has encountered since its start */
0064     QList<QSslError> sslErrors() const;
0065 
0066     QString debugIdentification() const override;
0067     QVariant taskData(const int role) const override;
0068     bool needsMailbox() const override {return false;}
0069 
0070 protected:
0071     /** @short A special, internal constructor used only by Fake_OpenConnectionTask */
0072     OpenConnectionTask(Model *model, void *dummy);
0073 
0074 private:
0075     bool stateMachine(const Imap::Responses::State *const resp);
0076 
0077     void startTlsOrLoginNow();
0078 
0079     bool checkCapabilitiesResult(const Imap::Responses::State *const resp);
0080 
0081     /** @short Wrapper around the _completed() call for optionally launching the ID command */
0082     void onComplete();
0083 
0084     void abortConnection(const QString &message);
0085 
0086     void askForAuth();
0087 
0088 private:
0089     CommandHandle startTlsCmd;
0090     CommandHandle capabilityCmd;
0091     CommandHandle loginCmd;
0092     CommandHandle compressCmd;
0093     QList<QSslCertificate> m_sslChain;
0094     QList<QSslError> m_sslErrors;
0095 };
0096 
0097 }
0098 }
0099 
0100 #endif // IMAP_OPENCONNECTIONTASK_H