Messagelib

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

KDE's Doxygen guidelines are available online.