Messagelib

dkimmanagerkey.cpp
1 /*
2  SPDX-FileCopyrightText: 2018-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "dkimmanagerkey.h"
8 #include "dkimutil.h"
9 #include <KConfig>
10 #include <KConfigGroup>
11 #include <QRegularExpression>
12 #include <QtCrypto>
13 using namespace MessageViewer;
14 DKIMManagerKey::DKIMManagerKey(QObject *parent)
15  : QObject(parent)
16  , mQcaInitializer(new QCA::Initializer(QCA::Practical, 64))
17 {
18  loadKeys();
19 }
20 
21 DKIMManagerKey::~DKIMManagerKey()
22 {
23  delete mQcaInitializer;
24  saveKeys();
25 }
26 
27 DKIMManagerKey *DKIMManagerKey::self()
28 {
29  static DKIMManagerKey s_self;
30  return &s_self;
31 }
32 
33 QString DKIMManagerKey::keyValue(const QString &selector, const QString &domain)
34 {
35  for (const KeyInfo &keyInfo : std::as_const(mKeys)) {
36  if (keyInfo.selector == selector && keyInfo.domain == domain) {
37  return keyInfo.keyValue;
38  }
39  }
40  return {};
41 }
42 
43 void DKIMManagerKey::updateLastUsed(const QString &selector, const QString &domain)
44 {
45  for (const KeyInfo &keyInfo : std::as_const(mKeys)) {
46  if (keyInfo.selector == selector && keyInfo.domain == domain) {
47  KeyInfo newKey = keyInfo;
48  newKey.lastUsedDateTime = QDateTime::currentDateTime();
49  addKey(newKey);
50  return;
51  }
52  }
53 }
54 
55 void DKIMManagerKey::addKey(const KeyInfo &key)
56 {
57  const QVector<KeyInfo> keys = mKeys;
58  for (const KeyInfo &keyInfo : keys) {
59  if (keyInfo.selector == key.selector && keyInfo.domain == key.domain) {
60  mKeys.removeAll(keyInfo);
61  }
62  }
63  mKeys.append(key);
64 }
65 
66 void DKIMManagerKey::removeKey(const QString &key)
67 {
68  for (const KeyInfo &keyInfo : std::as_const(mKeys)) {
69  if (keyInfo.keyValue == key) {
70  mKeys.removeAll(keyInfo);
71  break;
72  }
73  }
74 }
75 
76 QVector<KeyInfo> DKIMManagerKey::keys() const
77 {
78  return mKeys;
79 }
80 
81 QStringList DKIMManagerKey::keyRecorderList(KSharedConfig::Ptr &config) const
82 {
83  config = KSharedConfig::openConfig(MessageViewer::DKIMUtil::defaultConfigFileName(), KConfig::NoGlobals);
84  const QStringList keyGroups = config->groupList().filter(QRegularExpression(QStringLiteral("DKIM Key Record #\\d+")));
85  return keyGroups;
86 }
87 
88 void DKIMManagerKey::loadKeys()
89 {
90  KSharedConfig::Ptr config;
91  const QStringList keyGroups = keyRecorderList(config);
92 
93  mKeys.clear();
94  for (const QString &groupName : keyGroups) {
95  KConfigGroup group = config->group(groupName);
96  const QString selector = group.readEntry(QStringLiteral("Selector"), QString());
97  const QString domain = group.readEntry(QStringLiteral("Domain"), QString());
98  const QString key = group.readEntry(QStringLiteral("Key"), QString());
99  const QDateTime storedAt = QDateTime::fromString(group.readEntry(QStringLiteral("StoredAt"), QString()), Qt::ISODate);
100  QDateTime lastUsed = QDateTime::fromString(group.readEntry(QStringLiteral("LastUsed"), QString()), Qt::ISODate);
101  if (!lastUsed.isValid()) {
102  lastUsed = storedAt;
103  }
104  mKeys.append(KeyInfo{key, selector, domain, storedAt, lastUsed});
105  }
106 }
107 
108 void DKIMManagerKey::saveKeys()
109 {
110  KSharedConfig::Ptr config;
111  const QStringList filterGroups = keyRecorderList(config);
112 
113  for (const QString &group : filterGroups) {
114  config->deleteGroup(group);
115  }
116  for (int i = 0, total = mKeys.count(); i < total; ++i) {
117  const QString groupName = QStringLiteral("DKIM Key Record #%1").arg(i);
118  KConfigGroup group = config->group(groupName);
119  const KeyInfo &info = mKeys.at(i);
120  group.writeEntry(QStringLiteral("Selector"), info.selector);
121  group.writeEntry(QStringLiteral("Domain"), info.domain);
122  group.writeEntry(QStringLiteral("Key"), info.keyValue);
123  group.writeEntry(QStringLiteral("StoredAt"), info.storedAtDateTime.toString(Qt::ISODate));
124  group.writeEntry(QStringLiteral("LastUsed"), info.lastUsedDateTime.toString(Qt::ISODate));
125  }
126 }
127 
128 void DKIMManagerKey::saveKeys(const QVector<MessageViewer::KeyInfo> &lst)
129 {
130  mKeys = lst;
131  saveKeys();
132 }
133 
134 bool KeyInfo::operator==(const KeyInfo &other) const
135 {
136  return keyValue == other.keyValue && selector == other.selector && domain == other.domain;
137 }
138 
139 bool KeyInfo::operator!=(const KeyInfo &other) const
140 {
141  return !(operator==(other));
142 }
143 
144 QDebug operator<<(QDebug d, const KeyInfo &t)
145 {
146  d << " keyvalue " << t.keyValue;
147  d << " selector " << t.selector;
148  d << " domain " << t.domain;
149  d << " storedAtDateTime " << t.storedAtDateTime;
150  d << " lastUsedDateTime " << t.lastUsedDateTime;
151  return d;
152 }
QString toString(Qt::DateFormat format) const const
The DKIMManagerKey class.
int removeAll(const T &t)
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
QStringList filter(QStringView str, Qt::CaseSensitivity cs) const const
Practical
QDateTime fromString(const QString &string, Qt::DateFormat format)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
KConfigGroup group(const QString &group)
QDataStream & operator<<(QDataStream &out, const KDateTime::Spec &spec)
QDateTime currentDateTime()
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
The KeyInfo struct.
T readEntry(const QString &key, const T &aDefault) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.