SVN_SILENT made messages (.desktop file) - always resolve ours
[trojita.git] / src / MSA / SMTP.cpp
blobcd7c4d2376167bbc01922bfad54cdbaadc924341
1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
2 Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
4 This file is part of the Trojita Qt IMAP e-mail client,
5 http://trojita.flaska.net/
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of
10 the License or (at your option) version 3 or any later version
11 accepted by the membership of KDE e.V. (or its successor approved
12 by the membership of KDE e.V.), which shall act as a proxy
13 defined in Section 14 of version 3 of the license.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "SMTP.h"
25 namespace MSA
28 SMTP::SMTP(QObject *parent, const QString &host, quint16 port, bool encryptedConnect, bool startTls, bool auth,
29 const QString &user):
30 AbstractMSA(parent), host(host), port(port),
31 encryptedConnect(encryptedConnect), startTls(startTls), auth(auth),
32 user(user), failed(false), isWaitingForPassword(false), sendingMode(MODE_SMTP_INVALID)
34 qwwSmtp = new QwwSmtpClient(this);
35 // FIXME: handle SSL errors properly
36 connect(qwwSmtp, &QwwSmtpClient::sslErrors, qwwSmtp, &QwwSmtpClient::ignoreSslErrors);
37 connect(qwwSmtp, &QwwSmtpClient::connected, this, &AbstractMSA::sending);
38 connect(qwwSmtp, &QwwSmtpClient::done, this, &SMTP::handleDone);
39 connect(qwwSmtp, &QwwSmtpClient::socketError, this, &SMTP::handleError);
42 void SMTP::cancel()
44 qwwSmtp->disconnectFromHost();
45 if (!failed) {
46 failed = true;
47 emit error(tr("Sending of the message was cancelled"));
51 void SMTP::handleDone(bool ok)
53 if (failed) {
54 // This is a duplicate notification. The QwwSmtpClient is known to send contradicting results, see e.g. bug 321272.
55 return;
57 if (ok) {
58 emit sent();
59 } else {
60 failed = true;
61 if (qwwSmtp->errorString().isEmpty())
62 emit error(tr("Sending of the message failed."));
63 else
64 emit error(tr("Sending of the message failed with the following error: %1").arg(qwwSmtp->errorString()));
68 void SMTP::handleError(QAbstractSocket::SocketError err, const QString &msg)
70 Q_UNUSED(err);
71 failed = true;
72 emit error(msg);
75 void SMTP::setPassword(const QString &password)
77 pass = password;
78 if (isWaitingForPassword)
79 sendContinueGotPassword();
82 void SMTP::sendMail(const QByteArray &from, const QList<QByteArray> &to, const QByteArray &data)
84 this->from = from;
85 this->to = to;
86 this->data = data;
87 this->sendingMode = MODE_SMTP_DATA;
88 this->isWaitingForPassword = true;
89 emit progressMax(data.size());
90 emit progress(0);
91 emit connecting();
92 if (!auth || !pass.isEmpty()) {
93 sendContinueGotPassword();
94 return;
96 emit passwordRequested(user, host);
99 void SMTP::sendContinueGotPassword()
101 isWaitingForPassword = false;
102 if (encryptedConnect)
103 qwwSmtp->connectToHostEncrypted(host, port);
104 else
105 qwwSmtp->connectToHost(host, port);
106 if (startTls)
107 qwwSmtp->startTls();
108 if (auth)
109 qwwSmtp->authenticate(user, pass, QwwSmtpClient::AuthAny);
110 emit sending(); // FIXME: later
111 switch (sendingMode) {
112 case MODE_SMTP_DATA:
114 //RFC5321 specifies to prepend a period to lines starting with a period in section 4.5.2
115 if (data.startsWith('.'))
116 data.prepend('.');
117 data.replace("\n.", "\n..");
118 qwwSmtp->sendMail(from, to, data);
120 break;
121 case MODE_SMTP_BURL:
122 qwwSmtp->sendMailBurl(from, to, data);
123 break;
124 default:
125 failed = true;
126 emit error(tr("Unknown SMTP mode"));
127 break;
129 qwwSmtp->disconnectFromHost();
132 bool SMTP::supportsBurl() const
134 return true;
137 void SMTP::sendBurl(const QByteArray &from, const QList<QByteArray> &to, const QByteArray &imapUrl)
139 this->from = from;
140 this->to = to;
141 this->data = imapUrl;
142 this->sendingMode = MODE_SMTP_BURL;
143 this->isWaitingForPassword = true;
144 emit progressMax(1);
145 emit progress(0);
146 emit connecting();
147 if (!auth || !pass.isEmpty()) {
148 sendContinueGotPassword();
149 return;
151 emit passwordRequested(user, host);
154 SMTPFactory::SMTPFactory(const QString &host, quint16 port, bool encryptedConnect, bool startTls,
155 bool auth, const QString &user):
156 m_host(host), m_port(port), m_encryptedConnect(encryptedConnect), m_startTls(startTls),
157 m_auth(auth), m_user(user)
161 SMTPFactory::~SMTPFactory()
165 AbstractMSA *SMTPFactory::create(QObject *parent) const
167 return new SMTP(parent, m_host, m_port, m_encryptedConnect, m_startTls, m_auth, m_user);