Messagelib

dkimmanagerkey.cpp
1/*
2 SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
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>
13using namespace MessageViewer;
14DKIMManagerKey::DKIMManagerKey(QObject *parent)
15 : QObject(parent)
16 , mQcaInitializer(new QCA::Initializer(QCA::Practical, 64))
17{
18 loadKeys();
19}
20
21DKIMManagerKey::~DKIMManagerKey()
22{
23 delete mQcaInitializer;
24 saveKeys();
25}
26
27DKIMManagerKey *DKIMManagerKey::self()
28{
30 return &s_self;
31}
32
33QString 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
43void 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) {
48 newKey.lastUsedDateTime = QDateTime::currentDateTime();
49 addKey(newKey);
50 return;
51 }
52 }
53}
54
55void DKIMManagerKey::addKey(const KeyInfo &key)
56{
57 const QList<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
66void 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
76QList<KeyInfo> DKIMManagerKey::keys() const
77{
78 return mKeys;
79}
80
81QStringList 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
88void 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()) {
103 }
104 mKeys.append(KeyInfo{key, selector, domain, storedAt, lastUsed});
105 }
106}
107
108void 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
128void DKIMManagerKey::saveKeys(const QList<MessageViewer::KeyInfo> &lst)
129{
130 mKeys = lst;
131 saveKeys();
132}
133
134bool KeyInfo::operator==(const KeyInfo &other) const
135{
136 return keyValue == other.keyValue && selector == other.selector && domain == other.domain;
137}
138
139bool KeyInfo::operator!=(const KeyInfo &other) const
140{
141 return !(operator==(other));
142}
143
144QDebug 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}
153
154#include "moc_dkimmanagerkey.cpp"
KConfigGroup group(const QString &group)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
The DKIMManagerKey class.
QDebug operator<<(QDebug dbg, const PerceptualColor::LchaDouble &value)
QDateTime currentDateTime()
QDateTime fromString(const QString &string, Qt::DateFormat format)
QString toString(Qt::DateFormat format) const const
T qobject_cast(QObject *object)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
The KeyInfo struct.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.