-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneclickunsubscribejob.cpp
68 lines (56 loc) · 2.46 KB
/
oneclickunsubscribejob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "oneclickunsubscribejob.h"
#include <QNetworkReply>
#include <QHttpMultiPart>
#include "unsubscribe_debug.h"
using namespace MessageViewer;
OneClickUnsubscribeJob::OneClickUnsubscribeJob(QUrl &oneClickUrl, UnsubscribeManager *parent)
: QObject(parent),
mNetworkAccessManager(new QNetworkAccessManager(this))
{
mNetworkAccessManager->setStrictTransportSecurityEnabled(true);
mNetworkAccessManager->enableStrictTransportSecurityStore(true);
connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &OneClickUnsubscribeJob::slotFinished);
connect(mNetworkAccessManager, &QNetworkAccessManager::sslErrors, this, &OneClickUnsubscribeJob::slotSslErrors);
mUrl = oneClickUrl;
}
void OneClickUnsubscribeJob::start()
{
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType, this);
QHttpPart mainPart;
// Per RFC 8058, only "List-Unsubscribe=One-Click" is allowed. To avoid
// parsing issues, we simply won't parse anything
mainPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"List-Unsubscribe\""));
mainPart.setBody("One-Click");
multiPart->append(mainPart);
QNetworkRequest request(mUrl);
qCDebug(UnsubscribePlugin) << "Sending one-click unsubscribe request to" << mUrl;
mReply = mNetworkAccessManager->post(request, multiPart);
connect(mReply, &QNetworkReply::errorOccurred, this, &OneClickUnsubscribeJob::slotError);
}
void OneClickUnsubscribeJob::slotSslErrors(QNetworkReply *reply, const QList<QSslError> &error)
{
// TODO: allow override somehow
qCDebug(UnsubscribePlugin) << "Got" << error.count() << "SSL error(s)";
UnsubscribeManager::Result sslErrResult = {
.Type = UnsubscribeManager::Result::SslError,
.ErrorString = error.first().errorString(),
};
Q_EMIT result(sslErrResult);
}
void OneClickUnsubscribeJob::slotFinished(QNetworkReply *reply)
{
qCDebug(UnsubscribePlugin) << "Successful response from" << mUrl << "with result" << reply->errorString();
UnsubscribeManager::Result successResult = {
.Type = UnsubscribeManager::Result::None,
};
Q_EMIT result(successResult);
}
void OneClickUnsubscribeJob::slotError(QNetworkReply::NetworkError error)
{
UnsubscribeManager::Result errorResult = {
.Type = UnsubscribeManager::Result::NetworkError,
.ErrorString = mReply->errorString(),
};
Q_EMIT result(errorResult);
}
#include "moc_oneclickunsubscribejob.cpp"