File indexing completed on 2025-03-09 04:54:31

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "dmarcrecordjob.h"
0008 #include "messageviewer_dkimcheckerdebug.h"
0009 
0010 #include <QDnsLookup>
0011 
0012 using namespace MessageViewer;
0013 // see https://tools.ietf.org/html/rfc7489
0014 DMARCRecordJob::DMARCRecordJob(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 DMARCRecordJob::~DMARCRecordJob() = default;
0020 
0021 bool DMARCRecordJob::start()
0022 {
0023     if (!canStart()) {
0024         qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Impossible to start download dmarc key.";
0025         deleteLater();
0026         return false;
0027     }
0028     mDnsLookup = new QDnsLookup(this);
0029     connect(mDnsLookup, &QDnsLookup::finished, this, &DMARCRecordJob::resolvDnsDone);
0030 
0031     mDnsLookup->setType(QDnsLookup::TXT);
0032     mDnsLookup->setName(resolvDnsValue());
0033     mDnsLookup->lookup();
0034     return true;
0035 }
0036 
0037 QString DMARCRecordJob::resolvDnsValue() const
0038 {
0039     const QString name = QLatin1StringView("_dmarc.") + mDomainName;
0040     qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "DMARCRecordJob::resolvDnsValue" << name;
0041     return name;
0042 }
0043 
0044 void DMARCRecordJob::resolvDnsDone()
0045 {
0046     // Check the lookup succeeded.
0047     if (mDnsLookup->error() != QDnsLookup::NoError) {
0048         qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Error during resolving: " << mDnsLookup->errorString() << " mDnsLookup->error() " << mDnsLookup->error();
0049         Q_EMIT error(mDnsLookup->errorString(), mDomainName);
0050         deleteLater();
0051         return;
0052     }
0053 
0054     // Handle the results.
0055     const auto records = mDnsLookup->textRecords();
0056     QList<QByteArray> textRecordResult;
0057     textRecordResult.reserve(records.count());
0058     for (const QDnsTextRecord &record : records) {
0059         textRecordResult << record.values();
0060     }
0061 
0062     Q_EMIT success(textRecordResult, mDomainName);
0063     deleteLater();
0064 }
0065 
0066 bool DMARCRecordJob::canStart() const
0067 {
0068     return !mDomainName.isEmpty();
0069 }
0070 
0071 QString DMARCRecordJob::domainName() const
0072 {
0073     return mDomainName;
0074 }
0075 
0076 void DMARCRecordJob::setDomainName(const QString &domainName)
0077 {
0078     mDomainName = domainName;
0079 }
0080 
0081 #include "moc_dmarcrecordjob.cpp"