File indexing completed on 2024-11-24 04:53:26

0001 //
0002 // C++ Interface: qwwsmtpclient
0003 //
0004 // Description:
0005 //
0006 //
0007 // Author: Witold Wysota <wysota@wysota.eu.org>, (C) 2009
0008 //
0009 // Copyright: See COPYING file that comes with this distribution
0010 //
0011 //
0012 #ifndef QWWSMTPCLIENT_H
0013 #define QWWSMTPCLIENT_H
0014 
0015 #include <QObject>
0016 #include <QHostAddress>
0017 #include <QString>
0018 #include <QSslError>
0019 
0020 class QwwSmtpClientPrivate;
0021 
0022 /*!
0023         \class  QwwSmtpClient
0024         \author Witold Wysota <wysota@wysota.eu.org>
0025         \brief  Cross-platform asynchronous handling of client side SMTP connections
0026 
0027                 Features:
0028 
0029                 Connection mode - open, TLS, SSL
0030                 Authentication  - PLAIN, LOGIN
0031                 Handshake       - HELO, EHLO
0032 
0033                 - low-level mail sending (everything you pass, goes through to the server)
0034                 - raw command sending
0035                 - multiple rcpt
0036                 - option reporting
0037 
0038        \todo    CRAM-MD5 Authentication
0039                 VRFY
0040                 abort()
0041                 SSL errors handling
0042                 network errors
0043                 error handling (status codes, etc.)
0044 
0045 */
0046 class QwwSmtpClient : public QObject {
0047     Q_OBJECT
0048     Q_ENUMS(State);
0049     Q_FLAGS(Options);
0050     Q_ENUMS(AuthMode);
0051     Q_FLAGS(AuthModes);
0052 
0053 public:
0054     explicit QwwSmtpClient(QObject *parent = 0);
0055     ~QwwSmtpClient();
0056     enum State { Disconnected, Connecting, Connected, TLSRequested, Authenticating, Sending, Disconnecting };
0057     enum Option { NoOptions = 0, StartTlsOption, SizeOption, PipeliningOption, EightBitMimeOption, AuthOption };
0058     Q_DECLARE_FLAGS ( Options, Option );
0059     enum AuthMode { AuthNone = 0, AuthAny = 1, AuthPlain = 2, AuthLogin = 4 };
0060     Q_DECLARE_FLAGS ( AuthModes, AuthMode );
0061     void setLocalName(const QString &ln);
0062     void setLocalNameEncrypted(const QString &ln);
0063 
0064     int connectToHost ( const QString & hostName, quint16 port = 25);
0065     int connectToHostEncrypted(const QString &hostName, quint16 port = 465);
0066 //     int connectToHost ( const QHostAddress & address, quint16 port = 25);
0067     int authenticate(const QString &user, const QString &password, AuthMode mode = AuthAny);
0068     int sendMail(const QByteArray &from, const QList<QByteArray> &to, const QByteArray &content);
0069     int sendMailBurl(const QByteArray &from, const QList<QByteArray> &to, const QByteArray &url);
0070     int rawCommand(const QString &cmd);
0071     AuthModes supportedAuthModes() const;
0072     Options options() const;
0073     QString errorString() const;
0074 public slots:
0075     int disconnectFromHost();
0076     int startTls();
0077     void ignoreSslErrors();
0078 
0079 signals:
0080     void done(bool);
0081     void connected();
0082     void disconnected();
0083     void stateChanged(State);
0084     void commandFinished(int, bool error);
0085     void commandStarted(int);
0086     void tlsStarted();
0087     void authenticated();
0088     void rawCommandReply(int code, const QString &details);
0089     void sslErrors(const QList<QSslError> &);
0090     void socketError(QAbstractSocket::SocketError err, const QString& message);
0091     void logReceived(const QByteArray& data);
0092     void logSent(const QByteArray& data);
0093 
0094 private:
0095     QwwSmtpClientPrivate *d;
0096     Q_PRIVATE_SLOT(d, void onConnected());
0097     Q_PRIVATE_SLOT(d, void onDisconnected());
0098     Q_PRIVATE_SLOT(d, void onError(QAbstractSocket::SocketError));
0099     Q_PRIVATE_SLOT(d, void _q_readFromSocket());
0100     Q_PRIVATE_SLOT(d, void _q_encrypted());
0101     friend class QwwSmtpClientPrivate;
0102 
0103     QwwSmtpClient(const QwwSmtpClient&); // don't implement
0104     QwwSmtpClient& operator=(const QwwSmtpClient&); // don't implement
0105 };
0106 
0107 #endif