1 /*
2   SPDX-FileCopyrightText: 2006-2007 KovoKs <info@kovoks.nl>
3 
4   SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include <mailtransport_export.h>
10 
11 #include <QSslSocket>
12 
13 #include <memory>
14 
15 namespace MailTransport
16 {
17 class SocketPrivate;
18 
19 /**
20  * @class Socket
21  * Responsible for communicating with the server, it's designed to work
22  * with the ServerTest class.
23  * @author Tom Albers <tomalbers@kde.nl>
24  */
25 class Socket : public QObject
26 {
27     Q_OBJECT
28 
29 public:
30     /**
31      * Constructor, it will not auto connect. Call reconnect() to connect to
32      * the parameters given.
33      * @param parent the parent
34      */
35     explicit Socket(QObject *parent);
36 
37     /**
38      * Destructor
39      */
40     ~Socket() override;
41 
42     /**
43      * Existing connection will be closed and a new connection will be
44      * made
45      */
46     virtual void reconnect();
47 
48     /**
49      * Write @p text to the socket
50      */
51     virtual void write(const QString &text);
52 
53     /**
54      * @return true when the connection is live and kicking
55      */
56     virtual bool available();
57 
58     /**
59      * set the protocol to use
60      */
61     void setProtocol(const QString &proto);
62 
63     /**
64      * set the server to use
65      */
66     void setServer(const QString &server);
67 
68     /**
69      * set the port to use. If not specified, it will use the default
70      * belonging to the protocol.
71      */
72     void setPort(int port);
73 
74     /**
75      * returns the used port.
76      */
77     int port() const;
78 
79     /**
80      * this will be a secure connection
81      */
82     void setSecure(bool what);
83 
84     /**
85      * If you want to start TLS encryption, call this. For example after the starttls command.
86      */
87     void startTLS();
88 
89 private:
90     Q_DECLARE_PRIVATE(Socket)
91     std::unique_ptr<SocketPrivate> const d;
92 
93     Q_PRIVATE_SLOT(d, void slotConnected())
94     Q_PRIVATE_SLOT(d, void slotStateChanged(QAbstractSocket::SocketState state))
95     Q_PRIVATE_SLOT(d, void slotModeChanged(QSslSocket::SslMode state))
96     Q_PRIVATE_SLOT(d, void slotSocketRead())
97     Q_PRIVATE_SLOT(d, void slotSslErrors(const QList<QSslError> &errors))
98 
99 Q_SIGNALS:
100     /**
101      * emits the incoming data
102      */
103     void data(const QString &);
104 
105     /**
106      * emitted when there is a connection (ready to send something).
107      */
108     void connected();
109 
110     /**
111      * emitted when not connected.
112      */
113     void failed();
114 
115     /**
116      * emitted when startShake() is completed.
117      */
118     void tlsDone();
119 };
120 } // namespace MailTransport
121 
122