Messagelib

dmarcrecordjob.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 "dmarcrecordjob.h"
8 #include "messageviewer_dkimcheckerdebug.h"
9 
10 #include <QDnsLookup>
11 
12 using namespace MessageViewer;
13 // see https://tools.ietf.org/html/rfc7489
14 DMARCRecordJob::DMARCRecordJob(QObject *parent)
15  : QObject(parent)
16 {
17 }
18 
19 DMARCRecordJob::~DMARCRecordJob() = default;
20 
21 bool DMARCRecordJob::start()
22 {
23  if (!canStart()) {
24  qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Impossible to start download dmarc key.";
25  deleteLater();
26  return false;
27  }
28  mDnsLookup = new QDnsLookup(this);
29  connect(mDnsLookup, &QDnsLookup::finished, this, &DMARCRecordJob::resolvDnsDone);
30 
31  mDnsLookup->setType(QDnsLookup::TXT);
32  mDnsLookup->setName(resolvDnsValue());
33  mDnsLookup->lookup();
34  return true;
35 }
36 
37 QString DMARCRecordJob::resolvDnsValue() const
38 {
39  const QString name = QLatin1String("_dmarc.") + mDomainName;
40  qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "DMARCRecordJob::resolvDnsValue" << name;
41  return name;
42 }
43 
44 void DMARCRecordJob::resolvDnsDone()
45 {
46  // Check the lookup succeeded.
47  if (mDnsLookup->error() != QDnsLookup::NoError) {
48  qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Error during resolving: " << mDnsLookup->errorString() << " mDnsLookup->error() " << mDnsLookup->error();
49  Q_EMIT error(mDnsLookup->errorString(), mDomainName);
50  deleteLater();
51  return;
52  }
53 
54  // Handle the results.
55  const auto records = mDnsLookup->textRecords();
56  QList<QByteArray> textRecordResult;
57  textRecordResult.reserve(records.count());
58  for (const QDnsTextRecord &record : records) {
59  textRecordResult << record.values();
60  }
61 
62  Q_EMIT success(textRecordResult, mDomainName);
63  deleteLater();
64 }
65 
66 bool DMARCRecordJob::canStart() const
67 {
68  return !mDomainName.isEmpty();
69 }
70 
71 QString DMARCRecordJob::domainName() const
72 {
73  return mDomainName;
74 }
75 
76 void DMARCRecordJob::setDomainName(const QString &domainName)
77 {
78  mDomainName = domainName;
79 }
void reserve(int alloc)
const QLatin1String name
void finished()
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Nov 26 2021 23:16:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.