1 /*
2     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3     SPDX-FileContributor: Volker Krause <volker.krause@kdab.com>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #pragma once
9 
10 #include "response.h"
11 
12 #include <KJob>
13 #include <QObject>
14 #include <QQueue>
15 #include <QStringList>
16 #include <QUrl>
17 
18 class KSslErrorUiData;
19 
20 namespace KManageSieve
21 {
22 class SieveJob;
23 class SessionThread;
24 
25 struct AuthDetails {
26     QString username;
27     QString password;
28     bool valid;
29 };
30 
31 /** A network session with a manage sieve server.
32  * @internal
33  */
34 class Session : public QObject
35 {
36     Q_OBJECT
37 
38 public:
39     explicit Session(QObject *parent = nullptr);
40     ~Session() override;
41 
42     void connectToHost(const QUrl &url);
43     void disconnectFromHost(bool sendLogout = true);
44 
45     void scheduleJob(SieveJob *job);
46     void killJob(SieveJob *job, KJob::KillVerbosity verbosity);
47     void sendData(const QByteArray &data);
48 
49     Q_REQUIRED_RESULT QStringList sieveExtensions() const;
50 
51     /**
52      * @return true if we are not connected nor currently connecting, i.e. it's ok to call connectToHost()
53      */
54     bool disconnected() const;
55 
56 private:
57     Q_REQUIRED_RESULT bool requestCapabilitiesAfterStartTls() const;
58 
59     Q_REQUIRED_RESULT QStringList requestedSaslMethod() const;
60     Q_REQUIRED_RESULT bool allowUnencrypted() const;
61 
62 private Q_SLOTS:
63     void setErrorMessage(int error, const QString &msg);
64     void processResponse(const KManageSieve::Response &response, const QByteArray &data);
65     Q_REQUIRED_RESULT KManageSieve::AuthDetails requestAuthDetails(const QUrl &url);
66     void authenticationDone();
67     void sslError(const KSslErrorUiData &data);
68     void sslDone();
69 
70     void executeNextJob();
71 
72 private:
73     Q_DISABLE_COPY(Session)
74     SessionThread *const m_thread;
75     QUrl m_url;
76     QQueue<SieveJob *> m_jobs;
77     SieveJob *m_currentJob = nullptr;
78     QStringList m_sieveExtensions;
79     QStringList m_saslMethods;
80     QString m_implementation;
81     enum State { None, PreTlsCapabilities, PostTlsCapabilities, StartTls, Authenticating };
82     State m_state = None;
83     bool m_supportsStartTls = false;
84     bool m_connected = false;
85     bool m_disconnected = true;
86 
87     friend class SessionThread;
88 };
89 }
90 
91