Messagelib

dkimmanagerkey.cpp
1 /*
2  SPDX-FileCopyrightText: 2018-2023 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 readEntry(const char *key, const char *aDefault=nullptr) const
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
Practical
QDateTime currentDateTime()
The KeyInfo struct.
QDataStream & operator<<(QDataStream &out, const KDateTime &dateTime)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
The DKIMManagerKey class.
QDateTime fromString(const QString &string, Qt::DateFormat format)
KSharedConfigPtr config()
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isValid() const const
QString toString(Qt::DateFormat format) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Mar 22 2023 04:07:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.