Messagelib

dkimdownloadkeyjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2019-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "dkimdownloadkeyjob.h"
8 #include "messageviewer_dkimcheckerdebug.h"
9 
10 #include <QDnsLookup>
11 using namespace MessageViewer;
12 DKIMDownloadKeyJob::DKIMDownloadKeyJob(QObject *parent)
13  : QObject(parent)
14 {
15 }
16 
17 DKIMDownloadKeyJob::~DKIMDownloadKeyJob() = default;
18 
19 bool DKIMDownloadKeyJob::start()
20 {
21  if (!canStart()) {
22  qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Impossible to start download public keys";
23  deleteLater();
24  return false;
25  }
26  mDnsLookup = new QDnsLookup(this);
27  connect(mDnsLookup, &QDnsLookup::finished, this, &DKIMDownloadKeyJob::resolvDnsDone);
28 
29  mDnsLookup->setType(QDnsLookup::TXT);
30  mDnsLookup->setName(resolvDnsValue());
31  mDnsLookup->lookup();
32  return true;
33 }
34 
35 bool DKIMDownloadKeyJob::canStart() const
36 {
37  return !mDomainName.isEmpty() && !mSelectorName.isEmpty();
38 }
39 
40 QString DKIMDownloadKeyJob::domainName() const
41 {
42  return mDomainName;
43 }
44 
45 void DKIMDownloadKeyJob::setDomainName(const QString &domainName)
46 {
47  mDomainName = domainName;
48 }
49 
50 QString DKIMDownloadKeyJob::selectorName() const
51 {
52  return mSelectorName;
53 }
54 
55 void DKIMDownloadKeyJob::setSelectorName(const QString &selectorName)
56 {
57  mSelectorName = selectorName;
58 }
59 
60 QString DKIMDownloadKeyJob::resolvDnsValue() const
61 {
62  const QString name = mSelectorName + QLatin1String("._domainkey.") + mDomainName;
63  qDebug(MESSAGEVIEWER_DKIMCHECKER_LOG)
64  << "DKIMDownloadKeyJob::resolvDnsValue" << name;
65  return name;
66 }
67 
68 QDnsLookup *DKIMDownloadKeyJob::dnsLookup() const
69 {
70  return mDnsLookup;
71 }
72 
73 void DKIMDownloadKeyJob::resolvDnsDone()
74 {
75  // Check the lookup succeeded.
76  if (mDnsLookup->error() != QDnsLookup::NoError) {
77  qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Error during resolving: " << mDnsLookup->errorString();
78  Q_EMIT error(mDnsLookup->errorString());
79  deleteLater();
80  return;
81  }
82 
83  // Handle the results.
84  const auto records = mDnsLookup->textRecords();
85  QList<QByteArray> textRecordResult;
86  textRecordResult.reserve(records.count());
87  for (const QDnsTextRecord &record : records) {
88  textRecordResult << record.values();
89  }
90 
91  Q_EMIT success(textRecordResult, mDomainName, mSelectorName);
92  deleteLater();
93 }
void reserve(int alloc)
const QLatin1String name
void finished()
void deleteLater()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.