Messagelib

dkimmanagerkey.cpp
1 /*
2  SPDX-FileCopyrightText: 2018-2020 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 <KSharedConfig>
10 #include <KConfig>
11 #include <KConfigGroup>
12 #include <QRegularExpression>
13 #include <QtCrypto>
14 using namespace MessageViewer;
15 DKIMManagerKey::DKIMManagerKey(QObject *parent)
16  : QObject(parent)
17 {
18  mQcaInitializer = new QCA::Initializer(QCA::Practical, 64);
19  loadKeys();
20 }
21 
22 DKIMManagerKey::~DKIMManagerKey()
23 {
24  delete mQcaInitializer;
25  saveKeys();
26 }
27 
28 DKIMManagerKey *DKIMManagerKey::self()
29 {
30  static DKIMManagerKey s_self;
31  return &s_self;
32 }
33 
34 QString DKIMManagerKey::keyValue(const QString &selector, const QString &domain)
35 {
36  for (const KeyInfo &keyInfo : qAsConst(mKeys)) {
37  if (keyInfo.selector == selector && keyInfo.domain == domain) {
38  return keyInfo.keyValue;
39  }
40  }
41  return {};
42 }
43 
44 void DKIMManagerKey::addKey(const KeyInfo &key)
45 {
46  const QVector<KeyInfo> keys = mKeys;
47  for (const KeyInfo &keyInfo : keys) {
48  if (keyInfo.selector == key.selector && keyInfo.domain == key.domain) {
49  mKeys.removeAll(keyInfo);
50  }
51  }
52  mKeys.append(key);
53 }
54 
55 void DKIMManagerKey::removeKey(const QString &key)
56 {
57  for (const KeyInfo &keyInfo : qAsConst(mKeys)) {
58  if (keyInfo.keyValue == key) {
59  mKeys.removeAll(keyInfo);
60  break;
61  }
62  }
63 }
64 
65 QVector<KeyInfo> DKIMManagerKey::keys() const
66 {
67  return mKeys;
68 }
69 
70 QStringList DKIMManagerKey::keyRecorderList(KSharedConfig::Ptr &config) const
71 {
72  config = KSharedConfig::openConfig(MessageViewer::DKIMUtil::defaultConfigFileName(), KConfig::NoGlobals);
73  const QStringList keyGroups
74  = config->groupList().filter(QRegularExpression(QStringLiteral("DKIM Key Record #\\d+")));
75  return keyGroups;
76 }
77 
78 void DKIMManagerKey::loadKeys()
79 {
80  KSharedConfig::Ptr config;
81  const QStringList keyGroups = keyRecorderList(config);
82 
83  mKeys.clear();
84  for (const QString &groupName : keyGroups) {
85  KConfigGroup group = config->group(groupName);
86  const QString selector = group.readEntry(QStringLiteral("Selector"), QString());
87  const QString domain = group.readEntry(QStringLiteral("Domain"), QString());
88  const QString key = group.readEntry(QStringLiteral("Key"), QString());
89  mKeys.append(KeyInfo{key, selector, domain});
90  }
91 }
92 
93 void DKIMManagerKey::saveKeys()
94 {
95  KSharedConfig::Ptr config;
96  const QStringList filterGroups = keyRecorderList(config);
97 
98  for (const QString &group : filterGroups) {
99  config->deleteGroup(group);
100  }
101  for (int i = 0, total = mKeys.count(); i < total; ++i) {
102  const QString groupName = QStringLiteral("DKIM Key Record #%1").arg(i);
103  KConfigGroup group = config->group(groupName);
104  const KeyInfo &info = mKeys.at(i);
105  group.writeEntry(QStringLiteral("Selector"), info.selector);
106  group.writeEntry(QStringLiteral("Domain"), info.domain);
107  group.writeEntry(QStringLiteral("Key"), info.keyValue);
108  }
109 }
110 
111 void DKIMManagerKey::saveKeys(const QVector<MessageViewer::KeyInfo> &lst)
112 {
113  mKeys = lst;
114  saveKeys();
115 }
116 
117 bool KeyInfo::operator ==(const KeyInfo &other) const
118 {
119  return keyValue == other.keyValue
120  && selector == other.selector
121  && domain == other.domain;
122 }
123 
124 bool KeyInfo::operator !=(const KeyInfo &other) const
125 {
126  return !(operator ==(other));
127 }
128 
129 QDebug operator <<(QDebug d, const KeyInfo &t)
130 {
131  d << " keyvalue " << t.keyValue;
132  d << " selector " << t.selector;
133  d << " domain " << t.domain;
134  return d;
135 }
The DKIMManagerKey class.
int removeAll(const T &t)
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
QStringList filter(QStringView str, Qt::CaseSensitivity cs) const const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
KConfigGroup group(const QString &group)
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-2020 The KDE developers.
Generated on Sun Sep 20 2020 23:12:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.