1
0

Repair and adjust the SMTP library.

This commit is contained in:
Saturneric 2021-07-15 01:23:44 +08:00
parent a0b5b56e8f
commit 39acbb05c1
25 changed files with 858 additions and 817 deletions

View File

@ -23,27 +23,29 @@
#include "smtpexports.h"
class SMTP_EXPORT EmailAddress : public QObject
{
class SMTP_EXPORT EmailAddress : public QObject {
Q_OBJECT
public:
/* [1] Constructors and Destructors */
EmailAddress();
EmailAddress(const QString & address, const QString & name="");
EmailAddress() = default;
~EmailAddress();
explicit EmailAddress(const QString &address, const QString &name = "");
~EmailAddress() override;
/* [1] --- */
/* [2] Getters and Setters */
void setName(const QString &name);
void setAddress(const QString &address);
const QString & getName() const;
const QString & getAddress() const;
[[nodiscard]] const QString &getName() const;
[[nodiscard]] const QString &getAddress() const;
/* [2] --- */

View File

@ -25,17 +25,17 @@
#include "smtpexports.h"
class SMTP_EXPORT MimeAttachment : public MimeFile
{
class SMTP_EXPORT MimeAttachment : public MimeFile {
Q_OBJECT
public:
/* [1] Constructors and Destructors */
MimeAttachment(QFile* file);
explicit MimeAttachment(QFile *file);
MimeAttachment(const QByteArray &stream, const QString &fileName);
~MimeAttachment();
~MimeAttachment() override;
/* [1] --- */
@ -43,7 +43,7 @@ protected:
/* [2] Protected methods */
virtual void prepare();
void prepare() override;
/* [2] --- */
};

View File

@ -24,16 +24,16 @@
#include "smtpexports.h"
class SMTP_EXPORT MimeContentFormatter : public QObject
{
class SMTP_EXPORT MimeContentFormatter : public QObject {
Q_OBJECT
public:
MimeContentFormatter (int max_length = 76);
explicit MimeContentFormatter(int max_length = 76);
void setMaxLength(int l);
int getMaxLength() const;
QString format(const QString &content, bool quotedPrintable = false) const;
[[nodiscard]] int getMaxLength() const;
[[nodiscard]] QString format(const QString &content, bool quotedPrintable = false) const;
protected:
int max_length;

View File

@ -24,16 +24,17 @@
#include "smtpexports.h"
class SMTP_EXPORT MimeFile : public MimePart
{
class SMTP_EXPORT MimeFile : public MimePart {
Q_OBJECT
public:
/* [1] Constructors and Destructors */
MimeFile(const QByteArray &stream, const QString &fileName);
MimeFile(QFile *f);
~MimeFile();
explicit MimeFile(QFile *f);
~MimeFile() override;
/* [1] --- */
@ -53,7 +54,7 @@ protected:
/* [4] Protected methods */
virtual void prepare();
void prepare() override;
/* [4] --- */

View File

@ -23,15 +23,15 @@
#include "smtpexports.h"
class SMTP_EXPORT MimeHtml : public MimeText
{
class SMTP_EXPORT MimeHtml : public MimeText {
Q_OBJECT
public:
/* [1] Constructors and Destructors */
MimeHtml(const QString &html = "");
~MimeHtml();
explicit MimeHtml(const QString &html = "");
~MimeHtml() override;
/* [1] --- */
@ -40,7 +40,7 @@ public:
void setHtml(const QString &html);
const QString& getHtml() const;
[[nodiscard]] const QString &getHtml() const;
/* [2] --- */
@ -53,7 +53,7 @@ protected:
/* [4] Protected methods */
virtual void prepare();
void prepare() override;
/* [4] --- */
};

View File

@ -23,14 +23,14 @@
#include "smtpexports.h"
class SMTP_EXPORT MimeInlineFile : public MimeFile
{
class SMTP_EXPORT MimeInlineFile : public MimeFile {
public:
/* [1] Constructors and Destructors */
MimeInlineFile(QFile *f);
~MimeInlineFile();
explicit MimeInlineFile(QFile *f);
~MimeInlineFile() override;
/* [1] --- */
@ -48,7 +48,7 @@ protected:
/* [4] Protected methods */
virtual void prepare();
void prepare() override;
/* [4] --- */
};

View File

@ -26,8 +26,7 @@
#include "smtpexports.h"
class SMTP_EXPORT MimeMessage : public QObject
{
class SMTP_EXPORT MimeMessage : public QObject {
public:
enum RecipientType {
@ -38,8 +37,9 @@ public:
/* [1] Constructors and Destructors */
MimeMessage(bool createAutoMimeConent = true);
~MimeMessage();
explicit MimeMessage(bool createAutoMimeConent = true);
~MimeMessage() override;
/* [1] --- */
@ -47,25 +47,37 @@ public:
/* [2] Getters and Setters */
void setSender(EmailAddress *e);
void addRecipient(EmailAddress *rcpt, RecipientType type = To);
void addTo(EmailAddress *rcpt);
void addCc(EmailAddress *rcpt);
void addBcc(EmailAddress *rcpt);
void setSubject(const QString &subject);
void addPart(MimePart *part);
void setReplyTo(EmailAddress *rto);
void setInReplyTo(const QString &inReplyTo);
void setHeaderEncoding(MimePart::Encoding);
const EmailAddress & getSender() const;
const QList<EmailAddress*> & getRecipients(RecipientType type = To) const;
const QString & getSubject() const;
const QList<MimePart*> & getParts() const;
const EmailAddress* getReplyTo() const;
[[nodiscard]] const EmailAddress &getSender() const;
[[nodiscard]] const QList<EmailAddress *> &getRecipients(RecipientType type = To) const;
[[nodiscard]] const QString &getSubject() const;
[[nodiscard]] const QList<MimePart *> &getParts() const;
[[nodiscard]] const EmailAddress *getReplyTo() const;
MimePart &getContent();
void setContent(MimePart *content);
/* [2] --- */
@ -80,7 +92,7 @@ protected:
/* [4] Protected members */
EmailAddress* sender;
EmailAddress *sender{};
EmailAddress *replyTo;
QList<EmailAddress *> recipientsTo, recipientsCc, recipientsBcc;
QString subject;

View File

@ -40,18 +40,19 @@ public:
/* [0] --- */
/* [1] Constructors and Destructors */
MimeMultiPart(const MultiPartType type = Related);
explicit MimeMultiPart(MultiPartType type = Related);
~MimeMultiPart();
~MimeMultiPart() override;
/* [1] --- */
/* [2] Getters and Setters */
void setMimeType(const MultiPartType type);
MultiPartType getMimeType() const;
void setMimeType(MultiPartType type);
const QList<MimePart *> &getParts() const;
[[nodiscard]] MultiPartType getMimeType() const;
[[nodiscard]] const QList<MimePart *> &getParts() const;
/* [2] --- */
@ -59,7 +60,7 @@ public:
void addPart(MimePart *part);
virtual void prepare();
void prepare() override;
/* [3] --- */

View File

@ -28,41 +28,51 @@ class SMTP_EXPORT MimePart : public QObject {
Q_OBJECT
public:
/* [0] Enumerations */
enum Encoding { _7Bit, _8Bit, Base64, QuotedPrintable };
enum Encoding {
_7Bit, _8Bit, Base64, QuotedPrintable
};
/* [0] --- */
/* [1] Constructors and Destructors */
MimePart();
~MimePart();
~MimePart() override;
/* [1] --- */
/* [2] Getters and Setters */
const QString &getHeader() const;
const QByteArray &getContent() const;
[[nodiscard]] const QString &getHeader() const;
[[nodiscard]] const QByteArray &getContent() const;
void setContent(const QByteArray &content);
void setHeader(const QString &header);
void addHeaderLine(const QString &line);
void setContentId(const QString &cId);
const QString &getContentId() const;
[[nodiscard]] const QString &getContentId() const;
void setContentName(const QString &cName);
const QString &getContentName() const;
[[nodiscard]] const QString &getContentName() const;
void setContentType(const QString &cType);
const QString &getContentType() const;
[[nodiscard]] const QString &getContentType() const;
void setCharset(const QString &charset);
const QString &getCharset() const;
[[nodiscard]] const QString &getCharset() const;
void setEncoding(Encoding enc);
Encoding getEncoding() const;
[[nodiscard]] Encoding getEncoding() const;
MimeContentFormatter &getContentFormatter();

View File

@ -23,14 +23,14 @@
#include "smtpexports.h"
class SMTP_EXPORT MimeText : public MimePart
{
class SMTP_EXPORT MimeText : public MimePart {
public:
/* [1] Constructors and Destructors */
MimeText(const QString &text = "");
~MimeText();
explicit MimeText(const QString &text = "");
~MimeText() override;
/* [1] --- */
@ -39,7 +39,7 @@ public:
void setText(const QString &text);
const QString & getText() const;
[[nodiscard]] const QString &getText() const;
/* [2] --- */
@ -53,7 +53,7 @@ protected:
/* [4] Protected methods */
void prepare();
void prepare() override;
/* [4] --- */

View File

@ -24,12 +24,12 @@
#include "smtpexports.h"
class SMTP_EXPORT QuotedPrintable : public QObject
{
class SMTP_EXPORT QuotedPrintable : public QObject {
Q_OBJECT
public:
static QString encode(const QByteArray &input);
static QByteArray decode(const QString &input);
private:

View File

@ -25,21 +25,18 @@
#include "mimemessage.h"
#include "smtpexports.h"
class SMTP_EXPORT SmtpClient : public QObject
{
class SMTP_EXPORT SmtpClient : public QObject {
Q_OBJECT
public:
/* [0] Enumerations */
enum AuthMethod
{
enum AuthMethod {
AuthPlain,
AuthLogin
};
enum SmtpError
{
enum SmtpError {
ConnectionTimeoutError,
ResponseTimeoutError,
SendDataTimeoutError,
@ -48,8 +45,7 @@ public:
ClientError // 5xx smtp error
};
enum ConnectionType
{
enum ConnectionType {
TcpConnection,
SslConnection,
TlsConnection // STARTTLS
@ -60,46 +56,57 @@ public:
/* [1] Constructors and Destructors */
SmtpClient(const QString & host = "localhost", int port = 25, ConnectionType ct = TcpConnection);
explicit SmtpClient(const QString &host = "localhost", int port = 25, ConnectionType ct = TcpConnection);
~SmtpClient();
~SmtpClient() override;
/* [1] --- */
/* [2] Getters and Setters */
const QString& getHost() const;
[[nodiscard]] const QString &getHost() const;
void setHost(const QString &host);
int getPort() const;
[[nodiscard]] int getPort() const;
void setPort(int port);
const QString& getName() const;
[[nodiscard]] const QString &getName() const;
void setName(const QString &name);
ConnectionType getConnectionType() const;
[[nodiscard]] ConnectionType getConnectionType() const;
void setConnectionType(ConnectionType ct);
const QString & getUser() const;
[[nodiscard]] const QString &getUser() const;
void setUser(const QString &user);
const QString & getPassword() const;
[[nodiscard]] const QString &getPassword() const;
void setPassword(const QString &password);
SmtpClient::AuthMethod getAuthMethod() const;
[[nodiscard]] SmtpClient::AuthMethod getAuthMethod() const;
void setAuthMethod(AuthMethod method);
const QString & getResponseText() const;
int getResponseCode() const;
[[nodiscard]] const QString &getResponseText() const;
[[nodiscard]] int getResponseCode() const;
[[nodiscard]] int getConnectionTimeout() const;
int getConnectionTimeout() const;
void setConnectionTimeout(int msec);
int getResponseTimeout() const;
[[nodiscard]] int getResponseTimeout() const;
void setResponseTimeout(int msec);
int getSendMessageTimeout() const;
[[nodiscard]] int getSendMessageTimeout() const;
void setSendMessageTimeout(int msec);
QTcpSocket *getSocket();
@ -113,6 +120,7 @@ public:
bool connectToHost();
bool login();
bool login(const QString &user, const QString &password, AuthMethod method = AuthLogin);
bool sendMail(MimeMessage &email);
@ -146,8 +154,11 @@ protected:
int responseCode;
class ResponseTimeoutException {};
class SendMessageTimeoutException {};
class ResponseTimeoutException : public std::exception {
};
class SendMessageTimeoutException : public std::exception {
};
/* [4] --- */
@ -165,7 +176,9 @@ protected slots:
/* [6] Protected slots */
void socketStateChanged(QAbstractSocket::SocketState state);
void socketError(QAbstractSocket::SocketError error);
void socketReadyRead();
/* [6] --- */

View File

@ -2,9 +2,9 @@
#define SMTPEXPORTS_H
#ifdef SMTP_BUILD
#define SMTP_EXPORT Q_DECL_EXPORT
#define SMTP_EXPORT
#else
#define SMTP_EXPORT Q_DECL_IMPORT
#define SMTP_EXPORT
#endif
#endif // SMTPEXPORTS_H

View File

@ -22,11 +22,12 @@
/* [1] Constructors and Destructors */
MimeAttachment::MimeAttachment(QFile *file) : MimeFile(file) {}
MimeAttachment::MimeAttachment(const QByteArray &stream,
const QString &fileName)
: MimeFile(stream, fileName) {}
MimeAttachment::~MimeAttachment() {}
MimeAttachment::~MimeAttachment() = default;
/* [1] --- */

View File

@ -27,7 +27,7 @@ QString MimeContentFormatter::format(const QString &content,
QString out;
int chars = 0;
for (int i = 0; i < content.length(); ++i) {
for (auto i : content) {
chars++;
if (!quotedPrintable) {
if (chars > max_length) {
@ -35,20 +35,20 @@ QString MimeContentFormatter::format(const QString &content,
chars = 1;
}
} else {
if (content[i] == '\n') { // new line
out.append(content[i]);
if (i == '\n') { // new line
out.append(i);
chars = 0;
continue;
}
if ((chars > max_length - 1) ||
((content[i] == '=') && (chars > max_length - 3))) {
((i == '=') && (chars > max_length - 3))) {
out.append('=');
out.append("\r\n");
chars = 1;
}
}
out.append(content[i]);
out.append(i);
}
return out;

View File

@ -31,13 +31,12 @@ MimeFile::MimeFile(QFile *file) {
MimeFile::MimeFile(const QByteArray &stream, const QString &fileName) {
this->cEncoding = Base64;
this->cType = "application/octet-stream";
this->file = 0;
this->file = nullptr;
this->cName = fileName;
this->content = stream;
}
MimeFile::~MimeFile() {
if (file)
delete file;
}

View File

@ -24,7 +24,7 @@ MimeHtml::MimeHtml(const QString &html) : MimeText(html) {
this->cType = "text/html";
}
MimeHtml::~MimeHtml() {}
MimeHtml::~MimeHtml() = default;
/* [1] --- */

View File

@ -22,7 +22,7 @@
MimeInlineFile::MimeInlineFile(QFile *f) : MimeFile(f) {}
MimeInlineFile::~MimeInlineFile() {}
MimeInlineFile::~MimeInlineFile() = default;
/* [1] --- */

View File

@ -122,7 +122,7 @@ const QList<MimePart *> &MimeMessage::getParts() const {
if (typeid(*content) == typeid(MimeMultiPart)) {
return ((MimeMultiPart *) content)->getParts();
} else {
QList<MimePart *> *res = new QList<MimePart *>();
auto *res = new QList<MimePart *>();
res->append(content);
return *res;
}
@ -194,7 +194,7 @@ QString MimeMessage::toString() {
/* ---------------------------------- */
/* ------- Recipients / Cc ---------- */
if (recipientsCc.size() != 0) {
if (!recipientsCc.empty()) {
mime += "Cc:";
}
for (i = 0, it = recipientsCc.begin(); it != recipientsCc.end(); ++it, ++i) {
@ -223,7 +223,7 @@ QString MimeMessage::toString() {
}
mime += " <" + (*it)->getAddress() + ">";
}
if (recipientsCc.size() != 0) {
if (!recipientsCc.empty()) {
mime += "\r\n";
}
/* ---------------------------------- */

View File

@ -39,11 +39,11 @@ MimeMultiPart::MimeMultiPart(MultiPartType type) {
QRandomGenerator generator;
QCryptographicHash md5(QCryptographicHash::Md5);
md5.addData(QByteArray().append(generator.generate()));
md5.addData(QByteArray().append((char) generator.generate()));
cBoundary = md5.result().toHex();
}
MimeMultiPart::~MimeMultiPart() {}
MimeMultiPart::~MimeMultiPart() = default;
void MimeMultiPart::addPart(MimePart *part) { parts.append(part); }

View File

@ -27,7 +27,7 @@ MimePart::MimePart() {
cBoundary = "";
}
MimePart::~MimePart() { return; }
MimePart::~MimePart() = default;
/* [1] --- */

View File

@ -27,7 +27,7 @@ MimeText::MimeText(const QString &txt) {
this->cEncoding = _8Bit;
}
MimeText::~MimeText() {}
MimeText::~MimeText() = default;
/* [1] --- */

View File

@ -25,8 +25,8 @@ QString QuotedPrintable::encode(const QByteArray &input) {
const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
for (int i = 0; i < input.length(); ++i) {
byte = input[i];
for (char i : input) {
byte = i;
if ((byte == 0x20) || ((byte >= 33) && (byte <= 126) && (byte != 61))) {
output.append(byte);

View File

@ -110,7 +110,9 @@ void SmtpClient::setConnectionTimeout(int msec) { connectionTimeout = msec; }
int SmtpClient::getResponseTimeout() const { return responseTimeout; }
void SmtpClient::setResponseTimeout(int msec) { responseTimeout = msec; }
int SmtpClient::getSendMessageTimeout() const { return sendMessageTimeout; }
void SmtpClient::setSendMessageTimeout(int msec) { sendMessageTimeout = msec; }
/* [2] --- */
@ -369,7 +371,7 @@ void SmtpClient::waitForResponse() {
responseText = socket->readLine();
// Extract the respose code from the server's responce (first 3 digits)
responseCode = responseText.left(3).toInt();
responseCode = responseText.leftRef(3).toInt();
if (responseCode / 100 == 4)
emit smtpError(ServerError);