Messagelib

dkimcheckpolicyjob.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "dkimcheckpolicyjob.h"
8#include "dkim-verify/dkimmanagerrules.h"
9#include "dkim-verify/dmarcmanager.h"
10#include "messageviewer_dkimcheckerdebug.h"
11
12using namespace MessageViewer;
13DKIMCheckPolicyJob::DKIMCheckPolicyJob(QObject *parent)
14 : QObject(parent)
15{
16}
17
18DKIMCheckPolicyJob::~DKIMCheckPolicyJob() = default;
19
20bool DKIMCheckPolicyJob::canStart() const
21{
22 return !mEmailAddress.isEmpty();
23}
24
25bool DKIMCheckPolicyJob::start()
26{
27 if (!canStart()) {
28 qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Impossible to start DKIMCheckPolicyJob" << mEmailAddress;
29 Q_EMIT result(mCheckResult);
31 return false;
32 }
33 if (mPolicy.useDMarc()) {
34 if (DMARCManager::self()->isNoDMarcServerAddress(mEmailAddress)) {
35 Q_EMIT result(mCheckResult);
37 return true;
38 }
39 auto job = new DMARCPolicyJob(this);
40 job->setEmailAddress(mEmailAddress);
41 connect(job, &DMARCPolicyJob::result, this, &DKIMCheckPolicyJob::dmarcPolicyResult);
42 if (!job->start()) {
43 qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Impossible to start DKIMCheckPolicyJob" << mEmailAddress;
44 Q_EMIT result(mCheckResult);
46 return false;
47 }
48 } else {
49 if (mPolicy.useDefaultRules()) {
50 compareWithDefaultRules();
51 } else {
52 Q_EMIT result(mCheckResult);
54 }
55 }
56 return true;
57}
58
59void DKIMCheckPolicyJob::compareWithDefaultRules()
60{
61 const QList<DKIMRule> rules = DKIMManagerRules::self()->rules();
62 for (const DKIMRule &rule : rules) {
63 if (rule.enabled()) {
64 if (rule.from() == mEmailAddress || rule.from() == QLatin1Char('*')) {
65 // Check SDID
66 const QStringList signedDomainIdentifier = rule.signedDomainIdentifier();
67 for (const QString &ssid : signedDomainIdentifier) {
68 if (mCheckResult.sdid == ssid) {
69 switch (rule.ruleType()) {
70 case DKIMRule::RuleType::Unknown:
71 // Invalid rule !
72 qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Invalid rule found " << rule;
73 break;
74 case DKIMRule::RuleType::MustBeSigned:
75 mCheckResult.status = DKIMCheckSignatureJob::DKIMStatus::NeedToBeSigned;
76 break;
77 case DKIMRule::RuleType::CanBeSigned:
78 // Show a warning ?
79 break;
80 case DKIMRule::RuleType::IgnoreEmailNotSigned:
81 // Nothing !
82 break;
83 }
84 break;
85 }
86 }
87 }
88 }
89 }
90 Q_EMIT result(mCheckResult);
92}
93
94void DKIMCheckPolicyJob::dmarcPolicyResult(const MessageViewer::DMARCPolicyJob::DMARCResult &value, const QString &emailAddress)
95{
96 if (value.isValid()) {
97 if (mCheckResult.status == DKIMCheckSignatureJob::DKIMStatus::EmailNotSigned) {
98 mCheckResult.status = DKIMCheckSignatureJob::DKIMStatus::NeedToBeSigned;
99 // qDebug() << " void DKIMCheckPolicyJob::dmarcPolicyResult(const MessageViewer::DMARCPolicyJob::DMARCResult &value)"<<value.mDomain << "value " <<
100 // value.mSource;
101 // TODO verify it.
102 mCheckResult.sdid = value.mSource;
103 }
104 } else {
105 DMARCManager::self()->addNoDMarcServerAddress(emailAddress);
106 }
107 Q_EMIT result(mCheckResult);
108 deleteLater();
109}
110
111MessageViewer::DKIMCheckSignatureJob::CheckSignatureResult DKIMCheckPolicyJob::checkResult() const
112{
113 return mCheckResult;
114}
115
116void DKIMCheckPolicyJob::setCheckResult(const MessageViewer::DKIMCheckSignatureJob::CheckSignatureResult &checkResult)
117{
118 mCheckResult = checkResult;
119}
120
121QString DKIMCheckPolicyJob::emailAddress() const
122{
123 return mEmailAddress;
124}
125
126void DKIMCheckPolicyJob::setEmailAddress(const QString &emailAddress)
127{
128 mEmailAddress = emailAddress;
129}
130
131DKIMCheckPolicy DKIMCheckPolicyJob::policy() const
132{
133 return mPolicy;
134}
135
136void DKIMCheckPolicyJob::setPolicy(const DKIMCheckPolicy &policy)
137{
138 mPolicy = policy;
139}
140
141#include "moc_dkimcheckpolicyjob.cpp"
The DKIMCheckPolicy class.
The DKIMRule class.
Definition dkimrule.h:18
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.